adminFront/src/pages/dashboard/Content/Tariffs/utils.ts

255 lines
11 KiB
TypeScript
Raw Normal View History

2023-02-18 14:07:11 +00:00
import { CartSummary, Discount, Promocode } from "../../../../model/cart";
import { ArrayProps, Tariff } from "../../../../model/tariff";
export function calcFitDiscounts(discountsArray: Discount[], discountsActiveArray: number[], cartSummary: { [key: string]: CartSummary; }, fieldAddedValue: string) {
const result = discountsActiveArray.filter(e => {
const discount = discountsArray[e];
const summary = cartSummary[discount.privileges[0].good];
return (discount.incomeMore * 100 < parseInt(fieldAddedValue) && discount.incomeMore > 0) ||
(discount.toTime < (summary ? summary.days : 0) && discount.toTime > 0 && discount.toCapacity === 0) ||
(discount.toCapacity > 0 && discount.toCapacity < (summary ? summary.points : 0) && discount.toTime === 0) ||
(discount.toCapacity > 0 && discount.toTime > 0 && discount.toCapacity < (summary ? summary.points : 0) && discount.toTime < (summary ? summary.days : 0)) ||
(!discount.toCapacity && !discount.toTime && !discount.incomeMore && !discount.basketMore) ||
discount.basketMore;
}).filter((e, i, a) => {
const discount: Discount = discountsArray[e];
if (discount.incomeMore) {
return discount.incomeMore === a.reduce((a, e) => Math.max(a, discountsArray[e].incomeMore || 0), 0);
}
if (discount.toTime && discount.toCapacity) {
return discount.toTime === a.reduce((a, e) => Math.max(a, (discountsArray[e].toTime && discountsArray[e].toCapacity) ? discountsArray[e].toTime : 0), 0) && discount.toCapacity === a.reduce((a, e) => Math.max(a, (discountsArray[e].toCapacity && discountsArray[e].toTime) ? discountsArray[e].toCapacity : 0), 0);
}
if (discount.toTime && !discount.toCapacity) {
return discount.toTime === a.reduce((a, e) => Math.max(a, discountsArray[e].toTime && !discountsArray[e].toCapacity ? discountsArray[e].toTime : 0), 0);
}
if (!discount.toTime && discount.toCapacity) {
return discount.toCapacity === a.reduce((a, e) => Math.max(a, discountsArray[e].toCapacity && !discountsArray[e].toTime ? discountsArray[e].toCapacity : 0), 0);
}
return true;
});
return result;
}
export function separator(amount: number) {
if (String(amount).length < 4) { return amount; }
let result: Array<string> = [];
const arrs = String(amount).split('.');
const arr = arrs[0].split('').reverse();
arr.forEach((item, i: number) => {
result.push(String(arr[i]));
if (((i + 1) / 3) - Math.round((i + 1) / 3) === 0) result.push(" ");
});
if (arrs.length > 1) { return result.reverse().join("") + "." + arrs[1]; }
else { return result.reverse().join(""); }
};
export function formatPromocodePriveleges(promocode: Promocode) {
return promocode.privileges.map(privelege => `${privelege.good} - ${Math.round(privelege.discount * 100)}%`).join(", ");
}
export function calcTotalAndRowData(
cartRowsData: ArrayProps[],
isNonCommercial: boolean,
discountsArray: Discount[],
discountsActiveArray: number[],
fitDiscounts: number[],
addedValueField: string,
cartSummary: { [key: string]: CartSummary; },
promocode?: Promocode,
) {
let totalPrice = 0;
const calculatedCartRowData = cartRowsData.map(cartRow => {
let price = cartRow.price;
const appliedDiscounts: number[] = [];
if (!isNonCommercial) {
let percents = 0;
if (cartRow.type === "package") {
// считаем цену в ПАКЕТАХ
price = 0;
cartRow.tariffs?.forEach((tariff) => {
let tariffPrice = tariff.price;
percents = 0;
// применяем скидки по промокоду
if (promocode) {
promocode.privileges.forEach(privilege => {
if (tariff.service === privilege.good) {
percents = percents + privilege.discount;
}
});
} else {// применяем активные скидки
percents = applyActiveDiscounts(
percents,
tariff,
discountsArray,
discountsActiveArray,
addedValueField,
);
}
// применяем активные скидки по времени объему
if (!promocode) {
discountsActiveArray.forEach(activeDiscount => {
discountsArray.forEach((discount, i) => {
if (i === activeDiscount) {
if (tariff.time) {
const dTime = 0.1;
percents = percents + dTime;
}
if (tariff.points) {
//const cTime = discountCapacity( tariff.points );
//percents = percents + cTime;
//if( discounts ) discounts += " × ";
//if( cTime != 0 ) discounts += `${ Math.round(cTime * 100) }%`;
}
}
});
});
}
// применяем активные скидки на продукт
if (!promocode) {
discountsActiveArray.forEach(activeDiscount => {
discountsArray.forEach((discount, i) => {
if (i === activeDiscount) {
if (tariff.time && tariff.points) {
// const dProduct = discountProduct( tariff.time, tariff.points );
//percents = percents + dProduct;
//if( discounts ) discounts += " × ";
//if( dProduct != 0 ) discounts += `${ Math.round(dProduct * 100) }%`;
}
}
});
});
}
tariffPrice = tariffPrice - (tariffPrice * percents);
price += tariffPrice;
});
} else {
// считаем цену в ТАРИФАХ
price = cartRow.price;
percents = 0;
// применяем скидки по промокоду
if (promocode) {
promocode.privileges.forEach(privilege => {
if (cartRow.service === privilege.good) {
appliedDiscounts.push(privilege.discount);
price *= (1 - privilege.discount);
}
});
} else {
// применяем активные скидки
fitDiscounts.forEach(activeDiscount => {
const discount = discountsArray[activeDiscount];
discount.privileges.forEach((p) => {
const svcName = cartRow.service;
if (p.good === svcName) {
const summary = cartSummary[svcName] || { mbs: 0, points: 0, days: 0 };
if (
(discount.toCapacity === 0 && discount.toTime === 0 && discount.basketMore === 0 && !(discount.incomeMore)) ||
(discount.toCapacity > 0 && summary.points > discount.toCapacity && cartRow.points > 0 && discount.toTime === 0) ||
(discount.toTime > 0 && summary.days > discount.toTime * 100 && cartRow.time > 0 && discount.toCapacity === 0) ||
(discount.toTime > 0 && discount.toCapacity > 0 && summary.days > discount.toTime * 100 && summary.points > discount.toCapacity)
) {
price *= (1 - p.discount);
appliedDiscounts.push(p.discount);
}
}
});
});
}
percents = Number(percents.toFixed(2));
price = price - (price * percents);
}
}
totalPrice += price;
return {
...cartRow,
price,
appliedDiscounts,
};
});
return {
totalPrice,
calculatedCartRowData
};
}
function applyActiveDiscounts(
percents: number,
tariff: Tariff,
discountsArray: Discount[],
discountsActiveArray: number[],
addedValueField: string,
) {
discountsActiveArray.forEach(activeDiscountIndex => {
discountsArray[activeDiscountIndex].privileges.forEach((privilege) => {
if (privilege.discount !== 0) {
if (addedValueField) { // внесено
const addedValue = Number(addedValueField);
let minDiscount = 100;
let minI = -1;
discountsArray.forEach((discount, index) => {
discount.privileges.forEach((y) => {
if (
discount.active &&
addedValue - y.discount * 100 < minDiscount &&
addedValue - y.discount * 100 > 0
) {
minDiscount = addedValue - y.discount * 100;
minI = index;
}
});
});
if (minI >= 0) {
discountsArray[minI].privileges.forEach((y) => {
percents = percents + y.discount / discountsActiveArray.length; // костыль
});
}
} else { // не внесено
if (tariff.service === privilege.good) {
percents = percents + privilege.discount;
}
}
}
});
});
return percents;
}
export function convertTariffs(tariffsArray: ArrayProps[]) {
return tariffsArray.map((item) => {
if (item.type === "package" && item.tariffs) {
const result = item.tariffs.reduce((acc, tariff) => {
acc.service = acc.service ? `${acc.service}, ${tariff.service}` : tariff.service;
acc.disk = acc.disk + tariff.disk;
acc.time = acc.time + tariff.time;
acc.points = acc.points + tariff.points;
acc.price = acc.price + tariff.price;
return acc;
}, { service: "", disk: "", time: "", points: "", price: 0 });
return { id: item.id, name: item.name, type: item.type, ...result };
} else {
return item;
}
});
}