import { ServiceKeyToPrivilegesMap } from "@root/model/privilege"; import { CartData, Discount, Tariff, findCartDiscount, findLoyaltyDiscount, findPrivilegeDiscount, findServiceDiscount } from "@frontend/kitui"; export function calcIndividualTariffPrices( tariff: Tariff, discounts: Discount[], privilegies: ServiceKeyToPrivilegesMap, purchasesAmount: number, cart: CartData, ): { price: number | undefined; tariffPriceAfterDiscounts: number | undefined; } { let price = tariff.price || tariff.privilegies.reduce((sum, privilege) => sum + privilege.amount * privilege.price, 0); let tariffPriceAfterDiscounts = tariff.privilegies.reduce((sum, privilege) => { let privilegePrice = privilege.amount * privilege.price; 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; const serviceCartData = cart.services.find(e => e.serviceKey === privilege.serviceKey); let serviceprice = 0; if (serviceCartData) serviceprice = serviceCartData.price; const serviceDiscount = findServiceDiscount(privilege.serviceKey, privilegePrice + serviceprice, discounts); if (serviceDiscount) privilegePrice *= serviceDiscount.factor; return sum + privilegePrice; }, 0); const cartDiscount = findCartDiscount(tariffPriceAfterDiscounts + cart.priceAfterDiscounts, discounts); tariffPriceAfterDiscounts *= cartDiscount.factor; const loyalDiscount = findLoyaltyDiscount(purchasesAmount, discounts); tariffPriceAfterDiscounts *= loyalDiscount.factor; return { price, tariffPriceAfterDiscounts: tariffPriceAfterDiscounts, }; }