64 lines
1.4 KiB
PHP
64 lines
1.4 KiB
PHP
<?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;
|
||
}
|
||
}
|