Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Account.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Attributes\Scope;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Database\Eloquent\Casts\Attribute;
  17. use Illuminate\Database\Eloquent\Factories\Factory;
  18. use Illuminate\Database\Eloquent\Factories\HasFactory;
  19. use Illuminate\Database\Eloquent\Model;
  20. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  21. use Illuminate\Database\Eloquent\Relations\HasMany;
  22. use Illuminate\Database\Eloquent\Relations\HasOne;
  23. use Illuminate\Support\Facades\DB;
  24. #[ObservedBy(AccountObserver::class)]
  25. class Account extends Model
  26. {
  27. use Blamable;
  28. use CompanyOwned;
  29. use HasFactory;
  30. protected $table = 'accounts';
  31. protected $fillable = [
  32. 'company_id',
  33. 'subtype_id',
  34. 'parent_id',
  35. 'category',
  36. 'type',
  37. 'code',
  38. 'name',
  39. 'currency_code',
  40. 'description',
  41. 'archived',
  42. 'default',
  43. 'created_by',
  44. 'updated_by',
  45. ];
  46. protected $casts = [
  47. 'category' => AccountCategory::class,
  48. 'type' => AccountType::class,
  49. 'archived' => 'boolean',
  50. 'default' => 'boolean',
  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 bankAccount(): HasOne
  70. {
  71. return $this->hasOne(BankAccount::class, 'account_id');
  72. }
  73. public function adjustment(): HasOne
  74. {
  75. return $this->hasOne(Adjustment::class, 'account_id');
  76. }
  77. #[Scope]
  78. protected function budgetable(Builder $query): Builder
  79. {
  80. return $query->whereIn('category', [
  81. AccountCategory::Revenue,
  82. AccountCategory::Expense,
  83. ])
  84. ->whereNotIn('type', [
  85. AccountType::ContraRevenue,
  86. AccountType::ContraExpense,
  87. AccountType::UncategorizedRevenue,
  88. AccountType::UncategorizedExpense,
  89. ])
  90. ->whereDoesntHave('subtype', function (Builder $query) {
  91. $query->whereIn('name', [
  92. 'Receivables',
  93. 'Input Tax Recoverable',
  94. ]);
  95. })
  96. ->whereNotIn('name', [
  97. 'Gain on Foreign Exchange',
  98. 'Loss on Foreign Exchange',
  99. ])
  100. ->where('currency_code', CurrencyAccessor::getDefaultCurrency());
  101. }
  102. #[Scope]
  103. protected function withLastTransactionDate(Builder $query): Builder
  104. {
  105. return $query->addSelect([
  106. 'last_transaction_date' => JournalEntry::select(DB::raw('MAX(transactions.posted_at)'))
  107. ->join('transactions', 'journal_entries.transaction_id', '=', 'transactions.id')
  108. ->whereColumn('journal_entries.account_id', 'accounts.id')
  109. ->limit(1),
  110. ]);
  111. }
  112. protected function endingBalance(): Attribute
  113. {
  114. return Attribute::get(function () {
  115. $company = $this->company;
  116. $fiscalYearStart = $company->locale->fiscalYearStartDate();
  117. $fiscalYearEnd = $company->locale->fiscalYearEndDate();
  118. return Accounting::getEndingBalance($this, $fiscalYearStart, $fiscalYearEnd);
  119. });
  120. }
  121. public function isUncategorized(): bool
  122. {
  123. return $this->type->isUncategorized();
  124. }
  125. public function transactions(): HasMany
  126. {
  127. return $this->hasMany(Transaction::class, 'account_id');
  128. }
  129. public function journalEntries(): HasMany
  130. {
  131. return $this->hasMany(JournalEntry::class, 'account_id');
  132. }
  133. public static function getAccountsReceivableAccount(?int $companyId = null): self
  134. {
  135. return self::where('name', 'Accounts Receivable')
  136. ->when($companyId, function (Builder $query) use ($companyId) {
  137. $query->where('company_id', $companyId);
  138. })
  139. ->firstOrFail();
  140. }
  141. public static function getAccountsPayableAccount(?int $companyId = null): self
  142. {
  143. return self::where('name', 'Accounts Payable')
  144. ->when($companyId, function (Builder $query) use ($companyId) {
  145. $query->where('company_id', $companyId);
  146. })
  147. ->firstOrFail();
  148. }
  149. public static function getSalesDiscountAccount(?int $companyId = null): self
  150. {
  151. return self::where('name', 'Sales Discount')
  152. ->when($companyId, function (Builder $query) use ($companyId) {
  153. $query->where('company_id', $companyId);
  154. })
  155. ->firstOrFail();
  156. }
  157. public static function getPurchaseDiscountAccount(?int $companyId = null): self
  158. {
  159. return self::where('name', 'Purchase Discount')
  160. ->when($companyId, function (Builder $query) use ($companyId) {
  161. $query->where('company_id', $companyId);
  162. })
  163. ->firstOrFail();
  164. }
  165. protected static function newFactory(): Factory
  166. {
  167. return AccountFactory::new();
  168. }
  169. }