您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Account.php 3.4KB

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