<?php

namespace App\Models;

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

class Purchase extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $casts = [
        'module_id' => 'integer',
        'store_id' => 'integer',
        'supplier_id' => 'integer',
        'voucher_id' => 'integer',
        'purchase_date' => 'date',
        'total_quantity' => 'integer',
        'total_amount' => 'float',
        'paid_amount' => 'float',
        'due_amount' => 'float',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    public function items()
    {
        return $this->hasMany(PurchaseItem::class)->latest();
    }

    public function module()
    {
        return $this->belongsTo(Module::class);
    }

    public function store()
    {
        return $this->belongsTo(Store::class);
    }

    public function createdByAdmin()
    {
        return $this->belongsTo(Admin::class, 'created_by_admin_id');
    }

    public function supplier()
    {
        return $this->belongsTo(Supplier::class);
    }

    public function voucher()
    {
        return $this->belongsTo(AccountingVoucher::class, 'voucher_id');
    }
}
