Back to all posts

Frappe & ERPNext Journey ยท Episode 7

ERPNext Buying & Stock: Cotton Lots, Quality Inspection, and FIFO vs Moving Average

#ERPNext#Frappe#ERPNext Stock#ERPNext Buying#Inventory#Batch#Quality Inspection#erpnext buying module#erpnext stock module#erpnext material request#erpnext purchase receipt#erpnext batch tracking#erpnext quality inspection#erpnext projected qty#fifo vs moving average erpnext#erpnext inventory valuation#erpnext reorder level#erpnext textile cotton
ERPNext Buying & Stock: Cotton Lots, Quality Inspection, and FIFO vs Moving Average
November 21, 20268 min read

Raw cotton arrives at a spinning mill in bales of about 200 kg each, by the truckload. Every shipment is a lot, and no two lots are the same: one has longer fibres, another is a little damp, a third carries more trash. If a damp lot slips into the blowroom unchecked, an entire day's yarn can fail its strength test.

So the mill's stores had three jobs: buy the right cotton on time, check every lot before accepting it, and know exactly how much of each lot was left and what it was worth. In ERPNext, that's the Buying and Stock modules working together.

Procure to Pay: each step moves a different quantity

Keywords

  • Material Request (MR): An internal "we need this" note from stores or production.
  • Purchase Order (PO): The order sent to the supplier.
  • Purchase Receipt (PR): Goods received into a warehouse. Posts to the Stock Ledger.
  • Purchase Invoice (PI): The supplier's bill. Posts to the General Ledger.
  • Batch: A group of units that share properties (a cotton lot, a dye lot, an expiry date).
  • Quality Inspection (QI): A checklist of readings that accepts or rejects incoming goods.
  • Bin: ERPNext's running totals per item per warehouse (actual, ordered, reserved...).
  • Valuation method: How stock is priced: FIFO or Moving Average.

Character Mapping

  • Material Request: The storekeeper's note to the purchase officer.
  • Purchase Order: The purchase officer's order to the cotton supplier.
  • Quality Inspection: The lab technician with the micronaire tester at the gate.
  • Purchase Receipt: The gate entry that puts bales into the store.
  • Batch: The lot tag stapled to every bale.
  • Bin: The storekeeper's tally board on the wall.

1. Procure to Pay, Step by Step

Step 1: Material Request. Stores (or a reorder rule, below) raise a request for raw cotton. Requested Qty goes up.

Step 2: Purchase Order. Purchasing turns the request into a PO for a supplier. Ordered Qty goes up, Requested Qty comes down. (For bigger buys you can add Request for Quotation and Supplier Quotation in between.)

Step 3: Quality Inspection. The lab tests the arriving lot. If inspection is required, the receipt can't be submitted until it's done.

Step 4: Purchase Receipt. The bales enter the warehouse. Actual Qty goes up, a Stock Ledger Entry is written, and each lot is recorded as a Batch.

Step 5: Purchase Invoice and Payment. The supplier's bill posts to the GL (Creditors up), and the Payment Entry settles it.

Automatic Reorder

On each Item you can set a reorder level and reorder quantity per warehouse. Turn on automatic Material Requests in Stock Settings, and ERPNext raises an MR for every item that falls below its level, then emails users with the Purchase Manager role. For a mill that consumes cotton around the clock, this kept us from ever discovering an empty store on a Monday morning.


2. Cotton Lots as Batches, with a Quality Gate

Each cotton lot is a Batch; Quality Inspection sends it to the store or to rejection

Batches

Tick Has Batch No on the raw cotton Item, and every receipt must say which batch (lot) each row belongs to. We added custom fields on Batch for the fibre properties the mill cares about: micronaire, staple_length, trash_percent and moisture.

Those fields are what the Mixing Plan from Episode 6 fetches with fetch_from. The blend recipe always uses the lot's real, inspected numbers.

Quality Inspection

Enable quality inspection on the Item, and attach a Quality Inspection Template with the parameters and limits:

ParameterTypeAcceptance
MicronaireNumericMinimum 3.5 / Maximum 4.9
Moisture %NumericMaximum 8.0
Trash %NumericMaximum 3.0
Colour gradeNon-numericMust equal "Middling"

Readings outside the range are Rejected automatically on save. You can also use formula-based criteria, or mark a row Manual Inspection to decide yourself. Accepted bales go to the Raw Cotton Store. Rejected ones go to a rejected warehouse and back to the supplier.


3. Warehouses, the Bin, and Projected Qty

We modelled the mill floor as a warehouse tree: Raw Cotton Store โ†’ Blowroom (WIP) โ†’ Finished Yarn โ†’ Rejected. Stock moves between them with Stock Entries.

For every item in every warehouse, ERPNext keeps a Bin with running totals, so it never has to add up the whole ledger just to answer "how much do we have?". The most useful number is Projected Qty:

Projected Qty = Actual + Planned + Requested + Ordered โˆ’ Reserved โˆ’ Reserved for Production โˆ’ Reserved for Subcontracting โˆ’ Reserved for Production Plan

In plain words: what we have, plus what's coming, minus what's already promised. Reorder decisions should look at projected qty, not just actual.

From bench --site mill.localhost console:

python
frappe.db.get_value(
    "Bin",
    {"item_code": "RAW-COTTON-BR", "warehouse": "Raw Cotton Store - SML"},
    ["actual_qty", "ordered_qty", "reserved_qty", "projected_qty"],
    as_dict=True,
)

# Stock of one lot (batch) in a warehouse
from erpnext.stock.doctype.batch.batch import get_batch_qty
get_batch_qty(batch_no="LOT-BR-0007", warehouse="Raw Cotton Store - SML")

4. FIFO vs Moving Average

FIFO vs Moving Average on the same purchases and sale

Cotton prices swing with every season, so how stock is valued matters for the mill's profit numbers. ERPNext supports two methods, set per Item (the default comes from Stock Settings). Here's the example from Frappe's own blog post:

Buy 10 units at $12, then 5 units at $15, then sell 12.

  • FIFO (First In, First Out): The oldest stock leaves first. The 12 sold are 10 ร— $12 plus 2 ร— $15. What's left: 3 ร— $15 = $45.
  • Moving Average: Every receipt re-blends the rate: (10 ร— 12 + 5 ร— 15) รท 15 = $13. What's left: 3 ร— $13 = $39.
FIFOMoving Average
Stock value when prices riseHigher (recent, pricier stock remains)Lower (blended)
Matches physical flowYes, for perishable or lot-based goodsNot exactly
Negative stock allowedNoYes (value treated as zero until positive)
SimplicityTracks layers ("queues")One rate per item

One more thing the blog points out: back-dated entries force ERPNext to recalculate the valuation of every later transaction. On a busy mill with thousands of stock movements, that repost can take a while. It's a good reason to keep stock entries on time.


๐Ÿ• Explain It Like You're 5

You collect marbles in a jar. When you run low, you tell Dad (Material Request), Dad orders more from the shop (Purchase Order), and before you drop them in the jar you check each one isn't cracked (Quality Inspection).

Every bag gets a sticker saying which shop it came from (Batch). And your little notebook always says how many marbles you have, how many are coming, and how many you promised your friend (Projected Qty)!


๐ŸŽฏ Debugging Cheat Sheet

SymptomLikely causeWhere to look
Can't submit Purchase ReceiptQuality Inspection required but missingItem's inspection setting; create the QI
"Batch No is mandatory"Item has Has Batch No onPick or create the lot's Batch
No automatic Material RequestsReorder levels missing, or auto MR offItem reorder table; Stock Settings
Stock balance looks wrongDraft (unsubmitted) receipts or entriesStock Ledger by voucher, docstatus
Valuation changed after an old entryBack-dated entry triggered a repostRepost Item Valuation list
"Insufficient stock" on a Stock EntryProjected looks fine but actual is shortBin actual_qty vs projected_qty
Mixing Plan fetches empty micronaireCustom fields on Batch not filledBatch form; Quality Inspection readings

Key Takeaways for Interviews

  1. Procure to Pay: MR โ†’ PO โ†’ (QI) โ†’ PR โ†’ PI โ†’ Payment. Each step moves a different quantity (requested, ordered, actual).
  2. Batches track lot-level properties, and custom fields on Batch let manufacturing use real quality data.
  3. Quality Inspection templates enforce acceptance limits at receipt, before bad material enters the store.
  4. Projected Qty = have + coming โˆ’ promised. Reorder on projected, not actual.
  5. FIFO vs Moving Average gives different values from the same transactions, and back-dated entries trigger reposting.

๐Ÿ“š Official Sources

Everything here is checked against Frappe's own documentation and blog:

Next episode, we go back inside the code: the document lifecycle, covering validate, before_submit, on_submit, on_cancel and friends, and exactly when each one runs.

Enjoyed this article? Share it: