The problem
A distributor’s stock changes all day: orders come in, a van leaves, a return arrives, someone corrects a count. If each of these writes to the stock figure on its own, sooner or later two of them collide, and the figure on the screen no longer matches the shelf.
One writer
In Scoperly every change to on-hand stock goes through one service: a restock, an accepted order, a cancellation, a transfer between warehouses, a sale on the spot. The product’s total is not a separate number that can drift. It equals the sum of its warehouses, and the service keeps it that way.
Reserved is not gone
An order that has been placed does not take goods off the shelf yet. It reserves them, so that nobody else can promise the same box:
- Order placed: reserved goes up.
- Order accepted: on-hand and reserved go down together.
- Order rejected, or cancelled before it is accepted: reserved goes back down.
- Order cancelled after it is accepted: the goods return to on-hand.
Locks where it matters
At checkout the rows of the products in the order stay locked until the transaction ends. Two people buying the last box do not both succeed: the second one gets a clear “not enough stock”, not a promise the warehouse cannot keep.
Checking the sums
These rules are also written down as checks: the total equals the sum of the warehouses, reservations match open orders. We run them against the data before big releases, because a rule nobody checks slowly stops being true.