FIFO Does NOT Work on Repack Entry for Batch Items

I think this is ‘Urgent Issue’ that’s needs to be fixed as many ERPNext users including me are using it for production use. I just aware and I can say my stock value is wrong now due to this issue.

Repack Entry for batch items doesn’t look-up FIFO rate. (both source and target are batch items)

Steps to reproduce issue:

  1. Make 3 purchase receipts for 1 item with different qty and rate.
  2. Make repack, take 3rd batch id as source then create another batch-item. Set this new item as target
  3. Upon save, the value is calculated from 1st batch value, not from 3rd batch value. Fifo in-out should look-up the value per batch-id value if it is batch item

Is there anyone who having same issue?

Currently FIFO does not consider Batch Id. Please raise an issue on github.

@nabinhait I think the correct process is…if it’s a batch/serialized item then FIFO must look-up the value based on serial no/batch id…

Where to fix this? Can anybody help pointing-out ?


@nabinhait …Is this the code to get fifo rate (under ~/stock/ on submitting delivery note, sales invoice? Why it still doesn’t consider Batch ID on posting Cost of Goods Sold?

Can you help pointing out so that the FIFO system shall look-up Batch-ID/Serial No on posting Accounting entries?

def get_fifo_rate(previous_stock_queue, qty):
	"""get FIFO (average) Rate from Queue"""
	if qty >= 0:
		total = sum(f[0] for f in previous_stock_queue)
		return sum(flt(f[0]) * flt(f[1]) for f in previous_stock_queue) / flt(total) if total else 0.0
		available_qty_for_outgoing, outgoing_cost = 0, 0
		qty_to_pop = abs(qty)
		while qty_to_pop and previous_stock_queue:
			batch = previous_stock_queue[0]
			if 0 < batch[0] <= qty_to_pop:
				# if batch qty > 0
				# not enough or exactly same qty in current batch, clear batch
				available_qty_for_outgoing += flt(batch[0])
				outgoing_cost += flt(batch[0]) * flt(batch[1])
				qty_to_pop -= batch[0]
				# all from current batch
				available_qty_for_outgoing += flt(qty_to_pop)
				outgoing_cost += flt(qty_to_pop) * flt(batch[1])
				batch[0] -= qty_to_pop
				qty_to_pop = 0

		return outgoing_cost / available_qty_for_outgoing

The fix is not very easy. You have to maintain FIFO stack, like [[qty1, batch1, rate1], [qty2, batch2, rate2]] and while delivering you have to pick up qty and rate based on FIFO considering batch. Also we have to rebuild fifo stack for existing stock ledger entries.

Hi guys,

This is an issue I’m running into as well. Specifically, I’d like Material Transfer for Manufacture to autopopulate batches based on FIFO. Please let me know if there’s been any progress, of it the issue has been raised on Github.


Im not sure if anyone else is working in this but im planning to add an addittion costing model called “lot cost” which will provide costs based on receipted lots. Our current erp system works this way. I dont just want to hardcode this as many are using the other methods of valuation, and I can do it if I want in my fork but would like to contribute back :wink:

Also when it comes to stock valuation it’s not a good idea to mix methods so also have to consider this and how to switch between methods for current stock movements.

It’s challenging trying to understand the current code base for me and taking time as my other responsibilities at work is taking priority.

In principal all I’m planning to do is store all stock receipts with an auto generated lot number and the users will pick this lot number for the other stock movement docs. All stock costs lookups will use this lot number for valuation. If multiple lots are picked during a single movement then the cost is the sum of the lot costs.

Im sure there will be challenges with this in some business use cases but this is my initial design plan. Just trying to figure out how and where to make appropriate changes that is really configurable.


1 Like

Hi @hpema108,

Did you build this lot cost Model?

Fifo must consider batch id. This is actually core costing issue… but I think most of us pay less attention on this…


I’ve looked into it but it’s too complicated at this stage of my skill to figure out how to add to erpnext, so no i did not add it.

Would still like to see it eventually be added to erpnext.


1 Like