82 lines
2.3 KiB
PHP
82 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class OrdenProduccion extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'numero_orden',
|
|
'prenda_modelo',
|
|
'referencia',
|
|
'cantidad_total',
|
|
'tela_id',
|
|
'fecha_inicio',
|
|
'fecha_entrega_estimada',
|
|
'estado',
|
|
];
|
|
|
|
public function tela()
|
|
{
|
|
return $this->belongsTo(Tela::class);
|
|
}
|
|
|
|
public function traslados()
|
|
{
|
|
return $this->hasMany(\App\Models\TrasladoPrenda::class);
|
|
}
|
|
|
|
public function avanzarEstado(): void
|
|
{
|
|
$flujo = [
|
|
'en_corte',
|
|
'en_confeccion',
|
|
'en_tintoreria',
|
|
'en_acabados',
|
|
'finalizada',
|
|
];
|
|
|
|
$actual = array_search($this->estado, $flujo, true);
|
|
|
|
if ($actual !== false && isset($flujo[$actual + 1])) {
|
|
$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',
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
protected static function booted()
|
|
{
|
|
static::creating(function ($orden) {
|
|
if (!$orden->numero_orden) {
|
|
$ultimoId = self::max('id') + 1;
|
|
|
|
$orden->numero_orden = 'OP-' . str_pad($ultimoId, 6, '0', STR_PAD_LEFT);
|
|
}
|
|
});
|
|
}
|
|
}
|