up
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ProductVariantResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ProductVariantResource;
|
||||
use App\Models\ProductVariant;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Filament\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CreateProductVariant extends CreateRecord
|
||||
{
|
||||
protected static string $resource = ProductVariantResource::class;
|
||||
|
||||
protected function handleRecordCreation(array $data): ProductVariant
|
||||
{
|
||||
// Debug: Log de datos recibidos
|
||||
Log::info('Datos recibidos en createProductVariant:', $data);
|
||||
|
||||
// Extraer datos de bodega
|
||||
$bodegaId = $data['bodega_id'] ?? null;
|
||||
$stockInicial = $data['stock_inicial'] ?? 0;
|
||||
|
||||
Log::info('Bodega ID extraído:', ['bodega_id' => $bodegaId]);
|
||||
Log::info('Stock inicial extraído:', ['stock_inicial' => $stockInicial]);
|
||||
|
||||
// Remover campos que no pertenecen al modelo ProductVariant
|
||||
unset($data['bodega_id'], $data['stock_inicial']);
|
||||
|
||||
// Asegurar que stock esté en 0 en el modelo principal
|
||||
$data['stock'] = 0;
|
||||
|
||||
// Crear la variante
|
||||
$variante = ProductVariant::create($data);
|
||||
|
||||
Log::info('Variante creada:', ['id' => $variante->id]);
|
||||
|
||||
// Asignar a la bodega si se especificó
|
||||
if ($bodegaId) {
|
||||
$variante->bodegas()->attach($bodegaId, ['stock' => $stockInicial]);
|
||||
|
||||
Log::info('Relación bodega creada:', [
|
||||
'variante_id' => $variante->id,
|
||||
'bodega_id' => $bodegaId,
|
||||
'stock' => $stockInicial
|
||||
]);
|
||||
|
||||
$bodegaNombre = \App\Models\Bodega::find($bodegaId)->nombre;
|
||||
|
||||
Notification::make()
|
||||
->title('Variante creada exitosamente')
|
||||
->body("La variante se asignó a la bodega '{$bodegaNombre}' con stock de {$stockInicial} unidades.")
|
||||
->success()
|
||||
->send();
|
||||
}
|
||||
|
||||
return $variante;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user