Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Transaction.php 2.6KB

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