Stock scanner (P18)
Tally is a stock app for a warehouse with 10,000 items in 60 locations. A phone scans a barcode with its camera, records what happened to the item, and keeps working with no network.
The catalog on the phone
The phone downloads the catalog once and keeps it in IndexedDB, so a scan or a search finds its item with no network. A search by name looks through every item on the phone. A service worker keeps the app itself, so it opens with no network.
Reading a barcode
The camera's frames go to the browser's BarcodeDetector where the browser has one. Where it does not, Tally reads the frames with its own EAN-13 reader (prototypes/source/p18/ean13.mjs). It takes rows of pixels across the middle of a frame, finds the guard bars, matches each digit's two bars and two spaces against the three sets of patterns, and checks the last digit. A code counts when two frames read the same code. The Labels page draws the first twelve items' barcodes from the same module.
Changes that wait for the network
A count, a delivery, a pick or a move is an operation with an id made on the phone and the time it was made. It is stored on the phone first and sent when the network is there. The server stores each id once, so an operation sent twice counts once.
The server works out the stock of an item at a location by replaying its operations in the order they were made. A count sets the level, and a delivery, a pick or a move changes it. A count made offline and sent an hour later is placed at the time it was made, and a pick made after it on another phone still counts. At every sync the phone sends its clock, and the server corrects the times of the phone's operations by the difference.
The report
The report shows each count beside the stock the server expected at the moment the count was made, the difference, and how long after it was made the count arrived. It also lists shelves below zero and each phone with its last sync and its clock against the server's.
Previous: Ordering for a café (P17). Next: Course player (P21).