<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Supplier extends Model
{
    protected $fillable = ['name', 'phone', 'email', 'address', 'opening_balance', 'notes'];
    protected function casts(): array { return ['opening_balance' => 'decimal:2']; }
    public function purchases(): HasMany { return $this->hasMany(Purchase::class); }
    public function payments(): HasMany { return $this->hasMany(SupplierPayment::class); }
    public function getBalanceAttribute(): float { return (float) $this->opening_balance + (float) $this->purchases()->sum('due_amount'); }
}
