| RPC | Purpose | Key Parameters | Auth | Returns |
|---|---|---|---|---|
create_bill_v2 |
Atomic bill creation with FIFO lot allocation | farmer_id, items[], payment_type, amount_paid, branch_id, client_ref | dealer/staff | Bill record with bill_number |
create_bill_offline_sync |
Idempotent offline bill creation using client_ref unique index | Same as create_bill_v2 | dealer/staff | Bill record (skips if client_ref exists) |
collect_farmer_payment_v2 |
Payment collection + auto-allocate oldest-first | farmer_id, amount, method, notes | dealer/staff | Payment record + allocation details |
record_stock_purchase_v2 |
Record purchase + create FIFO lot | supplier_id, product_id, quantity, cost_price, batch_number, expiry_date | dealer/staff | Purchase record + lot |
record_supplier_payment_v2 |
Supplier payment + reduce total_due | supplier_id, amount, method | dealer/staff | Payment record |
cancel_bill_v1 |
Cancel same-day unpaid bill | bill_id | dealer/staff | Void status |
edit_bill_v1 |
Edit bill items/quantities/prices | bill_id, items[], payment changes | dealer/staff | Updated bill |
close_cash_day_v1 |
Daily cash closing reconciliation | branch_id, actual_closing, notes | dealer | Cash closing record |
adjust_inventory_stock_v1 |
Manual stock adjustment (count correction) | inventory_id, new_quantity, notes | dealer/staff | Adjusted record |
preview_fifo_bill_lines |
Preview FIFO consumption without committing | items[] | dealer/staff | Lot allocation preview (read-only) |
get_farmer_ledger_page |
Paginated farmer ledger (bills + payments interleaved) | farmer_id, page, page_size | dealer/staff | Ledger rows + total |
get_dashboard_aggregates |
Dashboard KPI aggregates | date range, branch_id | dealer/staff | Sales, payments, dues, stock value |
get_dues_ageing |
Ageing report buckets | branch_id | dealer/staff | 0-30, 31-60, 61-90, 90+ day buckets |
get_farmer_public_statement |
Public statement via share token | token | public | Statement rows (no auth required) |
create_stock_transfer |
Branch transfer preserving FIFO lots | from_branch_id, to_branch_id, items[] | dealer | Transfer record |
undo_transaction_v1 |
Undo transaction within 48h window | event_id | dealer | Reversed state |
staff_portal_login |
Staff PIN authentication | phone, pin, dealer_slug | public | Session token + permissions |
staff_portal_context_v2 |
Resolve shop name + branch for staff portal | dealer_slug | public | Shop info + branches |
verify_dealer_pin |
App lock PIN verification | pin | dealer | Boolean |
preview_farmer_return_v1 |
Preview return calculation | bill_id, items[] | dealer/staff | Return preview |
create_farmer_return_v1 |
Create sales return | bill_id, items[], notes | dealer/staff | Return record + credit |
replace_farmer_return_v1 |
Replace/edit existing return | return_id, items[], notes | dealer/staff | Updated return |
check_whatsapp_quota |
Verify monthly WhatsApp send quota | (none) | dealer | Remaining count |
get_profit_report_data |
Profit report with COGS | date range, branch_id | dealer | Revenue, COGS, gross profit |
send-bill-whatsapp| Runtime | Deno (Supabase Edge) |
|---|---|
| Provider | authkey.io WhatsApp Business API |
| Template | 8 body variables + 1 button (public statement link) |
| CORS | Wildcard (Access-Control-Allow-Origin: *) |
| Pattern | Fire-and-forget after bill creation |
| Status tracking | whatsapp_status column on bills tablepending → sent → delivered → read → failed |
| Rate limit | Checked via check_whatsapp_quota RPC before send |
All database access from the React frontend goes through service modules. No component calls Supabase directly.
src/features/*/services/*.ts.from().select(), .insert(), .rpc()// src/features/billing/services/billService.ts
export async function createBill(params: CreateBillParams) {
const { data, error } = await supabase.rpc('create_bill_v2', params)
if (error) throw new AppError(error.message)
return data
}