Skip to main content

Tax Calculation Engine

The calculation engine determines all applicable taxes based on tax rules, the company profile, and the customer's tax type.

Supported Taxes

TaxDescriptionJurisdiction
ISSService TaxMunicipal
PISSocial Integration ProgramFederal
COFINSContribution for Social Security FinancingFederal
CSLLSocial Contribution on Net IncomeFederal
IRPJCorporate Income TaxFederal
IRRFWithholding Income TaxFederal
CSRFPIS + COFINS + CSLL withheld togetherFederal
INSSSocial Security ContributionFederal
CBSContribution on Goods and Services (2026+)Federal
IBSTax on Goods and Services (2026+)Federal/State

Calculation Flow

1. Get Applicable Rules

Queries the TaxRuleService with the operation context. Rules are filtered by scope and priority.

2. Resolve Rates

  • Extracts rates from applicable rules
  • Falls back to the company's Tax Profile
  • Applies withholding overrides from the billing account

3. Calculate Each Tax

ISS = service_value × (iss_rate / 100)
PIS = service_value × (pis_rate / 100)
COFINS = service_value × (cofins_rate / 100)
CSLL = service_value × (csll_rate / 100)
IRRF = service_value × (irrf_rate / 100) [always withheld]
CSRF = service_value × (4.65 / 100) [always withheld]
INSS = service_value × (inss_rate / 100) [always withheld]

4. Rounding

Each tax is rounded independently with Math.round(). Rounding compensation is applied to the tax with the largest contribution.

5. Result

Total taxes = sum of all taxes
Total withheld = sum of taxes with isWithheld flag
Net amount = service value - total taxes

CBS/IBS (Tax Reform 2026+)

The system already supports the new taxes from the tax reform:

ModeDescription
informativeCalculated but not included in the total (2026)
compositiveIncluded in the total (2027+)

CBS and IBS rates are configurable in the company's Tax Profile.