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