Files
pos_heidiver/app/Models/Ojal.php
T
2026-02-04 16:35:34 -05:00

107 lines
4.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Ojal extends Model
{
protected $table = 'ojales';
protected $fillable = [
'proveedor_id',
'usuario_asignado_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 usuarioAsignado()
{
return $this->belongsTo(\App\Models\User::class, 'usuario_asignado_id');
}
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('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');
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' => $ojal->cantidad_enviada,
'fecha_envio' => $ojal->fecha_envio ?? now(),
'fecha_recepcion' => $ojal->fecha_envio ?? now(),
'estado' => 'recibido',
]);
}
});
// 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' => $ojal->cantidad_enviada,
'fecha_envio' => now(),
'fecha_recepcion' => now(),
'estado' => 'recibido',
]);
}
}
});
}
}