Update Prensilla.php
This commit is contained in:
+17
-16
@@ -42,24 +42,25 @@ class Prensilla extends Model
|
||||
protected static function booted()
|
||||
{
|
||||
static::creating(function ($p) {
|
||||
// Validar disponibilidad desde confecciones
|
||||
// Validar disponibilidad desde ojales terminados
|
||||
$opId = $p->orden_produccion_id;
|
||||
$totalRecibido = \App\Models\Confeccion::where('orden_produccion_id', $opId)
|
||||
->sum(\DB::raw('COALESCE(cantidad_recibida, 0)'));
|
||||
$totalArreglos = \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');
|
||||
$totalCobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class)
|
||||
->whereHas('referencia', 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');
|
||||
|
||||
// Total de ojales terminados
|
||||
$ojalesTerminados = \App\Models\Ojal::where('orden_produccion_id', $opId)
|
||||
->where('terminada', true)
|
||||
->sum('cantidad_enviada');
|
||||
|
||||
// Ya asignado en prensillas
|
||||
$yaAsignado = \App\Models\Prensilla::where('orden_produccion_id', $opId)
|
||||
->sum('cantidad_enviada');
|
||||
|
||||
if (($yaAsignado + ($p->cantidad_enviada ?? 0)) > $totalProducido) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad_enviada' => 'No hay suficientes unidades disponibles desde confecciones para asignar.']);
|
||||
// Verificar si hay suficiente disponible
|
||||
$disponible = $ojalesTerminados - $yaAsignado;
|
||||
|
||||
if ($disponible < ($p->cantidad_enviada ?? 0)) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||
'cantidad_enviada' => "Solo hay {$disponible} unidades disponibles desde Ojales terminados. No se pueden asignar " . ($p->cantidad_enviada ?? 0) . " unidades."
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user