Technical
ERPNext Stock Valuation: FIFO, Moving Average and Ledger Drift
How ERPNext values stock with FIFO and moving average, what the stock ledger records, how perpetual inventory posts to the books, what Repost Item Valuation does, and why stock and accounts drift apart.
Kalvyai Engineering · · 8 min read

On this page
When a stock balance report says one number and the balance sheet says another, the first instinct is usually to post a journal entry and move on. In most cases that hides the real cause. This post explains how ERPNext values stock, where the numbers live, and how to find out why the two sides disagree.
We cover:
- FIFO vs moving average, with a worked example
- What the Stock Ledger Entry records
- How perpetual inventory turns stock movements into GL entries
- What Repost Item Valuation does with backdated transactions
- The common reasons stock and accounts drift apart, and how to trace them
The three layers you need to keep in mind
ERPNext keeps stock information in three places:
- Stock Ledger Entry (SLE): one row per item, per warehouse, per stock transaction. This is the source of truth for quantity and value.
- Bin: a running summary per item and warehouse (current quantity, valuation rate, stock value). It is derived from the SLEs.
- GL Entry: the accounting side. With perpetual inventory enabled, each stock transaction also posts GL entries to the stock account linked to the warehouse.
Drift means layer 1 and layer 3 no longer agree. Understanding how each layer is written makes the cause much easier to find.
FIFO vs moving average
ERPNext supports FIFO and Moving Average as valuation methods. Recent versions also offer LIFO, but this post focuses on the two most companies use.
A worked example
Suppose you buy the same item twice and then ship some of it:
- Receive 10 units at 100
- Receive 10 units at 120
- Deliver 15 units
FIFO consumes the oldest stock first:
- Cost of the 15 delivered: 10 × 100 + 5 × 120 = 1,600
- Remaining: 5 units at 120 = 600
Moving average recalculates one rate after every receipt:
- Rate after step 2: (1,000 + 1,200) / 20 = 110
- Cost of the 15 delivered: 15 × 110 = 1,650
- Remaining: 5 units at 110 = 550
Total value is the same (2,200) either way. What changes is how it is split between cost of goods sold and closing stock. With FIFO, ERPNext stores the remaining layers as a queue on each SLE in the stock_queue field, for example [[5, 120.0]] after step 3.
Which one to pick
- FIFO keeps closing stock close to recent purchase prices. It suits businesses with rising costs or perishable goods, and gives more defensible numbers when prices move a lot.
- Moving average is simpler to explain and smooths price swings. It suits high-volume, low-variance items.
Tax and reporting rules in your country may restrict which methods you can use. Confirm with your accountant before go-live.
Where to set it
- The company-wide default is in Stock Settings (Default Valuation Method).
- Each Item can override it with its own Valuation Method field.
Changing the method later
ERPNext blocks changing the valuation method on items that already have stock transactions. This is deliberate: switching would make the existing ledger inconsistent. If you really need to change, the usual approach is to create a new item, move stock with a Stock Reconciliation or Stock Entry, and disable the old item. Plan this with your accountant, because it creates a visible valuation adjustment.
What the stock ledger records
Every submitted stock transaction (Purchase Receipt, Delivery Note, Stock Entry, Stock Reconciliation, invoices with Update Stock, and so on) writes SLEs. The fields that matter for valuation:
Field | Meaning |
|---|---|
| Quantity change from this transaction (+ in, − out) |
| Running quantity for this item and warehouse |
| Rate of incoming stock |
| Valuation rate after this transaction |
| Total value of this item in this warehouse after the transaction |
| Change in value caused by this transaction |
| FIFO layers remaining |
| Set when the voucher is cancelled |
The key point: SLEs are ordered by posting date and time. Each row depends on every row before it. Insert something in the past and every later row for that item and warehouse may need a new rate.
Perpetual inventory: how stock reaches the books
Perpetual inventory is enabled per Company. When it is on, each warehouse is linked to a stock account, and stock transactions post GL entries at the same time as SLEs.
Typical postings:
- Purchase Receipt: debit the warehouse stock account, credit Stock Received But Not Billed.
- Purchase Invoice (against that receipt): debit Stock Received But Not Billed, credit the supplier.
- Delivery Note: debit Cost of Goods Sold, credit the warehouse stock account.
- Material Issue (Stock Entry): debit the expense account on the row, credit the stock account.
- Material Transfer: moves value between stock accounts if the two warehouses map to different accounts.
- Stock Reconciliation: posts the value difference to the Stock Adjustment account (or a temporary opening account for opening stock).
The GL amount comes from stock_value_difference on the SLEs. That is why the two sides should always match: they are written from the same calculation.
Repost Item Valuation
When you submit or cancel a backdated stock transaction, ERPNext does not recalculate everything immediately. It creates a Repost Item Valuation document and a background job processes it.
The repost job:
- Finds all SLEs for the affected item and warehouse after the backdated posting date and time.
- Recalculates valuation rates, stock values and FIFO queues in order.
- Reposts the GL entries of the affected vouchers so the accounts follow the new values.
The document has a status such as Queued, In Progress, Completed, Failed or Skipped. Until it reaches Completed, your stock and accounts can legitimately disagree.
Stock Settings may also limit reposting to a time window so it does not slow down working hours.
Creating a repost manually
If you suspect values are stale, you can queue a repost yourself:
- Search for Repost Item Valuation in the search bar and create a new one.
- Set Based On to either Transaction (pick a voucher type and number) or Item and Warehouse.
- Set the posting date and time from which recalculation should start. Choose a date before the first suspect transaction.
- Submit. The document is queued for the background job.
- Check the status later. If it fails, open it and read the error log before retrying.
To see what is stuck, from bench --site yoursite console:
import frappe
rows = frappe.get_all(
"Repost Item Valuation",
filters={"status": ["in", ["Queued", "In Progress", "Failed"]], "docstatus": 1},
fields=["name", "status", "based_on", "voucher_no", "item_code", "warehouse", "posting_date"],
order_by="posting_date asc",
)
for r in rows:
print(r)Also check that the scheduler is running on the site. If it is disabled, reposts will sit in Queued forever.
Why stock and accounts drift apart
These are the causes we see most often, roughly in order of frequency.
1. Manual journal entries on stock accounts
Someone posts a Journal Entry directly to a warehouse stock account to "fix" a number. The GL moves, the stock ledger does not. Stock adjustments should go through Stock Reconciliation or Stock Entry, so both sides move together.
2. Reposts that are queued or failed
A backdated receipt was submitted, the repost failed (for example because of negative stock at some point in time), and nobody noticed. The SLEs and GL entries after that date are still based on old rates.
3. Negative stock
If Allow Negative Stock is on in Stock Settings, items can be issued before they are received. The outgoing rate at that moment is a guess (usually the last known rate). When stock arrives at a different rate, the difference has to be absorbed somewhere. Combined with backdated entries, this is a common source of large differences.
4. Warehouse account mapping changes
If you change the account on a warehouse after transactions exist, old GL entries stay on the old account while the stock ledger still counts that stock under the warehouse. Comparing by account then shows a gap on both accounts.
5. Several warehouses on one account, or a warehouse with no account
This is not wrong in itself, but it makes comparison harder and hides problems. A warehouse with no account resolves to the company default inventory account, which can surprise people.
6. Perpetual inventory switched on mid-year
If perpetual inventory was enabled after stock already existed, the opening stock value may never have been posted to the GL. Everything afterwards is correct relative to a wrong starting point.
7. Custom code writing stock or GL rows
Scripts or custom apps that insert SLEs, GL entries or update Bins directly skip the normal valuation and posting flow. Always create stock movements through standard documents.
8. Frozen periods
If accounts are frozen up to a date but a stock repost tries to change GL entries before that date, the repost can fail or leave older periods unchanged.
How to find and fix drift
ERPNext includes a Stock and Account Value Comparison report that lists vouchers where stock value and account value differ. Start there. If you need to go further, work through these steps.
- Pick a cut-off date and make sure no reposts are queued or failed before it.
- Get the stock value per warehouse from the ledger:
SELECT sle.warehouse, w.account, SUM(sle.stock_value_difference) AS stock_value
FROM `tabStock Ledger Entry` sle
JOIN `tabWarehouse` w ON w.name = sle.warehouse
WHERE sle.is_cancelled = 0
AND sle.company = 'Your Company'
AND sle.posting_date <= '2024-03-31'
GROUP BY sle.warehouse, w.account;- Get the GL balance per stock account:
SELECT account, SUM(debit) - SUM(credit) AS gl_balance
FROM `tabGL Entry`
WHERE is_cancelled = 0
AND company = 'Your Company'
AND posting_date <= '2024-03-31'
AND account IN (SELECT DISTINCT account FROM `tabWarehouse` WHERE account IS NOT NULL)
GROUP BY account;- Compare by account. Sum the stock values of all warehouses mapped to each account and compare with the GL balance.
- Look for non-stock vouchers on stock accounts, which are often the whole answer:
SELECT voucher_type, voucher_no, posting_date, account, debit, credit
FROM `tabGL Entry`
WHERE is_cancelled = 0
AND account IN (SELECT DISTINCT account FROM `tabWarehouse` WHERE account IS NOT NULL)
AND voucher_type IN ('Journal Entry', 'Payment Entry')
ORDER BY posting_date;- Narrow down by month. Move the cut-off date back until the difference disappears. The month where it appears tells you which transactions to inspect.
- Fix at the source. Cancel and amend wrong journal entries, repost from the right date, or correct opening stock. Use a Stock Reconciliation only when the physical count or valuation itself is wrong.
- Run the comparison again after reposts complete.
Always do this on a copy of production first. Reposting a long history can take time and change historic COGS, which matters if those periods are already reported.
Habits that keep the two sides aligned
- Keep Allow Negative Stock off unless you have a clear reason.
- Avoid backdating stock transactions; when you must, check the repost status the same day.
- Restrict who can post journal entries to stock accounts.
- Give each main warehouse its own stock account so differences are easy to locate.
- Run the Stock and Account Value Comparison report as part of month-end close.
- Freeze stock and accounts after each period is closed.
How Kalvyai can help
We set up and repair ERPNext inventory accounting: choosing valuation methods, mapping warehouses to accounts, clearing failed reposts, and tracing differences between the stock ledger and the general ledger back to specific vouchers. We also build integrations and AI agents that work inside ERPNext with human approvals and an audit trail, so automated stock movements go through standard documents. See our ERPNext services, browse our apps at store.kalvyai.com, or contact us with your stock and GL numbers and we will help you find where they split.
- ERPNext
- Stock Valuation
- Inventory Accounting
- FIFO
- Perpetual Inventory
- Repost Item Valuation
Kalvyai Engineering · Engineering team
The engineers who build, connect and fix business systems at Kalvyai: Odoo, ERPNext, Shopify and AI agents that work inside them with approvals, budgets and an audit trail.



Comments
No comments yet. Questions and corrections are welcome.
Rate this post or leave a comment