From e86966062adc4501adab2a43d0dd726ddc770fd7 Mon Sep 17 00:00:00 2001 From: Mohammed Al-yaseen Date: Sat, 17 Jan 2026 21:22:24 +0300 Subject: [PATCH] update loyalty discount calculations --- src/components/OrderSummary/OrderSummary.tsx | 16 +++++----- src/features/order/orderSlice.ts | 31 +++++++++++++------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/components/OrderSummary/OrderSummary.tsx b/src/components/OrderSummary/OrderSummary.tsx index e721340..55302f3 100644 --- a/src/components/OrderSummary/OrderSummary.tsx +++ b/src/components/OrderSummary/OrderSummary.tsx @@ -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 = { @@ -47,9 +49,9 @@ export default function OrderSummary() { // Calculate individual taxes const taxesList = useMemo(() => { if (!restaurant?.taxes) return []; - + const subtotalAfterDiscount = subtotal - discountAmount; - + return restaurant.taxes .filter((tax) => tax.is_active === 1) .map((tax) => { @@ -111,9 +113,9 @@ export default function OrderSummary() { }} > {isHasLoyaltyGift && - useLoyaltyPoints && - highestLoyaltyItem && - restaurant?.is_loyalty_enabled === 1 ? ( + useLoyaltyPoints && + highestLoyaltyItem && + restaurant?.is_loyalty_enabled === 1 ? ( ) : null} diff --git a/src/features/order/orderSlice.ts b/src/features/order/orderSlice.ts index 574d82d..f72bfba 100644 --- a/src/features/order/orderSlice.ts +++ b/src/features/order/orderSlice.ts @@ -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);