From ab8ea6afecaf3ba07366e2c09376a3b223c048b7 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Mon, 18 May 2026 11:54:11 +0200 Subject: [PATCH] BF stock calculation --- README.md | 6 +++--- src/Models/Product.php | 2 +- src/Traits/HasStocks.php | 8 +++++--- src/Traits/MayBePoolProduct.php | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 912854d..692f284 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ # Laravel Shop [![Tests](https://github.com/blax-software/laravel-shop/actions/workflows/tests.yml/badge.svg)](https://github.com/blax-software/laravel-shop/actions/workflows/tests.yml) -[![Tests Count](https://img.shields.io/badge/tests-1228%20passing-success?style=flat-square)](#testing) -[![Assertions](https://img.shields.io/badge/assertions-3291-blue?style=flat-square)](#testing) +[![Tests Count](https://img.shields.io/badge/tests-1349%20passing-success?style=flat-square)](#testing) +[![Assertions](https://img.shields.io/badge/assertions-3641-blue?style=flat-square)](#testing) [![Latest Version](https://img.shields.io/packagist/v/blax-software/laravel-shop.svg?style=flat-square)](https://packagist.org/packages/blax-software/laravel-shop) [![License](https://img.shields.io/packagist/l/blax-software/laravel-shop.svg?style=flat-square)](https://packagist.org/packages/blax-software/laravel-shop) [![PHP Version](https://img.shields.io/packagist/php-v/blax-software/laravel-shop.svg?style=flat-square)](https://packagist.org/packages/blax-software/laravel-shop) @@ -192,7 +192,7 @@ booking, Stripe sync and the event surface — so host applications can lean on the behaviour with confidence. ``` -Tests: 1228, Assertions: 3291 +Tests: 1349, Assertions: 3641 ``` CI runs the full suite on every push (see the badge above). To run it diff --git a/src/Models/Product.php b/src/Models/Product.php index ff71367..edaf856 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -663,7 +663,7 @@ class Product extends Model implements Purchasable, Cartable // Use base stock at the START of the booking period and subtract all overlapping reservations // We check availability at $from because claims that expire before then should not affect availability // Note: overlappingBookings is already negative (DECREASE entries), so we add it - $availableStock = $this->getAvailableStock($from) - abs($overlappingClaims) + $overlappingBookings; + $availableStock = $this->getAvailableStock($from) - abs((int) $overlappingClaims) + (int) $overlappingBookings; return $availableStock >= $quantity; } diff --git a/src/Traits/HasStocks.php b/src/Traits/HasStocks.php index 1854177..f0fa390 100644 --- a/src/Traits/HasStocks.php +++ b/src/Traits/HasStocks.php @@ -503,7 +503,9 @@ trait HasStocks */ public function getCurrentlyClaimedStock(): int { - return abs($this->stocks() + // SQL SUM comes back as a numeric string under PDO mysql; cast + // before abs() so strict types accept it. + return abs((int) $this->stocks() ->whereIn('type', StockType::claimTypeValues()) ->where('status', StockStatus::PENDING->value) ->willExpire() @@ -523,7 +525,7 @@ trait HasStocks */ public function getActiveAndPlannedClaimedStock(): int { - return abs($this->stocks() + return abs((int) $this->stocks() ->whereIn('type', StockType::claimTypeValues()) ->where('status', StockStatus::PENDING->value) ->willExpire() @@ -552,7 +554,7 @@ trait HasStocks }); } - return abs($query->sum('quantity')); + return abs((int) $query->sum('quantity')); } diff --git a/src/Traits/MayBePoolProduct.php b/src/Traits/MayBePoolProduct.php index b466ae5..952fc75 100644 --- a/src/Traits/MayBePoolProduct.php +++ b/src/Traits/MayBePoolProduct.php @@ -326,7 +326,7 @@ trait MayBePoolProduct // Get available stock at the START of the booking period // This ensures claims that will expire before the booking starts don't reduce availability - $available = max(0, $item->getAvailableStock($from) - abs($overlappingClaims)); + $available = max(0, $item->getAvailableStock($from) - abs((int) $overlappingClaims)); } } elseif (!$item->isBooking()) { $available = $item->getAvailableStock();