update loyalty discount calculations

This commit is contained in:
2026-01-17 21:22:24 +03:00
parent 3fe0c68526
commit e86966062a
2 changed files with 29 additions and 18 deletions

View File

@@ -6,6 +6,7 @@ import {
selectDiscountTotal,
selectGrandTotal,
selectHighestPricedLoyaltyItem,
totalTaxes,
} from "features/order/orderSlice";
import { OrderType } from "pages/checkout/hooks/types";
import { useTranslation } from "react-i18next";
@@ -25,12 +26,13 @@ export default function OrderSummary() {
const { orderType } = useAppSelector(selectCart);
const subtotal = useAppSelector(selectCartTotal);
const highestLoyaltyItem = useAppSelector(selectHighestPricedLoyaltyItem);
const totalTaxesAmount = useAppSelector(totalTaxes);
const grandTotal = useAppSelector(selectGrandTotal);
const discountAmount = useAppSelector(selectDiscountTotal);
const isHasLoyaltyGift =
(restaurant?.loyalty_stamps ?? 0) -
(restaurant?.customer_loyalty_points ?? 0) <=
(restaurant?.customer_loyalty_points ?? 0) <=
0;
const titlesStyle: CSSProperties = {
@@ -111,9 +113,9 @@ export default function OrderSummary() {
}}
>
{isHasLoyaltyGift &&
useLoyaltyPoints &&
highestLoyaltyItem &&
restaurant?.is_loyalty_enabled === 1 ? (
useLoyaltyPoints &&
highestLoyaltyItem &&
restaurant?.is_loyalty_enabled === 1 ? (
<Tag
color="green"
style={{
@@ -135,7 +137,7 @@ export default function OrderSummary() {
</Tag>
) : null}
<ArabicPrice
price={discountAmount}
price={discountAmount + (useLoyaltyPoints && restaurant?.is_loyalty_enabled === 1 ? ((highestLoyaltyItem?.price || 0) * (totalTaxesAmount / 100)) : 0)}
textStyle={{ ...titlesStyle, color: "#434E5C" }}
/>
</div>

View File

@@ -824,21 +824,31 @@ export const selectLoyaltyItems = createSelector([selectOrderItems], (items) =>
items.filter((item) => item.isHasLoyalty),
);
// Tax selectors
export const selectTaxes = (state: RootState) => state.order.restaurant.taxes;
export const totalTaxes = (state: RootState) => {
const taxes = selectTaxes(state);
const totalTaxes = taxes?.reduce((total, tax) => total + parseFloat(tax.percentage), 0) || 0;
const vat = (state.order.restaurant?.vat || 0);
return totalTaxes + vat;
};
export const selectHighestPricedLoyaltyItem = (state: RootState) => {
const loyaltyItems = selectLoyaltyItems(state);
if (loyaltyItems.length === 0) return null;
return loyaltyItems.reduce((highest, current) =>
const highestItem = loyaltyItems.reduce((highest, current) =>
current.price > highest.price ? current : highest,
);
)
return highestItem;
};
export const selectDiscountTotal = (state: RootState) =>
(state.order.discount.value / 100) * selectCartTotal(state) +
(state.order.useLoyaltyPoints &&
state.order.restaurant?.is_loyalty_enabled === 1
? selectHighestPricedLoyaltyItem(state)?.price || 0
: 0);
export const selectDiscountTotal = (state: RootState) => {
return (state.order.discount.value / 100) * selectCartTotal(state) +
(state.order.useLoyaltyPoints &&
state.order.restaurant?.is_loyalty_enabled === 1
? ((selectHighestPricedLoyaltyItem(state)?.price || 0))
: 0);
}
export const selectLoyaltyValidation = createSelector(
[(state: RootState) => state.order.useLoyaltyPoints, selectLoyaltyItems],
@@ -853,8 +863,7 @@ export const selectLoyaltyValidation = createSelector(
}),
);
// Tax selectors
export const selectTaxes = (state: RootState) => state.order.restaurant.taxes;
export const selectTaxAmount = (state: RootState) => {
const subtotal = selectCartTotal(state) - selectDiscountTotal(state);