garofalodeveloperThiago Garofalo

A tax engine with an official source: single-stage PIS/COFINS under Simples Nacional

Period: November 2026 · Role: research of the rules, decision logic, code, tests on real invoices and publishing · Status: live in Escrituro 0.65.0 since 2026-11-14; the classification and the refund arithmetic published as an open-source tool

One-sentence summary: small Brazilian businesses that resell tires, auto parts, medicines or cosmetics often pay the same federal contributions twice on the same product; I built the engine that flags each item sold as yes, no or to be confirmed, always with the law and article, carries the flag to the tax return and computes what can be recovered from the last five years.

1. Context and problem

For some products, Brazilian law concentrates PIS and COFINS (federal contributions on revenue) at the start of the chain: the manufacturer or importer pays a higher rate, and resellers have a zero rate. This is single-stage, or “monophasic”, taxation. It covers fuels, medicines, perfumery and personal hygiene, vehicles, auto parts, tires and inner tubes.

Under Simples Nacional, the simplified regime for small businesses, PIS and COFINS come embedded in a single monthly payment (DAS). A reseller of monophasic products must segregate that revenue in the PGDAS-D return and flag it as monophasic, so the PIS and COFINS share is removed from the payment (LC 123/2006, art. 18, §4-A, I; CGSN Resolution 140/2018, art. 25, §6). When this is not done, the business pays again a contribution the manufacturer has already paid.

In practice the segregation is rarely done, for three reasons:

  1. The list is not a list. The products are spread across several laws (9,718, 10,147, 10,485, 13,097), annexes of a normative instruction and two SPED tables. They do not always agree.
  2. The product code alone does not always decide. Some auto parts are monophasic only when made for tractors, excavators or vehicles (Annex II of RFB Normative Instruction 2,121/2022). Some codes count only under a specific “Ex” of the IPI table (TIPI).
  3. Mistakes are expensive both ways. Flagging as monophasic what is not creates a debt with penalties. Failing to flag means paying twice every month, silently.

2. What the engine does

flowchart LR
  A["Invoice item<br/>NCM and EXTIPI"] --> B["Official table<br/>longest prefix, exceptions,<br/>TIPI Ex, conditions"]
  B --> C{"yes, no or<br/>to be confirmed"}
  C --> D["Purchase cross-check<br/>PIS CST 02, 03 or 04"]
  D --> E["Human decision<br/>becomes a company rule"]
  E --> F["Simples Nacional<br/>calculation and PGDAS-D"]
  F --> G["Refund report<br/>60 months and deadline"]

3. Design decisions

Official table first, the supplier’s tax code only as a cross-check. The tempting shortcut was to trust the PIS tax situation code (CST) on the supplier’s invoice (02, 03 or 04 indicate a monophasic product). I did not, because suppliers make mistakes too. The table decides; the CST only raises a flag. If the table says “no” and the supplier sold the same NCM with a monophasic CST, the item becomes “to be confirmed”. The CST never turns an item into “yes” on its own, nor downgrades a “yes”.

Longest prefix wins. The law lists 4-digit headings in some places and 8-digit subheadings in others. Across all groups, the longest prefix that matches the NCM wins. That way 8431.41.00, which is in Annex I of the auto parts, beats 8431.49 from Annex II without any special case in the code. A test ensures no prefix appears in two groups.

The human decision becomes a rule, but chosen separately. When the accountant confirms an item, the decision becomes a learned rule of that company for that NCM and applies to all its sales. Escrituro already had a tax grid that picks one rule per item and, with it, the Simples Nacional activity, the bookkeeping CFOP and which shipments to ignore. Had the monophasic rule joined the same contest, it would win for being more specific (it has an NCM), and the item would lose its resale activity, or an ignored shipment would enter revenue. So “monophasic-only” rules are picked in a separate pass and decide only the flag.

Reclassify the past without touching the books. Recovering means flagging old sales. The report rereads the stored invoice XMLs (each item’s EXTIPI comes from there) and applies the table and rules to the sales of the last 60 months. It only flags items: entries, calculations and returns already filed stay unchanged. Amending a return is a deliberate act, outside the system.

Refund with a deadline and the origin of each number. For each month: monophasic revenue, the part already segregated, the bracket’s effective PIS and COFINS, the overpayment and the last day to claim (DAS due date plus five years). The report says where the rate came from: the filed return, the calculation stored in the system, or an estimate from revenue history. “To be confirmed” items stay out of the total until confirmed.

An expiry date written into the code. PIS and COFINS under Simples Nacional give way to the new CBS and IBS from 2027-01 (CGSN Resolution 190/2026). The flag still exists, but from 2027 on it is only a warning, and the refund report stops at 2026-12.

4. Bugs found with real data

The table passed the tests written from the law. The two bugs below only showed up when the engine ran over the pilot’s real invoices, in the development environment and before release: neither reached production. Each one became a regression test.

8431.43.90: a drilling rig part is not an excavator part. Item 2 of Annex II reaches parts of machines of heading 84.29 (excavators, loaders, crawler tractors), and those parts sit in 84.31. But 84.31 also groups parts of other machines. Code 8431.43.90 came up as an Annex II candidate, and it covers parts of boring and drilling machinery (8430.41 and 8430.49), which the law does not reach. The fix was to read the structure of the heading: only 8431.49 can be a part of an 84.29 machine; 8431.41 and 8431.42 are already in Annex I; the rest of 84.31 is out.

EXTIPI “0” read as an Ex. Five auto-part codes are monophasic only under a TIPI Ex (for example, 8481.80.99 only under Ex 01 or 02). Many issuers fill the invoice’s EXTIPI field with “0” when the product has no Ex. The code left-padded it, read “Ex 00”, did not find it in the list and answered “not monophasic”. It was a silent false negative, the kind that makes a business pay twice without anyone noticing. Now empty or all zeros means “no Ex”, and the item is “to be confirmed”.

A third finding came from reading the sources, not the invoices: SPED table 4.3.10 still lists heading 33.06 (oral hygiene), which left single-stage taxation with Law 12,839/2013. When the table and the law disagree, the law prevails, and a comment in the code says why.

5. A principle for tax AI

The engine is deterministic on purpose: the rule is law, and law is not settled by probability. But it made clear how AI should enter this domain. It answers only with a source: every statement points to the law, article or table it came from, and that source must exist in the knowledge base consulted. When no official source confirms, the right answer is “to be confirmed”, never a well-written guess.

It is the same contract as the table: the source field of each group and the “to be confirmed” state are the model-free version of that idea. For an AI, the rule becomes a check: an answer without a valid citation is rejected before it reaches the user.

6. How it is measured

7. Numbers

Measure Value
Product groups 9, each with law and article
NCM prefixes in the table 121
NCM codes depending on a TIPI Ex 5 (and 1 excluded only under an Ex)
Refund window 60 months, up to 2026-12
Check blocks in the Escrituro suite 13, from the table to the report
Tests in the public repository 20
Bugs found with real invoices 2, both with regression tests

8. Lessons

9. Limits and next steps

10. Stack and code

$ npm run ncm -- 84818099 --ex 0
8481.80.99  A CONFIRMAR  (Autopeças (Anexo I))
  o NCM 84818099 só é monofásico no Ex 01 ou 02 da TIPI, e a nota não informa o Ex
  Fonte: Lei 10.485/2002, art. 3º e Anexo I; IN RFB 2.121/2022, Anexo I (códigos da TIPI atual)