| ← All Docs API & RPCs

API & RPCs

Last updated Sep 2026 22 RPCs · 1 Edge Function

Supabase RPCs

RPCPurposeKey ParametersAuthReturns
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
All RPCs use SECURITY DEFINER and run as the function owner, bypassing row-level security for complex multi-table operations. Input validation happens inside the function.

Edge Functions

send-bill-whatsapp
RuntimeDeno (Supabase Edge)
Providerauthkey.io WhatsApp Business API
Template8 body variables + 1 button (public statement link)
CORSWildcard (Access-Control-Allow-Origin: *)
PatternFire-and-forget after bill creation
Status trackingwhatsapp_status column on bills table
pendingsentdeliveredreadfailed
Rate limitChecked via check_whatsapp_quota RPC before send

Client Service Layer

All database access from the React frontend goes through service modules. No component calls Supabase directly.

Example

// 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
}