Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/gr/vat.py
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2012-02-10 14:06:31 +0100
committerArthur de Jong <arthur@arthurdejong.org>2012-02-10 14:06:31 +0100
commit473b3cab02927707d11adb6f24e3c2c753ea2036 (patch)
tree35fa60487c96a71b7a001e0760087724bc3a4077 /stdnum/gr/vat.py
parent9f1d47b3ca49227fa5316066f264ab61cccdb03c (diff)
add a FPA, ΦΠΑ (Foros Prostithemenis Aksias, the Greek VAT number) module
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@103 9dea7c4f-944c-4273-ac1a-574ede026edc
Diffstat (limited to 'stdnum/gr/vat.py')
-rw-r--r--stdnum/gr/vat.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/stdnum/gr/vat.py b/stdnum/gr/vat.py
new file mode 100644
index 0000000..e37fe95
--- /dev/null
+++ b/stdnum/gr/vat.py
@@ -0,0 +1,63 @@
+# vat.py - functions for handling Greek 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 Greek VAT (Foros Prostithemenis Aksias, FPA, ΦΠΑ)
+numbers.
+
+>>> compact('GR 23456783')
+'023456783'
+>>> is_valid('EL 094259216 ')
+True
+>>> is_valid('EL 123456781')
+False
+"""
+
+from stdnum.util import clean
+
+
+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('EL') or number.startswith('GR'):
+ number = number[2:]
+ if len(number) == 8:
+ number = '0' + number # old format had 8 digits
+ return number
+
+
+def calc_check_digit(number):
+ """Calculate the check digit. The number passed should not have the
+ check digit included."""
+ checksum = 0
+ for n in number:
+ checksum = checksum * 2 + int(n)
+ return str(checksum * 2 % 11 % 10)
+
+
+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
+ return len(number) == 9 and number.isdigit() and \
+ calc_check_digit(number[:-1]) == number[-1]