Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Account.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Database\Factories\Accounting\AccountFactory;
  12. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  13. use Illuminate\Database\Eloquent\Casts\Attribute;
  14. use Illuminate\Database\Eloquent\Factories\Factory;
  15. use Illuminate\Database\Eloquent\Factories\HasFactory;
  16. use Illuminate\Database\Eloquent\Model;
  17. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  18. use Illuminate\Database\Eloquent\Relations\HasMany;
  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. 'archived',
  38. 'default',
  39. 'bank_account_id',
  40. 'created_by',
  41. 'updated_by',
  42. ];
  43. protected $casts = [
  44. 'category' => AccountCategory::class,
  45. 'type' => AccountType::class,
  46. 'archived' => 'boolean',
  47. 'default' => 'boolean',
  48. ];
  49. public function subtype(): BelongsTo
  50. {
  51. return $this->belongsTo(AccountSubtype::class, 'subtype_id');
  52. }
  53. public function parent(): BelongsTo
  54. {
  55. return $this->belongsTo(__CLASS__, 'parent_id')
  56. ->whereKeyNot($this->getKey());
  57. }
  58. public function children(): HasMany
  59. {
  60. return $this->hasMany(__CLASS__, 'parent_id');
  61. }
  62. public function currency(): BelongsTo
  63. {
  64. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  65. }
  66. public function bankAccount(): BelongsTo
  67. {
  68. return $this->belongsTo(BankAccount::class, 'bank_account_id');
  69. }
  70. public function getLastTransactionDate(): ?string
  71. {
  72. $lastJournalEntryTransaction = $this->journalEntries()
  73. ->join('transactions', 'journal_entries.transaction_id', '=', 'transactions.id')
  74. ->max('transactions.posted_at');
  75. if ($lastJournalEntryTransaction) {
  76. return Carbon::parse($lastJournalEntryTransaction)->format('F j, Y');
  77. }
  78. return null;
  79. }
  80. protected function endingBalance(): Attribute
  81. {
  82. return Attribute::get(function () {
  83. $company = $this->company;
  84. $fiscalYearStart = $company->locale->fiscalYearStartDate();
  85. $fiscalYearEnd = $company->locale->fiscalYearEndDate();
  86. return Accounting::getEndingBalance($this, $fiscalYearStart, $fiscalYearEnd);
  87. });
  88. }
  89. public function isUncategorized(): bool
  90. {
  91. return $this->type->isUncategorized();
  92. }
  93. public function transactions(): HasMany
  94. {
  95. return $this->hasMany(Transaction::class, 'account_id');
  96. }
  97. public function journalEntries(): HasMany
  98. {
  99. return $this->hasMany(JournalEntry::class, 'account_id');
  100. }
  101. protected static function newFactory(): Factory
  102. {
  103. return AccountFactory::new();
  104. }
  105. }