59 lines
2.0 KiB
PHP
59 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\OjalPresillaResource\Pages;
|
|
|
|
use App\Filament\Resources\OjalPresillaResource;
|
|
use Filament\Resources\Pages\CreateRecord;
|
|
|
|
class CreateOjalPresilla extends CreateRecord
|
|
{
|
|
protected static string $resource = OjalPresillaResource::class;
|
|
|
|
protected function mutateFormDataBeforeCreate(array $data): array
|
|
{
|
|
$distribuciones = $data['distribuciones'] ?? [];
|
|
$tipo = $data['tipo'] ?? 'accesorio';
|
|
$ordenProduccionId = $data['orden_produccion_id'];
|
|
$cantidadTotal = $data['cantidad_enviada'];
|
|
$terminada = $data['terminada'] ?? false;
|
|
|
|
// 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'));
|
|
|
|
// 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');
|
|
}
|
|
}
|