Support #12259
openSupport #12257: Vendor Management flow Issues
Editing an Invoice Deletes Active Batches and Uses Wrong Units (Batch Destruction)
0%
Description
Code Location: vendorInvoiceController.js:L2128-L2152
What is happening in code: When you edit an already-saved vendor invoice (e.g. fixing a typo in the invoice number or notes), the code tries to reverse the old stock and re-add the new stock. In doing so:
It blindly executes StockBatch.deleteMany({ invoiceId }).
When reversing product stock, it subtracts raw box counts (old.quantity) instead of total units (old.quantity * old.unitsPerBox).
Real-Life Example:
On Monday, you receive an invoice for 5 boxes of ceramic pots (10 pots per box = 50 pots total).
On Tuesday, you sell 15 pots to customers at the POS counter. (35 pots remain in that batch).
On Wednesday, the accountant edits the vendor invoice to correct the vendor's payment reference code.
What breaks immediately:
The system executes deleteMany({ invoiceId }) and wipes the entire batch from the database, even though 15 pots were already sold from it!
When reversing stock, it subtracts 5 (the number of boxes) instead of 50 (the total pots), leaving the inventory count completely wrong.
Business Impact: Your batch audit trail is destroyed, historical sales lose their batch records, and inventory numbers become permanently inaccurate.
Simple Fix:
Before deleting batches on invoice edit, check if batch.qtyRemaining < batch.qtyReceived. If any items were already sold, block the hard deletion and adjust the remaining balance instead.
Always reverse stock using old.totalUnits = old.quantity * old.unitsPerBox.
Subtasks
Related issues