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.

JournalEntry.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Casts\MoneyCast;
  4. use App\Models\Banking\BankAccount;
  5. use App\Traits\Blamable;
  6. use App\Traits\CompanyOwned;
  7. use Database\Factories\Accounting\JournalEntryFactory;
  8. use Illuminate\Database\Eloquent\Factories\Factory;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  12. use Wallo\FilamentCompanies\FilamentCompanies;
  13. class JournalEntry extends Model
  14. {
  15. use Blamable;
  16. use CompanyOwned;
  17. use HasFactory;
  18. protected $fillable = [
  19. 'company_id',
  20. 'account_id',
  21. 'transaction_id',
  22. 'type', // debit or credit
  23. 'amount',
  24. 'description',
  25. 'created_by',
  26. 'updated_by',
  27. ];
  28. protected $casts = [
  29. 'amount' => MoneyCast::class,
  30. ];
  31. public function company(): BelongsTo
  32. {
  33. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  34. }
  35. public function account(): BelongsTo
  36. {
  37. return $this->belongsTo(Account::class, 'account_id');
  38. }
  39. public function transaction(): BelongsTo
  40. {
  41. return $this->belongsTo(Transaction::class, 'transaction_id');
  42. }
  43. public function scopeDebit($query)
  44. {
  45. return $query->where('type', 'debit');
  46. }
  47. public function scopeCredit($query)
  48. {
  49. return $query->where('type', 'credit');
  50. }
  51. public function bankAccount(): BelongsTo
  52. {
  53. return $this->account()->where('accountable_type', BankAccount::class);
  54. }
  55. public function createdBy(): BelongsTo
  56. {
  57. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  58. }
  59. public function updatedBy(): BelongsTo
  60. {
  61. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  62. }
  63. protected static function newFactory(): Factory
  64. {
  65. return JournalEntryFactory::new();
  66. }
  67. }