Repository: joke2k/faker
Pull request #558 (2017-07-17 22:33:47)

Adding an implementation of `company_vat` for pl_PL company provider.
NIP is a tax identification number in Poland.


Patch:
diff --git a/faker/providers/company/pl_PL/__init__.py b/faker/providers/company/pl_PL/__init__.py
index a8d81a40..719d7e9b 100644
--- a/faker/providers/company/pl_PL/__init__.py
+++ b/faker/providers/company/pl_PL/__init__.py
@@ -38,6 +38,21 @@ def local_regon_checksum(digits):
     return check_digit
 
 
+def company_vat_checksum(digits):
+    """
+    Calculates and returns a control digit for given list of digits basing on NIP standard.
+    """
+    weights_for_check_digit = [6, 5, 7, 2, 3, 4, 5, 6, 7]
+    check_digit = 0
+
+    for i in range(0, 9):
+        check_digit += weights_for_check_digit[i] * digits[i]
+
+    check_digit %= 11
+
+    return check_digit
+
+
 class Provider(CompanyProvider):
 
     @classmethod
@@ -74,3 +89,29 @@ class Provider(CompanyProvider):
         regon_digits.append(local_regon_checksum(regon_digits))
 
         return ''.join(str(digit) for digit in regon_digits)
+
+    @classmethod
+    def company_vat(cls):
+        """
+        Returns 10 character tax identification number,
+        Polish: Numer identyfikacji podatkowej.
+
+        https://pl.wikipedia.org/wiki/NIP
+        """
+        vat_digits = []
+
+        for _ in range(3):
+            vat_digits.append(cls.random_digit_not_null())
+
+        for _ in range(6):
+            vat_digits.append(cls.random_digit())
+
+        check_digit = company_vat_checksum(vat_digits)
+
+        # in this case we must generate a tax number again, because check_digit cannot be 10
+        if check_digit == 10:
+            return cls.company_vat()
+
+        vat_digits.append(check_digit)
+
+        return ''.join(str(digit) for digit in vat_digits)


Test patch:
diff --git a/tests/providers/company.py b/tests/providers/company.py
index 62c864d4..06450c0a 100644
--- a/tests/providers/company.py
+++ b/tests/providers/company.py
@@ -9,7 +9,8 @@ from faker import Factory
 from faker.providers.company.hu_HU import Provider as HuProvider
 from faker.providers.company.ja_JP import Provider as JaProvider
 from faker.providers.company.pt_BR import Provider as PtProvider, company_id_checksum
-from faker.providers.company.pl_PL import Provider as PlProvider, regon_checksum, local_regon_checksum
+from faker.providers.company.pl_PL import (Provider as PlProvider, regon_checksum, local_regon_checksum,
+                                           company_vat_checksum)
 from .. import string_types
 
 
@@ -87,13 +88,23 @@ class TestPlPL(unittest.TestCase):
             self.assertTrue(re.search(r'^\d{9}$', PlProvider.regon()))
 
     def test_local_regon_checksum(self):
-        self.assertEquals(local_regon_checksum([1, 2, 3, 4, 5, 6, 7, 8, 5, 1, 2, 3, 4]), 7)
-        self.assertEquals(local_regon_checksum([6, 1, 1, 9, 4, 8, 8, 3, 2, 7, 5, 8, 0]), 3)
-        self.assertEquals(local_regon_checksum([8, 9, 2, 0, 0, 3, 6, 6, 0, 7, 0, 3, 2]), 3)
-        self.assertEquals(local_regon_checksum([3, 5, 7, 7, 1, 0, 2, 2, 2, 5, 4, 3, 3]), 0)
-        self.assertEquals(local_regon_checksum([9, 3, 5, 3, 1, 1, 0, 1, 2, 4, 8, 8, 2]), 1)
+        self.assertEqual(local_regon_checksum([1, 2, 3, 4, 5, 6, 7, 8, 5, 1, 2, 3, 4]), 7)
+        self.assertEqual(local_regon_checksum([6, 1, 1, 9, 4, 8, 8, 3, 2, 7, 5, 8, 0]), 3)
+        self.assertEqual(local_regon_checksum([8, 9, 2, 0, 0, 3, 6, 6, 0, 7, 0, 3, 2]), 3)
+        self.assertEqual(local_regon_checksum([3, 5, 7, 7, 1, 0, 2, 2, 2, 5, 4, 3, 3]), 0)
+        self.assertEqual(local_regon_checksum([9, 3, 5, 3, 1, 1, 0, 1, 2, 4, 8, 8, 2]), 1)
 
     def test_local_regon(self):
         for _ in range(100):
             self.assertTrue(re.search(r'^\d{14}$', PlProvider.local_regon()))
 
+    def test_company_vat_checksum(self):
+        self.assertEqual(company_vat_checksum([7, 7, 5, 7, 7, 7, 6, 0, 5]), 9)
+        self.assertEqual(company_vat_checksum([1, 8, 6, 5, 4, 9, 9, 6, 4]), 2)
+        self.assertEqual(company_vat_checksum([7, 1, 2, 8, 9, 2, 4, 9, 9]), 7)
+        self.assertEqual(company_vat_checksum([3, 5, 4, 6, 1, 0, 6, 5, 8]), 4)
+        self.assertEqual(company_vat_checksum([3, 1, 9, 5, 5, 7, 0, 4, 5]), 0)
+
+    def test_company_vat(self):
+        for _ in range(100):
+            self.assertTrue(re.search(r'^\d{10}$', PlProvider.company_vat()))