Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Category.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Models\Document\Document;
  4. use App\Models\Item;
  5. use Database\Factories\CategoryFactory;
  6. use Illuminate\Database\Eloquent\Factories\Factory;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Wallo\FilamentCompanies\FilamentCompanies;
  12. class Category extends Model
  13. {
  14. use HasFactory;
  15. protected $table = 'categories';
  16. protected $fillable = [
  17. 'company_id',
  18. 'name',
  19. 'type',
  20. 'color',
  21. 'enabled',
  22. 'created_by',
  23. 'updated_by',
  24. ];
  25. protected $casts = [
  26. 'enabled' => 'boolean',
  27. ];
  28. public static function getCategoryTypes(): array
  29. {
  30. return [
  31. 'expense' => 'Expense',
  32. 'income' => 'Income',
  33. 'item' => 'Item',
  34. 'other' => 'Other',
  35. ];
  36. }
  37. public function company(): BelongsTo
  38. {
  39. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  40. }
  41. public function createdBy(): BelongsTo
  42. {
  43. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  44. }
  45. public function updatedBy(): BelongsTo
  46. {
  47. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  48. }
  49. public function items(): HasMany
  50. {
  51. return $this->hasMany(Item::class);
  52. }
  53. public function documents(): HasMany
  54. {
  55. return $this->hasMany(Document::class);
  56. }
  57. public function bills(): HasMany
  58. {
  59. return $this->documents()->where('type', 'bill');
  60. }
  61. public function invoices(): HasMany
  62. {
  63. return $this->documents()->where('type', 'invoice');
  64. }
  65. protected static function newFactory(): Factory
  66. {
  67. return CategoryFactory::new();
  68. }
  69. }