retail.md 3.7 KB

Retail point of sale

A supermarket tracks inventory as product assets. It records a sale with the cost of goods sold, which this chapter calls COGS. It also manages cash accounts and bank accounts.

Setup:

// Assets, named by code. A product's `decimals` is what lets a quantity be
// written as "50.000" rather than counted in thousandths at the call site.
let gs = ledger.asset("GS", "\u{20b2}", 0).await?;          // Guaranies, no minor unit
let product_a = ledger.asset("RICE", "kg", 3).await?;    // rice 1kg, to the gram
let product_b = ledger.asset("OIL", "L", 3).await?;      // cooking oil 1L

// Account flags
const WAREHOUSE: AccountFlags = AccountFlags::USER_0;
const CUSTOMER: AccountFlags = AccountFlags::USER_1;
const REVENUE: AccountFlags = AccountFlags::USER_2;
const BANK: AccountFlags = AccountFlags::USER_3;

// Books
let sales_book = BookBuilder::new("sales")
    .allow_asset(&gs)
    .allow_asset(&product_a)
    .allow_asset(&product_b)
    .allow_flags(WAREHOUSE | CUSTOMER | REVENUE)
    .build();

let inventory_book = BookBuilder::new("inventory")
    .allow_asset(&product_a)
    .allow_asset(&product_b)
    .allow_flags(WAREHOUSE)
    .allow_account(world) // issuance source
    .build();

let banking_book = BookBuilder::new("banking")
    .allow_asset(&gs)
    .allow_flags(WAREHOUSE | BANK)
    .build();

// Accounts — start from a constructor, then set flags where needed.
// issuance source: mints product tokens on receipt (overdraft allowed)
let world = Account::new(AccountId::default());

let mut warehouse = Account::debit_must_not_exceed_credit(AccountId::default());
warehouse.flags |= WAREHOUSE;

let mut cash_register = Account::debit_must_not_exceed_credit(AccountId::default());
cash_register.flags |= WAREHOUSE;

let mut revenue = Account::new(AccountId::default());
revenue.flags = REVENUE;

let mut cogs = Account::new(AccountId::default()); // cost of goods sold
cogs.flags = REVENUE;

let mut bank = Account::debit_must_not_exceed_credit(AccountId::default());
bank.flags |= BANK;

Receive inventory from supplier (50 units of rice):

let receipt = TransferBuilder::new()
    .book(inventory_book.id)
    .pay(world, warehouse.id, ("50.000", &product_a))
    .build()?;
ledger.commit(receipt).await?;
// Warehouse: +50.000 rice
// World: -50.000 rice (offset: issued into the ledger)

Cash sale, customer buys 2 rice at 15,000 Gs each:

let sale = TransferBuilder::new()
    .book(sales_book.id)
    // Move product from warehouse to customer (consumed by sale)
    .pay(warehouse.id, customer.id, ("2.000", &product_a))
    // Customer pays cash
    .pay(customer.id, cash_register.id, ("30000", &gs))
    // Record revenue
    .pay(world, revenue.id, ("30000", &gs))
    // Record COGS (cost was 10,000 Gs per unit)
    .pay(world, cogs.id, ("20000", &gs))
    .build()?;
ledger.commit(sale).await?;

Deposit cash to bank:

let deposit = TransferBuilder::new()
    .book(banking_book.id)
    .pay(cash_register.id, bank.id, ("30000", &gs))
    .build()?;
ledger.commit(deposit).await?;

Query balances:

let warehouse_rice = ledger.balance(&warehouse.id, &product_a).await?;
// 48.000 units remaining

let bank_balance = ledger.balance(&bank.id, &gs).await?;
// 30,000 Gs

let total_revenue = ledger.balance(&revenue.id, &gs).await?;
// 30,000 Gs

let total_cogs = ledger.balance(&cogs.id, &gs).await?;
// 20,000 Gs gross profit = revenue - cogs = 10,000 Gs

Why books matter here: The sales book prevents a bug where a bank transfer credits the revenue account by accident. The banking book makes sure that only a cash account or a bank account participates in a deposit. Scope isolates each flow, and every flow shares the same global balances.