Every dollar accounted for

How an append-only double-entry platform accounts for late marketplace settlements, FIFO inventory, international goods flows, and Xero reconciliation.

role model, ingestion, inventory accounting, Xero integration stack PostgreSQL ledger · Python · React/TypeScript (China-ops module) scale 1,000+ authored commits; ~83k lines of code

The situation#

The business sells across a storefront, Amazon, several eBay accounts, Walmart, and Newegg, and moves physical goods from suppliers in China through a domestic warehouse and into FBA. Each of those channels reports money and movement on its own schedule, in its own format, with its own idea of what a “date” means. For years the financial picture was assembled by hand from exports and spreadsheets - slow, error-prone, and impossible to reconcile with confidence.

The business needed to know, at any moment, what it actually made and what its inventory was actually worth. Not an estimate assembled weeks later - a number an operator could act on and an accountant could defend.

The constraint#

The hard part is that commerce systems disagree about time and identity. An order, a payment, a fee, a return, a settlement, a shipment, and an accounting date can all arrive separately, out of order, days apart. Financial correctness cannot depend on pretending those events are simultaneous - and it cannot pretend a day is finished just because some of its data showed up.

Physical inventory makes it harder. The same goods can be on order from a supplier, in transit internationally, received at a warehouse, transferred to FBA, or sold as one of several pack SKUs - sometimes direct-shipped before the supplier’s final cost is even known, sometimes returned after revenue and fees were already posted. The constraint: the books must reflect reality even when the data arrives late, incomplete, and out of order.

Technical details

Architecture#

The pipeline follows a clear progression, with modules for marketplace ledger activity, inventory, advertising, postage, and China operations sharing accounting primitives:

  1. Ingest raw marketplace, payment, and inventory events.
  2. Classify them into business meanings.
  3. Post balanced entries to a shared ledger.
  4. Aggregate according to accounting and reporting policy.
  5. Synchronize approved aggregates to Xero.
  6. Reconcile internal and external results.

Core accounting rules#

  • Every posting balances. Debits and credits balance at the transaction boundary; validation runs before any external synchronization.
  • Posted history is append-only. Corrections create new entries. They do not silently mutate financial history.
  • Incomplete channel days remain provisional. A marketplace day is not final merely because order data exists. Channel-specific classification frontiers track how far complete source evidence has progressed.
  • External synchronization has its own frontier. The combined Xero frontier advances only when all required channel and accounting inputs are complete - one fast channel cannot make an incomplete business day look final.
  • Inventory value follows physical units. FIFO layers preserve landed cost; movement between goods-in-transit, warehouse, FBA, and COGS follows physical state and an explicit mapping from listing SKUs to component SKUs.

The settlement frontier#

The timeline below illustrates the core principle: accounting advances only to the date through which every required source is complete - and no further.

Mar 11Mar 12Mar 13Mar 14Mar 15Marketplace AMarketplace BLedgerXero syncsale + feelate fee arrivessettlement closes daysettlement closes daypost Mar 11aggregate postedlate adjustmentcorrection entry (new)posted through Mar 10posts Mar 11A business day is final only when every required channel has complete evidence through it.
The ledger posts March 11 only on the day both marketplaces’ evidence closes through it; late adjustments append new correction entries rather than rewriting posted history.

Inventory as accounting#

Consider an illustrative inventory batch: 100 units at $4 landed cost and 80 at $5.

Supplier obligation $400 In transit 100u @ $4.00 Warehouse layers: 100@4, 80@5 FBA location, not ownership Sale: 3-pack consumes 3 units COGS oldest layer first: $12.00FIFO inventory: value follows physical units through locationsA listing sale maps pack SKU → component quantity; COGS consumes the oldest landed-cost layer.
Inventory value follows physical units: location changes do not alter asset ownership, and a multi-pack sale consumes component units from the oldest cost layer.

Edge cases the model answers#

  • Late marketplace settlement and reopened periods.
  • Write-off followed months later by debt recovery.
  • Direct shipment before final supplier cost is known.
  • Listing-SKU to component-SKU mapping; packs consuming multiple physical units.
  • Returns whose financial and physical effects arrive separately.
  • Advertising and postage costs arriving from distinct sources.
  • Xero date immutability after synchronization.

My role#

I designed and built the shared accounting model, channel ingestion and classification paths, inventory accounting, Xero integration, reconciliation behavior, and supporting operational interfaces.

The retrospective#

What held up: the append-only rule and the frontier model. Once history could not be silently rewritten, every reconciliation became answerable, and once days were allowed to be provisional, late data stopped being an emergency and became a normal input. What I underestimated: how many edge cases live in “money that arrives out of order.” Returns, write-offs, direct shipments before final cost - each needed its own explicit path, and the system got more trustworthy every time one was added.

The part I did not fully anticipate was what this foundation would make possible. Because every dollar is allocated to the item and order it belongs to, the pricing system described in the next story can reason over actual costs rather than estimates. I built this to make the books trustworthy; it turned out to be the thing that made trustworthy autonomous pricing possible at all.