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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Setting\Currency;
  9. use App\Observers\AccountObserver;
  10. use Database\Factories\Accounting\AccountFactory;
  11. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  12. use Illuminate\Database\Eloquent\Casts\Attribute;
  13. use Illuminate\Database\Eloquent\Factories\Factory;
  14. use Illuminate\Database\Eloquent\Factories\HasFactory;
  15. use Illuminate\Database\Eloquent\Model;
  16. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  17. use Illuminate\Database\Eloquent\Relations\HasMany;
  18. use Illuminate\Database\Eloquent\Relations\MorphTo;
  19. use Illuminate\Support\Carbon;
  20. #[ObservedBy(AccountObserver::class)]
  21. class Account extends Model
  22. {
  23. use Blamable;
  24. use CompanyOwned;
  25. use HasFactory;
  26. protected $table = 'accounts';
  27. protected $fillable = [
  28. 'company_id',
  29. 'subtype_id',
  30. 'parent_id',
  31. 'category',
  32. 'type',
  33. 'code',
  34. 'name',
  35. 'currency_code',
  36. 'description',
  37. 'active',
  38. 'default',
  39. 'accountable_id',
  40. 'accountable_type',
  41. 'created_by',
  42. 'updated_by',
  43. ];
  44. protected $casts = [
  45. 'category' => AccountCategory::class,
  46. 'type' => AccountType::class,
  47. 'active' => 'boolean',
  48. 'default' => 'boolean',
  49. ];
  50. public function subtype(): BelongsTo
  51. {
  52. return $this->belongsTo(AccountSubtype::class, 'subtype_id');
  53. }
  54. public function parent(): BelongsTo
  55. {
  56. return $this->belongsTo(__CLASS__, 'parent_id')
  57. ->whereKeyNot($this->getKey());
  58. }
  59. public function children(): HasMany
  60. {
  61. return $this->hasMany(__CLASS__, 'parent_id');
  62. }
  63. public function currency(): BelongsTo
  64. {
  65. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  66. }
  67. public function accountable(): MorphTo
  68. {
  69. return $this->morphTo();
  70. }
  71. public function getLastTransactionDate(): ?string
  72. {
  73. $lastJournalEntryTransaction = $this->journalEntries()
  74. ->join('transactions', 'journal_entries.transaction_id', '=', 'transactions.id')
  75. ->max('transactions.posted_at');
  76. if ($lastJournalEntryTransaction) {
  77. return Carbon::parse($lastJournalEntryTransaction)->format('F j, Y');
  78. }
  79. return null;
  80. }
  81. protected function endingBalance(): Attribute
  82. {
  83. return Attribute::get(function () {
  84. $company = $this->company;
  85. $fiscalYearStart = $company->locale->fiscalYearStartDate();
  86. $fiscalYearEnd = $company->locale->fiscalYearEndDate();
  87. return Accounting::getEndingBalance($this, $fiscalYearStart, $fiscalYearEnd);
  88. });
  89. }
  90. public function isUncategorized(): bool
  91. {
  92. return $this->type->isUncategorized();
  93. }
  94. public function transactions(): HasMany
  95. {
  96. return $this->hasMany(Transaction::class, 'account_id');
  97. }
  98. public function journalEntries(): HasMany
  99. {
  100. return $this->hasMany(JournalEntry::class, 'account_id');
  101. }
  102. protected static function newFactory(): Factory
  103. {
  104. return AccountFactory::new();
  105. }
  106. }