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 2.0KB

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