feat: implement Stockable interface and methods for Bundle and ProductVariant models
This commit is contained in:
@@ -10,13 +10,15 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
|
|
||||||
|
use App\Domains\Shared\Contracts\Stockable;
|
||||||
|
|
||||||
#[Fillable([
|
#[Fillable([
|
||||||
'tenant_codigo',
|
'tenant_codigo',
|
||||||
'nombre',
|
'nombre',
|
||||||
'descripcion',
|
'descripcion',
|
||||||
'precio',
|
'precio',
|
||||||
])]
|
])]
|
||||||
class Bundle extends Model
|
class Bundle extends Model implements Stockable
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
@@ -47,6 +49,28 @@ class Bundle extends Model
|
|||||||
return $this->hasMany(BundleItem::class, 'bundle_id');
|
return $this->hasMany(BundleItem::class, 'bundle_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function incrementRealStock(int $amount): void
|
||||||
|
{
|
||||||
|
if ($amount < 0) {
|
||||||
|
throw new \InvalidArgumentException('El monto a incrementar debe ser positivo.');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->items as $item) {
|
||||||
|
$item->variant->incrementRealStock($amount * $item->cantidad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function decrementRealStock(int $amount): void
|
||||||
|
{
|
||||||
|
if ($amount < 0) {
|
||||||
|
throw new \InvalidArgumentException('El monto a decrementar debe ser positivo.');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->items as $item) {
|
||||||
|
$item->variant->decrementRealStock($amount * $item->cantidad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function incrementReservedStock(int $amount): void
|
public function incrementReservedStock(int $amount): void
|
||||||
{
|
{
|
||||||
if ($amount < 0) {
|
if ($amount < 0) {
|
||||||
@@ -80,6 +104,13 @@ class Bundle extends Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function validateStock(): void
|
||||||
|
{
|
||||||
|
if ($this->stock_tecnico <= 0) {
|
||||||
|
throw new \InvalidArgumentException('El bundle no tiene stock técnico disponible.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected function stockTecnico(): Attribute
|
protected function stockTecnico(): Attribute
|
||||||
{
|
{
|
||||||
return Attribute::get(function () {
|
return Attribute::get(function () {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
|
|
||||||
|
use App\Domains\Shared\Contracts\Stockable;
|
||||||
|
|
||||||
#[Fillable([
|
#[Fillable([
|
||||||
'producto_id',
|
'producto_id',
|
||||||
'stock_real',
|
'stock_real',
|
||||||
@@ -22,7 +24,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute;
|
|||||||
'minimum_use_date',
|
'minimum_use_date',
|
||||||
'maximum_use_date',
|
'maximum_use_date',
|
||||||
])]
|
])]
|
||||||
class ProductVariant extends Model
|
class ProductVariant extends Model implements Stockable
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
|
|||||||
15
app/Domains/Shared/Contracts/Stockable.php
Normal file
15
app/Domains/Shared/Contracts/Stockable.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Shared\Contracts;
|
||||||
|
|
||||||
|
interface Stockable
|
||||||
|
{
|
||||||
|
public function incrementRealStock(int $amount): void;
|
||||||
|
public function decrementRealStock(int $amount): void;
|
||||||
|
|
||||||
|
public function incrementReservedStock(int $amount): void;
|
||||||
|
public function decrementReservedStock(int $amount): void;
|
||||||
|
public function confirmReservedStock(int $amount): void;
|
||||||
|
|
||||||
|
public function validateStock(): void;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user