Skip to content

Commit

Permalink
fix: calculate discount price and economy from article price instead …
Browse files Browse the repository at this point in the history
…of article discount with amount for fixed price kind
  • Loading branch information
HoreKk committed Nov 21, 2024
1 parent 6749a72 commit a9071cf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
3 changes: 1 addition & 2 deletions webapp/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
13 changes: 9 additions & 4 deletions webapp/src/components/modals/ObizOrderProcessModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -164,9 +171,8 @@ const ObizOfferVariableContent = ({
setCheckedCGV={setCheckedCGV}
formError={error}
setFormError={setError}
discount={articles[0].reductionPercentage}
articles={selectedArticles}
amount={amount}
articles={selectedArticles}
offer={offer}
/>
)}
Expand All @@ -177,11 +183,10 @@ const ObizOfferVariableContent = ({
</>
);
case "payment":
const discount = articles[0].reductionPercentage;
return (
<LayoutOrderStatus
status="loading"
title={`Vous allez payer ${formatter2Digits.format(amount - (amount * discount) / 100)}€`}
title={`Vous allez payer ${formatter2Digits.format(amountWithDiscount)}€`}
>
{" "}
<Box mt="auto" textAlign="center">
Expand Down
44 changes: 33 additions & 11 deletions webapp/src/components/obiz/DiscountAmountBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}}
Expand Down Expand Up @@ -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(".", ",")}
</Text>
</Flex>
Expand Down Expand Up @@ -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(".", ",")}
</Text>
</Center>
Expand Down
18 changes: 15 additions & 3 deletions webapp/src/components/obiz/RecapOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { Dispatch, SetStateAction, useState } from "react";

type RecapOrderDefaultProps = {
amount: number;
discount: number;
offer: OfferIncluded;
checkedCGV: boolean;
setCheckedCGV: Dispatch<SetStateAction<boolean>>;
Expand All @@ -26,6 +25,7 @@ type RecapOrderDefaultProps = {

interface RecapOrderVariable extends RecapOrderDefaultProps {
kind: "variable_price";
discount: number;
}

interface RecapOrderFixed extends RecapOrderDefaultProps {
Expand All @@ -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<HTMLInputElement>) => {
Expand Down

0 comments on commit a9071cf

Please sign in to comment.