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
- One row per carton, never weight times count. Each carton is stored with its own weight in integer grams.
- Barcodes are read, or learned. GS1-128 labels are parsed; for undocumented layouts the operator types the weight printed on one carton and the app finds where it sits in the barcode.
- Stock is valued at weighted-average cost, not the last price paid. Every movement stores its own value when it happens, so a void reverses at the cost of the day it was sold.
- Wastage is reported, not expensed. Booking it as an expense in cash-basis books would charge the same money twice.
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:
- Four-character AIs must be tried before two-character ones, or
3102is read as AI31and every field after it shifts. - One unknown AI made the whole label unreadable. Meat labels carry plant approval numbers, countries of origin and slaughter, and batch fields, so we added them.
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.)
| Movement | Quantity | Value | Average changes? |
|---|---|---|---|
| Delivery | + grams | + invoice amount | Yes, re-averaged |
| Sale | − grams | − grams × average at that moment | No |
| Void of a sale | + grams | + the exact value the sale took out | No |
| Wastage / write-off | − grams | − grams × current average | No |
| Stocktake difference | ± grams | ± grams × current average | No |
| Manual revaluation | 0 | ± the change in value | Set 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.
- The one failure: the "same carton scanned twice" test still expects the old wording of the error message, rewritten when the app's texts moved to formal Arabic. The behaviour it guards (the line is not doubled) passes; the text assertion needs updating.
- The layout learner is checked against two real factory labels, one of which the original specification had given up on as unreadable, and it derives the same layout that the specification documents by hand for the other.
Checklist for weighed stock
- Store weight as integer grams and money as integer minor units. No floats anywhere.
- One row per physical item. Derive counts; never let anyone type them.
- Read GS1 properly: four-character AIs before two-character ones, and an unknown AI must not kill the whole label.
- For non-standard labels, learn the layout from a printed weight, take decimals exactly as typed, and refuse ambiguous answers.
- Value stock at weighted-average cost, re-averaged in the same transaction as the delivery.
- Stamp the cost on every movement. Reverse at the stamped value, never at today's average.
- 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.