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 + ); + } +}