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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Facades\DB;
  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 scopeWithLastTransactionDate(Builder $query): Builder
  101. {
  102. return $query->addSelect([
  103. 'last_transaction_date' => JournalEntry::select(DB::raw('MAX(transactions.posted_at)'))
  104. ->join('transactions', 'journal_entries.transaction_id', '=', 'transactions.id')
  105. ->whereColumn('journal_entries.account_id', 'accounts.id')
  106. ->limit(1),
  107. ]);
  108. }
  109. protected function endingBalance(): Attribute
  110. {
  111. return Attribute::get(function () {
  112. $company = $this->company;
  113. $fiscalYearStart = $company->locale->fiscalYearStartDate();
  114. $fiscalYearEnd = $company->locale->fiscalYearEndDate();
  115. return Accounting::getEndingBalance($this, $fiscalYearStart, $fiscalYearEnd);
  116. });
  117. }
  118. public function isUncategorized(): bool
  119. {
  120. return $this->type->isUncategorized();
  121. }
  122. public function transactions(): HasMany
  123. {
  124. return $this->hasMany(Transaction::class, 'account_id');
  125. }
  126. public function journalEntries(): HasMany
  127. {
  128. return $this->hasMany(JournalEntry::class, 'account_id');
  129. }
  130. public static function getAccountsReceivableAccount(): self
  131. {
  132. return self::where('name', 'Accounts Receivable')->firstOrFail();
  133. }
  134. public static function getAccountsPayableAccount(): self
  135. {
  136. return self::where('name', 'Accounts Payable')->firstOrFail();
  137. }
  138. public static function getSalesDiscountAccount(): self
  139. {
  140. return self::where('name', 'Sales Discount')->firstOrFail();
  141. }
  142. public static function getPurchaseDiscountAccount(): self
  143. {
  144. return self::where('name', 'Purchase Discount')->firstOrFail();
  145. }
  146. protected static function newFactory(): Factory
  147. {
  148. return AccountFactory::new();
  149. }
  150. }