When a business opens a second branch, inventory code that worked fine for one shop starts producing strange numbers. Sales look higher than they are, purchases look higher than they are, and stock "goes missing" at one location while the other has extra.
Almost always the cause is the same: goods moved between locations were recorded as a sale at one place and a purchase at the other. Here is a small, boring model that avoids that, from building inventory for RBTERP, our accounting software for Pakistani businesses.
Rule 1: stock lives at a location, never in a global total
The current quantity of an item is not a column on the item. It is the sum of movements at a location:
CREATE TABLE stock_movement ( id INTEGER PRIMARY KEY, item_id INTEGER NOT NULL REFERENCES item(id), location_id INTEGER NOT NULL REFERENCES location(id), qty NUMERIC NOT NULL, -- + in, - out unit_cost NUMERIC NOT NULL, -- cost carried by these units kind TEXT NOT NULL, -- 'purchase','sale','transfer_out','transfer_in','adjustment' transfer_id INTEGER REFERENCES transfer(id), moved_at TIMESTAMP NOT NULL ); On-hand stock and its value per location are then a query, not a field you have to keep in sync:
SELECT location_id, SUM(qty) AS on_hand, SUM(qty * unit_cost) AS value_at_cost FROM stock_movement WHERE item_id = :item GROUP BY location_id; A branch, a godown and a delivery van are all just locations. If you keep one global quantity per item, a shortage at one branch hides behind extra stock at another.
Rule 2: a transfer is two movements in one transaction
A transfer is not a sale and not a purchase. It is one header row plus two movement rows that cancel out for the business as a whole:
BEGIN; INSERT INTO transfer (id, from_location, to_location, note_no, created_at) VALUES (:t, :from, :to, :note, now()); INSERT INTO stock_movement (item_id, location_id, qty, unit_cost, kind, transfer_id, moved_at) VALUES (:item, :from, -:qty, :cost, 'transfer_out', :t, now()), (:item, :to, +:qty, :cost, 'transfer_in', :t, now()); COMMIT; Three things matter here:
- Both rows or neither. One database transaction, so you never have goods that left but never arrived.
- Same unit cost on both sides. The goods arrive carrying the cost they had when they left. Total stock value of the business does not change; only its place does.
- No revenue, no purchase. Reports for sales and purchases filter on
kind, so transfers never inflate them.
A quick invariant to test: for any transfer, SUM(qty) and SUM(qty * unit_cost) across its movements must both be zero.
Rule 3: refuse negative stock per location
Check availability at the source location, not the business total, inside the same transaction:
SELECT COALESCE(SUM(qty), 0) FROM stock_movement WHERE item_id = :item AND location_id = :from; -- if this is less than :qty, roll back This single check catches the most common real-world mistake: someone recording goods at the branch before the transfer from the main godown has been entered.
Rule 4: goods in transit, if trips take days
If trucks take a day or two between cities, add an in-transit location. The transfer becomes from -> transit when dispatched and transit -> to when the branch counts and signs for it. Any difference on arrival becomes an explicit adjustment with a reason, raised the same day.
Why it matters to the business
In a fictional example from our blog, a crockery trader in Lahore thought Rs 2.3 lakh of stock was missing at a new branch. It turned out to be three transfers that were sent but never recorded. With transfers modelled as above, that number would have been visible on day one.
The business side of the same problem (one item list, daily branch closing, profit per branch) is written up here: Running more than one branch? How to see every branch's stock and sales in one place.
How do you handle transfers in your own inventory code: two movements, or a single row with from/to columns?
