Example: Inventory Tracker
A complete warehouse tracker on web pages: items, locations, an append-only movement ledger, a live stock table and low-stock alerts, where the ledger is always right.
Three storages, one of them append-only
- items: sku, name, unit, reorder_level.
- locations: warehouse, zone, bin.
- movements: one append-only document per change.
{
"_meta": {
"cms": 1782166695055,
"created": 1782166695,
"entity": 1,
"expiry": -1,
"guid": "08193b4a5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708",
"removed": 0,
"ums": 1782166695092,
"update_hash": "5e0d38c7a1f294b6037ce8a51d9f27b046a3c81e",
"updated": 1782166695
},
"actor_guid": "b9ee6a631b24e78d1aa48aef0dc067fff7bfbacd24a56ef4ed1f08e537d48b6bd02",
"item_name": "essential cotton t-shirt",
"location": "wh-1/zone-a/bin-04",
"note": "po-10482",
"quantity": 24,
"sku": "TSHIRT-BLK-M",
"type": "receive"
}Movement types are receive, ship, adjust and transfer (two linked movements: out and in). Never update a movement: corrections are new adjust movements. It is the activity-log principle from Storage Examples.
Current stock can be read two ways
Ledger sum (exact)
Aggregate movements per sku and location when you need the precise figure.
Counter document (fast)
A stock storage with one document per sku and location. Every movement flow also applies mutations_increment_by_field (increment by field) through function_upsert_document_from_mutations (upsert document), with sku and location as guid_seed. Increments are applied sequentially per document (see mutations). Lists and alerts read only counters.
Audit job: a daily schedule_repeating_event (repeating event) recomputes the ledger sum against the counter and logs drift. You will catch a missed movement within a day, not at year-end.
The stock page updates one row at a time
web_page (new web page) with event stock and a custom scope warehouse. Search the stock storage with function_search (search), wire array into web_foreach (web for each) with the row template, and print it through web_render (web render). Each counter document carries what its row shows: sku, item_name, location, reorder_level and on_hand.
<tr class="{{if reorder_level > on_hand}}low{{/if}}">
<td>{{sku}}</td>
<td>{{item_name | capitalize}}</td>
<td>{{location}}</td>
<td>{{on_hand | number}}</td>
</tr><main>
<h1>{{title}}</h1>
<form data-action="receive">
<label>SKU <input name="sku" required></label>
<label>Location <input name="location" required></label>
<label>Quantity <input name="quantity" type="number" min="1" required></label>
<label>Purchase order <input name="source" required></label>
<div data-error-for="_form"></div>
<button type="submit" data-loading-text="Saving">Receive</button>
</form>
<table>
<tbody id="stock-rows">{{{rows}}}</tbody>
</table>
</main>Each movement is one flow
Receive is idempotent
Write the movement
web_action(on web action)receive, thenfunction_create_document_from_mutations(create document) with the source reference and sku (po-10482plus the sku) asguid_seed.Skip a repeat
Re-receiving the same reference makes the block answer
successfalse withDOCUMENT_GUID_ALREADY_EXISTS.branch(branch) on it: answer withweb_errors(web form errors) and leave the counter alone.Move the counter
function_upsert_document_from_mutationswithmutations_increment_by_fieldonon_hand.Update the page
web_template(web template) with{{> stock-row}}, the updatedobjectasdataand its guid askey.branchon the upsert'screatedpin: a new row goes in withweb_insert(web insert) on#stock-rows, an existing one is swapped withweb_replace(web replace).
Ship is guarded by a lock
"Only if enough is left" is a read, a decision and a write, so it needs a lock around the sequence (see Locking and Concurrency).
Claim
function_claim_lock_wait(lock or wait) withauto_clearon, keyed on the sku and location.Check
function_get_document(get document) reads the counter by guid;condition_gte(>=) compareson_handwith the quantity.Write
Enough: create the
shipmovement and apply a negative increment. Not enough: hold it as abackordermovement instead of going negative.
Transfer and count check
- Transfer: one flow writes the out-movement and in-movement as two linked documents sharing a transfer guid; the counters move in the same flow.
- Count check: an
adjustmovement with the counted quantity difference. The counter follows the same path as everything else.
Low-stock alerts fire on the crossing
Compare
After every counter change, compare against the item's
reorder_level.Alert once
Crossing the threshold, and only then, creates an alert document and emails the buyer via the email pattern. Track the previous state on the counter to avoid alert spam.
Close it
An alert page lists open alerts sorted by severity. A button runs a
web_actionwith a status mutation once the receive lands, andweb_remove(web remove) takes the row away.
Next steps
E-Commerce Catalog Sell through this stock. Storage Examples The ledger and counter models in full. Repeating Events The daily drift-check scheduler. Actions Replacing, inserting and removing rows from a flow.Frequently asked
How do I track current stock in RUAL?
Two ways: aggregate the movement documents per sku and location when you need the exact figure, or keep a counter document per sku and location that every movement flow updates with mutations_increment_by_field. Lists and alerts read only the counters, and a daily repeating event recomputes the ledger sum against the counter to catch drift.
How do I correct a wrong stock movement in RUAL?
Never update a movement: corrections are new adjust movements. The movements storage is append-only, so the ledger stays a complete, ordered history.
How do I build low-stock alerts in RUAL?
After every counter change, compare against the item's reorder_level, and only when the threshold is crossed create an alert document and email the buyer. Track the previous state on the counter so a stock level that stays low does not spam alerts.