| ← All Docs Financial Logic

Financial Logic

Cash flow, daily closing, profit calculation, expenses, and supplier finances

Cash Flow System

All money movement in AquaDealers flows through the cash_book table, providing a unified ledger for every financial transaction.

Cash Book Structure

FieldDescription
entry_typecash_in Money received (from bills, payments)   cash_out Money paid out (purchases, supplier payments, expenses)
sourceOrigin of the entry — see source types below
amountAbsolute amount of the transaction
reference_idLinks back to the originating record (bill ID, payment ID, etc.)

Cash Book Source Types

SourceEntry TypeCreated By
billcash_inBill creation (amount_paid portion)
paymentcash_inFarmer payment collection
purchasecash_outStock purchase from supplier
supplier_paymentcash_outPayment made to supplier
expensecash_outExpense recording
manualeitherManual cash book adjustment
Each financial operation (bill creation, payment collection, purchase, expense, etc.) automatically creates a corresponding cash_book entry via its respective RPC. This ensures the cash book is always in sync with the underlying transactions.

Daily Cash Closing

close_cash_day_v1 RPC

At the end of each business day, the dealer performs a cash closing to reconcile actual cash on hand with the system's expected balance.

FieldDescription
opening_balanceCash at start of day (previous day's closing or manual override)
total_inSum of all cash_in entries for the day
total_outSum of all cash_out entries for the day
expected_closingopening_balance + total_in − total_out
actual_closingActual cash counted by the dealer
varianceactual_closing − expected_closing
notesOptional notes explaining variance
How opening balance is determined

The opening balance for a new day is automatically set to the previous day's actual_closing value. If no previous closing exists (first day, or gap in closings), the dealer can enter a manual opening balance override. This ensures continuity even if days are skipped.

Key Financial Metrics

MetricCalculationSource
Today's Sales Σ bills.total WHERE bill_date = today AND status ≠ cancelled get_dashboard_aggregates RPC
Today's Collection Σ payments.amount WHERE payment_date = today get_dashboard_aggregates RPC
Outstanding Dues Σ farmers.total_due WHERE total_due > 0 Dashboard + Dues page
Cash in Hand opening + cash_in − cash_out for today Cashbook daily counter
Inventory Value Σ(inventory.quantity_in_stock × inventory.cost_price) Dashboard stat
Supplier Dues Σ suppliers.total_due Supplier list

Profit Calculation

Profit Report Route: /profit-report

Uses the get_profit_report_data RPC to calculate profitability.

Profit = Revenue − COGS − Expenses

Where:
  Revenue = Σ(bill totals) for the period
  COGS    = Σ(FIFO lot cost_price × quantity sold) per item
  Expenses = Σ(expenses.amount) for the period

FIFO Cost Basis

Cost of Goods Sold (COGS) is calculated using FIFO lot tracking. When items are sold, the cost price comes from the specific inventory lots that were consumed (earliest first). This gives an accurate per-item cost basis rather than using average cost.

The profit report is paginated for performance. Large date ranges with many transactions are broken into pages to avoid timeout issues.

Expense Tracking

Expense System Route: /expenses

FieldDescription
categoryUser-defined category (electricity, rent, transport, etc.)
amountExpense amount
descriptionFree-text description
paid_viaPayment method used (cash, upi, etc.)
expense_dateDate of the expense

Each expense automatically creates a cash_book entry with entry_type = cash_out and source = expense.

Expense categories

Categories are user-defined — dealers can create and manage their own expense categories. There are no hard-coded categories in the system. Common examples include: electricity, rent, transport, labour, repairs, feed wastage.

Supplier Financial Flow

Supplier finances mirror the farmer side but in the opposite direction — the dealer owes money to suppliers instead of receiving it.

Stock Purchases cash_out

Recorded in stock_purchases table.

  • Each purchase has a cost_price_per_unit
  • Creates inventory lots for FIFO tracking
  • Updates supplier.total_due via triggers
  • Creates cash_book entry if paid upfront

Supplier Payments Reduces Due

Recorded in supplier_payments table.

  • Reduces supplier.total_due
  • Creates cash_book entry (cash_out, source: supplier_payment)
  • Supports partial payments

Supplier Ledger

Available at /suppliers/:id. Shows a chronological view of all purchases and payments for a given supplier, with running balance calculation.

supplier.total_due is maintained by database triggers, similar to farmers.total_due. It is automatically recalculated whenever stock purchases or supplier payments are inserted, updated, or deleted.