RI cleaned up and refactored stocks
This commit is contained in:
parent
8dbf70168c
commit
5cc7b1f3f0
|
|
@ -7,7 +7,6 @@ enum StockStatus: string
|
|||
case PENDING = 'pending';
|
||||
case COMPLETED = 'completed';
|
||||
case CANCELLED = 'cancelled';
|
||||
case EXPIRED = 'expired';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
|
|
@ -15,7 +14,6 @@ enum StockStatus: string
|
|||
self::PENDING => 'Pending',
|
||||
self::COMPLETED => 'Completed',
|
||||
self::CANCELLED => 'Cancelled',
|
||||
self::EXPIRED => 'Expired',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,23 +5,17 @@ namespace Blax\Shop\Enums;
|
|||
enum StockType: string
|
||||
{
|
||||
case RESERVATION = 'reservation';
|
||||
case ADJUSTMENT = 'adjustment';
|
||||
case SALE = 'sale';
|
||||
case RETURN = 'return';
|
||||
case INCREASE = 'increase';
|
||||
case DECREASE = 'decrease';
|
||||
case RELEASE = 'release';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::RESERVATION => 'Reservation',
|
||||
self::ADJUSTMENT => 'Adjustment',
|
||||
self::SALE => 'Sale',
|
||||
self::RETURN => 'Return',
|
||||
self::INCREASE => 'Increase',
|
||||
self::DECREASE => 'Decrease',
|
||||
self::RELEASE => 'Release',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,12 +45,6 @@ class ProductStock extends Model
|
|||
static::created(function ($model) {
|
||||
$model->logStockChange();
|
||||
});
|
||||
|
||||
static::updated(function ($model) {
|
||||
if ($model->wasChanged('status') && $model->status === StockStatus::COMPLETED) {
|
||||
$model->releaseStock();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function product(): BelongsTo
|
||||
|
|
@ -174,25 +168,6 @@ class ProductStock extends Model
|
|||
]);
|
||||
}
|
||||
|
||||
protected function releaseStock(): void
|
||||
{
|
||||
if (!config('shop.stock.log_changes', true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('product_stock_logs')->insert([
|
||||
'product_id' => $this->product_id,
|
||||
'quantity_change' => $this->quantity,
|
||||
'quantity_after' => $this->product->stock_quantity,
|
||||
'type' => StockType::RELEASE->value,
|
||||
'note' => 'Stock released from reservation',
|
||||
'reference_type' => $this->reference_type,
|
||||
'reference_id' => $this->reference_id,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function releaseExpired(): int
|
||||
{
|
||||
$expired = self::expired()->get();
|
||||
|
|
|
|||
|
|
@ -20,10 +20,7 @@ trait HasStocks
|
|||
{
|
||||
return $this->stocks()
|
||||
->available()
|
||||
->where(function ($query) {
|
||||
$query->whereNull('expires_at')
|
||||
->orWhere('expires_at', '>', now());
|
||||
})
|
||||
->willExpire()
|
||||
->sum('quantity') ?? 0;
|
||||
}
|
||||
|
||||
|
|
@ -188,10 +185,7 @@ trait HasStocks
|
|||
$stockModel = config('shop.models.product_stock', 'Blax\Shop\Models\ProductStock');
|
||||
|
||||
return $stockModel::reservations()
|
||||
->where(function ($query) {
|
||||
$query->whereNull('expires_at')
|
||||
->orWhere('expires_at', '>', now());
|
||||
})
|
||||
->willExpire()
|
||||
->where('product_id', $this->id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue