- Add business_settings table for Markup configuration - Add product_sheets table for product technical sheets (CMV) - Add product_sheet_items table for cost components - Create BusinessSetting model with calculateMarkup() method - Create ProductSheet model with recalculateCmv() method - Create ProductSheetItem model for cost breakdown - Add BusinessSettingController with CRUD + simulate-price endpoint - Add ProductSheetController with CRUD + items management + duplicate - Add Business page with 3 tabs (Settings, Products, Calculator) - Add BusinessSettingsTab component with markup cards - Add ProductSheetsTab component with product grid - Add PriceCalculatorTab component with interactive simulator - Add i18n translations in ES, PT-BR, EN - Multi-currency support (EUR, BRL, USD)
105 lines
2.5 KiB
PHP
105 lines
2.5 KiB
PHP
<?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();
|
|
}
|
|
});
|
|
}
|
|
}
|