DEV Community

tarun Vaghasia
tarun Vaghasia

Posted on

Matching a Purchase Register Against GSTR-2B: A Practical Algorithm

Every business that claims input tax credit in India runs into the same reconciliation problem sooner or later: you have a purchase register (invoices you recorded), and you have GSTR-2B (invoices your suppliers reported to the GST network). They're supposed to agree. They don't, not perfectly, and "not perfectly" is doing a lot of work in that sentence — a supplier types an invoice number with a stray hyphen, rounds a value to the nearest rupee, or files three days later than you expected.

A naive invoiceNo === invoiceNo join will tell you almost nothing useful. Here's a three-pass approach that actually holds up against real invoice data, and the edge case that will bite you if you skip pass one.

The shape of the problem

Two lists of invoices, same supplier, same period, should-be-identical:

Field Your books GSTR-2B
Invoice number INV/2026/0847 INV20260847
Date 2026-10-05 2026-10-07
Value ₹1,18,000.00 ₹1,18,000.40

Same invoice. A strict equality join matches none of these three fields. If your reconciliation report treats this as "missing," you'll spend your afternoon manually re-checking invoices that were never actually wrong — which is worse than useless, because it trains whoever reads the report to stop trusting it.

Pass 1: normalize before you compare

Before any matching logic runs at all, normalize both invoice-number columns the same way:

const normalizeInvoiceNo = s =>
  String(s ?? '').toUpperCase().replace(/[\s/\-.,_]/g, '');

normalizeInvoiceNo('INV/2026/0847'); // 'INV20260847'
normalizeInvoiceNo('inv-2026-0847'); // 'INV20260847'
Enter fullscreen mode Exit fullscreen mode

This alone resolves a surprising fraction of "mismatches" that are really just formatting differences. Do this once, up front, to both sides — not inline inside your comparison function, where it's easy to normalize one side and forget the other.

Pass 2: a strict triple match

With normalized invoice numbers, run the cheap pass first: exact invoice number, exact date, value within a small rupee tolerance (a few paise of rounding is not a mismatch worth flagging). Pull every row that clears all three out of the pool before doing anything more expensive.

function strictMatch(bookRow, portalRow) {
  return (
    normalizeInvoiceNo(bookRow.invoiceNo) === normalizeInvoiceNo(portalRow.invoiceNo) &&
    bookRow.invoiceDate === portalRow.invoiceDate &&
    Math.abs(bookRow.invoiceValue - portalRow.invoiceValue) <= 1 // ₹1 tolerance
  );
}
Enter fullscreen mode Exit fullscreen mode

This is most of your invoices on a clean month. Resist the urge to skip straight to fuzzy scoring for everything — strict-first is both faster and safer, because a scored match can occasionally rank a wrong candidate above the right one when two invoices happen to be similar.

Pass 3: score what's left

For whatever didn't clear the strict pass, score every remaining books-row/portal-row pair on three signals, and keep the highest-scoring pair above a minimum threshold:

function fuzzyScore(bookRow, portalRow) {
  const nb = normalizeInvoiceNo(bookRow.invoiceNo);
  const np = normalizeInvoiceNo(portalRow.invoiceNo);
  let score = 0;

  // Invoice number: exact (after normalizing) scores highest; one
  // containing the other (a prefix/suffix typo) still counts for something.
  score += nb === np ? 5 : (nb && np && (nb.includes(np) || np.includes(nb))) ? 3 : 0;

  // Date: exact match scores highest; close dates still count — GSTR-2B
  // can lag a supplier's own invoice date by a few days.
  const daysApart = Math.abs((new Date(bookRow.invoiceDate) - new Date(portalRow.invoiceDate)) / 86400000);
  score += bookRow.invoiceDate === portalRow.invoiceDate ? 3 : daysApart <= 3 ? 1 : 0;

  // Value: exact match scores highest; within ~2% still counts (partial
  // credit notes, rounding, a late-applied discount).
  const valuesClose = Math.abs(bookRow.invoiceValue - portalRow.invoiceValue) <= 1;
  score += valuesClose ? 3 : Math.abs(bookRow.invoiceValue - portalRow.invoiceValue) <= bookRow.invoiceValue * 0.02 ? 2 : 0;

  return score;
}
Enter fullscreen mode Exit fullscreen mode

Anything that still doesn't clear your threshold after pass 2 is genuinely unmatched — either only in your books (the supplier hasn't reported it) or only in the portal data (you haven't recorded it yet). Report those two buckets separately; they mean different things and need different follow-up.

The mistake that will cost you a day

The first version of this I tested scored invoice-number similarity with includes() in both directions — nb.includes(np) || np.includes(nb) — and nothing else guarding it. On a real purchase register, that matched INV0847 against INV08471, INV08472, and INV08473 all with the same score, because a short invoice number is a substring of several longer ones in the same batch. The date and value signals in the same score are there specifically to break that tie — don't ship the invoice-number check without them, even as a "quick first version," because the quick version silently produces wrong matches that look plausible at a glance.

Where the GSTR-2B data itself comes from

If you're building this against India's GST network rather than a static test file, you still need the GSTR-2B statement itself before you can match anything against it. It's an auto-drafted statement the network generates per GSTIN per return period — gstinapi.in's GSTR-2B API covers fetching it with POST /v1/gstr2b/fetch once a GSTIN is connected with a one-time GST-portal OTP.

If you'd rather not build and maintain the matching logic yourself, gstinapi.in's GSTR-2B reconciliation runs this exact kind of three-pass match for you — upload a purchase register, it fetches that period's GSTR-2B and sorts every invoice into matched, partial, books-only or portal-only.

Top comments (0)