99 lines
3.8 KiB
PHP
99 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Ojal extends Model
|
|
{
|
|
protected $table = 'ojales';
|
|
|
|
protected $fillable = [
|
|
'proveedor_id',
|
|
'orden_produccion_id',
|
|
'tipo',
|
|
'fecha_envio',
|
|
'cantidad_enviada',
|
|
'fecha_recepcion',
|
|
'cantidad_recibida',
|
|
'perdidas',
|
|
'terminada',
|
|
'proveedor_tintoreria_id',
|
|
];
|
|
|
|
protected $casts = [
|
|
'terminada' => 'boolean',
|
|
];
|
|
|
|
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;
|
|
$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('cobreable_type', \App\Models\Confeccion::class)
|
|
->whereHas('cobreable', 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');
|
|
|
|
if (($yaAsignado + ($p->cantidad_enviada ?? 0)) > $totalProducido) {
|
|
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad_enviada' => 'No hay suficientes unidades disponibles desde confecciones para asignar.']);
|
|
}
|
|
});
|
|
|
|
// Crear traslado cuando se crea un Ojal/Prensilla (Confección → Ojales/Prensillas)
|
|
static::created(function ($ojal) {
|
|
if ($ojal->cantidad_enviada > 0) {
|
|
\App\Models\TrasladoPrenda::create([
|
|
'orden_produccion_id' => $ojal->orden_produccion_id,
|
|
'referencia_type' => self::class,
|
|
'referencia_id' => $ojal->id,
|
|
'origen' => 'confeccion',
|
|
'destino' => 'ojales_prensillas',
|
|
'cantidad_enviada' => $ojal->cantidad_enviada,
|
|
'cantidad_recibida' => null,
|
|
'fecha_envio' => $ojal->fecha_envio ?? now(),
|
|
'estado' => 'pendiente',
|
|
]);
|
|
}
|
|
});
|
|
|
|
// Crear traslado cuando se marca como terminada (Ojales/Prensillas → Tintorería)
|
|
static::updated(function ($ojal) {
|
|
if ($ojal->terminada && $ojal->getOriginal('terminada') == false) {
|
|
if ($ojal->cantidad_enviada > 0) {
|
|
\App\Models\TrasladoPrenda::create([
|
|
'orden_produccion_id' => $ojal->orden_produccion_id,
|
|
'referencia_type' => self::class,
|
|
'referencia_id' => $ojal->id,
|
|
'origen' => 'ojales_prensillas',
|
|
'destino' => 'tintoreria',
|
|
'cantidad_enviada' => $ojal->cantidad_enviada,
|
|
'cantidad_recibida' => null,
|
|
'fecha_envio' => now(),
|
|
'estado' => 'pendiente',
|
|
]);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|