add nko checkbox
This commit is contained in:
parent
378de99bff
commit
f2cf384fd1
@ -1,5 +1,5 @@
|
||||
import theme from "@theme";
|
||||
import { Button, Paper, Box, Typography, TableHead, TableRow, TableCell, TableBody, Table, Tooltip, Alert } from "@mui/material";
|
||||
import { Button, Paper, Box, Typography, TableHead, TableRow, TableCell, TableBody, Table, Tooltip, Alert, Checkbox, FormControlLabel } from "@mui/material";
|
||||
import Input from "@kitUI/input";
|
||||
import { useCartStore } from "@root/stores/cart";
|
||||
import { useState } from "react";
|
||||
@ -22,6 +22,7 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
const setCartTotal = useCartStore(store => store.setCartTotal);
|
||||
const [couponField, setCouponField] = useState<string>("");
|
||||
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
||||
const [isNonCommercial, setIsNonCommercial] = useState<boolean>(false);
|
||||
// const [coupon, setCoupon] = useState<string | undefined>();
|
||||
|
||||
const cartRows = cartTotal?.items.map(cartItemTotal => {
|
||||
@ -63,8 +64,8 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
};
|
||||
});
|
||||
|
||||
const cartDiscounts = cartTotal?.envolvedCartDiscounts
|
||||
const cartDiscountsResultFactor = cartDiscounts && cartDiscounts.reduce((acc, discount) => acc * findDiscountFactor(discount), 1);
|
||||
const cartDiscounts = cartTotal?.envolvedCartDiscounts;
|
||||
const cartDiscountsResultFactor = cartDiscounts && cartDiscounts?.length > 1 && cartDiscounts.reduce((acc, discount) => acc * findDiscountFactor(discount), 1);
|
||||
|
||||
const envolvedCartDiscountsElement = cartDiscounts && (
|
||||
<Box sx={{
|
||||
@ -89,7 +90,7 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
function handleCalcCartClick() {
|
||||
const cartTariffs = tariffs.filter(tariff => selectedTariffs.includes(tariff.id));
|
||||
const cartItems = cartTariffs.map(tariff => createCartItem(tariff));
|
||||
const cartData = calcCartData(testUser, cartItems, discounts, couponField);
|
||||
const cartData = calcCartData(testUser, cartItems, discounts, isNonCommercial, couponField);
|
||||
|
||||
if (cartData instanceof Error) {
|
||||
setErrorMessage(cartData.message);
|
||||
@ -118,7 +119,12 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
</Typography>
|
||||
<Paper
|
||||
variant="bar"
|
||||
sx={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}
|
||||
sx={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "space-between",
|
||||
gap: "20px",
|
||||
}}
|
||||
>
|
||||
<Box
|
||||
sx={{
|
||||
@ -150,7 +156,23 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
onClick={() => setCoupon(couponField)}
|
||||
>применить промокод</Button> */}
|
||||
</Box>
|
||||
<Button onClick={handleCalcCartClick}>рассчитать</Button>
|
||||
<FormControlLabel
|
||||
checked={isNonCommercial}
|
||||
onChange={(e, checked) => setIsNonCommercial(checked)}
|
||||
control={<Checkbox
|
||||
sx={{
|
||||
color: theme.palette.secondary.main,
|
||||
"&.Mui-checked": {
|
||||
color: theme.palette.secondary.main,
|
||||
},
|
||||
}}
|
||||
/>}
|
||||
label="НКО"
|
||||
sx={{
|
||||
color: theme.palette.secondary.main,
|
||||
}}
|
||||
/>
|
||||
<Button onClick={handleCalcCartClick} sx={{ ml: "auto" }}>рассчитать</Button>
|
||||
</Paper>
|
||||
|
||||
{cartTotal?.items && cartTotal.items.length > 0 &&
|
||||
|
@ -188,7 +188,7 @@ describe("cart tests", () => {
|
||||
it("юзер использовал промокод id33. он заменяет скидку на p6 собой. в один момент времени может быть активирован только 1 промокод, т.е. после активации следующего, предыдущий заменяется. но в промокоде может быть несколько скидок. промокоды имеют скидки только на привелеги", () => {
|
||||
const testCase = prepareTestCase(exampleCartValues.testCases[9]);
|
||||
|
||||
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts, "ABCD") as CartTotal;
|
||||
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts, false, "ABCD") as CartTotal;
|
||||
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts.map(discount => discount._id)];
|
||||
cartTotal.items.forEach(cartItem => {
|
||||
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts.map(discount => discount._id));
|
||||
@ -204,7 +204,7 @@ describe("cart tests", () => {
|
||||
it("юзер подтвердил свой статус НКО, поэтому, не смотря на то что он достиг по лояльности уровня скидки id2, она не применилась, а применилась id32", () => {
|
||||
const testCase = prepareTestCase(exampleCartValues.testCases[10]);
|
||||
|
||||
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts) as CartTotal;
|
||||
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts, true) as CartTotal;
|
||||
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts.map(discount => discount._id)];
|
||||
cartTotal.items.forEach(cartItem => {
|
||||
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts.map(discount => discount._id));
|
||||
|
@ -7,6 +7,7 @@ export function calcCartData(
|
||||
user: User,
|
||||
cartItems: CartItem[],
|
||||
discounts: AnyDiscount[],
|
||||
isNonCommercial: boolean = false,
|
||||
coupon?: string,
|
||||
): CartTotal | Error | null {
|
||||
let isIncompatibleTariffs = false;
|
||||
@ -48,7 +49,7 @@ export function calcCartData(
|
||||
|
||||
// layer 0
|
||||
for (const discount of discounts) {
|
||||
if (discount.conditionType !== "userType" || discount.condition.userType !== user.Type) continue;
|
||||
if (discount.conditionType !== "userType" || !isNonCommercial) continue;
|
||||
|
||||
cartItems.forEach(cartItem => {
|
||||
cartTotal.items.push({
|
||||
@ -123,7 +124,7 @@ export function calcCartData(
|
||||
}
|
||||
|
||||
// layer 4
|
||||
const totalPurchasesAmountDiscount = findMaxTotalPurchasesAmountDiscount(discounts, user);
|
||||
const totalPurchasesAmountDiscount = findMaxTotalPurchasesAmountDiscount(discounts, user.PurchasesAmount);
|
||||
if (totalPurchasesAmountDiscount) {
|
||||
cartTotal.totalPrice *= totalPurchasesAmountDiscount.factor;
|
||||
cartTotal.envolvedCartDiscounts.push(totalPurchasesAmountDiscount);
|
||||
@ -164,9 +165,9 @@ function findMaxCartPurchasesAmountDiscount(discounts: AnyDiscount[], cartTotal:
|
||||
return maxValueDiscount;
|
||||
}
|
||||
|
||||
function findMaxTotalPurchasesAmountDiscount(discounts: AnyDiscount[], user: User): PurchasesAmountDiscount | null {
|
||||
function findMaxTotalPurchasesAmountDiscount(discounts: AnyDiscount[], purchasesAmount: number): PurchasesAmountDiscount | null {
|
||||
const applicableDiscounts = discounts.filter((discount): discount is PurchasesAmountDiscount => {
|
||||
return discount.conditionType === "purchasesAmount" && user.PurchasesAmount >= discount.condition.purchasesAmount;
|
||||
return discount.conditionType === "purchasesAmount" && purchasesAmount >= discount.condition.purchasesAmount;
|
||||
});
|
||||
|
||||
if (!applicableDiscounts.length) return null;
|
||||
|
Loading…
Reference in New Issue
Block a user