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