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