diff --git a/src/Components/LoggedIn/Content/Discounts/index.tsx b/src/Components/LoggedIn/Content/Discounts/index.tsx index c2bb56d..50b44ac 100644 --- a/src/Components/LoggedIn/Content/Discounts/index.tsx +++ b/src/Components/LoggedIn/Content/Discounts/index.tsx @@ -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; }, "" ); diff --git a/src/Components/LoggedIn/Content/Tariffs/DataGridElement/index.tsx b/src/Components/LoggedIn/Content/Tariffs/DataGridElement/index.tsx index 990349f..d0dd701 100644 --- a/src/Components/LoggedIn/Content/Tariffs/DataGridElement/index.tsx +++ b/src/Components/LoggedIn/Content/Tariffs/DataGridElement/index.tsx @@ -109,9 +109,12 @@ const rowz:Array = [ ]; const DataGridElement: React.FC = ({ openModal }) => { + let priceBefore = 0; let price = 0; let prices = 0; let percents = 0; + let discounts = ""; + let discountsSum = ""; const { discountsArray, discountsArraySet } = useStore((state) => state); const { discountsActiveArray, discountsActiveArraySet } = useStore((state) => state); @@ -185,7 +188,7 @@ const DataGridElement: React.FC = ({ 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 = ({ openModal }) => { return 0; } + const 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(""); } + + } + + 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 ( @@ -380,7 +438,7 @@ const DataGridElement: React.FC = ({ openModal }) => { return( <> { - `${item.good} - ${item.discount * 100}%${period}` + `${item.good} - ${ Math.round(item.discount * 100) }%${period}` } ) @@ -445,7 +503,6 @@ const DataGridElement: React.FC = ({ openModal }) => { @@ -459,10 +516,16 @@ const DataGridElement: React.FC = ({ openModal }) => { + = ({ openModal }) => { - { 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 = ({ 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 = ({ 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 = ({ 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 = ({ 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 = ({ 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 = ({ 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 = ({ 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 = ({ 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 = ({ 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( @@ -685,10 +787,15 @@ const DataGridElement: React.FC = ({ openModal }) => { + handleRemoveBasket( item.id ) }> = ({ openModal }) => { ) } ) } + + Скидки:   { discountsAfter(prices) } + + - ИТОГО:   { checkboxStates * prices } ₽ + ИТОГО:   { separator( prices ) } ₽