<?php

namespace Modules\Supplier\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\BuyFactor\Entities\BuyFactor;
use Modules\Cheque\Entities\Cheque;
use Modules\Payment\Entities\Payment;

class Supplier extends Model
{
    protected $guarded=['id'];

    protected $appends=['total','paid'];

    public function cheques()
    {
        return $this->morphMany(Cheque::class, 'exporterable');
    }

    public function payments()
    {
        return $this->hasMany(Payment::class);
    }

    public function factors()
    {
        return $this->hasMany(BuyFactor::class);
    }

    public function getTotalAttribute()
    {
        return $this->factors()->sum('price');
    }

    public function getPaidAttribute()
    {
        return $this->payments()->sum('price');
    }
}
