Fix valuation rate in the purchase receipts

We have created many purchase receipts with GST tax included in the valuation rate. However we dont want the tax amount to be part of the valuation rate.

I understand that while creating purchase receipt we can specify how the tax amount should be considered. There are following 3 options -

  1. Total
  2. Valuation
  3. Valuation and total

When we had created purchase receipts by default the value ‘Valuation and Total’ was selected. We need to rectify the old recipts to exclude tax from valuation. Is there any way to do it?

All these items are batched items, so we cannot use the ‘Stock reconciliation’ for changing the valuation rate. Please suggest.


You may want to try using Repack in Stock Entry to adjust wrong valuation rate

Did you figure this out? We have the same issue. Thanks

Could we write a patch that would do this for all Purchase Receipts and their related Stock Ledger Entries? Apart from Stock Ledger Entries, are any other doctypes affected by this?

We have found a way to do this erpnext provides a function update_entries_after to update the stock ledgers for a given item, warehouse, posting date and posting time. What this function exactly do is recalculate valuation rate and quantity.

This functionality is highly used internally whenever there is any backdated entry/cancellation.

so to fix any entry following steps needs to be done

  1. Set correct category in tax table of that document, use doc.db_update function to update the database directly
  2. call update_valuation_rate function on items, this function is used to recalculate the valuation rate for each item in the document.
  3. update database directly by calling db_update function on each items
  4. update the stock ledger entry for that item with the new values.
  5. call update_entries_after for each item and warehouse
Sample Code
from erpnext.stock.stock_ledger import update_entries_after
from datetime import datetime, timedelta
		doc = frappe.get_doc('Purchase Receipt', <purchase receipt name>)
except Exception as e:
		print (e)
taxes = doc.get('taxes',  {'category': "Valuation and Total", "account_head": ('in', ['IGST - ER', 'CGST - ER', 'SGST - ER', "Cess - ER"])})
for tax in taxes:
		# step 1: update category
		tax.category = "Total"
# step 2: update valuation_rate for all items of that document

for item in doc.items:
		# step 3: update database with new valuation_rate
		# step 4: update stock ledger entry
		frappe.db.set_value('Stock Ledger Entry', {'voucher_detail_no':}, 'incoming_rate', item.valuation_rate)
		# step 5: update all entries
		update_entries_after({'item_code': item.item_code, 'warehouse': item.warehouse, "posting_date": doc.posting_date, "posting_time": doc.posting_time - timedelta(milliseconds=100)})


Valuation rates are calculated in the basics of FIFO or Moving average. In My use case when the new purchase receipt is made for an item, the valuation rate entered for the item needs to be calculated. (For new batch, New Valuation Rate(as entered in Item Valuation Rate)).
But when I make the next invoices the updated valuation rate will be calculated.

Is it possible to use item valuation rate(not update valuation rate) for new batch of items While making Purchase invoice and stock entry too.