Invoice Generator
Create invoices with line items and compute totals.
Example 084 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell Invoice has no face and no memory. Handlers: demo (line 20).
The source
084_invoice.cell
// ============================================================================
// 084 — Invoice Generator
// ============================================================================
// Create invoices with line items and compute totals.
//
// Run: soma run 084_invoice.cell demo
// Expect:
// === INVOICE ===
// Customer: Acme Corp
// Items:
// Widget x10 @ $9.99 = $99.90
// Gadget x5 @ $19.99 = $99.95
// Service x1 @ $150.00 = $150.00
// Subtotal: $349.85
// Tax (10%): $34.985
// Total: $384.835
cell Invoice {
on demo() {
let customer = "Acme Corp"
let items = list(
map("name", "Widget", "qty", 10, "price", 9.99),
map("name", "Gadget", "qty", 5, "price", 19.99),
map("name", "Service", "qty", 1, "price", 150.00)
)
let tax_rate = 0.10
print("=== INVOICE ===")
print("Customer: {customer}")
print("Items:")
let subtotal = 0.0
for item in items {
let line_total = to_float(item.qty) * item.price
subtotal = subtotal + line_total
print(" {item.name} x{item.qty} @ ${item.price} = ${line_total}")
}
let tax = subtotal * tax_rate
let total = subtotal + tax
print("Subtotal: ${subtotal}")
print("Tax (10%): ${tax}")
print("Total: ${total}")
return "done"
}
}What the toolchain says
$ soma check 084_invoice.cell
✓ All checks passed.$ soma run 084_invoice.cell demo
=== INVOICE ===
Customer: Acme Corp
Items:
Widget x10 @ $9.99 = $99.9
Gadget x5 @ $19.99 = $99.94999999999999
Service x1 @ $150.0 = $150.0
Subtotal: $349.85
Tax (10%): $34.98500000000001
Total: $384.83500000000004
done