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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Concerns\Blamable;
  4. use App\Concerns\CompanyOwned;
  5. use App\Enums\Accounting\AccountCategory;
  6. use App\Enums\Accounting\AccountType;
  7. use App\Facades\Accounting;
  8. use App\Models\Banking\BankAccount;
  9. use App\Models\Setting\Currency;
  10. use App\Observers\AccountObserver;
  11. use App\Utilities\Currency\CurrencyAccessor;
  12. use Database\Factories\Accounting\AccountFactory;
  13. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  14. use Illuminate\Database\Eloquent\Builder;
  15. use Illuminate\Database\Eloquent\Casts\Attribute;
  16. use Illuminate\Database\Eloquent\Factories\Factory;
  17. use Illuminate\Database\Eloquent\Factories\HasFactory;
  18. use Illuminate\Database\Eloquent\Model;
  19. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  20. use Illuminate\Database\Eloquent\Relations\HasMany;
  21. use Illuminate\Database\Eloquent\Relations\HasOne;
  22. use Illuminate\Support\Carbon;
  23. #[ObservedBy(AccountObserver::class)]
  24. class Account extends Model
  25. {
  26. use Blamable;
  27. use CompanyOwned;
  28. use HasFactory;
  29. protected $table = 'accounts';
  30. protected $fillable = [
  31. 'company_id',
  32. 'subtype_id',
  33. 'parent_id',
  34. 'category',
  35. 'type',
  36. 'code',
  37. 'name',
  38. 'currency_code',
  39. 'description',
  40. 'archived',
  41. 'default',
  42. 'created_by',
  43. 'updated_by',
  44. ];
  45. protected $casts = [
  46. 'category' => AccountCategory::class,
  47. 'type' => AccountType::class,
  48. 'archived' => 'boolean',
  49. 'default' => 'boolean',
  50. ];
  51. public function subtype(): BelongsTo
  52. {
  53. return $this->belongsTo(AccountSubtype::class, 'subtype_id');
  54. }
  55. public function parent(): BelongsTo
  56. {
  57. return $this->belongsTo(__CLASS__, 'parent_id')
  58. ->whereKeyNot($this->getKey());
  59. }
  60. public function children(): HasMany
  61. {
  62. return $this->hasMany(__CLASS__, 'parent_id');
  63. }
  64. public function currency(): BelongsTo
  65. {
  66. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  67. }
  68. public function bankAccount(): HasOne
  69. {
  70. return $this->hasOne(BankAccount::class, 'account_id');
  71. }
  72. public function adjustment(): HasOne
  73. {
  74. return $this->hasOne(Adjustment::class, 'account_id');
  75. }
  76. public function scopeBudgetable(Builder $query): Builder
  77. {
  78. return $query->whereNotIn('category', [
  79. AccountCategory::Equity,
  80. AccountCategory::Liability,
  81. ])
  82. ->whereNotIn('type', [
  83. AccountType::ContraAsset,
  84. AccountType::ContraRevenue,
  85. AccountType::ContraExpense,
  86. AccountType::UncategorizedRevenue,
  87. AccountType::UncategorizedExpense,
  88. ])
  89. ->whereDoesntHave('subtype', function (Builder $query) {
  90. $query->whereIn('name', [
  91. 'Receivables',
  92. 'Input Tax Recoverable',
  93. ]);
  94. })
  95. ->whereNotIn('name', [
  96. 'Gain on Foreign Exchange',
  97. 'Loss on Foreign Exchange',
  98. ])
  99. ->where('currency_code', CurrencyAccessor::getDefaultCurrency());
  100. }
  101. public function getLastTransactionDate(): ?string
  102. {
  103. $lastJournalEntryTransaction = $this->journalEntries()
  104. ->join('transactions', 'journal_entries.transaction_id', '=', 'transactions.id')
  105. ->max('transactions.posted_at');
  106. if ($lastJournalEntryTransaction) {
  107. return Carbon::parse($lastJournalEntryTransaction)->format('F j, Y');
  108. }
  109. return null;
  110. }
  111. protected function endingBalance(): Attribute
  112. {
  113. return Attribute::get(function () {
  114. $company = $this->company;
  115. $fiscalYearStart = $company->locale->fiscalYearStartDate();
  116. $fiscalYearEnd = $company->locale->fiscalYearEndDate();
  117. return Accounting::getEndingBalance($this, $fiscalYearStart, $fiscalYearEnd);
  118. });
  119. }
  120. public function isUncategorized(): bool
  121. {
  122. return $this->type->isUncategorized();
  123. }
  124. public function transactions(): HasMany
  125. {
  126. return $this->hasMany(Transaction::class, 'account_id');
  127. }
  128. public function journalEntries(): HasMany
  129. {
  130. return $this->hasMany(JournalEntry::class, 'account_id');
  131. }
  132. public static function getAccountsReceivableAccount(): self
  133. {
  134. return self::where('name', 'Accounts Receivable')->firstOrFail();
  135. }
  136. public static function getAccountsPayableAccount(): self
  137. {
  138. return self::where('name', 'Accounts Payable')->firstOrFail();
  139. }
  140. public static function getSalesDiscountAccount(): self
  141. {
  142. return self::where('name', 'Sales Discount')->firstOrFail();
  143. }
  144. public static function getPurchaseDiscountAccount(): self
  145. {
  146. return self::where('name', 'Purchase Discount')->firstOrFail();
  147. }
  148. protected static function newFactory(): Factory
  149. {
  150. return AccountFactory::new();
  151. }
  152. }