Skip to content

Commit

Permalink
Merge pull request #6 from gisce/IMP_update_n57_with_duedate
Browse files Browse the repository at this point in the history
Update Recibo507 with Due Date
  • Loading branch information
guilleJB committed Oct 26, 2015
2 parents deb5e8d + e9add58 commit 2ffd069
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 1 deletion.
52 changes: 51 additions & 1 deletion bankbarcode/cuaderno57.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=utf-8
from datetime import datetime

from bankbarcode import BankBarcode
from decimal import Decimal
Expand Down Expand Up @@ -76,14 +77,41 @@ 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):
"""
Receipt (Recibo) 507 for Recibos y otros (Cobros por Ventanilla y \
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.
Expand All @@ -92,13 +120,15 @@ 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
self.suffix = suffix
self.ref = ref
self.notice = notice
self.amount = amount
self.due_date = due_date

@property
def entity(self):
Expand Down Expand Up @@ -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)
Expand Down
Binary file added spec/20150115_FOLLETO57.pdf
Binary file not shown.
55 changes: 55 additions & 0 deletions spec/cuaderno57_Recibo507_due_date_spec.py
Original file line number Diff line number Diff line change
@@ -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))
36 changes: 36 additions & 0 deletions spec/cuaderno57_Recibo507_input_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
45 changes: 45 additions & 0 deletions spec/cuaderno57_Recibo_input_spec.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'))

0 comments on commit 2ffd069

Please sign in to comment.