GeoTech

Engineering / Case study

Catch-weight inventory and weighted-average cost for butcher shops

GeoTech Engineering · · 8 min read

POSInventoryFlutterAccounting

A butcher's stock does not come in units. A carton of frozen beef says 19,06 kg on the label, the next one from the same delivery says something else, and the price per kilo changes every week. Most point-of-sale systems quietly turn that into wrong numbers. This is how we built the meat edition of our offline POS (a fork of the ELKASHIER desktop app) so that every kilo and every piastre can be traced back to a label and an invoice.

TL;DR

The problem

Three things make meat different. Catch weight: cartons of the same cut all weigh something different, and the supplier invoices the exact total. Labels nobody standardised: some importers print GS1-128 barcodes with a net-weight field; many factories print a fixed-length digit string with the weight at an undocumented offset. Cost that moves: this week's delivery and last week's are in the fridge at the same time, at different prices.

Why the usual shortcuts fail

Average weight times count is a number nobody can reproduce from the labels in the fridge; the error stays invisible until a stocktake.

Floating-point kilos. Weight in a double is the same mistake as money in one. We store weight as integer grams and money as integer piastres everywhere, including the receiving tables, even though the original specification suggested DECIMAL(12,3).

Last price paid is easy to code and wrong by design: part of the fridge was bought at last week's price, so margins and stock value swing with every delivery.

A parser per supplier does not scale. There is always another factory, and a shop with a truck at the door cannot wait for a developer.

Receiving: one carton at a time

Each scanned carton becomes one line: weight in grams, item, raw barcode. The delivery's totals are derived from the lines, and a saved delivery is immutable, like a sales invoice. One rule is non-negotiable: the item is settled while the carton is in the operator's hands. Price can wait for the supplier's invoice; identity cannot, because nobody can tell two cartons apart on the shelf.

Reading GS1-128

GS1 labels are a run of Application Identifiers (AIs); net weight in kilograms is AI 310n (n = decimals) plus six digits. Two details caused real failures:

Learning a layout from one carton

Non-GS1 cartons still print their net weight in large type. When the app meets a barcode it cannot read, it asks the operator to type that weight and searches the digit string for it:

// "19,06" is searched as "1906" with exactly 2 decimals: the decimals are
// taken as typed, never guessed
var offsets = occurrences(raw, digits);
// only if nothing matched: a fixed-width field may be zero-padded ("0950")
for (var pad = 1; offsets.isEmpty && pad <= 2; pad++) { ... }
// a rule that cannot reproduce the carton it was learned from is discarded
candidates.where((r) => r.readGrams(raw) == typedGrams)

Taking decimals exactly as typed is the correctness argument: trying three decimals, "19060" might match on one label only because the next character happens to be zero, and the next carton would be read at the wrong scale. If the weight digits appear at more than one offset, the app asks for a second carton of a different weight and keeps only the layout that explains both; still ambiguous, it asks again rather than guess.

A real defect: the empty string that was not null

A supplier rule with a field length of zero used to return an empty string instead of "no field". An empty string is not null, so it slipped past every fallback and became a grouping key shared by every carton from that supplier: two different cuts overwrote each other's name, and the shop scanned one thing and was told it was another. Zero length now means "no field", pinned in a test.

Weighted-average cost, stamped on every movement

Each product carries an average cost (per kilogram for weighed items), and each stock movement carries its own value. Receiving a delivery re-averages in the same database transaction as the movement:

new average = (value on hand + invoice value of what arrived)
              / (grams on hand + grams that arrived)

The value of what arrived is the supplier invoice's own figure, not a recomputation. A worked example: 40 kg on hand at 400 EGP/kg is worth 16,000 EGP; a delivery of 60 kg invoiced at 25,800 EGP brings 100 kg worth 41,800 EGP, so the new average is 418 EGP/kg. Valuing the fridge at the last price (430 EGP/kg) would overstate it by 1,200 EGP. (Illustrative figures, not shop data.)

MovementQuantityValueAverage changes?
Delivery+ grams+ invoice amountYes, re-averaged
Sale− grams− grams × average at that momentNo
Void of a sale+ grams+ the exact value the sale took outNo
Wastage / write-off− grams− grams × current averageNo
Stocktake difference± grams± grams × current averageNo
Manual revaluation0± the change in valueSet by owner

A sale stamps its cost because the average moves with the next delivery; a margin recomputed next week is not the margin this sale made. A void reverses at the value the sale recorded, not today's average, or a delivery in between would make cancelling an invoice create or destroy money. A revaluation covers the fridge that was full on installation day, with no invoice behind it: the owner states the cost and the difference is written as a zero-quantity movement, so stock value never changes without a ledger row. Items with no cost yet are named on screen instead of being valued at zero.

Why wastage is not an expense

The books are cash-basis: paying the supplier is the expense, so the money for spoiled meat left long ago. What the shop loses is the sale it never makes, and profit already shows that by being lower. Expensing wastage would charge the same money twice. It is recorded as a stock movement and reported separately, in kilos and money, but never enters the expense ledger.

Selling a carton by scanning it

The till does not decode a whole carton's label again; it finds the carton that was received by its barcode digits, so any received label sells. The 1D barcode and 2D code on one box match as one carton (digit suffixes of at least 20 digits). A sold carton records its invoice; a second scan is refused with that number, and a void releases it.

Measured result

The rules above are pinned in automated tests, most of them against an in-memory database built from the real schema. We ran the seven suites that cover barcode parsing, layout learning, receiving, stock value, cost of sales, stocktake and selling by carton on 22 November 2026: 112 tests, 111 passing.

Checklist for weighed stock

  1. Store weight as integer grams and money as integer minor units. No floats anywhere.
  2. One row per physical item. Derive counts; never let anyone type them.
  3. Read GS1 properly: four-character AIs before two-character ones, and an unknown AI must not kill the whole label.
  4. For non-standard labels, learn the layout from a printed weight, take decimals exactly as typed, and refuse ambiguous answers.
  5. Value stock at weighted-average cost, re-averaged in the same transaction as the delivery.
  6. Stamp the cost on every movement. Reverse at the stamped value, never at today's average.
  7. In cash-basis books, report wastage; do not expense it.

Need this?

The meat edition of the ELKASHIER POS runs fully offline on a Windows PC, with catch-weight receiving, learned supplier barcodes, weighted-average costing and supplier accounts. If you run a butcher shop or a meat distributor, or need weighed-stock costing in your own system, talk to GeoTech.