From 5cc7b1f3f079ac5642552430b73fc43a4d619444 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Thu, 4 Dec 2025 10:51:45 +0100 Subject: [PATCH] RI cleaned up and refactored stocks --- src/Enums/StockStatus.php | 2 -- src/Enums/StockType.php | 6 ------ src/Models/ProductStock.php | 25 ------------------------- src/Traits/HasStocks.php | 10 ++-------- 4 files changed, 2 insertions(+), 41 deletions(-) diff --git a/src/Enums/StockStatus.php b/src/Enums/StockStatus.php index 9ae6d86..7e312b6 100644 --- a/src/Enums/StockStatus.php +++ b/src/Enums/StockStatus.php @@ -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', }; } } diff --git a/src/Enums/StockType.php b/src/Enums/StockType.php index 66fd166..00d2b8c 100644 --- a/src/Enums/StockType.php +++ b/src/Enums/StockType.php @@ -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', }; } } diff --git a/src/Models/ProductStock.php b/src/Models/ProductStock.php index 38c7567..eb6e256 100644 --- a/src/Models/ProductStock.php +++ b/src/Models/ProductStock.php @@ -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(); diff --git a/src/Traits/HasStocks.php b/src/Traits/HasStocks.php index fba329d..d1af5a2 100644 --- a/src/Traits/HasStocks.php +++ b/src/Traits/HasStocks.php @@ -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); } }