Update OjalPresillaResource.php
This commit is contained in:
@@ -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);
|
|
||||||
})->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
|
if ($tipo === 'Prensilla') {
|
||||||
$cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $op->id)
|
// Para Prensillas: mostrar OPs con ojales terminados
|
||||||
->sum('cantidad_enviada');
|
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');
|
||||||
|
|
||||||
// Calcular saldo disponible
|
$yaEnviado = \App\Models\Prensilla::where('orden_produccion_id', $op->id)
|
||||||
$saldoDisponible = $cantidadConfecciones - $cantidadEnOjales;
|
->sum('cantidad_enviada');
|
||||||
|
|
||||||
// Solo incluir si hay saldo disponible
|
$disponible = $ojalesTerminados - $yaEnviado;
|
||||||
if ($saldoDisponible > 0) {
|
|
||||||
return [$op->id => "{$op->numero_orden} (Disponible: {$saldoDisponible})"];
|
if ($disponible > 0) {
|
||||||
}
|
return [$op->id => "{$op->numero_orden} (Disponible: {$disponible})"];
|
||||||
return [];
|
}
|
||||||
})->filter(); // Eliminar valores vacíos
|
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()
|
->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)
|
|
||||||
$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);
|
$op = \App\Models\OrdenProduccion::find($state);
|
||||||
$set('prenda_nombre_info', $op->prenda_modelo ?? 'Sin especificar');
|
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 {
|
} 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'),
|
||||||
|
|
||||||
@@ -101,56 +143,6 @@ class OjalPresillaResource extends Resource
|
|||||||
->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')
|
||||||
->options(fn() => \App\Models\Proveedor::where('categoria', 'empleado')->pluck('nombre', 'id'))
|
->options(fn() => \App\Models\Proveedor::where('categoria', 'empleado')->pluck('nombre', 'id'))
|
||||||
|
|||||||
Reference in New Issue
Block a user