diff --git a/src/api/sales.js b/src/api/sales.js index 65dd7ec..8ffa0b9 100644 --- a/src/api/sales.js +++ b/src/api/sales.js @@ -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}` diff --git a/src/components/InventoryModal.jsx b/src/components/InventoryModal.jsx index 192b988..6e1b1c8 100644 --- a/src/components/InventoryModal.jsx +++ b/src/components/InventoryModal.jsx @@ -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; \ No newline at end of file diff --git a/src/hooks/useInventory.js b/src/hooks/useInventory.js index f934484..eb68870 100644 --- a/src/hooks/useInventory.js +++ b/src/hooks/useInventory.js @@ -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; };