From 396657f06221d4e14947502faf27cac1028a8c7b Mon Sep 17 00:00:00 2001 From: DevSviat Date: Sun, 1 Nov 2026 13:45:21 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=BE=D0=BA=D1=80=D1=83=D0=B3=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=86=D1=96=D0=BD=D0=B0=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B7=D0=BC=D1=96=D0=BD=D1=8E=D1=94=D1=82=D1=8C=D1=81=D1=8F=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=20=D0=B7=D0=B1=D0=B5=D1=80=D0=B5=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D1=96=20=D0=B7=D0=B0=D0=BC=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D1=8F=20=D0=B2=20=D0=B0=D0=B4=D0=BC=D1=96=D0=BD?= =?UTF-8?q?=D1=86=D1=96=20(1.0.20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Init/Init.php | 6 ++ Init/module.json | 2 +- Init/services.php | 5 ++ Services/PromoDiscountForDb.php | 41 ++++++++++++++ tests/PromoDiscountForDbTest.php | 94 ++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 Services/PromoDiscountForDb.php create mode 100644 tests/PromoDiscountForDbTest.php diff --git a/Init/Init.php b/Init/Init.php index b047e82..8481961 100644 --- a/Init/Init.php +++ b/Init/Init.php @@ -41,6 +41,7 @@ use Okay\Admin\Helpers\BackendOrdersHelper; use Okay\Modules\Sviat\Promo\Services\AdminOrderPromoApplier; use Okay\Modules\Sviat\Promo\Services\AdminPurchasePriceRounder; +use Okay\Modules\Sviat\Promo\Services\PromoDiscountForDb; use Okay\Modules\Sviat\Promo\Services\PurchasePriceRounder; class Init extends AbstractInit @@ -237,6 +238,11 @@ public function init(): void ['class' => PromoCartHooks::class, 'method' => 'getPromoGiftPurchases'] ); + $this->registerChainExtension( + ['class' => DiscountsHelper::class, 'method' => 'prepareForDB'], + ['class' => PromoDiscountForDb::class, 'method' => 'keepActualAmount'] + ); + $this->registerChainExtension( ['class' => Purchase::class, 'method' => 'getForDB'], ['class' => PurchasePriceRounder::class, 'method' => 'normalize'] diff --git a/Init/module.json b/Init/module.json index 99d6e5b..5c8b49d 100644 --- a/Init/module.json +++ b/Init/module.json @@ -1,6 +1,6 @@ { "Okay": "4.5.3", - "version": "1.0.19", + "version": "1.0.20", "moduleName": "Promo: акції та знижки", "vendor": { "email": "devsviat@proton.me", diff --git a/Init/services.php b/Init/services.php index ba2077f..5515580 100644 --- a/Init/services.php +++ b/Init/services.php @@ -20,6 +20,7 @@ use Okay\Modules\Sviat\Promo\Services\CartDiscountPipeline; use Okay\Modules\Sviat\Promo\Services\PromoFeedPriceResolver; use Okay\Modules\Sviat\Promo\Services\PromoProductDisplayService; +use Okay\Modules\Sviat\Promo\Services\PromoDiscountForDb; use Okay\Modules\Sviat\Promo\Services\PurchasePriceRounder; use Okay\Modules\Sviat\Promo\Services\AdminOrderPromoApplier; use Okay\Modules\Sviat\Promo\Services\AdminPurchasePriceRounder; @@ -69,6 +70,10 @@ new SR(EntityFactory::class), ], ], + PromoDiscountForDb::class => [ + 'class' => PromoDiscountForDb::class, + 'arguments' => [], + ], PurchasePriceRounder::class => [ 'class' => PurchasePriceRounder::class, 'arguments' => [ diff --git a/Services/PromoDiscountForDb.php b/Services/PromoDiscountForDb.php new file mode 100644 index 0000000..f8bf9cd --- /dev/null +++ b/Services/PromoDiscountForDb.php @@ -0,0 +1,41 @@ +sign !== 'sviat_promo') { + return $result; + } + + $configured = $discount->type === 'percent' + ? (float) $discount->priceBeforeDiscount * (float) $discount->value / 100 + : (float) $discount->value; + $actual = (float) $discount->absoluteDiscount; + + if (abs($configured - $actual) > self::PRECISION) { + $result[0]->type = 'absolute'; + $result[0]->value = $actual; + } + + return $result; + } +} diff --git a/tests/PromoDiscountForDbTest.php b/tests/PromoDiscountForDbTest.php new file mode 100644 index 0000000..b2b81ec --- /dev/null +++ b/tests/PromoDiscountForDbTest.php @@ -0,0 +1,94 @@ +sign = $sign; + $discount->type = $type; + $discount->value = $value; + $discount->priceBeforeDiscount = $before; + $discount->priceAfterDiscount = $after; + $discount->absoluteDiscount = round($before - $after, 2); + + return $discount; + } + + private function forDb(Discount $discount): object + { + $row = (object) ['type' => $discount->type, 'value' => $discount->value]; + + return new PromoDiscountForDb()->keepActualAmount([$row, []], $discount, 'purchase', 1)[0]; + } + + /** Те, що робить ядро при збереженні замовлення: новий Discount із рядка БД. */ + private function recalculatedPrice(object $row, float $before): float + { + $restored = new Discount(); + $restored->type = $row->type; + $restored->value = (float) $row->value; + $restored->calculate($before); + + return round($before - $restored->absoluteDiscount, 2); + } + + /** @return array */ + public static function roundedCases(): array + { + return [ + 'percent rounded up' => ['percent', 8.75, 1000.0, 913.0], + 'percent rounded down' => ['percent', 8.8, 1000.2, 912.0], + 'fixed rounded' => ['absolute', 100.0, 1000.5, 901.0], + ]; + } + + #[DataProvider('roundedCases')] + public function testRoundedPriceSurvivesRecalculation(string $type, float $value, float $before, float $after): void + { + $row = $this->forDb($this->discount($type, $value, $before, $after)); + + self::assertSame('absolute', $row->type); + self::assertSame($after, $this->recalculatedPrice($row, $before)); + } + + public function testExactPercentStaysPercent(): void + { + $row = $this->forDb($this->discount('percent', 10.0, 1000.0, 900.0)); + + self::assertSame('percent', $row->type); + self::assertSame(10.0, $row->value); + } + + public function testExactFixedStaysConfigured(): void + { + $row = $this->forDb($this->discount('absolute', 100.0, 1000.0, 900.0)); + + self::assertSame(100.0, $row->value); + } + + public function testForeignDiscountsAreLeftAlone(): void + { + $row = $this->forDb($this->discount('percent', 8.75, 1000.0, 913.0, 'coupon')); + + self::assertSame('percent', $row->type); + } + + public function testRegistrationPointsAtTheCoreMethod(): void + { + $init = (string) file_get_contents(__DIR__ . '/../../../../Okay/Modules/Sviat/Promo/Init/Init.php'); + + self::assertMatchesRegularExpression( + "/DiscountsHelper::class, 'method' => 'prepareForDB'\\],\\s*\\['class' => PromoDiscountForDb::class, 'method' => 'keepActualAmount'\\]/", + $init + ); + } +}