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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Casts\MoneyCast;
  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. 'contact_id',
  28. 'type', // 'deposit', 'withdrawal', 'journal'
  29. 'payment_channel',
  30. 'description',
  31. 'notes',
  32. 'reference',
  33. 'amount',
  34. 'pending',
  35. 'reviewed',
  36. 'posted_at',
  37. 'created_by',
  38. 'updated_by',
  39. ];
  40. protected $casts = [
  41. 'type' => TransactionType::class,
  42. 'amount' => MoneyCast::class,
  43. 'pending' => 'boolean',
  44. 'reviewed' => 'boolean',
  45. 'posted_at' => 'datetime',
  46. ];
  47. public function account(): BelongsTo
  48. {
  49. return $this->belongsTo(Account::class, 'account_id');
  50. }
  51. public function bankAccount(): BelongsTo
  52. {
  53. return $this->belongsTo(BankAccount::class, 'bank_account_id');
  54. }
  55. public function contact(): BelongsTo
  56. {
  57. return $this->belongsTo(Contact::class, 'contact_id');
  58. }
  59. public function journalEntries(): HasMany
  60. {
  61. return $this->hasMany(JournalEntry::class, 'transaction_id');
  62. }
  63. public function isUncategorized(): bool
  64. {
  65. return $this->journalEntries->contains(fn (JournalEntry $entry) => $entry->account->isUncategorized());
  66. }
  67. protected static function newFactory(): Factory
  68. {
  69. return TransactionFactory::new();
  70. }
  71. }