up
This commit is contained in:
+124
-3
@@ -17,8 +17,19 @@ class Confeccion extends Model
|
||||
'valor_por_prenda',
|
||||
'total_pagar',
|
||||
'estado',
|
||||
'descuentos_cobros',
|
||||
];
|
||||
|
||||
public function cobros()
|
||||
{
|
||||
return $this->morphMany(\App\Models\Cobro::class, 'referencia');
|
||||
}
|
||||
|
||||
public function ajustes()
|
||||
{
|
||||
return $this->morphMany(\App\Models\Ajuste::class, 'referencia');
|
||||
}
|
||||
|
||||
/* Relaciones */
|
||||
public function proveedor()
|
||||
{
|
||||
@@ -52,7 +63,14 @@ class Confeccion extends Model
|
||||
// Total a pagar: usa cantidad_recibida si existe, sino cantidad_enviada
|
||||
$cantidadBase = $confeccion->cantidad_recibida ?? $confeccion->cantidad_enviada ?? 0;
|
||||
$valorUnitario = $confeccion->valor_por_prenda ?? 0;
|
||||
$confeccion->total_pagar = $cantidadBase * $valorUnitario;
|
||||
|
||||
// Aplicar descuentos de cobros si existen (se guarda también en descuentos_cobros para compatibilidad)
|
||||
$descuentos = (float) ($confeccion->getCobrosTotalAttribute() ?? 0);
|
||||
|
||||
$confeccion->total_pagar = max(0, ($cantidadBase * $valorUnitario) - $descuentos);
|
||||
|
||||
// Mantener campo redundante 'descuentos_cobros' sincronizado
|
||||
$confeccion->descuentos_cobros = $descuentos;
|
||||
|
||||
// Estado automático
|
||||
if (! $confeccion->cantidad_recibida) {
|
||||
@@ -81,12 +99,115 @@ class Confeccion extends Model
|
||||
// Total recibido calculado desde recepciones (fuente de la verdad)
|
||||
public function getRecibidoTotalAttribute()
|
||||
{
|
||||
return (int) $this->recepciones()->sum(\DB::raw('cantidad - COALESCE(prendas_defectuosas, 0)'));
|
||||
$recepciones = (int) $this->recepciones()->sum(\DB::raw('cantidad - COALESCE(prendas_defectuosas, 0)'));
|
||||
$ajustes = (int) $this->ajustes()->where('tipo', 'arreglo')->sum('cantidad');
|
||||
|
||||
return max(0, $recepciones - $ajustes);
|
||||
}
|
||||
|
||||
// Cuantas faltan por recibir (usa la suma real de recepciones)
|
||||
// Cuantas faltan por recibir (usa la suma real de recepciones menos ajustes)
|
||||
public function getFaltantesAttribute()
|
||||
{
|
||||
return max(0, (int)($this->cantidad_enviada ?? 0) - $this->recibido_total);
|
||||
}
|
||||
|
||||
// Total de cobros aplicados
|
||||
public function getCobrosTotalAttribute()
|
||||
{
|
||||
return (float) $this->cobros()->sum('valor');
|
||||
}
|
||||
|
||||
/**
|
||||
* Registrar un arreglo como ajuste histórico y crear traslado de reparaciones
|
||||
*/
|
||||
public function registerArreglo(int $cantidad, ?string $notas = null, ?int $userId = null)
|
||||
{
|
||||
if ($cantidad <= 0) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad' => 'La cantidad debe ser mayor que 0.']);
|
||||
}
|
||||
|
||||
$recibido = $this->recibido_total;
|
||||
if ($cantidad > $recibido) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad' => 'La cantidad supera lo recibido.']);
|
||||
}
|
||||
|
||||
// Crear ajuste
|
||||
$aj = \App\Models\Ajuste::create([
|
||||
'referencia_type' => self::class,
|
||||
'referencia_id' => $this->id,
|
||||
'user_id' => $userId,
|
||||
'tipo' => 'arreglo',
|
||||
'cantidad' => $cantidad,
|
||||
'notas' => $notas,
|
||||
]);
|
||||
|
||||
// Crear traslado de reparaciones para reflejar la salida
|
||||
\App\Models\TrasladoPrenda::create([
|
||||
'orden_produccion_id' => $this->orden_produccion_id,
|
||||
'referencia_type' => self::class,
|
||||
'referencia_id' => $this->id,
|
||||
'origen' => 'confeccion',
|
||||
'destino' => 'reparaciones',
|
||||
'cantidad_enviada' => $cantidad,
|
||||
'cantidad_recibida' => 0,
|
||||
'prendas_defectuosas' => 0,
|
||||
'reparaciones' => $cantidad,
|
||||
'fecha_envio' => now(),
|
||||
'fecha_recepcion' => now(),
|
||||
'estado' => 'recibido',
|
||||
]);
|
||||
|
||||
// Recalcular cantidad_recibida almacenada para compatibilidad con UI
|
||||
$this->cantidad_recibida = $this->recibido_total;
|
||||
$this->save();
|
||||
|
||||
return $aj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registrar un cobro: descontar inventario y crear registro de cobro
|
||||
*/
|
||||
public function registerCobro(int $cantidad, float $valor, ?string $notas = null, ?int $userId = null)
|
||||
{
|
||||
if ($cantidad <= 0) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad' => 'La cantidad debe ser mayor que 0.']);
|
||||
}
|
||||
|
||||
// Buscar inventario disponible
|
||||
$inventario = \App\Models\InventarioPrenda::where('orden_produccion_id', $this->orden_produccion_id)
|
||||
->where('cantidad_disponible', '>=', $cantidad)
|
||||
->first();
|
||||
|
||||
if (! $inventario) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad' => 'No hay inventario disponible suficiente para descontar.']);
|
||||
}
|
||||
|
||||
// Descontar del inventario
|
||||
$inventario->cantidad_disponible = $inventario->cantidad_disponible - $cantidad;
|
||||
$inventario->save();
|
||||
|
||||
// Si está asociado a producto, decrementar stock
|
||||
if ($inventario->producto_id) {
|
||||
$producto = \App\Models\Producto::find($inventario->producto_id);
|
||||
if ($producto) {
|
||||
$producto->decrement('stock', $cantidad);
|
||||
}
|
||||
}
|
||||
|
||||
// Crear registro de cobro
|
||||
$c = \App\Models\Cobro::create([
|
||||
'referencia_type' => self::class,
|
||||
'referencia_id' => $this->id,
|
||||
'user_id' => $userId,
|
||||
'cantidad' => $cantidad,
|
||||
'valor' => $valor,
|
||||
'notas' => $notas,
|
||||
]);
|
||||
|
||||
// Mantener campo redundante sincronizado
|
||||
$this->descuentos_cobros = $this->getCobrosTotalAttribute();
|
||||
$this->save();
|
||||
|
||||
return $c;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user