From 07c54f6bcaa735a698e4cb21df32ca992990e72c Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Thu, 29 Jan 2026 23:25:27 -0500 Subject: [PATCH] Update OjalPresillaResource.php --- .../Resources/OjalPresillaResource.php | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/app/Filament/Resources/OjalPresillaResource.php b/app/Filament/Resources/OjalPresillaResource.php index 6b6eb85..c3b23cd 100644 --- a/app/Filament/Resources/OjalPresillaResource.php +++ b/app/Filament/Resources/OjalPresillaResource.php @@ -37,8 +37,17 @@ class OjalPresillaResource extends Resource return \App\Models\OrdenProduccion::whereHas('confecciones', function ($q) { $q->where('cantidad_recibida', '>', 0); })->get()->mapWithKeys(function ($op) { - // Calcular cantidad total de confecciones recibidas - $cantidadConfecciones = $op->confecciones->sum('cantidad_recibida'); + // Calcular cantidad disponible desde confecciones (recibidas - arreglos - cobros) + $cantidadRecibida = $op->confecciones->sum('cantidad_recibida'); + $arreglos = \App\Models\Ajuste::where('ajustable_type', \App\Models\Confeccion::class) + ->whereHas('ajustable', function($q) use ($op) { + $q->where('orden_produccion_id', $op->id); + })->where('tipo', 'arreglo')->sum('cantidad'); + $cobros = \App\Models\Cobro::where('cobreable_type', \App\Models\Confeccion::class) + ->whereHas('cobreable', 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) @@ -61,8 +70,17 @@ class OjalPresillaResource extends Resource if ($state) { $op = \App\Models\OrdenProduccion::find($state); if ($op) { - // Calcular cantidad disponible (confecciones - ojales ya enviados) - $cantidadConfecciones = $op->confecciones->sum('cantidad_recibida'); + // Calcular cantidad disponible (confecciones - arreglos - cobros - ojales ya enviados) + $cantidadRecibida = $op->confecciones->sum('cantidad_recibida'); + $arreglos = \App\Models\Ajuste::where('ajustable_type', \App\Models\Confeccion::class) + ->whereHas('ajustable', function($q) use ($state) { + $q->where('orden_produccion_id', $state); + })->where('tipo', 'arreglo')->sum('cantidad'); + $cobros = \App\Models\Cobro::where('cobreable_type', \App\Models\Confeccion::class) + ->whereHas('cobreable', 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;