webmoney/backend/app/Models/ServiceSheetItem.php

64 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ServiceSheetItem extends Model
{
use HasFactory;
protected $fillable = [
'service_sheet_id',
'name',
'type',
'unit_cost',
'quantity_used',
'unit',
'notes',
];
protected $casts = [
'unit_cost' => 'decimal:2',
'quantity_used' => 'decimal:4',
];
/**
* Tipos de item disponíveis
*/
public const TYPES = [
'supply' => 'Insumo',
'consumable' => 'Descartável',
'material' => 'Material',
'equipment_usage' => 'Uso de Equipamento',
'other' => 'Outro',
];
/**
* Relacionamento com ficha de serviço
*/
public function serviceSheet(): BelongsTo
{
return $this->belongsTo(ServiceSheet::class);
}
/**
* Calcula o custo total do item
* Custo Total = Custo Unitário × Quantidade Usada
*/
public function getTotalCostAttribute(): float
{
return round((float) $this->unit_cost * (float) $this->quantity_used, 2);
}
/**
* Retorna o nome do tipo traduzido
*/
public function getTypeNameAttribute(): string
{
return self::TYPES[$this->type] ?? $this->type;
}
}