up
This commit is contained in:
@@ -25,6 +25,11 @@ class InventarioPrenda extends Model
|
||||
return $this->belongsTo(\App\Models\Producto::class, 'producto_id');
|
||||
}
|
||||
|
||||
public function distribuciones()
|
||||
{
|
||||
return $this->hasMany(\App\Models\InventarioPrendaDistribucion::class, 'inventario_prenda_id');
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::creating(function ($inventario) {
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class InventarioPrendaDistribucion extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'inventario_prenda_distribuciones';
|
||||
|
||||
protected $fillable = [
|
||||
'inventario_prenda_id',
|
||||
'bodega_id',
|
||||
'color_id',
|
||||
'size_id',
|
||||
'cantidad',
|
||||
];
|
||||
|
||||
public function inventarioPrenda()
|
||||
{
|
||||
return $this->belongsTo(InventarioPrenda::class);
|
||||
}
|
||||
|
||||
public function bodega()
|
||||
{
|
||||
return $this->belongsTo(Bodega::class);
|
||||
}
|
||||
|
||||
public function color()
|
||||
{
|
||||
return $this->belongsTo(Color::class);
|
||||
}
|
||||
|
||||
public function size()
|
||||
{
|
||||
return $this->belongsTo(Size::class);
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
// Al crear una distribución, incrementar stock
|
||||
static::created(function ($dist) {
|
||||
$cantidad = $dist->cantidad ?? 0;
|
||||
if ($cantidad <= 0) return;
|
||||
|
||||
$bodegaId = $dist->bodega_id;
|
||||
|
||||
if ($dist->color_id && $dist->size_id) {
|
||||
// Variante
|
||||
$variant = \App\Models\ProductVariant::firstOrCreate([
|
||||
'producto_id' => $dist->inventarioPrenda->producto_id ?? ($dist->inventarioPrenda->ordenProduccion->producto_id ?? null),
|
||||
'color_id' => $dist->color_id,
|
||||
'size_id' => $dist->size_id,
|
||||
], ['stock' => 0, 'sku' => 'AUTO-'.uniqid(), 'barcode' => '']);
|
||||
|
||||
$existing = \DB::table('variante_bodega')
|
||||
->where('variante_id', $variant->id)
|
||||
->where('bodega_id', $bodegaId)
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
\DB::table('variante_bodega')
|
||||
->where('variante_id', $variant->id)
|
||||
->where('bodega_id', $bodegaId)
|
||||
->update(['stock' => ($existing->stock ?? 0) + $cantidad, 'updated_at' => now()]);
|
||||
} else {
|
||||
\DB::table('variante_bodega')->insert(['variante_id' => $variant->id, 'bodega_id' => $bodegaId, 'stock' => $cantidad, 'created_at' => now(), 'updated_at' => now()]);
|
||||
}
|
||||
|
||||
$variant->increment('stock', $cantidad);
|
||||
} else {
|
||||
// Producto
|
||||
$productoId = $dist->inventarioPrenda->producto_id ?? ($dist->inventarioPrenda->ordenProduccion->producto_id ?? null);
|
||||
if ($productoId) {
|
||||
$existing = \DB::table('producto_bodega')->where('producto_id', $productoId)->where('bodega_id', $bodegaId)->first();
|
||||
if ($existing) {
|
||||
\DB::table('producto_bodega')->where('producto_id', $productoId)->where('bodega_id', $bodegaId)->update(['stock' => ($existing->stock ?? 0) + $cantidad, 'updated_at' => now()]);
|
||||
} else {
|
||||
\DB::table('producto_bodega')->insert(['producto_id' => $productoId, 'bodega_id' => $bodegaId, 'stock' => $cantidad, 'created_at' => now(), 'updated_at' => now()]);
|
||||
}
|
||||
|
||||
$prod = \App\Models\Producto::find($productoId);
|
||||
if ($prod && ! $prod->variants()->exists()) {
|
||||
$prod->increment('stock', $cantidad);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Al actualizar, ajustar la diferencia
|
||||
static::updated(function ($dist) {
|
||||
$original = $dist->getOriginal();
|
||||
$oldCantidad = intval($original['cantidad'] ?? 0);
|
||||
$newCantidad = intval($dist->cantidad ?? 0);
|
||||
$delta = $newCantidad - $oldCantidad;
|
||||
if ($delta == 0) return;
|
||||
|
||||
$bodegaId = $dist->bodega_id;
|
||||
|
||||
if ($dist->color_id && $dist->size_id) {
|
||||
$variant = \App\Models\ProductVariant::firstOrCreate([
|
||||
'producto_id' => $dist->inventarioPrenda->producto_id ?? ($dist->inventarioPrenda->ordenProduccion->producto_id ?? null),
|
||||
'color_id' => $dist->color_id,
|
||||
'size_id' => $dist->size_id,
|
||||
], ['stock' => 0, 'sku' => 'AUTO-'.uniqid(), 'barcode' => '']);
|
||||
|
||||
$existing = \DB::table('variante_bodega')->where('variante_id', $variant->id)->where('bodega_id', $bodegaId)->first();
|
||||
if ($existing) {
|
||||
\DB::table('variante_bodega')->where('variante_id', $variant->id)->where('bodega_id', $bodegaId)->update(['stock' => ($existing->stock ?? 0) + $delta, 'updated_at' => now()]);
|
||||
} else {
|
||||
\DB::table('variante_bodega')->insert(['variante_id' => $variant->id, 'bodega_id' => $bodegaId, 'stock' => max(0, $delta), 'created_at' => now(), 'updated_at' => now()]);
|
||||
}
|
||||
|
||||
$variant->increment('stock', $delta);
|
||||
} else {
|
||||
$productoId = $dist->inventarioPrenda->producto_id ?? ($dist->inventarioPrenda->ordenProduccion->producto_id ?? null);
|
||||
if ($productoId) {
|
||||
$existing = \DB::table('producto_bodega')->where('producto_id', $productoId)->where('bodega_id', $bodegaId)->first();
|
||||
if ($existing) {
|
||||
\DB::table('producto_bodega')->where('producto_id', $productoId)->where('bodega_id', $bodegaId)->update(['stock' => ($existing->stock ?? 0) + $delta, 'updated_at' => now()]);
|
||||
} else {
|
||||
\DB::table('producto_bodega')->insert(['producto_id' => $productoId, 'bodega_id' => $bodegaId, 'stock' => max(0, $delta), 'created_at' => now(), 'updated_at' => now()]);
|
||||
}
|
||||
|
||||
$prod = \App\Models\Producto::find($productoId);
|
||||
if ($prod && ! $prod->variants()->exists()) {
|
||||
$prod->increment('stock', $delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Al eliminar, restar la cantidad
|
||||
static::deleted(function ($dist) {
|
||||
$cantidad = $dist->cantidad ?? 0;
|
||||
if ($cantidad <= 0) return;
|
||||
|
||||
$bodegaId = $dist->bodega_id;
|
||||
|
||||
if ($dist->color_id && $dist->size_id) {
|
||||
$variant = \App\Models\ProductVariant::where('color_id', $dist->color_id)->where('size_id', $dist->size_id)->first();
|
||||
if ($variant) {
|
||||
$existing = \DB::table('variante_bodega')->where('variante_id', $variant->id)->where('bodega_id', $bodegaId)->first();
|
||||
if ($existing) {
|
||||
$newStock = max(0, ($existing->stock ?? 0) - $cantidad);
|
||||
\DB::table('variante_bodega')->where('variante_id', $variant->id)->where('bodega_id', $bodegaId)->update(['stock' => $newStock, 'updated_at' => now()]);
|
||||
}
|
||||
|
||||
$variant->decrement('stock', $cantidad);
|
||||
}
|
||||
} else {
|
||||
$productoId = $dist->inventarioPrenda->producto_id ?? ($dist->inventarioPrenda->ordenProduccion->producto_id ?? null);
|
||||
if ($productoId) {
|
||||
$existing = \DB::table('producto_bodega')->where('producto_id', $productoId)->where('bodega_id', $bodegaId)->first();
|
||||
if ($existing) {
|
||||
$newStock = max(0, ($existing->stock ?? 0) - $cantidad);
|
||||
\DB::table('producto_bodega')->where('producto_id', $productoId)->where('bodega_id', $bodegaId)->update(['stock' => $newStock, 'updated_at' => now()]);
|
||||
}
|
||||
|
||||
$prod = \App\Models\Producto::find($productoId);
|
||||
if ($prod && ! $prod->variants()->exists()) {
|
||||
$prod->decrement('stock', $cantidad);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user