您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Category.php 2.3KB

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