From 372ca39aa3b8926afbadbecf191d2e593a6a041f Mon Sep 17 00:00:00 2001 From: Adam Daley Date: Thu, 9 Jul 2026 10:00:44 +0100 Subject: [PATCH] Fix crash on Product Promotions page for promo rows with NULL flags (#3946) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix crash on Product Promotions page for promo rows with NULL flags The `promo` table's maxuses/used/freesetup/once_per_client/recurring/ active columns are nullable in the DB, but the Doctrine entity mapped them as non-nullable typed properties. When a row (typically from a legacy install) has NULL in one of these columns, Doctrine's TypedNoDefaultPropertyAccessor unsets the property instead of applying the PHP default, so any read throws "must not be accessed before initialization" — crashing the admin Product Promotions page. Align the entity mapping with the actual nullable schema and coalesce null to the intended defaults in the getters and toApiArray(). Fixes #3942 * Coalesce NULL used counts in promo usage SQL promoCanBeApplied() now treats a NULL used column as 0, but PromoRepository's raw SQL for incrementing/decrementing usage did not: `used + 1` and `GREATEST(used - ?, 0)` both propagate NULL in MySQL, and `maxuses > used` is unknown (never true) when used IS NULL. That let checkout accept a legacy promo with a NULL used count and then immediately fail to reserve it, throwing "maximum number of uses reached". Coalesce used to 0 in both the SET and WHERE clauses so reservation matches what promoCanBeApplied() already allows. --- src/modules/Product/Entity/Promo.php | 54 ++++++++++--------- .../Product/Repository/PromoRepository.php | 4 +- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/modules/Product/Entity/Promo.php b/src/modules/Product/Entity/Promo.php index 041f76c2c..2c6649060 100644 --- a/src/modules/Product/Entity/Promo.php +++ b/src/modules/Product/Entity/Promo.php @@ -44,23 +44,29 @@ class Promo implements ApiArrayInterface, TimestampInterface #[ORM\Column(type: \Doctrine\DBAL\Types\Types::DECIMAL, precision: 18, scale: 2, nullable: true)] private ?string $value = null; - #[ORM\Column(name: 'maxuses', type: \Doctrine\DBAL\Types\Types::INTEGER, options: ['default' => 0])] - private int $maxUses = 0; + #[ORM\Column(name: 'maxuses', type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: true, options: ['default' => 0])] + /** @phpstan-ignore property.unusedType (column is nullable in the DB) */ + private ?int $maxUses = 0; - #[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, options: ['default' => 0])] - private int $used = 0; + #[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: true, options: ['default' => 0])] + /** @phpstan-ignore property.unusedType (column is nullable in the DB) */ + private ?int $used = 0; - #[ORM\Column(name: 'freesetup', type: \Doctrine\DBAL\Types\Types::BOOLEAN, options: ['default' => false])] - private bool $freeSetup = false; + #[ORM\Column(name: 'freesetup', type: \Doctrine\DBAL\Types\Types::BOOLEAN, nullable: true, options: ['default' => false])] + /** @phpstan-ignore property.unusedType (column is nullable in the DB) */ + private ?bool $freeSetup = false; - #[ORM\Column(name: 'once_per_client', type: \Doctrine\DBAL\Types\Types::BOOLEAN, options: ['default' => false])] - private bool $oncePerClient = false; + #[ORM\Column(name: 'once_per_client', type: \Doctrine\DBAL\Types\Types::BOOLEAN, nullable: true, options: ['default' => false])] + /** @phpstan-ignore property.unusedType (column is nullable in the DB) */ + private ?bool $oncePerClient = false; - #[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN, options: ['default' => false])] - private bool $recurring = false; + #[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN, nullable: true, options: ['default' => false])] + /** @phpstan-ignore property.unusedType (column is nullable in the DB) */ + private ?bool $recurring = false; - #[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN, options: ['default' => false])] - private bool $active = false; + #[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN, nullable: true, options: ['default' => false])] + /** @phpstan-ignore property.unusedType (column is nullable in the DB) */ + private ?bool $active = false; #[ORM\Column(type: \Doctrine\DBAL\Types\Types::TEXT, nullable: true)] private ?string $products = null; @@ -85,12 +91,12 @@ class Promo implements ApiArrayInterface, TimestampInterface 'description' => $this->description, 'type' => $this->type, 'value' => $this->value !== null ? (float) $this->value : null, - 'maxuses' => $this->maxUses, - 'used' => $this->used, - 'freesetup' => $this->freeSetup, - 'once_per_client' => $this->oncePerClient, - 'recurring' => $this->recurring, - 'active' => $this->active, + 'maxuses' => $this->maxUses ?? 0, + 'used' => $this->used ?? 0, + 'freesetup' => $this->freeSetup ?? false, + 'once_per_client' => $this->oncePerClient ?? false, + 'recurring' => $this->recurring ?? false, + 'active' => $this->active ?? false, 'products' => $this->products, 'periods' => $this->periods, 'client_groups' => $this->clientGroups, @@ -156,7 +162,7 @@ class Promo implements ApiArrayInterface, TimestampInterface public function getMaxUses(): int { - return $this->maxUses; + return $this->maxUses ?? 0; } public function setMaxUses(?int $maxUses): self @@ -168,7 +174,7 @@ class Promo implements ApiArrayInterface, TimestampInterface public function getUsed(): int { - return $this->used; + return $this->used ?? 0; } public function setUsed(?int $used): self @@ -180,7 +186,7 @@ class Promo implements ApiArrayInterface, TimestampInterface public function isFreeSetup(): bool { - return $this->freeSetup; + return $this->freeSetup ?? false; } public function setFreeSetup(bool $freeSetup): self @@ -192,7 +198,7 @@ class Promo implements ApiArrayInterface, TimestampInterface public function isOncePerClient(): bool { - return $this->oncePerClient; + return $this->oncePerClient ?? false; } public function setOncePerClient(bool $oncePerClient): self @@ -204,7 +210,7 @@ class Promo implements ApiArrayInterface, TimestampInterface public function isRecurring(): bool { - return $this->recurring; + return $this->recurring ?? false; } public function setRecurring(bool $recurring): self @@ -216,7 +222,7 @@ class Promo implements ApiArrayInterface, TimestampInterface public function isActive(): bool { - return $this->active; + return $this->active ?? false; } public function setActive(bool $active): self diff --git a/src/modules/Product/Repository/PromoRepository.php b/src/modules/Product/Repository/PromoRepository.php index 76423f8ac..fbdadc675 100644 --- a/src/modules/Product/Repository/PromoRepository.php +++ b/src/modules/Product/Repository/PromoRepository.php @@ -75,7 +75,7 @@ class PromoRepository extends EntityRepository public function incrementUsageIfAvailable(int $promoId, \DateTimeInterface $updatedAt): int { return $this->getEntityManager()->getConnection()->executeStatement( - 'UPDATE promo SET used = used + 1, updated_at = ? WHERE id = ? AND (maxuses = 0 OR maxuses IS NULL OR maxuses > used)', + 'UPDATE promo SET used = COALESCE(used, 0) + 1, updated_at = ? WHERE id = ? AND (maxuses = 0 OR maxuses IS NULL OR maxuses > COALESCE(used, 0))', [$updatedAt->format('Y-m-d H:i:s'), $promoId] ); } @@ -83,7 +83,7 @@ class PromoRepository extends EntityRepository public function decrementUsage(int $promoId, int $count, \DateTimeInterface $updatedAt): int { return $this->getEntityManager()->getConnection()->executeStatement( - 'UPDATE promo SET used = GREATEST(used - ?, 0), updated_at = ? WHERE id = ?', + 'UPDATE promo SET used = GREATEST(COALESCE(used, 0) - ?, 0), updated_at = ? WHERE id = ?', [$count, $updatedAt->format('Y-m-d H:i:s'), $promoId] ); }