<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * Model Supplier
 * Merepresentasikan pemasok bahan mentah.
 */
class Supplier extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'phone',
        'address',
        'email',
    ];

    // ──────────────────────────────────────────
    // Relationships
    // ──────────────────────────────────────────

    /** Semua transaksi pembelian dari supplier ini */
    public function pembelians()
    {
        return $this->hasMany(Pembelian::class);
    }

    // ──────────────────────────────────────────
    // Accessors
    // ──────────────────────────────────────────

    /** Total nilai pembelian dari supplier ini */
    public function getTotalPembelianAttribute(): float
    {
        return $this->pembelians()->sum(\DB::raw('qty * harga_satuan'));
    }
}
