Product EngineeringFeaturesOrder SplitBackend

Overview

Backend architecture, split API behavior, transactional data movement, and system integrations for Order Split.

๐Ÿ‘ค Sachin Sharma๐Ÿ“… Updated: May 2, 2026๐Ÿท๏ธ feature๐Ÿท๏ธ backend๐Ÿท๏ธ API๐Ÿท๏ธ finance

Architecture overview

The backend is split into two layers.

View layer (BillSplitView) handles HTTP concerns only: payload parsing, session validation, mode routing (is_validate, is_calculate, execute), and response serialization. It delegates all logic to the manager.

Manager layer (BillSplitManager) owns everything else: eligibility checks, financial calculations, the atomic transaction, and all post-commit side effects. The manager uses __getattribute__ to guard against calling business methods when validation failures exist, preventing partial operations on an invalid state.


Core responsibilities

LayerResponsibility
BillSplitViewParse request, route by mode flag, serialize response
BillSplitManager.__init__Bill and test eligibility checks (runs for all modes)
BillSplitManager.get_bill_calculationsProportional financial computation in memory
BillSplitManager.validate_splitGuard-table, advance, attachment, and residual-test checks
BillSplitManager.do_splitAtomic transaction โ€” all DB writes
BillSplitManager (post-commit)ES sync, Redis invalidation, ledger webhook, activity logs

Transactional data movement

All DB writes execute inside transaction.atomic(). If any step fails, the full transaction rolls back โ€” no partial split state is possible.

Execution order:

  1. Save split bill
  2. Save updated parent bill
  3. Create zero-amount CASH payment bootstrap
  4. Create new CollectedSample where parent and split would share a sample
  5. Shift LabReportRelation rows to split bill (and remap sample references)
  6. Shift BillingInfo rows to split bill
  7. Clone BillApprovalAction to split bill
  8. Migrate AOE and patient consent records (QuestionValue, LabForm, LabFormLinkedProcesses)
  9. Clone unresolved LabMissingDetails rows
  10. Shift/clone BillingICD rows
  11. Copy/shift/soft-delete Attachments
  12. Shift OrgTestCountLedger rows
  13. Shift/clone BillingModifier rows
  14. Clone SymptomsUserBillRelation rows

Post-transaction updates

Run after the transaction commits. A failure here does not roll back the split.

UpdateDetail
ElasticsearchBulk re-index moved LabReportRelation docs in patient_reports using labReportId as key
PusherBroadcast patient_reports and getLatestSamples events for live UI refresh
RedisDelete finance graph, collection graph, and sample batch cache keys
LedgerPOST Fusion webhook to /manageLedgerBalance/ (only when org has manageLedger enabled)
Activity logWrite two ActivityLog entries โ€” one for the split bill, one for the parent bill

Backend pages in this section

On this page