<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Traits\Auditable;

class BudgetItem extends Model
{
    use Auditable;

    protected $keyType = 'string';
    public $incrementing = false;

    protected $fillable = [
        'id',
        'budget_id',
        'medical_service_id',
        'quantity',
        'price',
        'subtotal',
        'notes'
    ];

    protected $casts = [
        'quantity' => 'integer',
        'price' => 'decimal:2',
        'subtotal' => 'decimal:2'
    ];

    public function budget()
    {
        return $this->belongsTo("App\Models\Budget", 'budget_id');
    }

    public function medicalService()
    {
        return $this->belongsTo("App\Models\MedicalService", 'medical_service_id');
    }

    public function calculateSubtotal()
    {
        $this->subtotal = $this->quantity * $this->price;
        $this->save();
        
        // Recalcular el total del presupuesto
        $this->budget->calculateTotal();
    }
} 