diff --git a/bankbarcode/cuaderno57.py b/bankbarcode/cuaderno57.py index d2f5a1c..dea428b 100644 --- a/bankbarcode/cuaderno57.py +++ b/bankbarcode/cuaderno57.py @@ -1,4 +1,5 @@ # coding=utf-8 +from datetime import datetime from bankbarcode import BankBarcode from decimal import Decimal @@ -76,6 +77,32 @@ def _check_amount(self, amount): description = 'amount lenth should be 10' return self._check_length(name, value, expected_length, description) + def _check_due_date(self, due_date, suffix): + """ + Check due date (Fecha limite) and suffix + + :param due_date: Due Date (Fecha limite) + :return: True if due date is a Datetime or string with format '%Y-%m-%d' + and suffix bigger than 499, otherwise False + """ + name = 'due_date' + if due_date is None: + return True + if isinstance(due_date, basestring): + try: + date = datetime.strptime(due_date, '%Y-%m-%d') + except: + raise ValueError( + "{} must be string with format '%Y-%m-%d'".format(name)) + else: + if not isinstance(due_date, datetime): + raise ValueError('{} must be a Datetime'.format(name)) + + if self._check_suffix(suffix) and int(suffix) <= 499: + raise ValueError('suffix with due date must be bigger than 499') + return True + + class Recibo507(Recibo): """ @@ -83,7 +110,8 @@ class Recibo507(Recibo): Autoservicio, V2001) """ - def __init__(self, entity, suffix, ref, notice, amount): + def __init__(self, entity=None, suffix=None, ref=None, notice=None, + amount=None, due_date=None): """ Create and object of Recibo507 with checked values. @@ -92,6 +120,7 @@ def __init__(self, entity, suffix, ref, notice, amount): :param ref: the reference code (Número de referencia) :param notice: the notice identification code (Identificación) :param amount: the amount (Importe) + :param due_date: Due date (Fecha limite) :return: an object of Recibo507 """ self.entity = entity @@ -99,6 +128,7 @@ def __init__(self, entity, suffix, ref, notice, amount): self.ref = ref self.notice = notice self.amount = amount + self.due_date = due_date @property def entity(self): @@ -199,6 +229,26 @@ def amount(self, value): if self._check_amount(unicode_value): self._amount = unicode_value + @property + def due_date(self): + """ + Due date (Fecha limite) + + :return: Datetime with Due date + """ + return self._due_date + + @due_date.setter + def due_date(self, due_date): + if due_date is None: + self._due_date = None + else: + if self._check_due_date(due_date, self.suffix): + if isinstance(due_date, basestring): + due_date = datetime.strptime(due_date, '%Y-%m-%d') + self._due_date = due_date + self._notice = self._due_date.strftime('%d%m%y') + def amount100(self): """ Remove the decimal point of the amount (Importe) diff --git a/spec/20150115_FOLLETO57.pdf b/spec/20150115_FOLLETO57.pdf new file mode 100644 index 0000000..e3bccc9 Binary files /dev/null and b/spec/20150115_FOLLETO57.pdf differ diff --git a/spec/cuaderno57_Recibo507_due_date_spec.py b/spec/cuaderno57_Recibo507_due_date_spec.py new file mode 100644 index 0000000..9fcf148 --- /dev/null +++ b/spec/cuaderno57_Recibo507_due_date_spec.py @@ -0,0 +1,55 @@ +from datetime import datetime + +from expects import expect, equal, raise_error +from bankbarcode.cuaderno57 import Recibo507 + +with description('Recibo507 with due date of cuaderno57'): + with it('Validate than suffix is less than 500'): + def callback(): + entity = '01234567' + suffix = '023' + ref = '12345678901' + notice = '123456' + amount = '6543.21' + due_date = datetime(2015, 11, 01) + recibo = Recibo507(entity, suffix, ref, notice, amount, due_date) + expect(callback).to( + raise_error(ValueError, + 'suffix with due date must be bigger than 499')) + + with it('Overwrites introduced notice for due date DDMMAA'): + entity = '22350466' + suffix = '501' + ref = '00000000015' + notice = '300815' + amount = '53.98' + due_date = datetime(2015, 11, 01) + recibo = Recibo507(entity, suffix, ref, notice, amount, due_date) + expect(recibo.notice).not_to(equal(notice)) + expect(recibo.notice).to(equal(due_date.strftime('%d%m%y'))) + + with it('accomplish the example of cuaderno57 with due date Datetime'): + entity = '22350466' + suffix = '501' + ref = '00000000015' + notice = '300815' + amount = '53.98' + checksum = '27' + due_date = datetime(2015, 11, 01) + recibo = Recibo507(entity, suffix, ref, notice, amount, due_date) + expect(recibo.notice).to(equal(due_date.strftime('%d%m%y'))) + expect(recibo.checksum()).to(equal(checksum)) + + with it('accomplish the example of cuaderno57 with due date string'): + entity = '22350466' + suffix = '501' + ref = '00000000015' + notice = '300815' + amount = '53.98' + checksum = '27' + due_date = '2015-11-01' + recibo = Recibo507(entity, suffix, ref, notice, amount, due_date) + expect(recibo.notice).to(equal( + datetime.strptime(due_date, '%Y-%m-%d').strftime('%d%m%y')) + ) + expect(recibo.checksum()).to(equal(checksum)) diff --git a/spec/cuaderno57_Recibo507_input_spec.py b/spec/cuaderno57_Recibo507_input_spec.py index a06dfe4..071068a 100644 --- a/spec/cuaderno57_Recibo507_input_spec.py +++ b/spec/cuaderno57_Recibo507_input_spec.py @@ -129,3 +129,39 @@ def callback(): self.recibo507._check_amount(float_value) expect(callback).to(raise_error(ValueError, 'amount have more than 2 decimals')) + + with context('Check amount'): + + with it('return True if amount is a unicode with less than 100000000 with 2 decimals'): + value = unicode(round(uniform(0, 99999999.99), 2)) + expect(self.recibo507._check_amount(value)).to(be_true) + + with it('return True is amount is a int less than 100000000'): + value = randint(0, 99999999) + expect(self.recibo507._check_amount(value)).to(be_true) + + with it('raise a value error if amount isn\'t less than 100000000'): + big_value = unicode(round(uniform(100000000, maxint), 2)) + + def callback(): + self.recibo507._check_amount(big_value) + + expect(callback).to(raise_error(ValueError, 'amount is too big')) + + with it('raise a value error if amount have more than 2 decimals'): + float_value = unicode(round(random(), randint(3, 10))) + + def callback(): + self.recibo507._check_amount(float_value) + + expect(callback).to(raise_error(ValueError, 'amount have more than 2 decimals')) + + with context('Check due date'): + with it('raise a value error if due date is not Datetime'): + + def callback(): + self.recibo507.amount = 10 + self.recibo507.due_date = 234 + + expect(callback).to(raise_error(ValueError, + 'due_date must be a Datetime')) diff --git a/spec/cuaderno57_Recibo_input_spec.py b/spec/cuaderno57_Recibo_input_spec.py index c79f3f5..400b0cd 100644 --- a/spec/cuaderno57_Recibo_input_spec.py +++ b/spec/cuaderno57_Recibo_input_spec.py @@ -1,4 +1,5 @@ from sys import maxint +from datetime import datetime from expects import expect, be_true, raise_error from random import randint, uniform, random from bankbarcode.cuaderno57 import Recibo @@ -123,3 +124,47 @@ def callback(): self.recibo._check_amount(float_value) expect(callback).to(raise_error(ValueError, 'amount have more than 2 decimals')) + + with context('Check due date'): + + with it('return True if due date is a Datetime and suffix > 499'): + value = datetime(2015, 1, 1) + expect( + self.recibo._check_due_date(value, '500') + ).to(be_true) + + with it('return True if due date is string with format %Y-%m-%d and suffix > 499'): + value = '2015-01-01' + expect( + self.recibo._check_due_date(value, '500') + ).to(be_true) + + with it('raise a value error if due date isn\'t Datetime'): + value = 2015 + def callback(): + self.recibo._check_due_date(value, '500') + expect(callback).to(raise_error(ValueError, + 'due_date must be a Datetime')) + + with it('raise a value error if due date isn\'t string with format %Y-%m-%d'): + value = 'qwerty' + def callback(): + self.recibo._check_due_date(value, '500') + expect(callback).to(raise_error(ValueError, + "due_date must be string with format '%Y-%m-%d'")) + + with it('raise a value error due date is datetime and suffix isn\'t bigger than 499'): + value = datetime(2015, 1, 1) + suffix = '499' + def callback(): + self.recibo._check_due_date(value, suffix) + expect(callback).to(raise_error(ValueError, + 'suffix with due date must be bigger than 499')) + + with it('raise a value error due date is string if suffix isn\'t bigger than 499'): + value = '2015-01-01' + suffix = '499' + def callback(): + self.recibo._check_due_date(value, suffix) + expect(callback).to(raise_error(ValueError, + 'suffix with due date must be bigger than 499')) \ No newline at end of file