fix: cart tariff calculation cleanup
This commit is contained in:
parent
2257c8cbfc
commit
1f508e8a9c
@ -337,7 +337,7 @@ console.log(cartTotal)
|
||||
color: theme.palette.secondary.main,
|
||||
}}
|
||||
>
|
||||
{row.price.toFixed(2)} ₽
|
||||
{(row.price/100).toFixed(2)} ₽
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
@ -366,7 +366,7 @@ console.log(cartTotal)
|
||||
marginTop: "10px",
|
||||
}}
|
||||
>
|
||||
ИТОГО: <span>{cartTotal?.totalPrice.toFixed(2)} ₽</span>
|
||||
ИТОГО: <span>{(cartTotal?.totalPrice/100).toFixed(2)} ₽</span>
|
||||
</Typography>
|
||||
</>
|
||||
)}
|
||||
|
@ -123,18 +123,19 @@ console.log(discounts)
|
||||
privilegesAffectedByCoupon.push(couponDiscount.Condition.Product);
|
||||
});
|
||||
|
||||
const privilege = findPrivilegeById(cartItem.tariff.privilegeId);
|
||||
const privilegeDiscount = findMaxApplicablePrivilegeDiscount(discounts, tariff);
|
||||
|
||||
privilegeDiscount?.Target.Products.forEach((product) => {
|
||||
if (privilegeDiscount.Condition.Product !== tariff.privilegeId) return;
|
||||
if (tariff.customPricePerUnit !== undefined) return;
|
||||
console.log( privilegeDiscount.Condition.Product !== privilege?.privilegeId)
|
||||
if (privilegeDiscount.Condition.Product !== privilege?.privilegeId) return;
|
||||
if (tariff.customPricePerUnit !== 0) return;
|
||||
if (privilegesAffectedByCoupon.includes(privilegeDiscount.Condition.Product)) return;
|
||||
|
||||
cartItemTotal.totalPrice *= product.Factor;
|
||||
cartItemTotal.envolvedDiscounts.push(privilegeDiscount);
|
||||
});
|
||||
|
||||
const privilege = findPrivilegeById(cartItem.tariff.privilegeId);
|
||||
if (!privilege)
|
||||
throw new Error(`Привилегия не найдена в тарифе ${cartItem.tariff.name} с id ${cartItem.tariff.id}`);
|
||||
|
||||
@ -162,6 +163,7 @@ console.log(discounts)
|
||||
|
||||
// layer 3
|
||||
const cartPurchasesAmountDiscount = findMaxCartPurchasesAmountDiscount(discounts, cartTotal);
|
||||
console.log(cartPurchasesAmountDiscount)
|
||||
if (cartPurchasesAmountDiscount) {
|
||||
cartTotal.totalPrice *= cartPurchasesAmountDiscount.Target.Factor;
|
||||
cartTotal.envolvedCartDiscounts.push(cartPurchasesAmountDiscount);
|
||||
@ -169,6 +171,7 @@ console.log(discounts)
|
||||
|
||||
// layer 4
|
||||
const totalPurchasesAmountDiscount = findMaxTotalPurchasesAmountDiscount(discounts, purchasesAmount);
|
||||
console.log(totalPurchasesAmountDiscount)
|
||||
if (totalPurchasesAmountDiscount) {
|
||||
cartTotal.totalPrice *= totalPurchasesAmountDiscount.Target.Factor;
|
||||
cartTotal.envolvedCartDiscounts.push(totalPurchasesAmountDiscount);
|
||||
@ -179,17 +182,17 @@ console.log(discounts)
|
||||
|
||||
function findMaxApplicablePrivilegeDiscount(discounts: Discount[], tariff: Tariff): Discount | null {
|
||||
const applicableDiscounts = discounts.filter((discount): discount is Discount => {
|
||||
|
||||
return (
|
||||
discount.Condition.Product.length !== 0 &&
|
||||
tariff.privilegeId === discount.Condition.Product &&
|
||||
tariff.amount >= Number(discount.Condition.Usage)
|
||||
discount.Condition.Product !== "" &&
|
||||
findPrivilegeById(tariff.privilegeId)?.privilegeId === discount.Condition.Product &&
|
||||
tariff.amount >= Number(discount.Condition.Term)
|
||||
);
|
||||
});
|
||||
|
||||
if (!applicableDiscounts.length) return null;
|
||||
|
||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) =>
|
||||
Number(current.Condition.Usage) > Number(prev.Condition.Usage) ? current : prev
|
||||
Number(current.Condition.Term) > Number(prev.Condition.Term) ? current : prev
|
||||
);
|
||||
|
||||
return maxValueDiscount;
|
||||
@ -201,9 +204,10 @@ function findMaxCartPurchasesAmountDiscount(
|
||||
): Discount | null {
|
||||
const applicableDiscounts = discounts.filter((discount): discount is Discount => {
|
||||
return (
|
||||
discount.Condition.UserType === "cartPurchasesAmount" && cartTotal.totalPrice >= Number(discount.Condition.CartPurchasesAmount)
|
||||
discount.Condition.CartPurchasesAmount > 0 && cartTotal.totalPrice >= Number(discount.Condition.CartPurchasesAmount)
|
||||
);
|
||||
});
|
||||
console.log(applicableDiscounts)
|
||||
|
||||
if (!applicableDiscounts.length) return null;
|
||||
|
||||
@ -219,8 +223,9 @@ function findMaxTotalPurchasesAmountDiscount(
|
||||
purchasesAmount: number
|
||||
): Discount | null {
|
||||
const applicableDiscounts = discounts.filter((discount): discount is Discount => {
|
||||
return discount.Condition.UserType === "purchasesAmount" && purchasesAmount >= Number(discount.Condition.PurchasesAmount);
|
||||
return discount.Condition.PurchasesAmount > 0 && purchasesAmount >= Number(discount.Condition.PurchasesAmount);
|
||||
});
|
||||
console.log(discounts)
|
||||
|
||||
if (!applicableDiscounts.length) return null;
|
||||
|
||||
@ -236,19 +241,18 @@ function findMaxServiceDiscount(
|
||||
discounts: Discount[],
|
||||
priceByService: ServiceToPriceMap
|
||||
): Discount | null {
|
||||
console.log(discounts)
|
||||
console.log(discounts,service)
|
||||
const discountsForTariffService = discounts.filter((discount): discount is Discount => {
|
||||
return (
|
||||
discount.Condition.Product.length !== 0 &&
|
||||
discount.Condition.Product === service &&
|
||||
priceByService[service] >= Number(discount.Condition.Usage)
|
||||
discount.Condition.Group === service &&
|
||||
priceByService[service] >= Number(discount.Condition.PriceFrom)
|
||||
);
|
||||
});
|
||||
|
||||
if (!discountsForTariffService.length) return null;
|
||||
|
||||
const maxValueDiscount = discountsForTariffService.reduce((prev, current) => {
|
||||
return Number(current.Condition.Usage) > Number(prev.Condition.Usage) ? current : prev;
|
||||
return Number(current.Condition.PriceFrom) > Number(prev.Condition.PriceFrom) ? current : prev;
|
||||
});
|
||||
|
||||
return maxValueDiscount;
|
||||
@ -280,28 +284,28 @@ export function createCartItem(tariff: Tariff): CartItem {
|
||||
}
|
||||
|
||||
export function findDiscountFactor(discount: Discount): any {
|
||||
switch (discount.Condition.UserType) {
|
||||
case "cartPurchasesAmount":
|
||||
if (discount.Condition.CartPurchasesAmount > 0)
|
||||
return Number(discount.Target.Factor);
|
||||
case "purchasesAmount":
|
||||
|
||||
if (discount.Condition.PurchasesAmount > 0)
|
||||
return Number(discount.Target.Factor);
|
||||
case "privilege": {
|
||||
|
||||
if (discount.Condition.Product !== "") {
|
||||
const product = discount.Target.Products[0];
|
||||
if (!product) throw new Error("Discount target product not found");
|
||||
return Number(product.Factor);
|
||||
|
||||
}
|
||||
case "user": {
|
||||
|
||||
if ((discount.Condition.Group as string) !== "")
|
||||
return Number(discount.Target.Factor);
|
||||
if (discount.Condition.UserType)
|
||||
return Number(discount.Target.Factor);
|
||||
if (discount.Condition.User !== "") {
|
||||
const product = discount.Target.Products[0];
|
||||
if (!product) throw new Error("Discount target product not found");
|
||||
|
||||
return Number(product.Factor);
|
||||
}
|
||||
case "service":
|
||||
return Number(discount.Target.Factor);
|
||||
case "userType":
|
||||
return Number(discount.Target.Factor);
|
||||
}
|
||||
}
|
||||
|
||||
export function formatDiscountFactor(factor: number): string {
|
||||
|
@ -81,6 +81,7 @@ console.log(selectedTariffs)
|
||||
|
||||
const gridData = tariffs
|
||||
?.map((tariff) => {
|
||||
console.log(tariff)
|
||||
const privilege = findPrivilegeById(tariff.privilegeId);
|
||||
return {
|
||||
id: tariff.id,
|
||||
|
Loading…
Reference in New Issue
Block a user