diff --git a/src/pages/dashboard/Content/Tariffs/CreateTariffModal.tsx b/src/pages/dashboard/Content/Tariffs/CreateTariffModal.tsx deleted file mode 100644 index d7d77a6..0000000 --- a/src/pages/dashboard/Content/Tariffs/CreateTariffModal.tsx +++ /dev/null @@ -1,187 +0,0 @@ -import * as React from "react"; -import { Box, Modal, Fade, Backdrop, Button, TextField } from "@mui/material"; -import theme from "../../../../theme"; -import { ArrayProps } from "../../../../model/tariff"; -import { useTariffStore } from "../../../../stores/tariffs"; - - -export interface MWProps { - open: boolean; - type: number; - variant: number; - close: () => void; -} - -const ModalMini = ({ open, type, variant, close }: MWProps) => { - let tariffsArray = useTariffStore(state => state.tariffs); - const tariffsArraySet = useTariffStore(state => state.setTariffs); - - const types = ["", "Шаблонизатор документов", "Опросник", "Сокращатель ссылок"]; - const variants = ["Количество", "Срок (дней)", "Количество (гб)"]; - - const fieldName = React.useRef(null); - const fieldTime = React.useRef(null); - const fieldPrice = React.useRef(null); - - const checkTariff = () => { - if (fieldName.current != null && fieldTime.current != null && fieldPrice.current != null) { - if (fieldName.current.value && fieldTime.current.value && fieldPrice.current.value) { - - const data = [0, 0, 0]; - - if (variant === 0) { data[0] = parseInt(fieldTime.current.value); } - if (variant === 1) { data[1] = parseInt(fieldTime.current.value); } - if (variant === 2) { data[2] = parseInt(fieldTime.current.value); } - - const tariffsArrayNew = [...tariffsArray, { - "id": new Date().getTime(), - "name": fieldName.current.value, - "type": "tariff", - "service": types[type], - "disk": data[2], - "time": data[1], - "points": data[0], - "price": +fieldPrice.current.value - } as ArrayProps]; - - tariffsArraySet(tariffsArrayNew); - - close(); - } - } - }; - - return ( - - close()} - closeAfterTransition - BackdropComponent={Backdrop} - BackdropProps={{ - timeout: 500, - }} - > - - - - - - - - - - - - - - - - - ); -}; - - -export default ModalMini; diff --git a/src/pages/dashboard/Content/Tariffs/TariffGrid.tsx b/src/pages/dashboard/Content/Tariffs/TariffGrid.tsx deleted file mode 100644 index 5fd6559..0000000 --- a/src/pages/dashboard/Content/Tariffs/TariffGrid.tsx +++ /dev/null @@ -1,242 +0,0 @@ -import * as React from "react"; -import { Box, Typography, TextField } from "@mui/material"; -import { DataGrid, GridColDef, GridSelectionModel, GridToolbar } from "@mui/x-data-grid"; -import theme from "../../../../theme"; -import { useMemo, useState } from "react"; -import { convertTariffs, formatPromocodePriveleges } from "./utils"; -import CustomButton from "./CustomButton"; -import Cart from "./Cart"; -import type { ArrayProps } from "../../../../model/tariff"; -import { Promocode } from "../../../../model/cart"; -import { usePromocodeStore } from "../../../../stores/promocodes"; -import { useTariffStore } from "../../../../stores/tariffs"; -import { useCartStore } from "../../../../stores/cart"; - - -export interface MWProps { - openModal: () => void; -} - -const columns: GridColDef[] = [ - { - field: "id", - headerName: "ID", - width: 30, - sortable: false, - }, - { - field: "name", - headerName: "Название тарифа", - width: 200, - sortable: false, - }, - { - field: "service", - headerName: "Сервис", - width: 210, - sortable: false, - }, { - field: "disk", - headerName: "Гигабайты", - type: "number", - width: 110, - sortable: false, - }, - { - field: "time", - headerName: "Время", - type: "number", - width: 110, - sortable: false, - }, - { - field: "points", - headerName: "Объем", - width: 110, - sortable: false, - }, - { - field: "price", - headerName: "Стоимость", - width: 160, - sortable: false, - }, - { - field: "conditions", - headerName: "Условия", - width: 110, - sortable: false, - }, -]; - -const DataGridElement: React.FC = ({ openModal }) => { - const promocodeArray = usePromocodeStore(state => state.promocodeArray); - const tariffsSelectedRowsData = useTariffStore(state => state.tariffsSelectedRowsData); - const tariffsSelectedRowsDataSet = useTariffStore(state => state.setTariffsSelectedRowsData); - const tariffsArray = useTariffStore(state => state.tariffs); - const cartRowsDataSet = useCartStore(state => state.setCartRowsData); - const [selectedPromocodeIndex, setSelectedPromocodeIndex] = useState(-1); - const [promocodeField, setPromocodeField] = useState(""); - - const onRowsSelectionHandler = (ids: GridSelectionModel) => { - const result: Array = []; - ids.forEach((id) => tariffsArray.forEach((row) => { - if (row.id === id) result.push(row); - })); - - tariffsSelectedRowsDataSet(result); - }; - - const handleToBasket = () => cartRowsDataSet(tariffsSelectedRowsData); - - const checkPromocode = () => { - setSelectedPromocodeIndex(promocodeArray.findIndex(promocode => promocode.name === promocodeField)); - }; - - const tariffsArrayConverted = useMemo(() => convertTariffs(tariffsArray), [tariffsArray]); - - const promocode = promocodeArray[selectedPromocodeIndex] as Promocode | undefined; - - return ( - - - onRowsSelectionHandler(ids)} - /> - - - openModal()} - sx={{ - padding: "6px 30px", - marginTop: "45px", - marginBottom: "15px", - maxWidth: '320px', - width: '100%', - }}> - Пакетизировать - - - handleToBasket()} - sx={{ - padding: "6px 69px", - marginBottom: "95px", - }}> - Сложить в корзину - - - - - - setPromocodeField(e.target.value)} - /> - - checkPromocode()} - sx={{ - width: "200px", - height: "48px", - }}> - Готово - - - - {selectedPromocodeIndex >= 0 && - - - - Введен промокод: - - - {promocodeArray[selectedPromocodeIndex].name} - - - - - - Привилегии:   - - - - - {formatPromocodePriveleges(promocodeArray[selectedPromocodeIndex])} - - - - - } - - - - - - - ); -}; - -export default DataGridElement; diff --git a/src/pages/dashboard/Content/Tariffs/utils.ts b/src/pages/dashboard/Content/Tariffs/utils.ts deleted file mode 100644 index 57339f8..0000000 --- a/src/pages/dashboard/Content/Tariffs/utils.ts +++ /dev/null @@ -1,260 +0,0 @@ -import { CartSummary, Discount, Promocode } from "../../../../model/cart"; -import { ArrayProps, Tariff } from "../../../../model/tariff"; - - -/** @deprecated */ -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; -} - -/** @deprecated */ -export function separator(amount: number) { - if (String(amount).length < 4) { return amount; } - - let result: Array = []; - 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(", "); -} - -/** @deprecated */ -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 - }; -} - -/** @deprecated */ -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; -} - -/** @deprecated */ -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; - } - }); -} \ No newline at end of file