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 PENDING = 'pending';
|
||||||
case COMPLETED = 'completed';
|
case COMPLETED = 'completed';
|
||||||
case CANCELLED = 'cancelled';
|
case CANCELLED = 'cancelled';
|
||||||
case EXPIRED = 'expired';
|
|
||||||
|
|
||||||
public function label(): string
|
public function label(): string
|
||||||
{
|
{
|
||||||
|
|
@ -15,7 +14,6 @@ enum StockStatus: string
|
||||||
self::PENDING => 'Pending',
|
self::PENDING => 'Pending',
|
||||||
self::COMPLETED => 'Completed',
|
self::COMPLETED => 'Completed',
|
||||||
self::CANCELLED => 'Cancelled',
|
self::CANCELLED => 'Cancelled',
|
||||||
self::EXPIRED => 'Expired',
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,23 +5,17 @@ namespace Blax\Shop\Enums;
|
||||||
enum StockType: string
|
enum StockType: string
|
||||||
{
|
{
|
||||||
case RESERVATION = 'reservation';
|
case RESERVATION = 'reservation';
|
||||||
case ADJUSTMENT = 'adjustment';
|
|
||||||
case SALE = 'sale';
|
|
||||||
case RETURN = 'return';
|
case RETURN = 'return';
|
||||||
case INCREASE = 'increase';
|
case INCREASE = 'increase';
|
||||||
case DECREASE = 'decrease';
|
case DECREASE = 'decrease';
|
||||||
case RELEASE = 'release';
|
|
||||||
|
|
||||||
public function label(): string
|
public function label(): string
|
||||||
{
|
{
|
||||||
return match ($this) {
|
return match ($this) {
|
||||||
self::RESERVATION => 'Reservation',
|
self::RESERVATION => 'Reservation',
|
||||||
self::ADJUSTMENT => 'Adjustment',
|
|
||||||
self::SALE => 'Sale',
|
|
||||||
self::RETURN => 'Return',
|
self::RETURN => 'Return',
|
||||||
self::INCREASE => 'Increase',
|
self::INCREASE => 'Increase',
|
||||||
self::DECREASE => 'Decrease',
|
self::DECREASE => 'Decrease',
|
||||||
self::RELEASE => 'Release',
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,12 +45,6 @@ class ProductStock extends Model
|
||||||
static::created(function ($model) {
|
static::created(function ($model) {
|
||||||
$model->logStockChange();
|
$model->logStockChange();
|
||||||
});
|
});
|
||||||
|
|
||||||
static::updated(function ($model) {
|
|
||||||
if ($model->wasChanged('status') && $model->status === StockStatus::COMPLETED) {
|
|
||||||
$model->releaseStock();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function product(): BelongsTo
|
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
|
public static function releaseExpired(): int
|
||||||
{
|
{
|
||||||
$expired = self::expired()->get();
|
$expired = self::expired()->get();
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,7 @@ trait HasStocks
|
||||||
{
|
{
|
||||||
return $this->stocks()
|
return $this->stocks()
|
||||||
->available()
|
->available()
|
||||||
->where(function ($query) {
|
->willExpire()
|
||||||
$query->whereNull('expires_at')
|
|
||||||
->orWhere('expires_at', '>', now());
|
|
||||||
})
|
|
||||||
->sum('quantity') ?? 0;
|
->sum('quantity') ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -188,10 +185,7 @@ trait HasStocks
|
||||||
$stockModel = config('shop.models.product_stock', 'Blax\Shop\Models\ProductStock');
|
$stockModel = config('shop.models.product_stock', 'Blax\Shop\Models\ProductStock');
|
||||||
|
|
||||||
return $stockModel::reservations()
|
return $stockModel::reservations()
|
||||||
->where(function ($query) {
|
->willExpire()
|
||||||
$query->whereNull('expires_at')
|
|
||||||
->orWhere('expires_at', '>', now());
|
|
||||||
})
|
|
||||||
->where('product_id', $this->id);
|
->where('product_id', $this->id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue