$data['name'], 'sku' => $data['sku'], 'barcode' => $data['barcode'] ?? null, 'category_id' => $data['category_id'], 'stock' => $data['stock'] ?? 0, ]); $price = Price::create([ 'inventory_id' => $inventory->id, 'cost' => $data['cost'], '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, 'stock' => $data['stock'] ?? 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']); }); } }