up
This commit is contained in:
@@ -50,11 +50,35 @@ class Confeccion extends Model
|
||||
}
|
||||
});
|
||||
|
||||
// Cuando se completa la confección → avanzar OP
|
||||
static::updated(function ($confeccion) {
|
||||
// Crear traslado cuando la confección tenga recepción (en creación y en actualización)
|
||||
$createTrasladoFn = function ($confeccion) {
|
||||
if ($confeccion->cantidad_recibida !== null) {
|
||||
// Evitar duplicados
|
||||
$exists = \App\Models\TrasladoPrenda::where('referencia_type', self::class)
|
||||
->where('referencia_id', $confeccion->id)
|
||||
->exists();
|
||||
|
||||
if (! $exists) {
|
||||
$destino = 'tintoreria';
|
||||
|
||||
\App\Models\TrasladoPrenda::crearDesdeReferencia(
|
||||
$confeccion,
|
||||
'confeccion',
|
||||
$destino
|
||||
);
|
||||
|
||||
// Marcar el paso como terminado y avanzar la OP incluso si fue parcial
|
||||
$confeccion->ordenProduccion?->avanzarEstado();
|
||||
}
|
||||
}
|
||||
|
||||
// Mantener el comportamiento anterior: si está completo, también avanzamos (por seguridad)
|
||||
if ($confeccion->estado === 'completo') {
|
||||
$confeccion->ordenProduccion?->avanzarEstado();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
static::updated($createTrasladoFn);
|
||||
static::saved($createTrasladoFn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,11 @@ class OrdenProduccion extends Model
|
||||
return $this->belongsTo(Tela::class);
|
||||
}
|
||||
|
||||
public function traslados()
|
||||
{
|
||||
return $this->hasMany(\App\Models\TrasladoPrenda::class);
|
||||
}
|
||||
|
||||
public function avanzarEstado(): void
|
||||
{
|
||||
$flujo = [
|
||||
@@ -41,6 +46,25 @@ class OrdenProduccion extends Model
|
||||
$this->update([
|
||||
'estado' => $flujo[$actual + 1],
|
||||
]);
|
||||
|
||||
// Si la orden llega a finalizada, crear entrada en inventario con lo que haya ingresado
|
||||
if ($this->estado === 'finalizada') {
|
||||
// Sumar traslados cuyo destino sea 'bodega'
|
||||
$cantidad = \App\Models\TrasladoPrenda::where('orden_produccion_id', $this->id)
|
||||
->where('destino', 'bodega')
|
||||
->sum('cantidad_recibida');
|
||||
|
||||
// Crear inventario solo si hay traslados a bodega con cantidad
|
||||
if ($cantidad > 0 && !\App\Models\InventarioPrenda::where('orden_produccion_id', $this->id)->exists()) {
|
||||
\App\Models\InventarioPrenda::create([
|
||||
'orden_produccion_id' => $this->id,
|
||||
'cantidad_terminada' => $cantidad,
|
||||
'cantidad_disponible' => $cantidad,
|
||||
'fecha_ingreso' => now(),
|
||||
'estado' => 'en_bodega',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +32,27 @@ class ProcesoAcabado extends Model
|
||||
/* Lógica de negocio */
|
||||
protected static function booted()
|
||||
{
|
||||
// Cuando se recibe el proceso
|
||||
static::updated(function ($acabado) {
|
||||
$createTrasladoFn = function ($acabado) {
|
||||
if ($acabado->cantidad_recibida !== null) {
|
||||
// Evitar duplicados
|
||||
$exists = \App\Models\TrasladoPrenda::where('referencia_type', self::class)
|
||||
->where('referencia_id', $acabado->id)
|
||||
->exists();
|
||||
|
||||
if (! $exists) {
|
||||
$destino = 'bodega';
|
||||
|
||||
\App\Models\TrasladoPrenda::crearDesdeReferencia(
|
||||
$acabado,
|
||||
'acabados',
|
||||
$destino
|
||||
);
|
||||
|
||||
// Avanzar la OP al cerrar este proceso
|
||||
$acabado->ordenProduccion?->avanzarEstado();
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
$acabado->fecha_recepcion &&
|
||||
$acabado->cantidad_recibida !== null
|
||||
@@ -49,6 +68,9 @@ class ProcesoAcabado extends Model
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
static::updated($createTrasladoFn);
|
||||
static::saved($createTrasladoFn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,14 +39,42 @@ class Tintoreria extends Model
|
||||
}
|
||||
});
|
||||
|
||||
// Cuando vuelve de tintorería → avanzar OP
|
||||
static::updated(function ($tintoreria) {
|
||||
$createTrasladoFn = function ($tintoreria) {
|
||||
if ($tintoreria->cantidad_recibida !== null) {
|
||||
// Evitar duplicados
|
||||
$exists = \App\Models\TrasladoPrenda::where('referencia_type', self::class)
|
||||
->where('referencia_id', $tintoreria->id)
|
||||
->exists();
|
||||
|
||||
if (! $exists) {
|
||||
// Mapear "perdidas" a prendas_defectuosas en el traslado
|
||||
$payload = [
|
||||
'prendas_defectuosas' => $tintoreria->perdidas ?? 0,
|
||||
];
|
||||
|
||||
$destino = 'acabados';
|
||||
|
||||
\App\Models\TrasladoPrenda::crearDesdeReferencia(
|
||||
$tintoreria,
|
||||
'tintoreria',
|
||||
$destino,
|
||||
$payload
|
||||
);
|
||||
|
||||
// Avanzar la OP al cerrar el paso
|
||||
$tintoreria->ordenProduccion?->avanzarEstado();
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
$tintoreria->fecha_recepcion &&
|
||||
$tintoreria->cantidad_recibida !== null
|
||||
) {
|
||||
$tintoreria->ordenProduccion?->avanzarEstado();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
static::updated($createTrasladoFn);
|
||||
static::saved($createTrasladoFn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class TrasladoPrenda extends Model
|
||||
{
|
||||
protected $table = 'traslados_prenda';
|
||||
|
||||
protected $fillable = [
|
||||
'orden_produccion_id',
|
||||
'referencia_type',
|
||||
'referencia_id',
|
||||
'origen',
|
||||
'destino',
|
||||
'cantidad_enviada',
|
||||
'cantidad_recibida',
|
||||
'prendas_defectuosas',
|
||||
'reparaciones',
|
||||
'saldos',
|
||||
'residual',
|
||||
'fecha_envio',
|
||||
'fecha_recepcion',
|
||||
'estado',
|
||||
'ingresado_por',
|
||||
'notas',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'fecha_envio' => 'datetime',
|
||||
'fecha_recepcion' => 'datetime',
|
||||
];
|
||||
|
||||
public function ordenProduccion()
|
||||
{
|
||||
return $this->belongsTo(OrdenProduccion::class);
|
||||
}
|
||||
|
||||
public function referencia()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function ingresadoPor()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'ingresado_por');
|
||||
}
|
||||
|
||||
public static function crearDesdeReferencia(Model $referencia, string $origen, string $destino, array $payload = []) : self
|
||||
{
|
||||
$defaults = [
|
||||
'orden_produccion_id' => $referencia->orden_produccion_id,
|
||||
'referencia_type' => get_class($referencia),
|
||||
'referencia_id' => $referencia->id,
|
||||
'origen' => $origen,
|
||||
'destino' => $destino,
|
||||
'cantidad_enviada' => $payload['cantidad_enviada'] ?? ($referencia->cantidad_enviada ?? 0),
|
||||
'cantidad_recibida' => $payload['cantidad_recibida'] ?? ($referencia->cantidad_recibida ?? null),
|
||||
'prendas_defectuosas' => $payload['prendas_defectuosas'] ?? ($referencia->prendas_defectuosas ?? 0),
|
||||
'reparaciones' => $payload['reparaciones'] ?? ($referencia->reparaciones ?? 0),
|
||||
'saldos' => $payload['saldos'] ?? ($referencia->saldos ?? 0),
|
||||
'fecha_envio' => $payload['fecha_envio'] ?? ($referencia->fecha_envio ?? null),
|
||||
'fecha_recepcion' => $payload['fecha_recepcion'] ?? ($referencia->fecha_recepcion ?? null),
|
||||
'estado' => 'recibido',
|
||||
];
|
||||
|
||||
$cantidad_enviada = (int) $defaults['cantidad_enviada'];
|
||||
$cantidad_recibida = (int) ($defaults['cantidad_recibida'] ?? 0);
|
||||
$prendas_defectuosas = (int) ($defaults['prendas_defectuosas'] ?? 0);
|
||||
$reparaciones = (int) ($defaults['reparaciones'] ?? 0);
|
||||
$saldos = (int) ($defaults['saldos'] ?? 0);
|
||||
|
||||
$defaults['residual'] = $cantidad_enviada - ($cantidad_recibida + $prendas_defectuosas + $reparaciones + $saldos);
|
||||
|
||||
$traslado = self::create($defaults);
|
||||
|
||||
// Si el traslado es a bodega y la OP ya está finalizada, crear inventario (si no existe)
|
||||
if ($traslado->destino === 'bodega') {
|
||||
$traslado->maybeCreateInventory();
|
||||
}
|
||||
|
||||
return $traslado;
|
||||
}
|
||||
|
||||
public function markAsReceived(array $data = [], $userId = null)
|
||||
{
|
||||
// Actualizar campos si se envían
|
||||
$this->cantidad_recibida = $data['cantidad_recibida'] ?? $this->cantidad_recibida;
|
||||
$this->prendas_defectuosas = $data['prendas_defectuosas'] ?? $this->prendas_defectuosas ?? 0;
|
||||
$this->reparaciones = $data['reparaciones'] ?? $this->reparaciones ?? 0;
|
||||
$this->saldos = $data['saldos'] ?? $this->saldos ?? 0;
|
||||
$this->fecha_recepcion = $data['fecha_recepcion'] ?? now();
|
||||
$this->estado = 'recibido';
|
||||
|
||||
// Si cantidad_recibida no se proporcionó, inferirla
|
||||
if ($this->cantidad_recibida === null) {
|
||||
$this->cantidad_recibida = (int) $this->cantidad_enviada - ((int) $this->prendas_defectuosas + (int) $this->reparaciones + (int) $this->saldos);
|
||||
if ($this->cantidad_recibida < 0) {
|
||||
$this->cantidad_recibida = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if ($userId) {
|
||||
$this->ingresado_por = $userId;
|
||||
}
|
||||
|
||||
$this->residual = (int) $this->cantidad_enviada - ((int) $this->cantidad_recibida + (int) $this->prendas_defectuosas + (int) $this->reparaciones + (int) $this->saldos);
|
||||
|
||||
$this->save();
|
||||
|
||||
// Si destino bodega, intentar crear inventario
|
||||
if ($this->destino === 'bodega') {
|
||||
$this->maybeCreateInventory();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function maybeCreateInventory()
|
||||
{
|
||||
$op = $this->ordenProduccion;
|
||||
if (! $op) return;
|
||||
|
||||
if ($op->estado === 'finalizada' && !\App\Models\InventarioPrenda::where('orden_produccion_id', $op->id)->exists()) {
|
||||
$cantidad = self::where('orden_produccion_id', $op->id)
|
||||
->where('destino', 'bodega')
|
||||
->sum('cantidad_recibida');
|
||||
|
||||
if ($cantidad > 0) {
|
||||
\App\Models\InventarioPrenda::create([
|
||||
'orden_produccion_id' => $op->id,
|
||||
'cantidad_terminada' => $cantidad,
|
||||
'cantidad_disponible' => $cantidad,
|
||||
'fecha_ingreso' => now(),
|
||||
'estado' => 'en_bodega',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user