применение скидок

This commit is contained in:
Nikolai 2022-10-22 12:05:13 +06:00
parent 0dade0de9a
commit 9059f2013e
2 changed files with 81 additions and 6 deletions

@ -107,8 +107,16 @@ const Discounts: React.FC = () => {
setService(event.target.value as string); setService(event.target.value as string);
}; };
let { discountsArray, discountsArraySet } = useStore<StoreState>((state) => state); let { discountsArray, discountsArraySet } = useStore<StoreState>((state) => state);
//discountsArray = [...rows]; //УДАЛИТЬ
const getDiscounts = localStorage.getItem("discounts");
const store = useStore( (state) => state );
if( getDiscounts && !store.discountsArray.length ) {
const rows:Array<DiscountProps> = JSON.parse(getDiscounts);
if( rows.length ) { store.discountsArraySet( rows ); };
}
const { discountsActiveArray, discountsActiveArraySet } = useStore<StoreState>((state) => state); const { discountsActiveArray, discountsActiveArraySet } = useStore<StoreState>((state) => state);
let discountsActiveArrayUpdated:Array<number>; let discountsActiveArrayUpdated:Array<number>;
@ -124,11 +132,19 @@ const Discounts: React.FC = () => {
if( JSON.stringify(discountsActiveArray) != JSON.stringify(discountsActiveArrayUpdated) ) { if( JSON.stringify(discountsActiveArray) != JSON.stringify(discountsActiveArrayUpdated) ) {
discountsActiveArraySet( discountsActiveArrayUpdated ); discountsActiveArraySet( discountsActiveArrayUpdated );
localStorage.setItem("activeDiscounts", JSON.stringify( discountsActiveArrayUpdated ));
} }
} }
findActiveDiscounts(); findActiveDiscounts();
const getActiveDiscounts = localStorage.getItem("activeDiscounts");
if( getActiveDiscounts && !store.discountsArray.length ) {
const rows:Array<number> = JSON.parse(getActiveDiscounts);
if( rows.length ) { store.discountsActiveArraySet( rows ); };
}
const discountsArrayConverted = discountsArray.map( (item) => { const discountsArrayConverted = discountsArray.map( (item) => {
const dateFrom = item.from ? new Date( Number(item.from) ) : ""; const dateFrom = item.from ? new Date( Number(item.from) ) : "";
const dateDueTo = item.from ? new Date( Number(item.dueTo) ) : ""; const dateDueTo = item.from ? new Date( Number(item.dueTo) ) : "";
@ -172,6 +188,7 @@ const Discounts: React.FC = () => {
const discountsArrayUpdated = [ ...discountsArray, newDiscount ]; const discountsArrayUpdated = [ ...discountsArray, newDiscount ];
discountsArraySet( discountsArrayUpdated ); discountsArraySet( discountsArrayUpdated );
localStorage.setItem("discounts", JSON.stringify( discountsArrayUpdated ));
} }
const fieldName = React.useRef<HTMLInputElement | null>(null); const fieldName = React.useRef<HTMLInputElement | null>(null);
@ -208,6 +225,7 @@ const Discounts: React.FC = () => {
} ); } );
discountsArraySet( discountsArray ); discountsArraySet( discountsArray );
localStorage.setItem("discounts", JSON.stringify( discountsArray ));
} }
return ( return (

@ -15,6 +15,7 @@ import DeleteIcon from "@mui/icons-material/Delete";
import FormControlLabel from "@mui/material/FormControlLabel"; import FormControlLabel from "@mui/material/FormControlLabel";
import Checkbox from "@mui/material/Checkbox"; import Checkbox from "@mui/material/Checkbox";
import { PrivilegesProps, PromocodeProps } from "../../Promocode/types"; import { PrivilegesProps, PromocodeProps } from "../../Promocode/types";
import { DiscountProps } from "../../Discounts/types";
import theme from "../../../../../theme"; import theme from "../../../../../theme";
@ -107,6 +108,24 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
let price = 0; let price = 0;
let prices = 0; let prices = 0;
const { discountsArray, discountsArraySet } = useStore<StoreState>((state) => state);
const { discountsActiveArray, discountsActiveArraySet } = useStore<StoreState>((state) => state);
const getDiscounts = localStorage.getItem("discounts");
const store = useStore( (state) => state );
if( getDiscounts && !store.discountsArray.length ) {
const rows:Array<DiscountProps> = JSON.parse(getDiscounts);
if( rows.length ) { store.discountsArraySet( rows ); };
}
const getActiveDiscounts = localStorage.getItem("activeDiscounts");
if( getActiveDiscounts && !store.discountsArray.length ) {
const rows:Array<number> = JSON.parse(getActiveDiscounts);
if( rows.length ) { store.discountsActiveArraySet( rows ); };
}
const [checkboxStates, setCheckboxStates] = React.useState(1); const [checkboxStates, setCheckboxStates] = React.useState(1);
const checkboxToggle = () => { const checkboxToggle = () => {
@ -403,25 +422,45 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
{ cartRowsData.map( (item) => { { cartRowsData.map( (item) => {
if( item.type == "package" ) { if( item.type == "package" ) {
// считаем цену // считаем цену в пакетах
price = 0; price = 0;
if( item.tariffs ) { if( item.tariffs ) {
item.tariffs.forEach( (tariff) => { item.tariffs.forEach( (tariff) => {
price += tariff.price; let tariffPrice = tariff.price;
// применяем скидки по промокоду // применяем скидки по промокоду
if( selectedPromocode >= 0 && checkboxStates == 1 ) { if( selectedPromocode >= 0 && checkboxStates == 1 ) {
promocodeArray[ selectedPromocode ].privileges.forEach( (privilege) => { promocodeArray[ selectedPromocode ].privileges.forEach( (privilege) => {
if( tariff.service == privilege.good ) { if( tariff.service == privilege.good ) {
price = price * privilege.discount; tariffPrice = tariffPrice * privilege.discount;
} }
} ) } )
} }
// применяем активные скидки
if( discountsActiveArray.length >= 0 && checkboxStates == 1 && selectedPromocode < 0 ) {
discountsActiveArray.forEach( (activeDiscount) => {
discountsArray.forEach( (discount, i) => {
if( i == activeDiscount ) {
discount.privileges.forEach( (privilege) => {
if( tariff.service == privilege.good ) {
if( privilege.discount != 0 ) {
tariffPrice = tariffPrice * privilege.discount;
}
}
});
}
});
});
}
price += tariffPrice;
} ); } );
} }
} else { } else {
// считаем цену // считаем цену в тарифах
price = item.price; price = item.price;
// применяем скидки по промокоду // применяем скидки по промокоду
@ -432,6 +471,24 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
} }
} ) } )
} }
// применяем активные скидки
if( discountsActiveArray.length >= 0 && checkboxStates == 1 && selectedPromocode < 0 ) {
discountsActiveArray.forEach( (activeDiscount) => {
discountsArray.forEach( (discount, i) => {
if( i == activeDiscount ) {
discount.privileges.forEach( (privilege) => {
if( item.service == privilege.good ) {
if( privilege.discount != 0 ) {
price = price * privilege.discount;
}
}
});
}
});
});
}
} }
prices += price; prices += price;