TransactionType::class, 'amount' => TransactionAmountCast::class, 'pending' => 'boolean', 'reviewed' => 'boolean', 'posted_at' => 'date', ]; public function account(): BelongsTo { return $this->belongsTo(Account::class, 'account_id'); } public function bankAccount(): BelongsTo { return $this->belongsTo(BankAccount::class, 'bank_account_id'); } public function contact(): BelongsTo { return $this->belongsTo(Contact::class, 'contact_id'); } public function journalEntries(): HasMany { return $this->hasMany(JournalEntry::class, 'transaction_id'); } public function transactionable(): MorphTo { return $this->morphTo(); } public function isUncategorized(): bool { return $this->journalEntries->contains(fn (JournalEntry $entry) => $entry->account->isUncategorized()); } public function updateAmountIfBalanced(): void { if ($this->journalEntries->areBalanced() && $this->journalEntries->sumDebits()->formatSimple() !== $this->getAttributeValue('amount')) { $this->setAttribute('amount', $this->journalEntries->sumDebits()->formatSimple()); $this->save(); } } protected static function newFactory(): Factory { return TransactionFactory::new(); } }