From a9071cf664ff3fbe133216bd66c21add4ea78293 Mon Sep 17 00:00:00 2001 From: Antoine Lelong Date: Thu, 21 Nov 2024 15:13:36 +0100 Subject: [PATCH] fix: calculate discount price and economy from article price instead of article discount with amount for fixed price kind --- webapp/CHANGELOG.md | 3 +- .../modals/ObizOrderProcessModal.tsx | 13 ++++-- .../components/obiz/DiscountAmountBlock.tsx | 44 ++++++++++++++----- webapp/src/components/obiz/RecapOrder.tsx | 18 ++++++-- 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/webapp/CHANGELOG.md b/webapp/CHANGELOG.md index ae75d9ac..60126ba1 100644 --- a/webapp/CHANGELOG.md +++ b/webapp/CHANGELOG.md @@ -1,9 +1,8 @@ ## [0.70.16](https://github.com/SocialGouv/carte-jeune-engage/compare/v0.70.15...v0.70.16) (2024-11-21) - ### Bug Fixes -* remove link to page help and put crisp ([16a9d60](https://github.com/SocialGouv/carte-jeune-engage/commit/16a9d60405fc003e1e46a60aeba71a7957c47968)) +- remove link to page help and put crisp ([16a9d60](https://github.com/SocialGouv/carte-jeune-engage/commit/16a9d60405fc003e1e46a60aeba71a7957c47968)) ## [0.70.15](https://github.com/SocialGouv/carte-jeune-engage/compare/v0.70.14...v0.70.15) (2024-11-21) diff --git a/webapp/src/components/modals/ObizOrderProcessModal.tsx b/webapp/src/components/modals/ObizOrderProcessModal.tsx index 9092a91f..01e74dfd 100644 --- a/webapp/src/components/modals/ObizOrderProcessModal.tsx +++ b/webapp/src/components/modals/ObizOrderProcessModal.tsx @@ -46,6 +46,13 @@ const ObizOfferVariableContent = ({ const isVariablePrice = articles.length === 1 && articles[0].kind === "variable_price"; + const amountWithDiscount = isVariablePrice + ? amount - (amount * articles[0].reductionPercentage) / 100 + : selectedArticles.reduce( + (acc, { article, quantity }) => acc + quantity * (article.price ?? 0), + 0 + ); + switch (step) { case "amount": if (isVariablePrice) { @@ -164,9 +171,8 @@ const ObizOfferVariableContent = ({ setCheckedCGV={setCheckedCGV} formError={error} setFormError={setError} - discount={articles[0].reductionPercentage} - articles={selectedArticles} amount={amount} + articles={selectedArticles} offer={offer} /> )} @@ -177,11 +183,10 @@ const ObizOfferVariableContent = ({ ); case "payment": - const discount = articles[0].reductionPercentage; return ( {" "} diff --git a/webapp/src/components/obiz/DiscountAmountBlock.tsx b/webapp/src/components/obiz/DiscountAmountBlock.tsx index 9da40fe2..2cd52aa2 100644 --- a/webapp/src/components/obiz/DiscountAmountBlock.tsx +++ b/webapp/src/components/obiz/DiscountAmountBlock.tsx @@ -89,11 +89,13 @@ const DiscountArticleBlock = ({ if (quantity === 0) return; setIsMaximumQuantity(false); setSelectedArticles((prev) => - prev.map((a) => - a.article.reference === article.reference - ? { ...a, quantity: a.quantity - 1 } - : a - ) + prev + .map((a) => + a.article.reference === article.reference + ? { ...a, quantity: a.quantity - 1 } + : a + ) + .filter((a) => a.quantity > 0) ); setAmount((prev) => prev - (article.publicPrice as number)); }} @@ -278,9 +280,18 @@ const DiscountAmountBlock = (props: DiscountAmountBlockProps) => { fontWeight={800} opacity={isDisabled ? 0.25 : 1} > - {(amount - amount * (discount / 100)) - .toFixed(isDisabled ? 0 : 2) - .replace(".", ",")} + {kind === "variable_price" + ? (amount - amount * (discount / 100)) + .toFixed(isDisabled ? 0 : 2) + .replace(".", ",") + : props.selectedArticles + .reduce( + (acc, { article, quantity }) => + acc + (article.price ?? 0) * quantity, + 0 + ) + .toFixed(isDisabled ? 0 : 2) + .replace(".", ",")} € @@ -314,9 +325,20 @@ const DiscountAmountBlock = (props: DiscountAmountBlockProps) => { fontWeight={800} opacity={isDisabled ? 0.25 : 1} > - {(amount * (discount / 100)) - .toFixed(isDisabled ? 0 : 2) - .replace(".", ",")} + {kind === "variable_price" + ? (amount * (discount / 100)) + .toFixed(isDisabled ? 0 : 2) + .replace(".", ",") + : props.selectedArticles + .reduce( + (acc, { article, quantity }) => + acc + + ((article.publicPrice ?? 0) - (article.price ?? 0)) * + quantity, + 0 + ) + .toFixed(isDisabled ? 0 : 2) + .replace(".", ",")} € diff --git a/webapp/src/components/obiz/RecapOrder.tsx b/webapp/src/components/obiz/RecapOrder.tsx index bb2065a1..1c29badf 100644 --- a/webapp/src/components/obiz/RecapOrder.tsx +++ b/webapp/src/components/obiz/RecapOrder.tsx @@ -16,7 +16,6 @@ import { Dispatch, SetStateAction, useState } from "react"; type RecapOrderDefaultProps = { amount: number; - discount: number; offer: OfferIncluded; checkedCGV: boolean; setCheckedCGV: Dispatch>; @@ -26,6 +25,7 @@ type RecapOrderDefaultProps = { interface RecapOrderVariable extends RecapOrderDefaultProps { kind: "variable_price"; + discount: number; } interface RecapOrderFixed extends RecapOrderDefaultProps { @@ -39,16 +39,28 @@ const RecapOrder = (props: RecapOrderProps) => { const { kind, amount, - discount, offer, checkedCGV, setCheckedCGV, formError, setFormError, } = props; - const amountWithDiscount = amount - (amount * discount) / 100; + + const amountWithDiscount = + kind === "variable_price" + ? amount - (amount * props.discount) / 100 + : props.articles.reduce( + (acc, { article, quantity }) => acc + quantity * (article.price ?? 0), + 0 + ); + const discountAmount = amount - amountWithDiscount; + const discount = + kind === "variable_price" + ? props.discount + : Math.round(((amount - amountWithDiscount) / amount) * 100); + const [showCondition, setShowCondition] = useState(false); const handleCheckedCGV = (e: React.ChangeEvent) => {