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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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->whereIn('category', [
  79. AccountCategory::Revenue,
  80. AccountCategory::Expense,
  81. ])
  82. ->whereNotIn('type', [
  83. AccountType::ContraRevenue,
  84. AccountType::ContraExpense,
  85. AccountType::UncategorizedRevenue,
  86. AccountType::UncategorizedExpense,
  87. ])
  88. ->whereDoesntHave('subtype', function (Builder $query) {
  89. $query->whereIn('name', [
  90. 'Receivables',
  91. 'Input Tax Recoverable',
  92. ]);
  93. })
  94. ->whereNotIn('name', [
  95. 'Gain on Foreign Exchange',
  96. 'Loss on Foreign Exchange',
  97. ])
  98. ->where('currency_code', CurrencyAccessor::getDefaultCurrency());
  99. }
  100. public function getLastTransactionDate(): ?string
  101. {
  102. $lastJournalEntryTransaction = $this->journalEntries()
  103. ->join('transactions', 'journal_entries.transaction_id', '=', 'transactions.id')
  104. ->max('transactions.posted_at');
  105. if ($lastJournalEntryTransaction) {
  106. return Carbon::parse($lastJournalEntryTransaction)->format('F j, Y');
  107. }
  108. return null;
  109. }
  110. protected function endingBalance(): Attribute
  111. {
  112. return Attribute::get(function () {
  113. $company = $this->company;
  114. $fiscalYearStart = $company->locale->fiscalYearStartDate();
  115. $fiscalYearEnd = $company->locale->fiscalYearEndDate();
  116. return Accounting::getEndingBalance($this, $fiscalYearStart, $fiscalYearEnd);
  117. });
  118. }
  119. public function isUncategorized(): bool
  120. {
  121. return $this->type->isUncategorized();
  122. }
  123. public function transactions(): HasMany
  124. {
  125. return $this->hasMany(Transaction::class, 'account_id');
  126. }
  127. public function journalEntries(): HasMany
  128. {
  129. return $this->hasMany(JournalEntry::class, 'account_id');
  130. }
  131. public static function getAccountsReceivableAccount(): self
  132. {
  133. return self::where('name', 'Accounts Receivable')->firstOrFail();
  134. }
  135. public static function getAccountsPayableAccount(): self
  136. {
  137. return self::where('name', 'Accounts Payable')->firstOrFail();
  138. }
  139. public static function getSalesDiscountAccount(): self
  140. {
  141. return self::where('name', 'Sales Discount')->firstOrFail();
  142. }
  143. public static function getPurchaseDiscountAccount(): self
  144. {
  145. return self::where('name', 'Purchase Discount')->firstOrFail();
  146. }
  147. protected static function newFactory(): Factory
  148. {
  149. return AccountFactory::new();
  150. }
  151. }