retail.md 3.5 KB

Retail point of sale

A supermarket tracks inventory as product assets, records sales with COGS, and manages cash and bank accounts.

Setup:

// Assets
let gs = AssetId::new(1);            // Guaranies (currency)
let product_a = AssetId::new(100);   // Product: rice 1kg
let product_b = AssetId::new(101);   // Product: 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, product_a, Cent::from(50_000)) // 50.000 units (precision 3)
    .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, product_a, Cent::from(2_000))
    // Customer pays cash
    .pay(customer.id, cash_register.id, gs, Cent::from(30_000))
    // Record revenue
    .pay(world, revenue.id, gs, Cent::from(30_000))
    // Record COGS (cost was 10,000 Gs per unit)
    .pay(world, cogs.id, gs, Cent::from(20_000))
    .build();
ledger.commit(sale).await?;

Deposit cash to bank:

let deposit = TransferBuilder::new()
    .book(banking_book.id)
    .pay(cash_register.id, bank.id, gs, Cent::from(30_000))
    .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 accidentally credits the revenue account. The banking book ensures only cash and bank accounts participate in deposits. Each flow is isolated by scope while sharing the same global balances.