You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Account.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Enums\Accounting\AccountCategory;
  4. use App\Enums\Accounting\AccountType;
  5. use App\Models\Setting\Currency;
  6. use App\Observers\AccountObserver;
  7. use App\Traits\Blamable;
  8. use App\Traits\CompanyOwned;
  9. use Database\Factories\Accounting\AccountFactory;
  10. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  11. use Illuminate\Database\Eloquent\Factories\Factory;
  12. use Illuminate\Database\Eloquent\Factories\HasFactory;
  13. use Illuminate\Database\Eloquent\Model;
  14. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  15. use Illuminate\Database\Eloquent\Relations\HasMany;
  16. use Illuminate\Database\Eloquent\Relations\MorphTo;
  17. use Wallo\FilamentCompanies\FilamentCompanies;
  18. #[ObservedBy(AccountObserver::class)]
  19. class Account extends Model
  20. {
  21. use Blamable;
  22. use CompanyOwned;
  23. use HasFactory;
  24. protected $table = 'accounts';
  25. protected $fillable = [
  26. 'company_id',
  27. 'subtype_id',
  28. 'parent_id',
  29. 'category',
  30. 'type',
  31. 'code',
  32. 'name',
  33. 'currency_code',
  34. 'description',
  35. 'active',
  36. 'default',
  37. 'accountable_id',
  38. 'accountable_type',
  39. 'created_by',
  40. 'updated_by',
  41. ];
  42. protected $casts = [
  43. 'category' => AccountCategory::class,
  44. 'type' => AccountType::class,
  45. 'active' => 'boolean',
  46. 'default' => 'boolean',
  47. ];
  48. public function company(): BelongsTo
  49. {
  50. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  51. }
  52. public function subtype(): BelongsTo
  53. {
  54. return $this->belongsTo(AccountSubtype::class, 'subtype_id');
  55. }
  56. public function parent(): BelongsTo
  57. {
  58. return $this->belongsTo(__CLASS__, 'parent_id')
  59. ->whereKeyNot($this->getKey());
  60. }
  61. public function children(): HasMany
  62. {
  63. return $this->hasMany(__CLASS__, 'parent_id');
  64. }
  65. public function currency(): BelongsTo
  66. {
  67. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  68. }
  69. public function createdBy(): BelongsTo
  70. {
  71. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  72. }
  73. public function updatedBy(): BelongsTo
  74. {
  75. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  76. }
  77. public function accountable(): MorphTo
  78. {
  79. return $this->morphTo();
  80. }
  81. public function transactions(): HasMany
  82. {
  83. return $this->hasMany(Transaction::class, 'account_id');
  84. }
  85. public function journalEntries(): HasMany
  86. {
  87. return $this->hasMany(JournalEntry::class, 'account_id');
  88. }
  89. protected static function newFactory(): Factory
  90. {
  91. return AccountFactory::new();
  92. }
  93. }