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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Casts\MoneyCast;
  4. use App\Enums\Accounting\AccountCategory;
  5. use App\Enums\Accounting\AccountType;
  6. use App\Models\Setting\Category;
  7. use App\Models\Setting\Currency;
  8. use App\Traits\Blamable;
  9. use App\Traits\CompanyOwned;
  10. use Database\Factories\Accounting\AccountFactory;
  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\HasManyThrough;
  17. use Illuminate\Database\Eloquent\Relations\MorphTo;
  18. use Wallo\FilamentCompanies\FilamentCompanies;
  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. 'starting_balance',
  35. 'debit_balance',
  36. 'credit_balance',
  37. 'net_movement',
  38. 'ending_balance',
  39. 'description',
  40. 'active',
  41. 'default',
  42. 'accountable_id',
  43. 'accountable_type',
  44. 'created_by',
  45. 'updated_by',
  46. ];
  47. protected $casts = [
  48. 'category' => AccountCategory::class,
  49. 'type' => AccountType::class,
  50. 'starting_balance' => MoneyCast::class,
  51. 'debit_balance' => MoneyCast::class,
  52. 'credit_balance' => MoneyCast::class,
  53. 'ending_balance' => MoneyCast::class,
  54. 'active' => 'boolean',
  55. 'default' => 'boolean',
  56. ];
  57. public function company(): BelongsTo
  58. {
  59. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  60. }
  61. public function categories(): HasMany
  62. {
  63. return $this->hasMany(Category::class, 'account_id');
  64. }
  65. public function subtype(): BelongsTo
  66. {
  67. return $this->belongsTo(AccountSubtype::class, 'subtype_id');
  68. }
  69. public function parent(): BelongsTo
  70. {
  71. return $this->belongsTo(__CLASS__, 'parent_id')
  72. ->whereKeyNot($this->getKey());
  73. }
  74. public function children(): HasMany
  75. {
  76. return $this->hasMany(__CLASS__, 'parent_id');
  77. }
  78. public function currency(): BelongsTo
  79. {
  80. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  81. }
  82. public function createdBy(): BelongsTo
  83. {
  84. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  85. }
  86. public function updatedBy(): BelongsTo
  87. {
  88. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  89. }
  90. public function accountable(): MorphTo
  91. {
  92. return $this->morphTo();
  93. }
  94. public function transactions(): HasManyThrough
  95. {
  96. return $this->hasManyThrough(
  97. Transaction::class,
  98. JournalEntry::class,
  99. 'account_id',
  100. 'id',
  101. 'id',
  102. 'transaction_id',
  103. );
  104. }
  105. public function journalEntries(): HasMany
  106. {
  107. return $this->hasMany(JournalEntry::class, 'account_id');
  108. }
  109. protected static function newFactory(): Factory
  110. {
  111. return AccountFactory::new();
  112. }
  113. }