From b9fea477704d791f8cb78679d4c524cd0a7090ed Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Thu, 29 Jan 2026 23:23:26 -0500 Subject: [PATCH] up --- app/Models/Ojal.php | 12 +++++++++++- app/Models/Prensilla.php | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/Models/Ojal.php b/app/Models/Ojal.php index f95c29d..8bba881 100644 --- a/app/Models/Ojal.php +++ b/app/Models/Ojal.php @@ -40,7 +40,17 @@ class Ojal extends Model static::creating(function ($p) { // Validar disponibilidad desde confecciones $opId = $p->orden_produccion_id; - $totalProducido = \App\Models\Confeccion::where('orden_produccion_id', $opId)->sum('cantidad_recibida'); + $totalRecibido = \App\Models\Confeccion::where('orden_produccion_id', $opId) + ->sum(\DB::raw('COALESCE(cantidad_recibida, 0)')); + $totalArreglos = \App\Models\Ajuste::where('ajustable_type', \App\Models\Confeccion::class) + ->whereHas('ajustable', function($q) use ($opId) { + $q->where('orden_produccion_id', $opId); + })->where('tipo', 'arreglo')->sum('cantidad'); + $totalCobros = \App\Models\Cobro::where('cobreable_type', \App\Models\Confeccion::class) + ->whereHas('cobreable', function($q) use ($opId) { + $q->where('orden_produccion_id', $opId); + })->sum('cantidad'); + $totalProducido = $totalRecibido - $totalArreglos - $totalCobros; $yaAsignado = \App\Models\Prensilla::where('orden_produccion_id', $opId)->sum('cantidad_enviada') + \App\Models\Ojal::where('orden_produccion_id', $opId)->sum('cantidad_enviada'); diff --git a/app/Models/Prensilla.php b/app/Models/Prensilla.php index 4b57e69..18b02e6 100644 --- a/app/Models/Prensilla.php +++ b/app/Models/Prensilla.php @@ -38,7 +38,17 @@ class Prensilla extends Model static::creating(function ($p) { // Validar disponibilidad desde confecciones $opId = $p->orden_produccion_id; - $totalProducido = \App\Models\Confeccion::where('orden_produccion_id', $opId)->sum('cantidad_recibida'); + $totalRecibido = \App\Models\Confeccion::where('orden_produccion_id', $opId) + ->sum(\DB::raw('COALESCE(cantidad_recibida, 0)')); + $totalArreglos = \App\Models\Ajuste::where('ajustable_type', \App\Models\Confeccion::class) + ->whereHas('ajustable', function($q) use ($opId) { + $q->where('orden_produccion_id', $opId); + })->where('tipo', 'arreglo')->sum('cantidad'); + $totalCobros = \App\Models\Cobro::where('cobreable_type', \App\Models\Confeccion::class) + ->whereHas('cobreable', function($q) use ($opId) { + $q->where('orden_produccion_id', $opId); + })->sum('cantidad'); + $totalProducido = $totalRecibido - $totalArreglos - $totalCobros; $yaAsignado = \App\Models\Prensilla::where('orden_produccion_id', $opId)->sum('cantidad_enviada') + \App\Models\Ojal::where('orden_produccion_id', $opId)->sum('cantidad_enviada');