up
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Ojal extends Model
|
||||
{
|
||||
protected $table = 'ojales';
|
||||
|
||||
protected $fillable = [
|
||||
'proveedor_id',
|
||||
'orden_produccion_id',
|
||||
'fecha_envio',
|
||||
'cantidad_enviada',
|
||||
'fecha_recepcion',
|
||||
'cantidad_recibida',
|
||||
'perdidas',
|
||||
];
|
||||
|
||||
public function proveedor()
|
||||
{
|
||||
return $this->belongsTo(Proveedor::class);
|
||||
}
|
||||
|
||||
public function ordenProduccion()
|
||||
{
|
||||
return $this->belongsTo(OrdenProduccion::class);
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
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');
|
||||
$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) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad_enviada' => 'No hay suficientes unidades disponibles desde confecciones para asignar.']);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user