Use Cases and How Models Interact
Below, each use case shows:
- which models/tables are touched
- the ledger entries inserted
- how balances/holds/lots change
1.0 Account Provisioning (Billing Account created when Company is created)
Scenario
A new Org::Company is created (e.g. via team onboarding). The billing system must be ready before any commercial activity can happen.
What happens automatically
When Org::Companies::Shared::CreateService creates a company, it calls Billing::Accounts::Shared::CreateService in the same transaction:
-
Create
billing_accountsrow:org_company_id= new companystatus = activeuuid= generated
-
Seed
billing_entitlement_balances— one row perBilling::Entitlement(e.g.placement_credit,gig_credit_cents):units_available = 0units_reserved = 0
Tables touched
billing_accounts(insert)billing_entitlement_balances(insert × N entitlement types)
Invariants
- Exactly one
billing_accountperOrg::Company(enforced by unique index onorg_company_id). - Balance rows are seeded with zeros — no entitlements exist until a grant (via invoice posting).
- No ledger entries are created at this stage.
Result
The company now has a billing account and zero-balance entitlement rows. Sales can proceed to create an agreement.
2.0 Service Agreement Creation (Sales creates a Billing Agreement with commercial terms)
Scenario
Before invoicing a company, the sales/business team establishes a service agreement that captures the negotiated commercial terms. For example:
- Gig platform fee rate of 20% (2000 bps)
- Placement credit unit price of $5.00 (500 cents)
- A discount rate for enterprise clients
Prerequisites
billing_accountsmust exist for the company (created in step 1.0).- An admin user (sales/ops) initiates the agreement via the team dashboard.
Step-by-step
Step 1 — Sales prepares the agreement
Sales uploads the signed service agreement document (stored in S3) and enters:
code(e.g.SG-SA-0001) — a human-readable agreement identifier{Country Code}-SA-{Running Number}
document_url— path to the signed agreement PDFeffective_from/effective_to— optional, for time-limited agreements
Step 2 — Sales defines agreement terms
Each term is tied to a specific Billing::Entitlement (instrument) and captures one negotiated value:
term_key | term_value | term_unit | Example meaning |
|---|---|---|---|
fee_rate | 2000 | bps | Gig platform fee = 20.00% |
unit_price | 500 | cents | Placement credit = $5.00 each |
discount_rate | 1000 | bps | 10% discount off list price |
Step 3 — Agreement is created (single transaction)
Billing::Agreements::TeamCreateManager runs inside a DB transaction:
- Validate inputs (via
TeamCreateValidator) - Create
billing_agreementsrow with admin audit trail (admin_created_by_id) - Create
billing_agreement_termsrows (nested attributes, one per instrument × term_key)
Tables touched
billing_agreements(insert)billing_agreement_terms(insert × N terms)
Invariants
- Each
(agreement, entitlement, term_key)combination is unique (enforced byidx_agreement_terms_unique_per_instrument). - Agreement terms capture negotiated commercial values only (fee rates, prices, discounts) — not tax rates. Tax is determined by the seller's legal entity and product pricing at invoice time.
- A company can have multiple agreements (e.g. renewed annually, or separate agreements per product).
What this does NOT do
- Does not grant any entitlements — that only happens via invoice posting.
- Does not determine tax — tax comes from
billing_legal_entities.tax_regime+billing_product_prices.tax_code/tax_rateat invoice creation time.
Result
The company now has a signed agreement on file with negotiated terms. When sales creates an invoice, the agreement terms inform pricing (e.g. the gig platform fee rate from the agreement is used to compute the platform fee invoice item).
3.0 Admin-initiated Purchase Flow (Invoice → Payment → Posting)
This section describes the admin-initiated purchase flow where a sales or ops team member creates the invoice on behalf of a company. This is used for:
- Enterprise deals and custom pricing that require sales involvement
- Purchases above the self-serve threshold (e.g., SGD 3,000)
- Any scenario where the admin needs to review or customise the invoice before issuing
For employer self-serve purchase flows (where the employer browses packages and the system generates the invoice automatically), see sections 3.1–3.3.
Scenario
Actor: Identities::Admin (sales/ops)
An Org::Company (Singapore) wants to buy:
- 1,000 Placement Credits (sold as "Visibility Credits" in UI) to run sponsored placements / job posting / boosts, or
- 1,000 Gig Credits (stored value) with a negotiated platform fee rate.
The admin creates and reviews the invoice before issuing it to the customer.
Step-by-step (models interacting)
Step 1 — Admin selects product and resolves pricing
Admin selects the billing account, product, and quantity. The system resolves the applicable price:
- Find
billing_productsby theirBilling::Entitlement.instrument(e.g.:placement,:gig) and admin-selected name - Find a matching
billing_product_pricesfor:billing_legal_entity.country_id = org_company.country_id(joined viabilling_legal_entity_id)status = :active(and parent productstatus = :active)- If
billing_account_idis set on a price, it is a private price for that specific client — admin can choose it for that account (private overrides standard during resolution)
- If the company has an active
Billing::Agreement, the system retrieves the relevant terms (fee rates, discounts, unit prices)
Pricing always comes from
ProductPrice— never hard-code prices into application code.
Step 2 — Admin generates invoice in draft
Admin creates billing_invoices in draft status and billing_invoice_items.
The admin can review and edit the draft before issuing (see Invoice UC-2, UC-3).
Invoice items must snapshot at creation time:
- unit price, tax, total
- entitlement type
- units_to_grant
- gig: platform fee rate (from agreement) and principal vs platform fee breakdown
Once the admin is satisfied, they issue the invoice (draft → issued). The invoice is now immutable.
Key difference from self-serve (3.1–3.3): Admin-initiated invoices start as
draftfor review. Self-serve invoices go straight toissuedbecause the system generates them automatically with no admin review needed.
Step 3 — Customer pays via bank transfer (offline)
The customer receives the issued invoice and pays via bank transfer. The business team records the payment when they receive proof:
- Create
billing_paymentswithstatus = submitted,amount_cents,bank_reference, andproof_url
The invoice stays issued until payment is verified.
Step 4 — Finance verifies bank transfer(s)
When finance confirms money received in the bank account:
- Update
billing_payments.status = verified - Recompute
verified_total = sum(billing_payments.amount_cents where status = verified)- If
verified_total == 0→ invoice staysissued - If
0 < verified_total < invoice.total_cents→billing_invoices.status = partially_paid - If
verified_total >= invoice.total_cents→billing_invoices.status = paidandsettled_at = now
- If
Policy: Credits are granted only after the invoice is paid. Partial payments do not grant partial credits.
Step 5 — Post the invoice (grant entitlements; idempotent)
In the same DB transaction:
- Lock the invoice row (
SELECT ... FOR UPDATE) - Create
billing_invoice_postings(unique by invoice id — prevents double-granting) - For each invoice item:
- Write one or more
billing_ledger_entriesto grant units + deferred revenue deltas - If entitlement policy is lot-based (gig): create
billing_entitlement_lotsusing the snapshot terms
- Write one or more
Then update projections:
billing_entitlement_balances- (gig) lot remaining units / platform fee deferred remaining
Result: The company can immediately spend their entitlements.
Why the posting record is important
- Idempotency — the unique constraint on
billing_invoice_postings.billing_invoice_idprevents double-granting if someone clicks verify twice - Audit trail — records who posted the invoice and when
- Finance exports — clean pivot for "entitlements posted today" reports
SOA impact
Because posting writes to the ledger, Statement of Account generation stays simple:
- "Top-ups" are ledger entries with
entry_type = grant - "Usage" is ledger entries with
entry_type = consume - "Reservations" are ledger entries with
entry_type = reserve / release
Note: Steps 3–5 above (payment, verification, and posting) are shared by both admin-initiated and self-serve flows. Sections 3.1–3.3 reference these steps rather than repeating them.
3.1 Self-serve Gig Credits Purchase (new company, no agreement)
Scenario
A new Org::Company in Singapore wants to purchase gig credits for the first time. They do not have a Billing::Agreement yet. The employer browses available packages in their dashboard, accepts the service terms, and the system auto-generates both the agreement and the invoice.
Prerequisites
billing_accountsmust exist for the company (created in step 1.0).- The company does NOT have an active
Billing::Agreementwith afee_rateterm for gig. - Active
Billing::ProductPricerows exist for gig products in the company's country. - A
Billing::LegalEntityexists for the company's country (determines currency, tax rules, and the Jod entity selling the service).
Step-by-step
Step 1 — Employer browses gig credit packages
The employer navigates to the Gig section in their dashboard. The system shows available gig credit packages:
- Resolve the company's country via
Org::Company.country_id - Find active standard
Billing::ProductPricerows where:billing_product.billing_entitlement.instrument = :gigbilling_account_id IS NULL(standard catalog prices only — private client-specific prices are excluded from the employer dashboard)billing_legal_entity.country_idmatches the company's country (joined viabilling_legal_entity_id)status = :active- Parent product
status = :active
- For each package, display:
- Product name and credit amount (e.g., "100 Gig Credits")
- Credit price (from
unit_price_cents) - Platform fee (calculated from
Billing::ProductPrice.platform_fee_rate_bps— the list rate, e.g. 30%) - Tax (from
tax_code/tax_rateviaBilling::LegalEntity.tax_regime) - Total price
- If the package total exceeds the self-serve threshold for that currency (e.g., SGD 3,000), it is not shown as self-serve — instead show a "Contact Sales" / "Enterprise" option
Note: The SGD 3,000 threshold is a current business decision and may change. The threshold is currency-specific — different markets may have different thresholds. The
Billing::LegalEntitylinked to theProductPricedetermines the currency.
Example display for Singapore (SGD, 30% list platform fee, 9% GST on platform fee):
| Package | Credits | Price | Platform Fee (30%) | GST (9% on fee) | Total |
|---|---|---|---|---|---|
| A | 100 Gig Credits ($1.00) | $100 | $30 | $2.70 | $132.70 |
| B | 1,000 Gig Credits ($10.00) | $1,000 | $300 | $27.00 | $1,327.00 |
| Enterprise | Custom | Contact Sales | — | — | — |
Step 2 — Employer selects a package
- If employer selects a self-serve package (A or B): proceed to Step 3
- If employer selects "Contact Sales" / "Enterprise": sales team is notified (e.g. via email or internal notification), and the flow ends here. Sales will follow up manually.
Step 3 — Employer accepts service terms
After the employer selects a package (this is an indicator of purchase interest):
- System displays the service agreement terms, including:
- The platform fee rate (e.g., "Jod charges a 30% platform fee on gig credit usage")
- Terms of service for using the gig platform
- A downloadable copy of the agreement document
- Employer checks the "I agree to the terms" checkbox
- If the employer does not accept, the flow ends — no agreement or invoice is created
Future enhancement: Replace the checkbox with legally binding digital signatures (e.g., DocuSign or Google Docs eSignature API).
Step 4 — System auto-generates agreement
Once the employer accepts terms, the system creates a Billing::Agreement in a single transaction:
- Create
billing_agreementsrow:code— auto-generated (e.g.,SG-SA-AUTO-{running_number})document_url— path to the generated agreement document (PDF stored in S3)effective_from= noweffective_to= null (open-ended)
- Create
billing_agreement_termsrow:billing_entitlement_id= gig entitlementterm_key=fee_rateterm_value= the list platform fee rate fromProductPrice.platform_fee_rate_bps(e.g., 3000 for 30%)term_unit=bps
- Agreement document is emailed to the employer
Step 5 — System generates invoice
Immediately after the agreement is created, the system generates the invoice:
- Create
billing_invoiceswithstatus = issued(notdraft— no admin review needed for self-serve):billing_legal_entity_idfrom the ProductPrice's legal entitybilling_account_idfor the companyinvoice_numbergenerated fromBilling::LegalEntity.invoice_number_sequencecurrencyfrom the legal entity / bill-to profile- Snapshot bill-to fields from
Billing::BillToProfile
- Create
billing_invoice_items:- Line 1 (Gig Credits): quantity,
unit_price_cents,amount_cents,tax_cents = 0(principal is tax-exempt for gig),units_to_grant - Line 2 (Platform Fee): platform fee amount calculated from the agreement's
fee_rate,tax_cents= fee × tax_rate (e.g., 9% GST),units_to_grant = 0
- Line 1 (Gig Credits): quantity,
- Compute invoice totals:
subtotal_cents,tax_cents,total_cents - Invoice is emailed to the currently logged-in employer user
Step 6 onwards — Payment and posting (same as 3.0 Steps 3–5)
- Employer downloads the invoice and sends it to their finance team for payment
- Finance pays via bank transfer
- Jod business team records payment proof (
billing_payments,status = submitted) - Jod finance verifies the payment → invoice becomes
paid - System posts the invoice → entitlements granted (gig credits available, FIFO lot created)
Tables touched
billing_agreements(insert)billing_agreement_terms(insert)billing_invoices(insert)billing_invoice_items(insert × 2: principal + platform fee)
(Payment, verification, and posting tables are same as 3.0)
Invariants
- Self-serve is only available for packages at or below the self-serve threshold for that currency (e.g., SGD 3,000). Above that threshold, the employer must contact sales.
- The agreement must be created and accepted BEFORE the invoice is generated. If the employer rejects the terms, no invoice is created.
- The auto-generated agreement uses the list platform fee rate from
ProductPrice.platform_fee_rate_bps— not a negotiated rate. - The invoice goes directly to
issuedstatus (notdraft). There is no admin review step for self-serve purchases. - The agreement document and invoice are emailed separately.
Result
The company now has:
- A
Billing::Agreementwith the list platform fee rate - An
issuedinvoice ready for payment - Once paid and posted: gig credits available in their balance
3.2 Self-serve Gig Credits Top-up (existing company with agreement)
Scenario
An existing Org::Company that already has a Billing::Agreement (with a negotiated or list platform fee rate) wants to top up their gig credits. They can enter a custom amount of gig cents they want to purchase.
Prerequisites
billing_accountsmust exist for the company.- An active
Billing::Agreementexists with afee_rateterm for gig. - Active
Billing::ProductPricerows exist for gig products in the company's country.
Step-by-step
Step 1 — Employer enters top-up amount
- Employer navigates to the Gig Credits section in their dashboard
- System displays current gig credit balance
- System optionally shows a recommended top-up amount based on the company's last 30 days of gig credit usage (e.g., "Based on your recent usage, we recommend topping up $500")
- Employer enters a custom gig cents amount, or selects a suggested package / listed ProductPrice
- If the total (credits + platform fee + tax) exceeds the self-serve threshold (e.g., SGD 3,000): show "Contact Sales" instead — the employer cannot self-serve above this amount
Note: Sales can also create private
ProductPricerows (withbilling_account_idset) to offer special pricing to specific companies, encouraging larger top-ups. These private prices are only visible to admins when creating invoices, not in the employer dashboard.
Step 2 — System calculates and displays total
- System looks up the company's active
Billing::Agreementto get the negotiatedfee_rate(e.g., 2000 bps = 20%) - System resolves the
Billing::ProductPricefor gig credits in the company's country - System calculates:
- Credits: custom amount ×
unit_price_cents - Platform fee: credit amount ×
fee_rate/ 10,000 - Tax: platform fee ×
tax_rate(e.g., 9% GST — tax is only on the platform fee for gig) - Total: credits + platform fee + tax
- Credits: custom amount ×
- Employer reviews the breakdown and confirms
Example (existing agreement with 20% fee rate):
| Item | Amount |
|---|---|
| Gig Credits (500 credits = $5.00) | $500.00 |
| Platform Fee (20%) | $100.00 |
| GST (9% on platform fee) | $9.00 |
| Total | $609.00 |
Step 3 — System generates invoice
Same as 3.1 Step 5, but using the existing agreement's fee rate (not the list rate):
- Create
billing_invoiceswithstatus = issued - Create
billing_invoice_items:- Line 1: Gig credits (tax-exempt)
- Line 2: Platform fee at the agreement's negotiated rate (with GST)
- Invoice emailed to the currently logged-in employer user
Step 4 onwards — Payment and posting (same as 3.0 Steps 3–5)
Tables touched
billing_invoices(insert)billing_invoice_items(insert × 2)
(No agreement changes — the existing agreement is reused)
Invariants
- The platform fee rate comes from the company's active
Billing::Agreement, NOT fromProductPrice.platform_fee_rate_bps. The agreement rate may differ from the list rate (e.g., 20% negotiated vs 30% list). - Self-serve top-ups are limited to the self-serve threshold for that currency.
- The recommended top-up amount is a suggestion only — the employer can enter any custom amount within the threshold.
- Invoice goes directly to
issued(no admin review).
Result
The company has a new issued invoice for their gig credit top-up. Once paid and posted, their gig credit balance increases and a new FIFO lot is created with the agreement's platform fee rate.
3.3 Self-serve Placement Credits Purchase
Scenario
An Org::Company wants to purchase placement credits (shown as "Visibility Credits" in the UI) to run sponsored placements, job postings, or boosts. Placement credits do not have a platform fee — the full price goes toward the credit value.
Prerequisites
billing_accountsmust exist for the company.- Active
Billing::ProductPricerows exist for placement products in the company's country.
Step-by-step
Step 1 — Employer browses placement credit packages
- Resolve the company's country via
Org::Company.country_id - Find active standard
Billing::ProductPricerows for placement products (same resolution as 3.1 Step 1, but for placement entitlement) - Display packages with: product name, credit amount, price, tax (GST on full value), total
- Packages above the self-serve threshold: show "Contact Sales"
Example for Singapore (SGD, 9% GST on full value):
| Package | Credits | Price | GST (9%) | Total |
|---|---|---|---|---|
| A | 50 Placement Credits | $250 | $22.50 | $272.50 |
| B | 100 Placement Credits | $500 | $45.00 | $545.00 |
| Enterprise | Custom | Contact Sales | — | — |
Step 2 — Employer accepts terms and conditions
- System displays the terms and conditions for using the ads/placement service
- Employer checks the "I agree to the terms and conditions" checkbox
- If the employer does not accept, the flow ends
Note: For placement credits, a formal
Billing::Agreementdocument is not generated in the initial version. The checkbox acceptance covers the T&C for service usage. Future enhancement: If enterprise clients need formal agreements for placement credits (e.g., custom unit pricing or volume discounts), generate an agreement document and store it underBilling::Agreement, following the same pattern as gig (3.1 Step 4).
Step 3 — System generates invoice
- Create
billing_invoiceswithstatus = issued:- Associated with the company's
Billing::LegalEntityfor that country - Invoice number generated from legal entity sequence
- Snapshot bill-to fields
- Associated with the company's
- Create
billing_invoice_items:- Single line item: placement credits — quantity,
unit_price_cents,amount_cents,tax_cents= amount × tax_rate (GST on full value),units_to_grant
- Single line item: placement credits — quantity,
- Invoice emailed to the currently logged-in employer user
Step 4 onwards — Payment and posting (same as 3.0 Steps 3–5)
Tables touched
billing_invoices(insert)billing_invoice_items(insert × 1)
Invariants
- Placement credit invoices have a single line item (unlike gig which has principal + platform fee).
- GST applies to the full value of placement credits (not just a fee portion).
- No
Billing::Agreementis required for standard placement credit purchases. - Invoice goes directly to
issued(no admin review). - Self-serve is limited to the self-serve threshold for that currency.
Result
The company has an issued invoice for placement credits. Once paid and posted, their placement credit balance increases (pooled, not lot-based).
4.0 Placement Credits: Grant (top-up) after payment verified
Scenario
Company buys a Placement Credits package (shown as "Visibility Credits" in UI):
- +100
placement_credit - +$500 deferred revenue
Steps
-
Billing service called:
GrantEntitlements -
Lock
billing_entitlement_balancesfor(account, visibility_credit) -
Insert ledger entry:
entry_type = grantavailable_delta = +100deferred_revenue_delta_cents = +50000
-
Update balance projection:
units_available += 100deferred_revenue_cents += 50000
Statement line
"Purchased Visibility Credits +100"
4.1 Placement Credits: Reserve for an Ads campaign placement (visibility promise)
Scenario
Ads books a homepage placement for 14 days. You reserve upfront so they can't spend the credits elsewhere.
Steps
-
Ads creates campaign line item (e.g.
ads_campaign_placements.id = 999) -
Ads calls Billing:
ReserveEntitlements(reference: Ads::CampaignPlacement#999, units: 14) -
Billing locks placement balance row
-
Ensure
units_available >= 14 -
Insert ledger entry:
entry_type = reserveavailable_delta = -14reserved_delta = +14reference_type='Ads::CampaignPlacement',reference_id=999
-
Upsert hold projection:
- status
active units_held = 14opened_ledger_entry_id = <reserve_entry_id>
- status
-
Update balance:
- available -= 14
- reserved += 14
Statement lines
"Reserved 14 Visibility Credits for CampaignPlacement #999"
4.2 Placement Credits: Daily consumption (deliver service + recognize revenue)
Scenario
Each day the campaign runs, consume 1 credit from reserved. At the same time, recognize revenue proportionally.
Revenue recognition rule (pooled proportional)
At consume time:
recognized = units_consumed × (deferred_revenue_before / pool_units_before)
Where:
pool_units_before = balance.units_available + balance.units_reserveddeferred_revenue_before = balance.deferred_revenue_cents
Steps (daily job)
-
Scheduler triggers:
ConsumeEntitlements(reference: Ads::CampaignPlacement#999, units: 1) -
Lock balance row
-
Confirm hold is active and has units held
-
Compute snapshots:
pool_units_before = available + reserveddeferred_revenue_before_cents = deferred_revenue
-
Compute recognized:
recognized = 1 * deferred_revenue_before / pool_units_before- use integer math with rounding rule you choose (recommend: round half up to cents)
-
Insert ledger entry:
entry_type = consumereserved_delta = -1recognized_revenue_cents = recognizeddeferred_revenue_delta_cents = -recognized- snapshot fields:
pool_units_before,pool_deferred_revenue_before_cents - reference to campaign placement
-
Update balance projection:
- reserved -= 1
- deferred_revenue -= recognized
-
Update hold projection:
- units_held -= 1
- if units_held becomes 0: mark hold consumed, closed_at
Statement line
"Consumed 1 Placement Credit for CampaignPlacement #999 (recognized $X.XX)"
4.3 Placement Credits: Cancel campaign and release remaining reservation
Scenario
Campaign canceled with 5 days remaining. Release held credits back to available.
Steps
-
Ads calls Billing:
ReleaseHold(reference: Ads::CampaignPlacement#999) -
Find active hold with units_held=5
-
Lock balance row
-
Insert ledger entry:
entry_type = releaseavailable_delta = +5reserved_delta = -5
-
Update balance
-
Mark hold as released
Statement line
"Released 5 Visibility Credits for CampaignPlacement #999"
4.4 Careers Job Posting: consume credits (two pricing modes)
Pricing mode A: per job posting
At job publish:
- reserve (optional) then consume immediately
Recommended: consume immediately (no need to reserve unless you have multi-step approvals).
Ledger entry:
consumewithavailable_delta=-Xorreserved_delta=-X- reference:
Careers::Job
Pricing mode B: per application
At application creation:
- consume 1 credit per application
- reference:
Careers::JobApplication
This is exactly why we keep the ledger generic: pricing changes don't require schema changes.
4.5 Job Boost: reserve then daily consume (same as Ads)
Boost is a "visibility placement" product. Treat the boosted listing as a reference:
reference_type='Listings::Boost',reference_id=<boost_id>
Reserve N days upfront, consume 1/day.
5.0 Gig: Grant credits (top-up) and create FIFO lot
Scenario
Company buys:
- $100.00 gig credits (10000 cents)
- platform fee rate 20% → $20.00 deferred platform fee
Steps
-
Billing locks gig balance
-
Insert ledger entry:
entry_type=grantavailable_delta=+10000(cents)platform_fee_deferred_delta_cents=+2000
-
Create lot:
units_purchased=10000units_available=10000units_reserved=0platform_fee_rate_bps=2000platform_fee_remaining_cents=2000
-
Update balance projection:
- available += 10000
- platform_fee_deferred += 2000
Statement line
"Purchased Gig Credits $100.00 (+ platform fee deferred $20.00)"
5.1 Gig: Reserve credits when posting a shift (may span lots)
Scenario
Posting a gig shift reserves estimated wage value, e.g. 1800 cents ($18.00).
Steps
-
Gig calls Billing:
ReserveEntitlements(reference: Gig::Shift#123, units: 1800) -
Lock gig balance row
-
Outlet budget check (if outlet is budget-controlled):
- Look up
Billing::OutletBudgetfor the shift's outlet - If exists and active: verify
OutletBudget.units_available >= 1800 - If no budget exists: verify unallocated pool >= 1800 (i.e.
EntitlementBalance.units_available - SUM(active OutletBudgets.units_available) >= 1800)
- Look up
-
Lock lots with
units_available > 0FIFO order -
Allocate 1800 across lots FIFO:
- Lot A take 1000
- Lot B take 800
-
Insert ledger entry:
entry_type=reserveavailable_delta=-1800reserved_delta=+1800org_outlet_id= shift's outlet (denormalized for SOA)- reference shift
-
Insert allocations:
- (reserve, lot A, 1000)
- (reserve, lot B, 800)
-
Update lots:
- lot A
units_available -=1000,units_reserved +=1000 - lot B
units_available -=800,units_reserved +=800
- lot A
-
Update balance:
- available -=1800, reserved +=1800
-
Update outlet budget (if outlet is budget-controlled):
OutletBudget.units_available -= 1800OutletBudget.units_reserved += 1800
- Create hold projection:
- units_held=1800, status active
Note: Steps 3 and 10 only apply when the outlet has an active
Billing::OutletBudget. SeeBilling::OutletBudgetmodel spec for details.
Statement line
"Reserved $18.00 Gig Credits for Shift #123"
5.2 Gig: Complete shift (consume actual wage, handle differences vs reserved)
Scenario
Reserved 1800 cents earlier. Actual wage payable becomes 1750 cents due to deductions/adjustments.
Recommended billing behavior
- Consume actual amount (1750) from reserved
- Release remainder (50) back to available This keeps statements clean and mirrors reality.
Steps
-
Gig calls Billing:
CompleteShift(reference: Gig::Shift#123, actual_units: 1750, metadata: insurance...) -
Find active hold units_held=1800
-
Lock gig balance + related lots (based on hold allocations)
-
Consume from lots that were reserved (proportionally to reserved allocations FIFO):
- Lot A consume 1000
- Lot B consume 750 (of its 800 reserved)
-
Compute platform fee recognized per allocation:
- per lot:
fee_recognized = (consumed_cents * rate_bps) / 10_000
- per lot:
-
Insert ledger entry (consume):
entry_type=consumereserved_delta=-1750org_outlet_id= shift's outlet (denormalized for SOA)platform_fee_recognized_cents=<sum fee recognized>
-
Insert allocation rows (consume):
- lot A consume 1000, fee recognized
- lot B consume 750, fee recognized
-
Update lots:
- lot A
units_reserved -=1000 - lot B
units_reserved -=750 - reduce lot
platform_fee_remaining_centsby recognized portion (based on consumed)
- lot A
-
Update balance:
- reserved -=1750
- platform_fee_deferred -= fee_recognized (optional cache update)
-
Update outlet budget (if outlet is budget-controlled):
OutletBudget.units_reserved -= 1750
- Insert ledger entry (release remainder 50):
entry_type=releaseavailable_delta=+50,reserved_delta=-50org_outlet_id= shift's outlet
- Update lots for release remainder:
-
release remaining 50 back to the lot it was reserved from (lot B):
- lot B
units_reserved -=50,units_available +=50
- lot B
- Update outlet budget for release remainder (if outlet is budget-controlled):
OutletBudget.units_reserved -= 50OutletBudget.units_available += 50- Released credits return to the outlet budget (not the unallocated pool), because the outlet originally reserved them.
- Close hold: consumed/released
Note: Steps 10 and 13 only apply when the outlet has an active
Billing::OutletBudget. SeeBilling::OutletBudgetmodel spec for details.
Statement lines
- "Consumed $17.50 Gig Credits for Shift #123"
- "Released $0.50 Gig Credits for Shift #123"
5.3 Gig: Cancel shift before completion (release reservation)
Steps
-
Find active hold for shift
-
Insert ledger entry release:
available_delta=+held,reserved_delta=-heldorg_outlet_id= shift's outlet (denormalized for SOA)
-
Update allocations: release units back into lots (reverse of reserve allocations)
-
Update lots: reserved → available
-
Update outlet budget (if outlet is budget-controlled):
OutletBudget.units_available += heldOutletBudget.units_reserved -= held- Released credits return to the outlet budget, not the unallocated pool.
-
Close hold
Note: Step 5 only applies when the outlet has an active
Billing::OutletBudget. SeeBilling::OutletBudgetmodel spec for details.
6) Statements of Account (SOA)
6.1 The SOA contract (what we want)
For a given company:
- Show a chronological list of ledger entries
- Group by reference when needed (e.g. shift/campaign)
- Provide running balances
- Provide totals within period
6.2 SOA query pattern (single ledger = simple)
Gig SOA
Filter by:
- account_id
- entitlement_type =
gig_credit_cents - date range
Order by occurred_at ASC, id ASC.
Placement Credits usage report
Same, with entitlement type = placement_credit.
Running balance calculation
Option A (fast): use projections + reconstruct running changes from ledger lines in memory. Option B (SQL window): use window sums if you really need it in SQL.
Recommendation for MVP: do it in Ruby:
- fetch rows ordered
- running_available += available_delta
- running_reserved += reserved_delta
6.3 SOA formatting guidance
Ledger entry should render as:
- timestamp
- action label (grant/reserve/consume/release/adjust)
- units change
- money change (recognized revenue / deferred changes)
- reference label (Shift #123, CampaignPlacement #999)
- metadata (insurance, notes)
This is why ledger is the canonical statement source.