<?php

namespace Modules\MedAccounting\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * One debit/credit leg of a journal entry (SIFA P7-T2). Exactly one of debit /
 * credit is non-zero. Belongs to a COA account and, optionally, a cost centre.
 */
class MedaccJournalLine extends Model
{
    protected $table = 'medacc_journal_lines';

    protected $guarded = ['id'];

    protected $casts = [
        'debit'  => 'decimal:4',
        'credit' => 'decimal:4',
    ];

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    public function entry()
    {
        return $this->belongsTo(MedaccJournalEntry::class, 'journal_entry_id');
    }

    public function account()
    {
        return $this->belongsTo(MedaccAccount::class, 'account_id');
    }

    public function costCenter()
    {
        return $this->belongsTo(MedaccCostCenter::class, 'cost_center_id');
    }
}
