Update OjalPresillaResource.php

This commit is contained in:
Lizandro Guarnizo
2026-02-04 16:55:23 -05:00
parent 30b843b402
commit 3dae97413d
+96 -104
View File
@@ -30,67 +30,109 @@ class OjalPresillaResource extends Resource
public static function form(Forms\Form $form): Forms\Form public static function form(Forms\Form $form): Forms\Form
{ {
return $form->schema([ 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') Select::make('orden_produccion_id')
->label('Orden de Producción') ->label('Orden de Producción')
->options(function () { ->options(function ($get) {
// Solo OPs que tienen confecciones recibidas $tipo = $get('tipo');
return \App\Models\OrdenProduccion::whereHas('confecciones', function ($q) {
$q->where('cantidad_recibida', '>', 0); if ($tipo === 'Prensilla') {
})->get()->mapWithKeys(function ($op) { // Para Prensillas: mostrar OPs con ojales terminados
// Calcular cantidad disponible desde confecciones (recibidas - arreglos - cobros) return \App\Models\OrdenProduccion::whereHas('ojales', function ($q) {
$cantidadRecibida = $op->confecciones->sum('cantidad_recibida'); $q->where('terminada', true);
$arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class) })->get()->mapWithKeys(function ($op) {
->whereHas('referencia', function($q) use ($op) { // Calcular ojales terminados menos prensillas ya enviadas
$q->where('orden_produccion_id', $op->id); $ojalesTerminados = \App\Models\Ojal::where('orden_produccion_id', $op->id)
})->where('tipo', 'arreglo')->sum('cantidad'); ->where('terminada', true)
$cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class) ->sum('cantidad_enviada');
->whereHas('referencia', function($q) use ($op) {
$q->where('orden_produccion_id', $op->id); $yaEnviado = \App\Models\Prensilla::where('orden_produccion_id', $op->id)
})->sum('cantidad'); ->sum('cantidad_enviada');
$cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros;
$disponible = $ojalesTerminados - $yaEnviado;
// Calcular cantidad ya enviada en ojales
$cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $op->id) if ($disponible > 0) {
->sum('cantidad_enviada'); return [$op->id => "{$op->numero_orden} (Disponible: {$disponible})"];
}
// Calcular saldo disponible return [];
$saldoDisponible = $cantidadConfecciones - $cantidadEnOjales; })->filter();
} else {
// Solo incluir si hay saldo disponible // Para Ojales: mostrar OPs con confecciones disponibles
if ($saldoDisponible > 0) { return \App\Models\OrdenProduccion::whereHas('confecciones', function ($q) {
return [$op->id => "{$op->numero_orden} (Disponible: {$saldoDisponible})"]; $q->where('cantidad_recibida', '>', 0);
} })->get()->mapWithKeys(function ($op) {
return []; $cantidadRecibida = $op->confecciones->sum('cantidad_recibida');
})->filter(); // Eliminar valores vacíos $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() ->searchable()
->required() ->required()
->live() ->reactive()
->afterStateUpdated(function ($state, $set) { ->afterStateUpdated(function ($state, $set, $get) {
if ($state) { $tipo = $get('tipo');
$op = \App\Models\OrdenProduccion::find($state); if (!$state || !$tipo) return;
if ($op) {
// Calcular cantidad disponible (confecciones - arreglos - cobros - ojales ya enviados) $op = \App\Models\OrdenProduccion::find($state);
$cantidadRecibida = $op->confecciones->sum('cantidad_recibida'); if (!$op) return;
$arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class)
->whereHas('referencia', function($q) use ($state) { // Calcular cantidad disponible según el tipo
$q->where('orden_produccion_id', $state); if ($tipo === 'Ojal') {
})->where('tipo', 'arreglo')->sum('cantidad'); // Para ojales: desde confecciones
$cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class) $cantidadRecibida = $op->confecciones->sum('cantidad_recibida');
->whereHas('referencia', function($q) use ($state) { $arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class)
$q->where('orden_produccion_id', $state); ->whereHas('referencia', function($q) use ($state) {
})->sum('cantidad'); $q->where('orden_produccion_id', $state);
$cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros; })->where('tipo', 'arreglo')->sum('cantidad');
$cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $state) $cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class)
->sum('cantidad_enviada'); ->whereHas('referencia', function($q) use ($state) {
$cantidadDisponible = $cantidadConfecciones - $cantidadEnOjales; $q->where('orden_produccion_id', $state);
})->sum('cantidad');
$set('cantidad_enviada', $cantidadDisponible); $cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros;
$set('prenda_nombre_info', $op->prenda_modelo ?? 'Sin especificar'); $cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $state)
} ->sum('cantidad_enviada');
$cantidadDisponible = $cantidadConfecciones - $cantidadEnOjales;
} else { } 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'), ->helperText('Solo se muestran órdenes con prendas disponibles para enviar'),
@@ -100,56 +142,6 @@ class OjalPresillaResource extends Resource
->dehydrated(false) ->dehydrated(false)
->visible(fn ($get) => $get('orden_produccion_id') !== null) ->visible(fn ($get) => $get('orden_produccion_id') !== null)
->helperText('Nombre de la prenda de la orden de producción seleccionada'), ->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') Select::make('proveedor_id')
->label('Proveedor/Usuario Principal') ->label('Proveedor/Usuario Principal')