Skip to content

fix(cz/dic): correct special 9-digit DIC check to match python-stdnum (fixes #164) - #169

Open
youdie006 wants to merge 1 commit into
koblas:mainfrom
youdie006:fix/164-cz-dic-special-check
Open

fix(cz/dic): correct special 9-digit DIC check to match python-stdnum (fixes #164)#169
youdie006 wants to merge 1 commit into
koblas:mainfrom
youdie006:fix/164-cz-dic-special-check

Conversation

@youdie006

Copy link
Copy Markdown

Fixes #164.

Root cause

cz/dic checkSpecial (the special 9-digit DIČ case, numbers starting with 6) diverged from the python-stdnum reference (cz/dic.py::calc_check_digit_special) in two ways:

  1. It included the leading 6 in the weighted sum (splitAt(value, -1) -> 8-digit front, weights [8, 7, 6, 5, 4, 3, 2, 1]). The reference sums digits 2..8 only (number[1:8], weights [8, 7, 6, 5, 4, 3, 2]), excluding both the leading 6 and the check digit.
  2. The final reduction (8 - ((10 - sum) % 11)) % 10 relied on Python's always-non-negative modulo. JS % keeps the dividend's sign, so when the inner value is negative the computed digit becomes e.g. "-1" and never matches.

As a result every special-case Czech VAT (9-digit DIČ starting with 6) was falsely rejected via validate() -> InvalidChecksum.

Fix

Drop the leading digit from the weighted sum, use weights [8, 7, 6, 5, 4, 3, 2], and compute the final digit with the existing pymod helper (Python modulo).

Verified valid VATs (accepted per the EU VIES service)

  • CZ687836437, CZ681208919 (from the issue)
  • 640903926 (python-stdnum's own doctest)
  • 699005400 (VAT-group ID, format 699nnnnnk, reported in the issue comment)

Wrong check digit (687836438) is still rejected, and normal non-special DIČ (e.g. CZ991231123, delegated to cz/rc) is unchanged.

Testing

Red/green verified: reverting the fix fails exactly the four valid special VATs; with the fix all pass. Full Jest suite (200 suites / 1641 tests), tsc --noEmit, and eslint all clean.


This contribution was prepared with AI assistance (Claude); the change and tests were reviewed and verified locally.

cz/dic checkSpecial (the special 9-digit DIC case, numbers starting with 6)
diverged from the python-stdnum reference (cz/dic.py::calc_check_digit_special)
two ways: (1) it included the leading 6 in the weighted sum (splitAt(value,-1) ->
8-digit front, weights [8,7,6,5,4,3,2,1]); the reference sums digits 2..8 only
(number[1:8], weights [8,7,6,5,4,3,2]), excluding the leading 6 and the check
digit. (2) the final reduction (8 - ((10 - sum) % 11)) % 10 relied on Python's
non-negative modulo, but JS % keeps the dividend's sign, so a negative inner value
yielded e.g. '-1' and never matched. As a result every special-case Czech VAT was
falsely rejected via validate() -> InvalidChecksum.

Drop the leading digit from the weighted sum, use weights [8,7,6,5,4,3,2], and
compute the final digit with the existing pymod helper.

Fixes koblas#164
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.

cz/dic rejects valid Czech VAT: checkSpecial differs from python-stdnum

1 participant