diff --git a/app/Filament/Resources/OjalPresillaResource.php b/app/Filament/Resources/OjalPresillaResource.php index 0156d6a..4e3a7a2 100644 --- a/app/Filament/Resources/OjalPresillaResource.php +++ b/app/Filament/Resources/OjalPresillaResource.php @@ -30,67 +30,109 @@ class OjalPresillaResource extends Resource public static function form(Forms\Form $form): Forms\Form { return $form->schema([ + Select::make('tipo') + ->label('Tipo de Proceso') + ->options([ + 'Ojal' => 'Ojal', + 'Prensilla' => 'Prensilla', + ]) + ->required() + ->reactive() + ->helperText(fn ($get) => $get('tipo') === 'Prensilla' + ? '⚠️ Solo se pueden enviar prendas que hayan terminado el proceso de Ojales' + : 'Primer proceso: las prendas vienen desde Confección'), + Select::make('orden_produccion_id') ->label('Orden de Producción') - ->options(function () { - // Solo OPs que tienen confecciones recibidas - return \App\Models\OrdenProduccion::whereHas('confecciones', function ($q) { - $q->where('cantidad_recibida', '>', 0); - })->get()->mapWithKeys(function ($op) { - // Calcular cantidad disponible desde confecciones (recibidas - arreglos - cobros) - $cantidadRecibida = $op->confecciones->sum('cantidad_recibida'); - $arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class) - ->whereHas('referencia', function($q) use ($op) { - $q->where('orden_produccion_id', $op->id); - })->where('tipo', 'arreglo')->sum('cantidad'); - $cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class) - ->whereHas('referencia', function($q) use ($op) { - $q->where('orden_produccion_id', $op->id); - })->sum('cantidad'); - $cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros; - - // Calcular cantidad ya enviada en ojales - $cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $op->id) - ->sum('cantidad_enviada'); - - // Calcular saldo disponible - $saldoDisponible = $cantidadConfecciones - $cantidadEnOjales; - - // Solo incluir si hay saldo disponible - if ($saldoDisponible > 0) { - return [$op->id => "{$op->numero_orden} (Disponible: {$saldoDisponible})"]; - } - return []; - })->filter(); // Eliminar valores vacíos + ->options(function ($get) { + $tipo = $get('tipo'); + + if ($tipo === 'Prensilla') { + // Para Prensillas: mostrar OPs con ojales terminados + return \App\Models\OrdenProduccion::whereHas('ojales', function ($q) { + $q->where('terminada', true); + })->get()->mapWithKeys(function ($op) { + // Calcular ojales terminados menos prensillas ya enviadas + $ojalesTerminados = \App\Models\Ojal::where('orden_produccion_id', $op->id) + ->where('terminada', true) + ->sum('cantidad_enviada'); + + $yaEnviado = \App\Models\Prensilla::where('orden_produccion_id', $op->id) + ->sum('cantidad_enviada'); + + $disponible = $ojalesTerminados - $yaEnviado; + + if ($disponible > 0) { + return [$op->id => "{$op->numero_orden} (Disponible: {$disponible})"]; + } + return []; + })->filter(); + } else { + // Para Ojales: mostrar OPs con confecciones disponibles + return \App\Models\OrdenProduccion::whereHas('confecciones', function ($q) { + $q->where('cantidad_recibida', '>', 0); + })->get()->mapWithKeys(function ($op) { + $cantidadRecibida = $op->confecciones->sum('cantidad_recibida'); + $arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class) + ->whereHas('referencia', function($q) use ($op) { + $q->where('orden_produccion_id', $op->id); + })->where('tipo', 'arreglo')->sum('cantidad'); + $cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class) + ->whereHas('referencia', function($q) use ($op) { + $q->where('orden_produccion_id', $op->id); + })->sum('cantidad'); + $cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros; + + $cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $op->id) + ->sum('cantidad_enviada'); + + $saldoDisponible = $cantidadConfecciones - $cantidadEnOjales; + + if ($saldoDisponible > 0) { + return [$op->id => "{$op->numero_orden} (Disponible: {$saldoDisponible})"]; + } + return []; + })->filter(); + } }) ->searchable() ->required() - ->live() - ->afterStateUpdated(function ($state, $set) { - if ($state) { - $op = \App\Models\OrdenProduccion::find($state); - if ($op) { - // Calcular cantidad disponible (confecciones - arreglos - cobros - ojales ya enviados) - $cantidadRecibida = $op->confecciones->sum('cantidad_recibida'); - $arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class) - ->whereHas('referencia', function($q) use ($state) { - $q->where('orden_produccion_id', $state); - })->where('tipo', 'arreglo')->sum('cantidad'); - $cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class) - ->whereHas('referencia', function($q) use ($state) { - $q->where('orden_produccion_id', $state); - })->sum('cantidad'); - $cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros; - $cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $state) - ->sum('cantidad_enviada'); - $cantidadDisponible = $cantidadConfecciones - $cantidadEnOjales; - - $set('cantidad_enviada', $cantidadDisponible); - $set('prenda_nombre_info', $op->prenda_modelo ?? 'Sin especificar'); - } + ->reactive() + ->afterStateUpdated(function ($state, $set, $get) { + $tipo = $get('tipo'); + if (!$state || !$tipo) return; + + $op = \App\Models\OrdenProduccion::find($state); + if (!$op) return; + + // Calcular cantidad disponible según el tipo + if ($tipo === 'Ojal') { + // Para ojales: desde confecciones + $cantidadRecibida = $op->confecciones->sum('cantidad_recibida'); + $arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class) + ->whereHas('referencia', function($q) use ($state) { + $q->where('orden_produccion_id', $state); + })->where('tipo', 'arreglo')->sum('cantidad'); + $cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class) + ->whereHas('referencia', function($q) use ($state) { + $q->where('orden_produccion_id', $state); + })->sum('cantidad'); + $cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros; + $cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $state) + ->sum('cantidad_enviada'); + $cantidadDisponible = $cantidadConfecciones - $cantidadEnOjales; } else { - $set('prenda_nombre_info', null); + // Para prensillas: desde ojales terminados + $ojalesTerminados = \App\Models\Ojal::where('orden_produccion_id', $state) + ->where('terminada', true) + ->sum('cantidad_enviada'); + $yaEnviado = \App\Models\Prensilla::where('orden_produccion_id', $state) + ->sum('cantidad_enviada'); + $cantidadDisponible = $ojalesTerminados - $yaEnviado; } + + $set('cantidad_enviada', max(0, $cantidadDisponible)); + $set('prenda_nombre_info', $op->prenda_modelo ?? 'Sin especificar'); }) ->helperText('Solo se muestran órdenes con prendas disponibles para enviar'), @@ -100,56 +142,6 @@ class OjalPresillaResource extends Resource ->dehydrated(false) ->visible(fn ($get) => $get('orden_produccion_id') !== null) ->helperText('Nombre de la prenda de la orden de producción seleccionada'), - - Select::make('tipo') - ->label('Tipo de Proceso') - ->options([ - 'Ojal' => 'Ojal', - 'Prensilla' => 'Prensilla', - ]) - ->required() - ->reactive() - ->afterStateUpdated(function ($state, $set, $get) { - $opId = $get('orden_produccion_id'); - if (!$opId) return; - - $op = \App\Models\OrdenProduccion::find($opId); - if (!$op) return; - - // Calcular disponibilidad según el tipo - $cantidadRecibida = $op->confecciones->sum('cantidad_recibida'); - $arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class) - ->whereHas('referencia', function($q) use ($opId) { - $q->where('orden_produccion_id', $opId); - })->where('tipo', 'arreglo')->sum('cantidad'); - $cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class) - ->whereHas('referencia', function($q) use ($opId) { - $q->where('orden_produccion_id', $opId); - })->sum('cantidad'); - $cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros; - - if ($state === 'Ojal') { - // Para ojales: disponible desde confección - $yaEnviado = \App\Models\Ojal::where('orden_produccion_id', $opId) - ->sum('cantidad_enviada'); - $disponible = $cantidadConfecciones - $yaEnviado; - $set('cantidad_enviada', max(0, $disponible)); - } else if ($state === 'Prensilla') { - // Para prensillas: solo lo que ya terminó en ojales - $ojalesTerminados = \App\Models\Ojal::where('orden_produccion_id', $opId) - ->where('terminada', true) - ->sum('cantidad_enviada'); - - $yaEnviado = \App\Models\Prensilla::where('orden_produccion_id', $opId) - ->sum('cantidad_enviada'); - - $disponible = $ojalesTerminados - $yaEnviado; - $set('cantidad_enviada', max(0, $disponible)); - } - }) - ->helperText(fn ($get) => $get('tipo') === 'Prensilla' - ? '⚠️ Solo se pueden enviar prendas que hayan terminado el proceso de Ojales' - : 'Primer proceso: las prendas vienen desde Confección'), Select::make('proveedor_id') ->label('Proveedor/Usuario Principal')