Skip to content

Extract GST/tax calculation logic to service module#357

Merged
nikhilb2 merged 1 commit into
mainfrom
350-extract-gst-tax-calculation-logic
May 5, 2026
Merged

Extract GST/tax calculation logic to service module#357
nikhilb2 merged 1 commit into
mainfrom
350-extract-gst-tax-calculation-logic

Conversation

@nikhilb2
Copy link
Copy Markdown
Owner

@nikhilb2 nikhilb2 commented May 5, 2026

Summary

Extracts GST and tax calculation logic from backend/src/api/routes/invoices.py into a dedicated service module for better testability and separation of concerns.

Changes

  • Created backend/src/services/gst_tax_service.py with:

    • _money() — Utility function for Decimal rounding to 2 places
    • is_interstate_supply() — Detects if supply is interstate vs intrastate based on GST state codes
    • assign_item_tax_split() — Assigns tax split (CGST/SGST/IGST) to invoice items
    • TaxCalculator class — Encapsulates tax aggregation with methods:
      • calculate_tax_totals() — Calculates total CGST, SGST, IGST from items
      • assign_invoice_tax_totals() — Assigns invoice-level tax amounts based on supply type
  • Updated backend/src/api/routes/invoices.py:

    • Imports tax functions from new gst_tax_service module
    • Replaces manual tax calculations with TaxCalculator.assign_invoice_tax_totals()
    • Removes duplicate functions: _money(), _is_interstate_supply(), _assign_item_tax_split()

Testing

✅ All 25 invoice-related tests pass:

  • test_invoice_tax_split.py — Tax splitting logic tests
  • test_invoice_round_off.py — Rounding with tax calculations
  • test_invoice_pan_display.py — PAN/GST display in PDFs
  • test_invoice_update_inventory.py — Inventory tracking with tax calcs
  • test_invoice_dues_allocations.py — Payment allocations
  • test_invoice_reference_notes.py — Reference notes handling

Related Issue

Closes #350

Extract tax-related functions from routes/invoices.py into a dedicated
service module for better testability and separation of concerns.

Changes:
- Create backend/src/services/gst_tax_service.py with:
  * _money() utility for Decimal rounding
  * is_interstate_supply() to detect interstate vs intrastate GST
  * assign_item_tax_split() to split taxes into CGST/SGST/IGST
  * TaxCalculator class with tax aggregation methods

- Update backend/src/api/routes/invoices.py to:
  * Import tax functions from new service module
  * Replace manual tax calculations with TaxCalculator.assign_invoice_tax_totals()
  * Remove duplicated _money(), _is_interstate_supply(), _assign_item_tax_split()

All 25 invoice-related tests pass after refactoring.
@nikhilb2 nikhilb2 merged commit e511f17 into main May 5, 2026
1 check passed
@nikhilb2 nikhilb2 deleted the 350-extract-gst-tax-calculation-logic branch May 5, 2026 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract GST/tax calculation logic to service module

1 participant