2023-06-16 20:09:56 +00:00
|
|
|
import { Tariff } from "@root/model/tariff";
|
2023-07-13 18:59:23 +00:00
|
|
|
import { ServiceKeyToPrivilegesMap } from "@root/model/privilege";
|
2023-07-11 01:33:36 +00:00
|
|
|
import { useCartStore } from "@root/stores/cart";
|
|
|
|
import { useUserStore } from "@root/stores/user";
|
2023-07-13 18:59:23 +00:00
|
|
|
import { Discount, findCartDiscount, findLoyaltyDiscount, findPrivilegeDiscount, findServiceDiscount } from "@frontend/kitui";
|
2023-06-16 20:09:56 +00:00
|
|
|
|
2023-07-13 18:59:23 +00:00
|
|
|
export function calcIndividualTariffPrices(tariff: Tariff, discounts: Discount[], privilegies: ServiceKeyToPrivilegesMap): {
|
2023-06-16 20:09:56 +00:00
|
|
|
price: number | undefined;
|
|
|
|
priceWithDiscounts: number | undefined;
|
|
|
|
} {
|
2023-06-30 15:35:31 +00:00
|
|
|
let price = tariff.price || tariff.privilegies.reduce((sum, privilege) => sum + privilege.amount * privilege.price, 0);
|
2023-06-16 20:09:56 +00:00
|
|
|
|
2023-07-11 01:33:36 +00:00
|
|
|
let priceWithDiscounts = tariff.privilegies.reduce((sum, privilege) => {
|
2023-06-20 10:11:55 +00:00
|
|
|
let privilegePrice = privilege.amount * privilege.price;
|
2023-07-13 18:59:23 +00:00
|
|
|
|
|
|
|
let realprivilegie = privilegies[privilege.serviceKey].find(e => e._id === privilege.privilegeId);
|
|
|
|
if (realprivilegie) privilege.privilegeId = realprivilegie.privilegeId;
|
|
|
|
|
|
|
|
const privilegeDiscount = findPrivilegeDiscount(privilege.privilegeId, privilege.price * privilege.amount, discounts);
|
|
|
|
privilegePrice *= privilegeDiscount.factor;
|
|
|
|
|
2023-07-11 01:33:36 +00:00
|
|
|
const serviceCartData = useCartStore.getState().cart.services.find(e => e.serviceKey === privilege.serviceKey);
|
|
|
|
let serviceprice = 0;
|
|
|
|
if (serviceCartData) serviceprice = serviceCartData.price;
|
2023-06-16 20:09:56 +00:00
|
|
|
|
2023-07-11 01:33:36 +00:00
|
|
|
const serviceDiscount = findServiceDiscount(privilege.serviceKey, privilegePrice + serviceprice, discounts);
|
2023-07-13 18:59:23 +00:00
|
|
|
if (serviceDiscount) privilegePrice *= serviceDiscount.factor;
|
2023-06-16 20:09:56 +00:00
|
|
|
|
|
|
|
return sum + privilegePrice;
|
|
|
|
}, 0);
|
|
|
|
|
2023-07-11 01:33:36 +00:00
|
|
|
const cartDiscount = findCartDiscount(priceWithDiscounts + useCartStore.getState().cart.priceAfterDiscounts, discounts);
|
2023-07-13 18:59:23 +00:00
|
|
|
priceWithDiscounts *= cartDiscount.factor;
|
2023-07-11 01:33:36 +00:00
|
|
|
|
|
|
|
const acc = useUserStore.getState();
|
2023-07-13 18:59:23 +00:00
|
|
|
const loyalDiscount = findLoyaltyDiscount(acc!.userAccount!.wallet.purchasesAmount, discounts);
|
|
|
|
priceWithDiscounts *= loyalDiscount.factor;
|
2023-07-11 01:33:36 +00:00
|
|
|
|
2023-06-16 20:09:56 +00:00
|
|
|
return {
|
|
|
|
price,
|
|
|
|
priceWithDiscounts,
|
|
|
|
};
|
|
|
|
}
|