From f97b83062c9dccb10a725a8c4cabc9a85365f9ef Mon Sep 17 00:00:00 2001 From: Mohammed Al-yaseen Date: Sat, 17 Jan 2026 11:44:04 +0300 Subject: [PATCH] apply restaurant decimails formating & fix tax calcualtions --- src/components/ArabicPrice/ArabicPrice.tsx | 2 +- .../PaymentMethods/PaymentMethods.tsx | 3 ++- src/features/order/orderSlice.ts | 24 ++++++++++--------- src/pages/login/page.tsx | 9 +++++-- src/pages/order/page.tsx | 7 +++--- src/pages/product/components/Extra.tsx | 5 ++-- src/pages/product/components/Variants.tsx | 4 ++-- src/utils/types/appTypes.ts | 1 + 8 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/components/ArabicPrice/ArabicPrice.tsx b/src/components/ArabicPrice/ArabicPrice.tsx index d8fedae..45b1a7c 100644 --- a/src/components/ArabicPrice/ArabicPrice.tsx +++ b/src/components/ArabicPrice/ArabicPrice.tsx @@ -27,7 +27,7 @@ const ArabicPrice: React.FC = ({ // Format the price to ensure it has 2 decimal places const formattedPrice = - typeof price === "number" ? formatPriceUi(price, 3) : price; + typeof price === "number" ? formatPriceUi(price, restaurant?.currency_decimals ?? 3) : price; const { textDecoration, ...restStyle } = style; const decorationStyle = textDecoration ? ({ textDecoration } as React.CSSProperties) diff --git a/src/components/PaymentMethods/PaymentMethods.tsx b/src/components/PaymentMethods/PaymentMethods.tsx index f9031e0..b3c0315 100644 --- a/src/components/PaymentMethods/PaymentMethods.tsx +++ b/src/components/PaymentMethods/PaymentMethods.tsx @@ -21,6 +21,7 @@ const PaymentMethods = () => { const { paymentMethod, orderType } = useAppSelector(selectCart); const dispatch = useAppDispatch(); const grandTotal = useAppSelector(selectGrandTotal); + const { restaurant } = useAppSelector((state) => state.order); // const { isRTL } = useAppSelector((state) => state.locale); const options: { @@ -50,7 +51,7 @@ const PaymentMethods = () => { ), value: "cash", - price: formatPriceUi(grandTotal, 3), + price: formatPriceUi(grandTotal, restaurant.currency_decimals ?? 3), style: { color: colors.primary, }, diff --git a/src/features/order/orderSlice.ts b/src/features/order/orderSlice.ts index 4d09f60..a02164d 100644 --- a/src/features/order/orderSlice.ts +++ b/src/features/order/orderSlice.ts @@ -771,23 +771,21 @@ export const { // Tax calculation helper functions const calculateTaxAmount = ( - state: RootState, amount: number, tax: Tax, ): number => { const percentage = parseFloat(tax.percentage); - return (((state.order.restaurant?.vat || 0) + percentage) * amount) / 100; + return (percentage * amount) / 100; }; const calculateTotalTax = ( - state: RootState, subtotal: number, taxes: Tax[], ): number => { return taxes .filter((tax) => tax.is_active === 1) .reduce( - (total, tax) => total + calculateTaxAmount(state, subtotal, tax), + (total, tax) => total + calculateTaxAmount(subtotal, tax), 0, ); }; @@ -838,7 +836,7 @@ export const selectHighestPricedLoyaltyItem = (state: RootState) => { export const selectDiscountTotal = (state: RootState) => (state.order.discount.value / 100) * selectCartTotal(state) + (state.order.useLoyaltyPoints && - state.order.restaurant?.is_loyalty_enabled === 1 + state.order.restaurant?.is_loyalty_enabled === 1 ? selectHighestPricedLoyaltyItem(state)?.price || 0 : 0); @@ -861,25 +859,29 @@ export const selectTaxes = (state: RootState) => state.order.restaurant.taxes; export const selectTaxAmount = (state: RootState) => { const subtotal = selectCartTotal(state) - selectDiscountTotal(state); const taxes = selectTaxes(state); - return calculateTotalTax(state, subtotal, taxes || []); + return calculateTotalTax(subtotal, taxes || []); }; export const selectGrandTotal = (state: RootState) => { const totalDiscount = selectDiscountTotal(state); const taxAmount = selectTaxAmount(state); + const vatAmount = ((state.order.restaurant?.vat || 0) / 100) * (selectCartTotal(state) - selectDiscountTotal(state)); const subtotal = selectCartTotal(state); const deliveryFee = state.order.orderType === OrderType.Delivery ? Number(state.order.restaurant?.delivery_fees) || 0 : 0; + console.log(subtotal, totalDiscount, taxAmount, deliveryFee); + return ( - subtotal + - taxAmount - + subtotal - totalDiscount + - deliveryFee - - state.order.splitBillAmount + - Number(state.order.tip) + taxAmount + + vatAmount - + deliveryFee + // state.order.splitBillAmount + + // Number(state.order.tip) ); }; diff --git a/src/pages/login/page.tsx b/src/pages/login/page.tsx index eff1f33..5c1f2ab 100644 --- a/src/pages/login/page.tsx +++ b/src/pages/login/page.tsx @@ -13,13 +13,14 @@ import "react-phone-input-2/lib/style.css"; import { useNavigate, useParams } from "react-router-dom"; import { useSendOtpMutation } from "redux/api/auth"; import { useGetRestaurantDetailsQuery } from "redux/api/others"; -import { useAppSelector } from "redux/hooks"; +import { useAppDispatch, useAppSelector } from "redux/hooks"; import { colors, DisabledColor, ProGray1 } from "ThemeConstants"; import { default_image } from "utils/constants"; import styles from "./login.module.css"; import { Layout } from "antd"; import ProHeader from "components/ProHeader/ProHeader"; import useBreakPoint from "hooks/useBreakPoint"; +import { updateCustomerName } from "features/order/orderSlice"; export default function LoginPage() { const { t } = useTranslation(); @@ -32,7 +33,7 @@ export default function LoginPage() { skip: !subdomain, }); const { isTablet } = useBreakPoint(); - + const dispatch = useAppDispatch(); // const [phone, setPhone] = useState(""); const [selectedDate, setSelectedDate] = useState(""); const [isOpen, setIsOpen] = useState(false); @@ -113,6 +114,10 @@ export default function LoginPage() { height: 50, fontSize: 14, }} + onBlur={(e) => { + dispatch(updateCustomerName(e.target.value)); + }} + disabled={isLoading} /> diff --git a/src/pages/order/page.tsx b/src/pages/order/page.tsx index 4d9af4d..2865c83 100644 --- a/src/pages/order/page.tsx +++ b/src/pages/order/page.tsx @@ -37,13 +37,14 @@ import NewRateIcon from "components/Icons/order/NewRateIcon"; import NoteIcon from "components/Icons/NoteIcon"; import SuccessIcon from "components/Icons/SuccessIcon"; import { SplitBillParticipantsBottomSheet } from "./components/SplitBillParticipantsBottomSheet"; +import { OrderType } from "pages/checkout/hooks/types"; export default function OrderPage() { const { t } = useTranslation(); const { orderId } = useParams(); const navigate = useNavigate(); const { isRTL } = useAppSelector((state) => state.locale); - const { restaurant } = useAppSelector((state) => state.order); + const { restaurant, orderType } = useAppSelector((state) => state.order); const hasRefetchedRef = useRef(false); const [isOpen, setIsOpen] = useState(false); const [isRateOrderOpen, setIsRateOrderOpen] = useState(false); @@ -429,7 +430,7 @@ export default function OrderPage() { - {!hasClosedStatus && ( + {!hasClosedStatus && orderType === OrderType.DineIn && (
)} - {hasClosedStatus && ( + {hasClosedStatus && orderType === OrderType.DineIn && (
>; }) { const { t } = useTranslation(); - + const { restaurant } = useAppSelector((state) => state.order); return ( <> {extrasList.length > 0 && ( @@ -46,7 +47,7 @@ export default function Extra({ return { value: value.id.toString(), label: value.name, - price: `+${formatPriceUi(value.price, 3)}`, + price: `+${formatPriceUi(value.price, restaurant.currency_decimals ?? 3)}`, }; })} value={selectedExtras.map((ex) => ex.id.toString())} diff --git a/src/pages/product/components/Variants.tsx b/src/pages/product/components/Variants.tsx index fa84a62..3cea112 100644 --- a/src/pages/product/components/Variants.tsx +++ b/src/pages/product/components/Variants.tsx @@ -21,7 +21,7 @@ export default function Variants({ const { isRTL } = useAppSelector((state) => state.locale); const { t } = useTranslation(); const { isDesktop } = useBreakPoint(); - + const { restaurant } = useAppSelector((state) => state.order); // Determine variant levels based on options array length const variantLevels = useMemo(() => { if (!variantsList || variantsList.length === 0) return []; @@ -170,7 +170,7 @@ export default function Variants({ value: value, label: value, price: variant - ? `+${formatPriceUi(variant.price, 3)}` + ? `+${formatPriceUi(variant.price, restaurant.currency_decimals ?? 3)}` : "", }; })} diff --git a/src/utils/types/appTypes.ts b/src/utils/types/appTypes.ts index 015b437..1dcfe5e 100644 --- a/src/utils/types/appTypes.ts +++ b/src/utils/types/appTypes.ts @@ -534,6 +534,7 @@ export interface RestaurantDetails { closingTime: string; isOpened: boolean; isFav: boolean; + currency_decimals: number; } export interface Banner {