Add files via upload
This commit is contained in:
1 parent
03399ea1bf
commit
2462631b4f
3 files changed
+27
-18
No files matched your search
+12
-2
@@ -27,18 +27,28 @@ export const salesAPI = {
|
||||
}
|
||||
},
|
||||
|
||||
// In api/sales.js - createSale function
|
||||
// Remove total from the items when sending to the API
|
||||
createSale: async (saleData) => {
|
||||
try {
|
||||
// Remove total from items if present
|
||||
const sanitizedSaleData = {
|
||||
...saleData,
|
||||
items: saleData.items.map(item => {
|
||||
const { total, ...itemWithoutTotal } = item;
|
||||
return itemWithoutTotal;
|
||||
})
|
||||
};
|
||||
|
||||
const response = await fetch(`${BASE_URL}/create-sale`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(saleData),
|
||||
body: JSON.stringify(sanitizedSaleData),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
// Extract error message from response
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(
|
||||
errorData.error || `HTTP error! status: ${response.status}`
|
||||
|
||||
@@ -59,6 +59,7 @@ const InventoryModal = ({ product, action, onClose, onInventoryUpdated }) => {
|
||||
alert("Stock level cannot be negative");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!valid) return;
|
||||
@@ -70,7 +71,7 @@ const InventoryModal = ({ product, action, onClose, onInventoryUpdated }) => {
|
||||
product_id: product.product_id,
|
||||
user_id: user.user_id || user.id,
|
||||
action,
|
||||
quantity: change, // always relative change
|
||||
quantity: action === 'adjust' ? qty : change, // always relative change
|
||||
remarks
|
||||
});
|
||||
|
||||
@@ -236,5 +237,4 @@ const InventoryModal = ({ product, action, onClose, onInventoryUpdated }) => {
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
export default InventoryModal;
|
||||
export default InventoryModal;
|
||||
+12
-13
@@ -30,24 +30,23 @@ export const useInventory = (initialProductId = null) => {
|
||||
|
||||
const user_id = user.user_id ?? user.id;
|
||||
|
||||
if (!product_id || !action || !quantity || !user_id) {
|
||||
if (!product_id || !action || quantity === undefined || !user_id) {
|
||||
throw new Error("Missing required fields for inventory log");
|
||||
}
|
||||
|
||||
// Only positive quantities sent; API handles remove/add
|
||||
const normalizedQuantity = Math.abs(quantity);
|
||||
// For 'adjust', send the quantity exactly
|
||||
const payload = {
|
||||
product_id,
|
||||
user_id,
|
||||
action,
|
||||
quantity: action === "adjust" ? quantity : Math.abs(quantity),
|
||||
remarks
|
||||
};
|
||||
|
||||
try {
|
||||
const payload = { product_id, user_id, action, quantity: normalizedQuantity, remarks };
|
||||
const created = await createInventoryLogAPI(payload);
|
||||
const created = await createInventoryLogAPI(payload);
|
||||
|
||||
// update local logs cache
|
||||
setLogs(prev => [created, ...prev]);
|
||||
return created;
|
||||
} catch (err) {
|
||||
console.error("addLog error", err);
|
||||
throw err;
|
||||
}
|
||||
setLogs(prev => [created, ...prev]);
|
||||
return created;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user