From 7b017114b5925ecbf82f5b7acdeb0254d938f206 Mon Sep 17 00:00:00 2001 From: lizandrogd <77708265+lizandrogd@users.noreply.github.com> Date: Mon, 19 Jan 2026 22:03:04 -0500 Subject: [PATCH] Update TintoreriaResource.php --- app/Filament/Resources/TintoreriaResource.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/Filament/Resources/TintoreriaResource.php b/app/Filament/Resources/TintoreriaResource.php index 53a2ea4..87e6d31 100644 --- a/app/Filament/Resources/TintoreriaResource.php +++ b/app/Filament/Resources/TintoreriaResource.php @@ -50,15 +50,25 @@ class TintoreriaResource extends Resource Select::make('orden_produccion_id') ->label('Orden de Producción') - ->relationship('ordenProduccion', 'numero_orden') + ->options(fn () => \App\Models\OrdenProduccion::whereRaw('( + select coalesce(sum(cantidad_recibida),0) from confeccions where confeccions.orden_produccion_id = orden_produccions.id + ) - ( + select coalesce(sum(cantidad_enviada),0) from tintorerias where tintorerias.orden_produccion_id = orden_produccions.id and tintorerias.fecha_recepcion is null + ) > 0')->pluck('numero_orden', 'id')->toArray()) ->searchable() ->preload() ->reactive() ->required() ->afterStateUpdated(function ($state, $set) { if ($state) { - $sum = \App\Models\Confeccion::where('orden_produccion_id', $state)->sum('cantidad_recibida'); - $set('cantidad_enviada', $sum ?: 0); + $totalRecibido = \App\Models\Confeccion::where('orden_produccion_id', $state)->sum('cantidad_recibida'); + $enviadasEnTint = \App\Models\Tintoreria::where('orden_produccion_id', $state) + ->whereNull('fecha_recepcion') + ->sum('cantidad_enviada'); + + $pending = max(0, (int)$totalRecibido - (int)$enviadasEnTint); + + $set('cantidad_enviada', $pending ?: 0); $set('fecha_envio', now()->toDateString()); } }),