A return that used to go through quietly
Picture a stores clerk on a Tuesday afternoon. A batch of parts came in last month from a supplier at 300 per unit. The batch was received into the Stores warehouse on one Purchase Receipt.
A few days later, the same item also sits in the Finished Goods warehouse. That stock came from an earlier Material Receipt, at 100 per unit. Nobody thinks of the two lots as connected.
Five units from the supplier batch turn out to be damaged. The clerk opens a Purchase Return against the Purchase Receipt, picks Finished Goods as the return warehouse, and submits.
Before ERPNext v16.61, that submit went through. The numbers it produced were wrong, and nothing in the form warned anyone.
This article covers what changed, why the old behaviour was a problem, and how to work with the new check.
What the old return did
The bug report is PR #69062 in frappe/erpnext. It is an automatic backport of #67530 into the version-16 hotfix branch. The release bot lists it in v16.61.0.
The PR gives a clean repro. Take these two entries:
- A Material Receipt of 10 units at 100 into Finished Goods.
- A Purchase Receipt of 10 units at 300 into Stores.
Now return 5 units against the Purchase Receipt, with Finished Goods as the warehouse.
The return is valued at the receipt rate, 300. Five units at 300 should be 1,500. But the stock engine looks for stock at that rate in the warehouse you picked. Finished Goods has none at 300.
So the engine takes the oldest bin in that warehouse instead. That is the 100 rate lot. Five units at 100 come out, 500 in total.
The gap between 1,500 and 500 is 1,000. The PR says that amount was booked to Cost of Goods Sold. The stock ledger and the accounts ledger now disagree about what left the building.
Why it happens
For FIFO and LIFO items, the valuation engine works in lots. Each lot has a quantity and a rate. A return is supposed to take out the lot it came from.
The return row names a warehouse. If that warehouse holds no lot at the receipt rate, the engine has nothing correct to pick. It falls back to whatever is there.
The mistake is quiet because every individual step looks fine. The warehouse exists. The quantity is positive. The item is valid. Only the combination is wrong.
The fix in v16.61
The new check lives in erpnext/controllers/sales_and_purchase_return.py. The entry point is validate_return_warehouse_stock(doc), which validate_return calls.
The flow is simpler than the code around it. Here is the logic in plain terms:
The diagram is a simplification of the controller, not a copy of it. The point is the branch. A return from the wrong warehouse at the wrong rate stops at the refusal.
The error message is the useful part. It names the row, the return warehouse, the item, the receipt rate and the receipt warehouse. It then tells the user to move the goods back to the receipt warehouse and return them from there.
Scope: what the check does not cover
Read the scope before you change a process around it. The check is narrower than it sounds.
- Stock items only. Non-stock rows are skipped.
- Serial and batch items are skipped. The PR says those items go back at their own rate, so the check does not apply.
- FIFO and LIFO only. Moving Average items are not checked. The PR body says a moving average return goes out at the warehouse average, so the rate mismatch cannot happen the same way.
- Submitted documents only. The check runs when
docstatusis 1. - Purchase Receipt and Purchase Invoice. For a Purchase Invoice, it also needs
update_stockset. - No switch. The diff we read has no setting that turns the check off.
If your returns are mostly serial or batch tracked, or your valuation is Moving Average, this change will not touch your daily flow.
The process you will actually see
The sequence below is what a user experiences when the check fires, and what they do next.
The refusal is the first step, not the end. The fix is a physical transfer. The clerk moves five units from Stores to Finished Goods, so there is stock at the right rate in the warehouse the return names.
The PR gives this exact path. After the transfer, the return submits and takes out 1,500.
Why we think the refusal is the right behaviour
The clerk would prefer the return to just work. That preference is understandable, and it is also the reason the bug lasted as long as it did.
A purchase return is a claim about physical goods. It says these units are leaving this location, at this value. If the system cannot find those units at that value in that place, the honest answer is no.
Think of a library. A book issued from the north branch goes back to the north branch. The desk in the south branch can take the book, but its shelf records will not match what the books on hand are worth.
The tradeoff is friction. A clerk now has to make a transfer before a return. For a small return, that feels like paperwork. For a large one, it is the difference between a correct cost of goods and a quiet gain nobody can explain.
What to do before you upgrade
If you run FIFO or LIFO items with purchase returns, test the path before you trust it:
- Check your returns from non-receipt warehouses. Look for returns where the return warehouse differs from the receipt warehouse. Those are the rows the check targets.
- Plan the transfers. If your team returns goods from a secondary location often, the transfer step will show up in your process. Decide who owns it before the first refusal lands on a Monday.
- Read the scope list again against your own item setup. Batch and serial items are out of scope. Moving Average is out of scope.
We have not run this against every combination of valuation method, item tracking and return type. The scope above is what the PR and its diff describe. Test your own case on a staging site before you rely on it.
The takeaway
A return is a physical claim. Before you post it, ask where the goods are and what they are worth there. If the system says it cannot find them at that value, the fix is usually to move the goods, not to force the return.
Have you hit a purchase return that posted from the wrong lot? How did you find it, and did the gain show up in Cost of Goods Sold or somewhere else?
Have questions about implementing this? Drop us a line — we're happy to help.


