Enhance primary item and order item handling in POS Mobile components
- Added checks to prevent redundant primary item fetches in `PosDepartmentStepMobile2.vue`. - Introduced `updateLastOrderItemPrices` to dynamically update item prices based on product changes. - Updated `primaryItem` logic in `PosDepartmentStepMobileFlow.vue` to ensure reactivity and vehicle type synchronization. - Improved last vehicle order handling by validating items and updating their prices efficiently. - Extended `SessionUser.objects.products.get.all` to support additional metadata options.
This commit is contained in:
@@ -28,6 +28,7 @@ import PosDepartmentStepMobileButtonClearAll
|
||||
from "@/components/displays/department/pos/steps/mobile/elements/PosDepartmentStepMobileButtonClearAll.vue";
|
||||
import PosDepartmentStepMobileFixedBottomControl
|
||||
from "@/components/displays/department/pos/steps/mobile/elements/PosDepartmentStepMobileFixedBottomControl.vue";
|
||||
import {PosOrderItem} from "@/components/displays/department/pos/steps/mobile/objects/PosOrderItem.vue";
|
||||
|
||||
onMounted(() => {
|
||||
// Set the header to be transparent
|
||||
@@ -62,6 +63,11 @@ const fetchPrimaryItemProduct = () => {
|
||||
if (!vehicles.vehicle_1.value.type) {
|
||||
return; // If no vehicle type is set, do not proceed.
|
||||
}
|
||||
// If the primary item is already set and matches the vehicle type, do not fetch again.
|
||||
if (transactionItems.primaryItem.value && transactionItems.primaryItem.value.id === vehicles.vehicle_1.value.type) {
|
||||
console.warn('Primary item already set and matches vehicle type, skipping fetch.');
|
||||
return;
|
||||
}
|
||||
SessionUser.objects.products.get.single(vehicles.vehicle_1.value.type, {department_id: department_id.value, customer_id: metadata.getCustomerId(), category_id: null, final_price: true})
|
||||
.then(product => {
|
||||
// If the product is found, set it as the primary item
|
||||
@@ -90,11 +96,26 @@ const fetchLastOrderItems = (vehicleIndex: number) => {
|
||||
// If the lastOrder is set, fetch the order items for the last order
|
||||
if (lastOrders.get(vehicleIndex)) {
|
||||
getOrderItems(lastOrders.get(vehicleIndex).id)
|
||||
.then(response => lastOrders.set(vehicleIndex, {...lastOrders.get(vehicleIndex), items: response.data.data}))
|
||||
.then(response => lastOrders.set(vehicleIndex, {...lastOrders.get(vehicleIndex), items: updateLastOrderItemPrices(response.data.data)}))
|
||||
.catch(error => console.error('Error fetching last order items:', error));
|
||||
}
|
||||
}
|
||||
|
||||
const updateLastOrderItemPrices = (items: PosOrderItem[]) => {
|
||||
// Update the prices of the last order items to reflect any changes in the product prices
|
||||
return items.map(item => {
|
||||
SessionUser.objects.products.get.single(item.product_id, {category_id: null, department_id: department_id.value, customer_id: customer_id.value, final_price: true}).then(product => {
|
||||
console.warn('Fetched product for last order item:', product);
|
||||
transactionItems.updateTransactionPrices([product]); // Update the transaction prices with the fetched product
|
||||
}).catch(error => {
|
||||
console.error('Error fetching product for last order item:', error);
|
||||
});
|
||||
return {
|
||||
...item, // Keep existing item properties, they will be overridden if the product fetch is successful
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const layout = {
|
||||
classes: <string[]>[]
|
||||
}
|
||||
|
||||
+34
@@ -336,7 +336,16 @@ const getAdditionalItemsTotal = () => {
|
||||
};
|
||||
// Function to set the primary item for the transaction
|
||||
const setPrimaryItem = (item: PosProduct | null) => {
|
||||
console.warn('Setting primary item:', item);
|
||||
primaryItem.value = item;
|
||||
// Set the vehicle type to the new primary item id (This is done to prevent overriding the vehicle type when changing the primary item)
|
||||
if (item && vehicles.vehicle_1.value) {
|
||||
vehicles.vehicle_1.value.type = item.id;
|
||||
}
|
||||
// Wait 0.5 second and then log the primary item value to ensure reactivity has updated it
|
||||
setTimeout(() => {
|
||||
primaryItem.value = item;
|
||||
}, 500);
|
||||
};
|
||||
// Function to clear the primary item from the transaction
|
||||
const clearPrimaryItem = () => {
|
||||
@@ -371,6 +380,25 @@ const updateTransactionProduct = (updatedProduct: PosProduct) => {
|
||||
}
|
||||
return item;
|
||||
});
|
||||
// Update last vehicle orders if they match the updated product
|
||||
for (const vehicleKey in lastVehicleOrders.value) {
|
||||
const order = lastVehicleOrders.value[vehicleKey as keyof typeof lastVehicleOrders.value];
|
||||
if (order && order.items && order.items.length > 0) {
|
||||
order.items = order.items.map(item => {
|
||||
if (item.product.id === updatedProduct.id) {
|
||||
return {
|
||||
...item,
|
||||
price: updatedProduct.price, // Update the item price
|
||||
product: {
|
||||
...item.product, // Keep other product details (quantity, etc.)
|
||||
price: updatedProduct.price // Update the product price
|
||||
}
|
||||
};
|
||||
}
|
||||
return item;
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
// Function to update product prices in additional items if needed (e.g., if an item is already in the cart, update its price)
|
||||
const updateTransactionPrices = (updatedProducts: PosProduct[] | null = null) => {
|
||||
@@ -717,6 +745,11 @@ const selectLastVehicleOrder = (vehicleIndex: number) => {
|
||||
// This function is used to use the last vehicle order for the selected vehicle.
|
||||
const lastOrder = getLastVehicleOrder(vehicleIndex);
|
||||
if (lastOrder) {
|
||||
// If the last order does not have items, we cannot use it.
|
||||
if (!lastOrder.items || lastOrder.items.length === 0) {
|
||||
console.warn(`Last vehicle order for vehicle ${vehicleIndex} has no items. aborting selection.`);
|
||||
return;
|
||||
}
|
||||
const addons: Addon[] = lastOrder.items.filter(item => item.related_item_id === lastOrder.items[0].id) // Assuming the first item is the primary item for the transaction.
|
||||
.map(item => {
|
||||
return {
|
||||
@@ -739,6 +772,7 @@ const selectLastVehicleOrder = (vehicleIndex: number) => {
|
||||
// If the last order exists, set it as the primary item for the transaction.
|
||||
vehicles.select(vehicleIndex, {...vehicles.get(vehicleIndex), type: primaryItem.id});
|
||||
transactionItems.setPrimaryItem({...primaryItem}); // Clear addons for the primary item
|
||||
transactionItems.setPrimaryItem({...primaryItem}); // Set the primary item for the transaction
|
||||
console.log(`Selected last vehicle order for vehicle ${vehicleIndex}:`, lastOrder);
|
||||
} else {
|
||||
console.warn(`No last vehicle order found for vehicle ${vehicleIndex}.`);
|
||||
|
||||
@@ -231,8 +231,8 @@ export const Products = {
|
||||
},
|
||||
},
|
||||
get: {
|
||||
all: async () => {
|
||||
return ObjectsGlobal.get.objects(Products.meta.endpoint);
|
||||
all: async (options = {department_id: null, customer_id: null, category_id: null, final_price: false}) => {
|
||||
return ObjectsGlobal.get.objects(Products.meta.endpoint, {...options});
|
||||
},
|
||||
single: async (id, options = {department_id: null, customer_id: null, category_id: null, final_price: false}) => {
|
||||
return ObjectsGlobal.get.object(Products.meta.endpoint, {id: id, ...options});
|
||||
|
||||
Reference in New Issue
Block a user