lists.arthurdejong.org
RSS feed

python-stdnum commit: r149 - in python-stdnum: . stdnum stdnum/bg tests

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum commit: r149 - in python-stdnum: . stdnum stdnum/bg tests



Author: arthur
Date: Sat Feb 18 22:52:42 2012
New Revision: 149
URL: http://arthurdejong.org/viewvc/python-stdnum?revision=149&view=revision

Log:
add a VAT (Идентификационен номер по ДДС, Bulgarian VAT numbers) module

Added:
   python-stdnum/stdnum/bg/vat.py
   python-stdnum/tests/test_bg_vat.doctest
Modified:
   python-stdnum/README
   python-stdnum/stdnum/__init__.py
   python-stdnum/tests/test_robustness.doctest

Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README        Sat Feb 18 22:32:34 2012        (r148)
+++ python-stdnum/README        Sat Feb 18 22:52:42 2012        (r149)
@@ -59,6 +59,7 @@
  * ID za DDV (Davčna številka, Slovenian VAT number)
  * VAT (Moms, Mervärdesskatt, Swedish VAT number)
  * VAT (United Kingdom (and Isle of Man) VAT registration number)
+ * VAT (Идентификационен номер по ДДС, Bulgarian VAT numbers)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)
  * MEID (Mobile Equipment Identifier)

Modified: python-stdnum/stdnum/__init__.py
==============================================================================
--- python-stdnum/stdnum/__init__.py    Sat Feb 18 22:32:34 2012        (r148)
+++ python-stdnum/stdnum/__init__.py    Sat Feb 18 22:52:42 2012        (r149)
@@ -73,6 +73,7 @@
  * ID za DDV (Davčna številka, Slovenian VAT number)
  * VAT (Moms, Mervärdesskatt, Swedish VAT number)
  * VAT (United Kingdom (and Isle of Man) VAT registration number)
+ * VAT (Идентификационен номер по ДДС, Bulgarian VAT numbers)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)
  * MEID (Mobile Equipment Identifier)

Added: python-stdnum/stdnum/bg/vat.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/bg/vat.py      Sat Feb 18 22:52:42 2012        (r149)
@@ -0,0 +1,79 @@
+# vat.py - functions for handling Bulgarian VAT numbers
+# coding: utf-8
+#
+# Copyright (C) 2012 Arthur de Jong
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+"""Module for handling Bulgarian VAT (Идентификационен номер по ДДС) numbers.
+
+The Bulgarian VAT (Данък върху добавената стойност) number is either 9
+(for legal entities) or 10 digits (for physical persons, foreigners and
+others) long. Each type of number has it's own check digit algorithm.
+
+>>> compact('BG 175 074 752')
+'175074752'
+>>> is_valid('175074752')
+True
+>>> is_valid('175074751')  # invalid check digit
+False
+"""
+
+from stdnum.util import clean
+from stdnum.bg import egn, pnf
+
+
+def compact(number):
+    """Convert the number to the minimal representation. This strips the
+    number of any valid separators and removes surrounding whitespace."""
+    number = clean(number, ' -.').upper().strip()
+    if number.startswith('BG'):
+        number = number[2:]
+    return number
+
+
+def calc_check_digit_legal(number):
+    """Calculate the check digit for legal entities. The number passed
+    should not have the check digit included."""
+    check = sum((i + 1) * int(n) for i, n in enumerate(number)) % 11
+    if check == 10:
+        check = sum((i + 3) * int(n) for i, n in enumerate(number)) % 11
+    return str(check % 10)
+
+
+def calc_check_digit_other(number):
+    """Calculate the check digit for others. The number passed should not
+    have the check digit included."""
+    weights = (4, 3, 2, 7, 6, 5, 4, 3, 2)
+    return str((11 - sum(weights[i] * int(n) for i, n in enumerate(number))) % 
11)
+
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid VAT number. This
+    checks the length, formatting and check digit."""
+    try:
+        number = compact(number)
+    except:
+        return False
+    if len(number) == 9 and number.isdigit():
+        # 9 digit numbers are for legal entities
+        return number[-1] == calc_check_digit_legal(number[:-1])
+    if len(number) == 10 and number.isdigit():
+        # 10 digit numbers are for physical persons, foreigners and others
+        return egn.is_valid(number) or \
+               pnf.is_valid(number) or \
+               number[-1] == calc_check_digit_other(number[:-1])
+    return False

Added: python-stdnum/tests/test_bg_vat.doctest
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/tests/test_bg_vat.doctest     Sat Feb 18 22:52:42 2012        
(r149)
@@ -0,0 +1,49 @@
+test_bg_vat.doctest - more detailed doctests for stdnum.bg.vat module
+
+Copyright (C) 2012 Arthur de Jong
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA
+
+
+This file contains more detailed doctests for the stdnum.bg.vat module. It
+tries to cover more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum.bg import vat
+
+
+Normal values that should just work.
+
+>>> vat.is_valid('103873594')  # 9-digit legal entity
+True
+>>> vat.is_valid('131272009')  # legal entity with fallback checksum
+True
+>>> vat.is_valid('7501020018')  # physical person
+True
+>>> vat.is_valid('8001010008')  # physical person
+True
+>>> vat.is_valid('8032056031')  # physical person
+True
+>>> vat.is_valid('7111042925')  # foreigners
+True
+>>> vat.is_valid('7153849522')  # others
+True
+
+
+Invalid checksum:
+
+>>> vat.is_valid('175074751')  # invalid check digit
+False

Modified: python-stdnum/tests/test_robustness.doctest
==============================================================================
--- python-stdnum/tests/test_robustness.doctest Sat Feb 18 22:32:34 2012        
(r148)
+++ python-stdnum/tests/test_robustness.doctest Sat Feb 18 22:52:42 2012        
(r149)
@@ -28,7 +28,7 @@
 >>> from stdnum import luhn, meid, verhoeff
 >>> from stdnum.at import uid
 >>> from stdnum.be import vat as be_vat
->>> from stdnum.bg import egn, pnf
+>>> from stdnum.bg import egn, pnf, vat as bg_vat
 >>> from stdnum.br import cpf
 >>> from stdnum.cy import vat as cy_vat
 >>> from stdnum.cz import dic, rc
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/