123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
-
- namespace App\Models\Accounting;
-
- use App\Casts\MoneyCast;
- use App\Models\Banking\BankAccount;
- use App\Traits\Blamable;
- use App\Traits\CompanyOwned;
- use Database\Factories\Accounting\JournalEntryFactory;
- use Illuminate\Database\Eloquent\Factories\Factory;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Wallo\FilamentCompanies\FilamentCompanies;
-
- class JournalEntry extends Model
- {
- use Blamable;
- use CompanyOwned;
- use HasFactory;
-
- protected $fillable = [
- 'company_id',
- 'account_id',
- 'transaction_id',
- 'type', // debit or credit
- 'amount',
- 'description',
- 'created_by',
- 'updated_by',
- ];
-
- protected $casts = [
- 'amount' => MoneyCast::class,
- ];
-
- public function company(): BelongsTo
- {
- return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
- }
-
- public function account(): BelongsTo
- {
- return $this->belongsTo(Account::class, 'account_id');
- }
-
- public function transaction(): BelongsTo
- {
- return $this->belongsTo(Transaction::class, 'transaction_id');
- }
-
- public function scopeDebit($query)
- {
- return $query->where('type', 'debit');
- }
-
- public function scopeCredit($query)
- {
- return $query->where('type', 'credit');
- }
-
- public function bankAccount(): BelongsTo
- {
- return $this->account()->where('accountable_type', BankAccount::class);
- }
-
- public function createdBy(): BelongsTo
- {
- return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
- }
-
- public function updatedBy(): BelongsTo
- {
- return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
- }
-
- protected static function newFactory(): Factory
- {
- return JournalEntryFactory::new();
- }
- }
|