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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. ];
  45. protected $casts = [
  46. 'type' => TransactionType::class,
  47. 'payment_method' => PaymentMethod::class,
  48. 'amount' => TransactionAmountCast::class,
  49. 'pending' => 'boolean',
  50. 'reviewed' => 'boolean',
  51. 'posted_at' => 'date',
  52. ];
  53. public function account(): BelongsTo
  54. {
  55. return $this->belongsTo(Account::class, 'account_id');
  56. }
  57. public function bankAccount(): BelongsTo
  58. {
  59. return $this->belongsTo(BankAccount::class, 'bank_account_id');
  60. }
  61. public function contact(): BelongsTo
  62. {
  63. return $this->belongsTo(Contact::class, 'contact_id');
  64. }
  65. public function journalEntries(): HasMany
  66. {
  67. return $this->hasMany(JournalEntry::class, 'transaction_id');
  68. }
  69. public function transactionable(): MorphTo
  70. {
  71. return $this->morphTo();
  72. }
  73. public function isUncategorized(): bool
  74. {
  75. return $this->journalEntries->contains(fn (JournalEntry $entry) => $entry->account->isUncategorized());
  76. }
  77. public function updateAmountIfBalanced(): void
  78. {
  79. if ($this->journalEntries->areBalanced() && $this->journalEntries->sumDebits()->formatSimple() !== $this->getAttributeValue('amount')) {
  80. $this->setAttribute('amount', $this->journalEntries->sumDebits()->formatSimple());
  81. $this->save();
  82. }
  83. }
  84. protected static function newFactory(): Factory
  85. {
  86. return TransactionFactory::new();
  87. }
  88. }