refactor calculation code

- implement fee
- centralize the code
This commit is contained in:
2025-10-27 08:08:22 +03:00
parent 251e7a9369
commit cd49a3756f
13 changed files with 395 additions and 284 deletions

View File

@@ -1,228 +1,248 @@
import { Variant } from "pages/orders/types"
import { Extra3 } from "utils/types/appTypes"
import { Variant } from "pages/orders/types";
import { Extra3 } from "utils/types/appTypes";
export interface OrderDetails {
orderItems: OrderItem[]
order: Order
status: Status[]
laststatus: Status2[]
restaurant: string
restaurantAR: string
restaurantID: number
global_currency: string
local_currency: string
address: string
phone: string
restaurant_iimage: string
itemsImagePrefixOld: string
itemsImagePrefix: string
}
export interface OrderItem {
id: number
is_loyalty_used: number
no_of_stamps_give: number
isHasLoyalty: boolean
is_vat_disabled: number
name: string
price: number
qty: number
variant_price: string
image: string
imageName: string
variantName: string
variantLocalName?: string
extras: any[]
itemline: string
itemlineAR: string
itemlineAREN: string
extrasgroups: any[]
itemComment: string
variant?: Variant
itemExtras: any[]
AvaiilableVariantExtras: Extra3[]
isPrinted: number
category_id: number
pos_order_id: string
updated_at: string
created_at: string
old_qty: number
new_qty: number
last_printed_qty: number
deleted_qty: number
discount_value: any
discount_type_id: any
original_price: number
pricing_method: string
is_already_paid: number
hash_item: string
}
orderItems: OrderItem[];
order: Order;
status: Status[];
laststatus: Status2[];
restaurant: string;
restaurantAR: string;
restaurantID: number;
global_currency: string;
local_currency: string;
address: string;
phone: string;
restaurant_iimage: string;
itemsImagePrefixOld: string;
itemsImagePrefix: string;
}
export interface Order {
id: number
pos_order_id: string
created_at: string
updated_at: string
table: string
phone: string
user_name: string
restaurant_name: string
lat: string
lng: string
restaurant_icon: string
location: any
status: string
status_id: number
delivery_method: number
orderItems: OrderItem2[]
discount: string
vat: number
total_price: number
comment: string
pickup_comments: any
car_plate: string
pickup_time: any
pickup_date: any
delivery_pickup_interval: any
office_no: any
room_no: any
time_to_prepare: any
last_status_id: number
currency: string
gift_id: any
is_loyalty_used: number
payment_status: string
created_by: string
split_order_group_id: any
split_sequence: any
is_split_order: number
split_at: any
split_by_user_id: any
}
export interface OrderItem2 {
id: number
is_loyalty_used: number
no_of_stamps_give: number
isHasLoyalty: boolean
is_vat_disabled: number
name: string
price: number
qty: number
variant_price: string
image: string
imageName: string
variantName: string
variantLocalName?: string
extras: any[]
itemline: string
itemlineAR: string
itemlineAREN: string
extrasgroups: any[]
itemComment: string
variant?: Variant2
itemExtras: any[]
AvaiilableVariantExtras: AvaiilableVariantExtra2[]
isPrinted: number
category_id: number
pos_order_id: string
updated_at: string
created_at: string
old_qty: number
new_qty: number
last_printed_qty: number
deleted_qty: number
discount_value: any
discount_type_id: any
original_price: number
pricing_method: string
is_already_paid: number
hash_item: string
}
export interface Status {
id: number
name: string
alias: string
pivot: Pivot5
}
export interface Pivot5 {
order_id: number
status_id: number
user_id: number
created_at: string
comment: string
}
export interface Status2 {
id: number
name: string
alias: string
pivot: Pivot6
}
export interface OrderItem {
id: number;
is_loyalty_used: number;
no_of_stamps_give: number;
isHasLoyalty: boolean;
is_vat_disabled: number;
name: string;
price: number;
qty: number;
variant_price: string;
image: string;
imageName: string;
variantName: string;
variantLocalName?: string;
extras: any[];
itemline: string;
itemlineAR: string;
itemlineAREN: string;
extrasgroups: any[];
itemComment: string;
variant?: Variant;
itemExtras: any[];
AvaiilableVariantExtras: Extra3[];
isPrinted: number;
category_id: number;
pos_order_id: string;
updated_at: string;
created_at: string;
old_qty: number;
new_qty: number;
last_printed_qty: number;
deleted_qty: number;
discount_value: any;
discount_type_id: any;
original_price: number;
pricing_method: string;
is_already_paid: number;
hash_item: string;
}
export interface Pivot6 {
order_id: number
status_id: number
user_id: number
created_at: string
comment: string
}
export interface Order {
id: number;
pos_order_id: string;
created_at: string;
updated_at: string;
table: string;
phone: string;
user_name: string;
restaurant_name: string;
lat: string;
lng: string;
restaurant_icon: string;
location: any;
status: string;
status_id: number;
delivery_method: number;
orderItems: OrderItem2[];
discount: string;
vat: number;
total_price: number;
comment: string;
pickup_comments: any;
car_plate: string;
pickup_time: any;
pickup_date: any;
delivery_pickup_interval: any;
office_no: any;
room_no: any;
time_to_prepare: any;
last_status_id: number;
currency: string;
gift_id: any;
is_loyalty_used: number;
payment_status: string;
created_by: string;
split_order_group_id: any;
split_sequence: any;
is_split_order: number;
split_at: any;
split_by_user_id: any;
}
export interface Variant2 {
id: number
price: number
options: string
local_name: any
optionsArray: OptionsArray2[]
OptionsList: string
extras: Extra2[]
}
export interface OptionsArray2 {
id: number
name: string
}
export interface Extra2 {
id: number
item_id: number
price: number
name: string
nameAR: string
created_at: string
updated_at: string
deleted_at: any
extra_for_all_variants: number
is_custome: number
is_available: number
modifier_id: any
pivot: Pivot3
}
export interface Pivot3 {
variant_id: number
extra_id: number
}
export interface AvaiilableVariantExtra2 {
id: number
item_id: number
price: number
name: string
nameAR: string
created_at: string
updated_at: string
deleted_at: any
extra_for_all_variants: number
is_custome: number
is_available: number
modifier_id: any
pivot: Pivot4
}
export interface OrderItem2 {
id: number;
is_loyalty_used: number;
no_of_stamps_give: number;
isHasLoyalty: boolean;
is_vat_disabled: number;
name: string;
price: number;
qty: number;
variant_price: string;
image: string;
imageName: string;
variantName: string;
variantLocalName?: string;
extras: any[];
itemline: string;
itemlineAR: string;
itemlineAREN: string;
extrasgroups: any[];
itemComment: string;
variant?: Variant2;
itemExtras: any[];
AvaiilableVariantExtras: AvaiilableVariantExtra2[];
isPrinted: number;
category_id: number;
pos_order_id: string;
updated_at: string;
created_at: string;
old_qty: number;
new_qty: number;
last_printed_qty: number;
deleted_qty: number;
discount_value: any;
discount_type_id: any;
original_price: number;
pricing_method: string;
is_already_paid: number;
hash_item: string;
}
export interface Pivot4 {
variant_id: number
extra_id: number
}
export interface Status {
id: number;
name: string;
alias: string;
pivot: Pivot5;
}
export interface Pivot5 {
order_id: number;
status_id: number;
user_id: number;
created_at: string;
comment: string;
}
export interface Status2 {
id: number;
name: string;
alias: string;
pivot: Pivot6;
}
export interface Pivot6 {
order_id: number;
status_id: number;
user_id: number;
created_at: string;
comment: string;
}
export interface Variant2 {
id: number;
price: number;
options: string;
local_name: any;
optionsArray: OptionsArray2[];
OptionsList: string;
extras: Extra2[];
}
export interface OptionsArray2 {
id: number;
name: string;
}
export interface Extra2 {
id: number;
item_id: number;
price: number;
name: string;
nameAR: string;
created_at: string;
updated_at: string;
deleted_at: any;
extra_for_all_variants: number;
is_custome: number;
is_available: number;
modifier_id: any;
pivot: Pivot3;
}
export interface Pivot3 {
variant_id: number;
extra_id: number;
}
export interface AvaiilableVariantExtra2 {
id: number;
item_id: number;
price: number;
name: string;
nameAR: string;
created_at: string;
updated_at: string;
deleted_at: any;
extra_for_all_variants: number;
is_custome: number;
is_available: number;
modifier_id: any;
pivot: Pivot4;
}
export interface Pivot4 {
variant_id: number;
extra_id: number;
}
export enum DeliveryMethod {
DineIn = 3,
Delivery = 1,
Pickup = 2,
Gift = 10,
ScheduledOrder = 9,
ToRoom = 5,
ToOffice = 4,
}
export enum OrderType {
DineIn = "dine-in",
Delivery = "delivery",
Pickup = "pickup",
Gift = "gift",
ScheduledOrder = "scheduled_order",
ToRoom = "room",
ToOffice = "office",
}

View File

@@ -2,8 +2,8 @@ import { message } from "antd";
import {
clearCart,
selectCart,
selectCartTotalWithLoyaltyDiscount,
selectHighestPricedLoyaltyItem,
selectGrandTotal,
selectHighestPricedLoyaltyItem
} from "features/order/orderSlice";
import { useCallback } from "react";
import { useTranslation } from "react-i18next";
@@ -37,7 +37,7 @@ export default function useOrder() {
const highestLoyaltyItem = useAppSelector(selectHighestPricedLoyaltyItem);
const { useLoyaltyPoints } = useAppSelector(selectCart);
const orderPrice = useAppSelector(selectCartTotalWithLoyaltyDiscount);
const orderPrice = useAppSelector(selectGrandTotal);
const [createOrder] = useCreateOrderMutation();

View File

@@ -7,6 +7,7 @@ import ProText from "components/ProText";
import ProTitle from "components/ProTitle";
import { useScrollHandler } from "contexts/ScrollHandlerContext";
import useBreakPoint from "hooks/useBreakPoint";
import { useRestaurant } from "hooks/useRestaurant";
import { useTranslation } from "react-i18next";
import { useParams } from "react-router-dom";
import {
@@ -44,6 +45,9 @@ function MenuPage() {
const { isMobile, isTablet, isDesktop } = useBreakPoint();
const isLoading = isLoadingRestaurant || isLoadingMenu;
// Automatically load restaurant taxes when restaurant data is available
useRestaurant(restaurant);
return (
<>
<LocalStorageHandler restaurantID={restaurant?.restautantId || ""} />

View File

@@ -12,6 +12,7 @@ import RestaurantServices from "./RestaurantServices";
// Import the Client Component for localStorage handling
import Ads1 from "components/Ads/Ads1";
import { Loader } from "components/Loader/Loader";
import { useRestaurant } from "hooks/useRestaurant";
import { useParams } from "react-router-dom";
import { useGetRestaurantDetailsQuery } from "redux/api/others";
import LocalStorageHandler from "../menu/components/LocalStorageHandler";
@@ -19,12 +20,12 @@ import LocalStorageHandler from "../menu/components/LocalStorageHandler";
export default function RestaurantPage() {
const param = useParams();
const { isRTL } = useAppSelector((state) => state.locale);
const { data: restaurant, isLoading } = useGetRestaurantDetailsQuery(
"595",
{
skip: !param.id,
},
);
const { data: restaurant, isLoading } = useGetRestaurantDetailsQuery("595", {
skip: !param.id,
});
// Automatically load restaurant taxes when restaurant data is available
useRestaurant(restaurant);
if (isLoading) {
return <Loader />;

View File

@@ -2,15 +2,20 @@ import ActionsButtons from "components/ActionsButtons/ActionsButtons";
import { selectCart, setTmp } from "features/order/orderSlice";
import { useAppDispatch, useAppSelector } from "redux/hooks";
interface SplitBillTmp {
totalPeople?: number;
payFor?: number;
}
export default function PayForActions() {
const dispatch = useAppDispatch();
const { tmp } = useAppSelector(selectCart);
return (
<ActionsButtons
quantity={tmp?.payFor || 1}
setQuantity={(value) => dispatch(setTmp({ ...tmp, payFor: value }))}
max={tmp?.totalPeople || 10}
quantity={(tmp as SplitBillTmp)?.payFor || 1}
setQuantity={(value) => dispatch(setTmp({ ...(tmp as SplitBillTmp), payFor: value }))}
max={(tmp as SplitBillTmp)?.totalPeople || 10}
min={1}
/>
);

View File

@@ -1,23 +1,27 @@
import { Card, Divider, Space } from "antd";
import ArabicPrice from "components/ArabicPrice";
import ProText from "components/ProText";
import { selectCart, selectCartTotal } from "features/order/orderSlice";
import {
selectCart,
selectGrandTotal
} from "features/order/orderSlice";
import { useTranslation } from "react-i18next";
import { useAppSelector } from "redux/hooks";
import { ProGray1 } from "ThemeConstants";
import styles from "../SplitBillPage.module.css";
interface SplitBillTmp {
totalPeople?: number;
payFor?: number;
}
export default function PaymentSummary() {
const { t } = useTranslation();
const { tmp } = useAppSelector(selectCart);
const getTotal = useAppSelector(selectCartTotal);
const total = useAppSelector(selectGrandTotal);
const subtotal = getTotal;
const tax = subtotal * 0.1; // 10% tax
const total = subtotal + tax;
const costPerPerson = total / (tmp?.totalPeople || 1);
const remainingAmount = total - (tmp?.payFor || 1) * costPerPerson;
const costPerPerson = total / ((tmp as SplitBillTmp)?.totalPeople || 1);
const remainingAmount = total - ((tmp as SplitBillTmp)?.payFor || 1) * costPerPerson;
return (
<Card className={`${styles.orderSummary}`}>