- Habilita entradas, salidas y traspasos masivos con validación. - Implementa cálculo de costo promedio ponderado y campo de costo unitario. - Agrega filtro por almacén y ajusta manejo de costos nulos.
65 lines
2.1 KiB
PHP
65 lines
2.1 KiB
PHP
<?php namespace App\Services;
|
|
|
|
use App\Models\Inventory;
|
|
use App\Models\Price;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ProductService
|
|
{
|
|
public function createProduct(array $data)
|
|
{
|
|
return DB::transaction(function () use ($data) {
|
|
$inventory = Inventory::create([
|
|
'name' => $data['name'],
|
|
'sku' => $data['sku'],
|
|
'barcode' => $data['barcode'] ?? null,
|
|
'category_id' => $data['category_id'],
|
|
'track_serials' => $data['track_serials'] ?? false,
|
|
]);
|
|
|
|
Price::create([
|
|
'inventory_id' => $inventory->id,
|
|
'cost' => $data['cost'] ?? 0,
|
|
'retail_price' => $data['retail_price'],
|
|
'tax' => $data['tax'] ?? 16.00,
|
|
]);
|
|
|
|
return $inventory->load(['category', 'price']);
|
|
});
|
|
}
|
|
|
|
public function updateProduct(Inventory $inventory, array $data)
|
|
{
|
|
return DB::transaction(function () use ($inventory, $data) {
|
|
// Actualizar campos de Inventory solo si están presentes
|
|
$inventoryData = array_filter([
|
|
'name' => $data['name'] ?? null,
|
|
'sku' => $data['sku'] ?? null,
|
|
'barcode' => $data['barcode'] ?? null,
|
|
'category_id' => $data['category_id'] ?? null,
|
|
'track_serials' => $data['track_serials'] ?? null,
|
|
], fn($value) => $value !== null);
|
|
|
|
if (!empty($inventoryData)) {
|
|
$inventory->update($inventoryData);
|
|
}
|
|
|
|
// Actualizar campos de Price solo si están presentes
|
|
$priceData = array_filter([
|
|
'cost' => $data['cost'] ?? null,
|
|
'retail_price' => $data['retail_price'] ?? null,
|
|
'tax' => $data['tax'] ?? null,
|
|
], fn($value) => $value !== null);
|
|
|
|
if (!empty($priceData)) {
|
|
$inventory->price()->updateOrCreate(
|
|
['inventory_id' => $inventory->id],
|
|
$priceData
|
|
);
|
|
}
|
|
|
|
return $inventory->fresh(['category', 'price']);
|
|
});
|
|
}
|
|
}
|