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.

Transaction.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Casts\TransactionAmountCast;
  4. use App\Concerns\Blamable;
  5. use App\Concerns\CompanyOwned;
  6. use App\Enums\Accounting\PaymentMethod;
  7. use App\Enums\Accounting\TransactionType;
  8. use App\Models\Banking\BankAccount;
  9. use App\Models\Common\Contact;
  10. use App\Observers\TransactionObserver;
  11. use Database\Factories\Accounting\TransactionFactory;
  12. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  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. #[ObservedBy(TransactionObserver::class)]
  20. class Transaction extends Model
  21. {
  22. use Blamable;
  23. use CompanyOwned;
  24. use HasFactory;
  25. protected $fillable = [
  26. 'company_id',
  27. 'account_id', // Account from Chart of Accounts (Income/Expense accounts)
  28. 'bank_account_id', // Cash/Bank Account
  29. 'plaid_transaction_id',
  30. 'contact_id',
  31. 'type', // 'deposit', 'withdrawal', 'journal'
  32. 'payment_channel',
  33. 'payment_method',
  34. 'is_payment',
  35. 'description',
  36. 'notes',
  37. 'reference',
  38. 'amount',
  39. 'pending',
  40. 'reviewed',
  41. 'posted_at',
  42. 'created_by',
  43. 'updated_by',
  44. 'meta',
  45. ];
  46. protected $casts = [
  47. 'type' => TransactionType::class,
  48. 'payment_method' => PaymentMethod::class,
  49. 'amount' => TransactionAmountCast::class,
  50. 'pending' => 'boolean',
  51. 'reviewed' => 'boolean',
  52. 'posted_at' => 'date',
  53. 'meta' => 'array',
  54. ];
  55. public function account(): BelongsTo
  56. {
  57. return $this->belongsTo(Account::class, 'account_id');
  58. }
  59. public function bankAccount(): BelongsTo
  60. {
  61. return $this->belongsTo(BankAccount::class, 'bank_account_id');
  62. }
  63. public function contact(): BelongsTo
  64. {
  65. return $this->belongsTo(Contact::class, 'contact_id');
  66. }
  67. public function journalEntries(): HasMany
  68. {
  69. return $this->hasMany(JournalEntry::class, 'transaction_id');
  70. }
  71. public function transactionable(): MorphTo
  72. {
  73. return $this->morphTo();
  74. }
  75. public function isUncategorized(): bool
  76. {
  77. return $this->journalEntries->contains(fn (JournalEntry $entry) => $entry->account->isUncategorized());
  78. }
  79. public function updateAmountIfBalanced(): void
  80. {
  81. if ($this->journalEntries->areBalanced() && $this->journalEntries->sumDebits()->formatSimple() !== $this->getAttributeValue('amount')) {
  82. $this->setAttribute('amount', $this->journalEntries->sumDebits()->formatSimple());
  83. $this->save();
  84. }
  85. }
  86. protected static function newFactory(): Factory
  87. {
  88. return TransactionFactory::new();
  89. }
  90. }