up
This commit is contained in:
@@ -11,19 +11,48 @@ class CreateOjalPresilla extends CreateRecord
|
||||
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
$tipo = $data['tipo'] ?? 'ojal';
|
||||
unset($data['tipo']);
|
||||
$distribuciones = $data['distribuciones'] ?? [];
|
||||
$tipo = $data['tipo'] ?? 'accesorio';
|
||||
$ordenProduccionId = $data['orden_produccion_id'];
|
||||
$cantidadTotal = $data['cantidad_enviada'];
|
||||
$terminada = $data['terminada'] ?? false;
|
||||
|
||||
// Guardar en la tabla correspondiente
|
||||
if ($tipo === 'prensilla') {
|
||||
$record = \App\Models\Prensilla::create($data);
|
||||
} else {
|
||||
$record = \App\Models\Ojal::create($data);
|
||||
// Validar que las distribuciones sumen la cantidad total
|
||||
$sumaDistribucion = array_sum(array_column($distribuciones, 'cantidad'));
|
||||
if ($sumaDistribucion != $cantidadTotal) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||
'distribuciones' => "La suma de las distribuciones ({$sumaDistribucion}) debe ser igual a la cantidad total ({$cantidadTotal})."
|
||||
]);
|
||||
}
|
||||
|
||||
// Crear un registro por cada proveedor
|
||||
foreach ($distribuciones as $distribucion) {
|
||||
\App\Models\Ojal::create([
|
||||
'proveedor_id' => $distribucion['proveedor_id'],
|
||||
'orden_produccion_id' => $ordenProduccionId,
|
||||
'tipo' => $tipo,
|
||||
'fecha_envio' => now(),
|
||||
'cantidad_enviada' => $distribucion['cantidad'],
|
||||
'terminada' => $terminada,
|
||||
]);
|
||||
}
|
||||
|
||||
// Redirigir al listado
|
||||
$this->redirect(OjalPresillaResource::getUrl('index'));
|
||||
|
||||
return $data;
|
||||
// Retornar vacío para evitar que Filament intente crear el registro automáticamente
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
// Ya se crearon los registros en mutateFormDataBeforeCreate
|
||||
// Redirigir al index
|
||||
$this->redirect(OjalPresillaResource::getUrl('index'));
|
||||
}
|
||||
|
||||
protected function getRedirectUrl(): string
|
||||
{
|
||||
return OjalPresillaResource::getUrl('index');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user