вывод скидок

This commit is contained in:
Nikolai 2022-11-14 17:40:01 +06:00
parent 8efe2e54b3
commit c7e12e1668
2 changed files with 174 additions and 58 deletions

@ -151,9 +151,9 @@ const Discounts: React.FC = () => {
findActiveDiscounts();
const discountsArrayConverted = discountsArray.map( (item) => {
const basketMorePerc = Number(item.basketMore) * 100 + "%";
const toTimePerc = Number(item.toTime) * 100 + "%";
const toCapacityPerc = Number(item.toCapacity) * 100 + "%";
const basketMorePerc = Math.round(Number(item.basketMore) * 100) + "%";
const toTimePerc = Math.round(Number(item.toTime) * 100) + "%";
const toCapacityPerc = Math.round(Number(item.toCapacity) * 100) + "%";
const dateFrom = item.from ? new Date( Number(item.from) ) : "";
const dateDueTo = item.from ? new Date( Number(item.dueTo) ) : "";
@ -170,7 +170,7 @@ const Discounts: React.FC = () => {
const result = item.privileges.reduce( (acc, privilege) => {
acc = acc
? `${acc}, ${privilege.good} - ${privilege.discount}%`
: `${privilege.good} - ${privilege.discount * 100}%`;
: `${privilege.good} - ${ Math.round(privilege.discount * 100) }%`;
return acc;
}, "" );

@ -109,9 +109,12 @@ const rowz:Array<PromocodeProps> = [
];
const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
let priceBefore = 0;
let price = 0;
let prices = 0;
let percents = 0;
let discounts = "";
let discountsSum = "";
const { discountsArray, discountsArraySet } = useStore<StoreState>((state) => state);
const { discountsActiveArray, discountsActiveArraySet } = useStore<StoreState>((state) => state);
@ -185,7 +188,7 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
let codeNumber = -1;
promocodeArray.forEach( (item, i) => {
if( item.name == name ) codeNumber = i;
if( name != "" && item.name == name ) codeNumber = i;
} );
setSelectedPromocode( codeNumber );
@ -223,6 +226,61 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
return 0;
}
const 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(""); }
}
const discountsAfter = ( getPrice:number ) => {
discountsSum = "";
priceBefore = getPrice;
// применяем активные скидки за объем корзины
let more = 0;
let moreI = -1;
if( discountsActiveArray.length >= 0 && checkboxStates == 1 && selectedPromocode < 0 ) {
discountsActiveArray.forEach( (activeDiscount) => {
discountsArray.forEach( (discount, i) => {
if( i == activeDiscount && discount.basketMore ) {
if( more <= discount.basketMore ) {
more = discount.basketMore;
moreI = i;
}
}
});
});
}
if( moreI >= 0 ) {
getPrice = getPrice - getPrice * discountsArray[ moreI ].basketMore;
if( discountsSum ) { discountsSum += " × "; }
discountsSum += `${ Math.round(discountsArray[ moreI ].basketMore * 100) }%`;
}
if( checkboxStates != 1 ) {
getPrice = getPrice - getPrice * checkboxStates;
discountsSum += `${ Math.round(checkboxStates * 100) }%`;
}
prices = getPrice;
if(discountsSum) discountsSum += " = " + Math.round(prices / priceBefore * 100) +"%";
return discountsSum;
}
return (
<Box style={{ width: "100%" }}>
@ -380,7 +438,7 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
return(
<>
{
`${item.good} - ${item.discount * 100}%${period}`
`${item.good} - ${ Math.round(item.discount * 100) }%${period}`
}
</>
)
@ -445,7 +503,6 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
</Box>
<List sx={{
width: "50%",
border: "1px solid",
borderColor: theme.palette.secondary.main
}}>
@ -459,10 +516,16 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
</Avatar>
</ListItemAvatar>
<ListItemText
primary="Название тарифа"
primary="Название"
sx={{ textAlign: "center", minWidth: "250px", maxWidth: "250px" }}
/>
<ListItemText
primary="Цена ( ₽ )"
primary="Цена"
sx={{ textAlign: "center", minWidth: "200px", maxWidth: "200px" }}
/>
<ListItemText
primary="Скидки"
sx={{ textAlign: "center", minWidth: "200px", maxWidth: "400px" }}
/>
<IconButton edge="end" aria-label="delete">
<DeleteIcon sx={{
@ -472,10 +535,12 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
</IconButton>
</ListItem>
{ cartRowsData.map( (item) => {
{ cartRowsData.map( (item) => {
if( item.type == "package" ) {
// считаем цену в пакетах
// считаем цену в ПАКЕТАХ
price = 0;
discounts = "";
priceBefore = 0;
if( item.tariffs ) {
item.tariffs.forEach( (tariff) => {
@ -487,6 +552,9 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
promocodeArray[ selectedPromocode ].privileges.forEach( (privilege) => {
if( tariff.service == privilege.good ) {
percents = percents + privilege.discount;
if( discounts ) { discounts += " × "; }
discounts += `${ Math.round(privilege.discount * 100) }%`;
}
} )
}
@ -497,7 +565,6 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
discountsArray.forEach( (discount, i) => {
if( i == activeDiscount ) {
discount.privileges.forEach( (privilege) => {
//if( tariff.service == privilege.good ) {
if( privilege.discount != 0 ) {
if( fieldAddedValue ) { // внесено
@ -507,30 +574,33 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
discountsArray.forEach( (x, ii) => {
x.privileges.forEach( (y) => {
//if( item.service == y.good ) {
if( x.active && f - y.discount * 100 < minDiscount
&& f - y.discount * 100 > 0 ) {
minDiscount = f - y.discount * 100;
minI = ii;
}
//}
});
});
if( minI >= 0 ) {
discountsArray[ minI ].privileges.forEach( (y) => {
percents = percents + y.discount / discountsActiveArray.length; // костыль
percents = percents + y.discount / discountsActiveArray.length; // костыль
if( discounts ) { discounts += " × "; }
discounts += `${y.discount / discountsActiveArray.length * 100}%`;
});
}
} else { // не внесено
if( tariff.service == privilege.good ) {
percents = percents + privilege.discount;
if( discounts ) { discounts += " × "; }
discounts += `${ Math.round(privilege.discount * 100) }%`;
}
}
}
//}
});
}
});
@ -541,8 +611,23 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
if( discountsActiveArray.length >= 0 && checkboxStates == 1 && selectedPromocode < 0 ) {
discountsActiveArray.forEach( (activeDiscount) => {
discountsArray.forEach( (discount, i) => {
if( tariff.time ) { percents = percents + discountTime( tariff.time ); }
if( tariff.points ) { percents = percents + discountCapacity( tariff.time ); }
if( i == activeDiscount ) {
if( tariff.time ) {
const dTime = discountTime( tariff.time );
percents = percents + dTime;
if( discounts ) discounts += " × ";
if( dTime != 0 ) discounts += `${ Math.round(dTime * 100) }%`;
}
if( tariff.points ) {
const cTime = discountCapacity( tariff.points );
percents = percents + cTime;
if( discounts ) discounts += " × ";
if( cTime != 0 ) discounts += `${ Math.round(cTime * 100) }%`;
}
}
});
});
}
@ -551,8 +636,14 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
if( discountsActiveArray.length >= 0 && checkboxStates == 1 && selectedPromocode < 0 ) {
discountsActiveArray.forEach( (activeDiscount) => {
discountsArray.forEach( (discount, i) => {
if( tariff.time && tariff.points ) {
percents = percents + discountProduct( tariff.time, tariff.points );
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) }%`;
}
}
});
});
@ -560,20 +651,26 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
percents = Number( percents.toFixed(2) );
priceBefore += tariffPrice;
tariffPrice = tariffPrice - (tariffPrice * percents);
price += tariffPrice;
} );
}
} else {
// считаем цену в тарифах
// считаем цену в ТАРИФАХ
price = item.price;
percents = 0;
discounts = "";
// применяем скидки по промокоду
if( selectedPromocode >= 0 && checkboxStates == 1 ) {
promocodeArray[ selectedPromocode ].privileges.forEach( (privilege) => {
if( item.service == privilege.good ) {
price = price - (price * privilege.discount);
percents = percents + privilege.discount
if( discounts ) { discounts += " × "; }
discounts += `${ Math.round(privilege.discount * 100) }%`;
}
} )
}
@ -585,7 +682,6 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
if( i == activeDiscount ) {
discount.privileges.forEach( (privilege) => {
//if( item.service == privilege.good ) {
if( privilege.discount != 0 ) {
if( fieldAddedValue ) { // внесено
@ -595,42 +691,60 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
discountsArray.forEach( (x, ii) => {
x.privileges.forEach( (y) => {
//if( item.service == y.good ) {
if( x.active && f - y.discount * 100 < minDiscount
&& f - y.discount * 100 > 0 ) {
minDiscount = f - y.discount * 100;
minDiscount = f - Math.round(y.discount * 100);
minI = ii;
}
//}
});
});
if( minI >= 0 ) {
discountsArray[ minI ].privileges.forEach( (y) => {
percents = percents + y.discount / discountsActiveArray.length; // костыль
if( discounts ) { discounts += " × "; }
discounts += `${ Math.round(y.discount / discountsActiveArray.length * 100) }%`;
});
}
} else { // не внесено
if( item.service == privilege.good ) {
percents = percents + privilege.discount;
if( item.service == privilege.good ) {
percents = percents + privilege.discount;
if( discounts ) { discounts += " × "; }
discounts += `${ Math.round(privilege.discount * 100) }%`;
}
}
}
//}
});
}
});
});
}
}
// применяем активные скидки по времени / объему
if( discountsActiveArray.length >= 0 && checkboxStates == 1 && selectedPromocode < 0 ) {
discountsActiveArray.forEach( (activeDiscount) => {
discountsArray.forEach( (discount, i) => {
if( item.time ) { percents = percents + discountTime( item.time ); }
if( item.points ) { percents = percents + discountCapacity( item.time ); }
if( i == activeDiscount ) {
if( item.time ) {
const dTime = discountTime( item.time );
percents = percents + dTime;
if( discounts ) discounts += " × ";
if( dTime != 0 ) discounts += `${ Math.round(dTime * 100) }%`;
}
if( item.points ) {
const cTime = discountCapacity( item.points );
percents = percents + cTime;
if( discounts ) discounts += " × ";
if( cTime != 0 ) discounts += `${ Math.round(cTime * 100) }%`;
}
}
});
});
}
@ -639,40 +753,28 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
if( discountsActiveArray.length >= 0 && checkboxStates == 1 && selectedPromocode < 0 ) {
discountsActiveArray.forEach( (activeDiscount) => {
discountsArray.forEach( (discount, i) => {
if( item.time && item.points ) {
percents = percents + discountProduct( item.time, item.points );
if( i == activeDiscount ) {
if( item.time && item.points ) {
const dProduct = discountProduct( item.time, item.points );
percents = percents + dProduct;
if( discounts ) discounts += " × ";
if( dProduct != 0 ) discounts += `${ Math.round(dProduct * 100) }%`;
}
}
});
});
}
percents = Number( percents.toFixed(2) );
priceBefore = price;
price = price - (price * percents);
}
prices += price;
// применяем активные скидки за объем корзины
let more = 0;
let moreI = -1;
if( discountsActiveArray.length >= 0 && checkboxStates == 1 && selectedPromocode < 0 ) {
discountsActiveArray.forEach( (activeDiscount) => {
discountsArray.forEach( (discount, i) => {
if( i == activeDiscount && discount.basketMore ) {
if( more <= discount.basketMore ) {
more = discount.basketMore;
moreI = i;
}
}
});
});
}
if( moreI >= 0 ) {
price = price - ( price * discountsArray[ moreI ].basketMore );
}
if(discounts) discounts += " = " + Math.round(price / priceBefore * 100) +"%";
return(
<ListItem key={ item.id }>
@ -685,10 +787,15 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
</ListItemAvatar>
<ListItemText
primary={ item.name }
sx={{ minWidth: "250px", maxWidth: "250px" }}
/>
<ListItemText
primary={ price }
sx={{ textAlign: "center" }}
primary={ `${separator(price)}` }
sx={{ textAlign: "center", minWidth: "200px", maxWidth: "200px" }}
/>
<ListItemText
primary={ discounts }
sx={{ textAlign: "center", minWidth: "400px", maxWidth: "400px" }}
/>
<IconButton edge="end" aria-label="delete" onClick={ () => handleRemoveBasket( item.id ) }>
<DeleteIcon sx={{
@ -699,12 +806,21 @@ const DataGridElement: React.FC<MWProps> = ({ openModal }) => {
)
} ) }
<Typography id="transition-modal-title" variant="h6" sx={{
fontWeight: "normal",
textAlign: "center",
marginTop: "15px",
fontSize: "16px"
}}>
Скидки: &ensp; { discountsAfter(prices) }
</Typography>
<Typography id="transition-modal-title" variant="h6" sx={{
fontWeight: "normal",
textAlign: "center",
marginTop: "10px"
}}>
ИТОГО: &ensp; { checkboxStates * prices }
ИТОГО: &ensp; { separator( prices ) }
</Typography>
</List>