Extract GST/tax calculation logic to service module#357
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extracts GST and tax calculation logic from
backend/src/api/routes/invoices.pyinto a dedicated service module for better testability and separation of concerns.Changes
Created
backend/src/services/gst_tax_service.pywith:_money()— Utility function for Decimal rounding to 2 placesis_interstate_supply()— Detects if supply is interstate vs intrastate based on GST state codesassign_item_tax_split()— Assigns tax split (CGST/SGST/IGST) to invoice itemsTaxCalculatorclass — Encapsulates tax aggregation with methods:calculate_tax_totals()— Calculates total CGST, SGST, IGST from itemsassign_invoice_tax_totals()— Assigns invoice-level tax amounts based on supply typeUpdated
backend/src/api/routes/invoices.py:gst_tax_servicemoduleTaxCalculator.assign_invoice_tax_totals()_money(),_is_interstate_supply(),_assign_item_tax_split()Testing
✅ All 25 invoice-related tests pass:
test_invoice_tax_split.py— Tax splitting logic teststest_invoice_round_off.py— Rounding with tax calculationstest_invoice_pan_display.py— PAN/GST display in PDFstest_invoice_update_inventory.py— Inventory tracking with tax calcstest_invoice_dues_allocations.py— Payment allocationstest_invoice_reference_notes.py— Reference notes handlingRelated Issue
Closes #350