- Removido README.md padrão do Laravel (backend) - Removidos scripts de deploy (não mais necessários) - Atualizado copilot-instructions.md para novo fluxo - Adicionada documentação de auditoria do servidor - Sincronizado código de produção com repositório Novo workflow: - Trabalhamos diretamente em /root/webmoney (symlink para /var/www/webmoney) - Mudanças PHP são instantâneas - Mudanças React requerem 'npm run build' - Commit após validação funcional
105 lines
2.5 KiB
PHP
Executable File
105 lines
2.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class ProductSheetItem extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'product_sheet_id',
|
|
'name',
|
|
'type',
|
|
'amount',
|
|
'quantity',
|
|
'unit',
|
|
'unit_cost',
|
|
'sort_order',
|
|
];
|
|
|
|
protected $casts = [
|
|
'amount' => 'decimal:2',
|
|
'quantity' => 'decimal:4',
|
|
'unit_cost' => 'decimal:2',
|
|
'sort_order' => 'integer',
|
|
];
|
|
|
|
/**
|
|
* Tipos de componentes disponíveis
|
|
*/
|
|
const TYPE_PRODUCT_COST = 'product_cost';
|
|
const TYPE_PACKAGING = 'packaging';
|
|
const TYPE_LABEL = 'label';
|
|
const TYPE_SHIPPING = 'shipping';
|
|
const TYPE_HANDLING = 'handling';
|
|
const TYPE_OTHER = 'other';
|
|
|
|
/**
|
|
* Lista de tipos para validação
|
|
*/
|
|
public static function getTypes(): array
|
|
{
|
|
return [
|
|
self::TYPE_PRODUCT_COST => 'Custo do Produto',
|
|
self::TYPE_PACKAGING => 'Embalagem',
|
|
self::TYPE_LABEL => 'Etiqueta',
|
|
self::TYPE_SHIPPING => 'Frete',
|
|
self::TYPE_HANDLING => 'Manuseio',
|
|
self::TYPE_OTHER => 'Outro',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Relacionamento com ficha técnica
|
|
*/
|
|
public function productSheet(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ProductSheet::class);
|
|
}
|
|
|
|
/**
|
|
* Calcula o custo unitário
|
|
*
|
|
* @return float
|
|
*/
|
|
public function calculateUnitCost(): float
|
|
{
|
|
if ($this->quantity <= 0) {
|
|
return (float) $this->amount;
|
|
}
|
|
|
|
return round($this->amount / $this->quantity, 2);
|
|
}
|
|
|
|
/**
|
|
* Boot do modelo para recalcular automaticamente
|
|
*/
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
// Antes de salvar, calcular o custo unitário
|
|
static::saving(function ($item) {
|
|
$item->unit_cost = $item->calculateUnitCost();
|
|
});
|
|
|
|
// Após salvar, recalcular o CMV da ficha técnica
|
|
static::saved(function ($item) {
|
|
if ($item->productSheet) {
|
|
$item->productSheet->recalculateCmv();
|
|
}
|
|
});
|
|
|
|
// Após deletar, recalcular o CMV da ficha técnica
|
|
static::deleted(function ($item) {
|
|
if ($item->productSheet) {
|
|
$item->productSheet->recalculateCmv();
|
|
}
|
|
});
|
|
}
|
|
}
|