summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristophe Boulanger <christophe.boulanger@imio.be>2018-03-06 11:12:07 (GMT)
committerChristophe Boulanger <christophe.boulanger@imio.be>2018-03-06 11:12:07 (GMT)
commit6bb00d4c721efbfc1c039772e879f8fd15d58eef (patch)
treee513b029f7b535ff4364a5ff2779476dbf2f0d7f
parent3653926f59248e1b62734981a065f9678a36ea7f (diff)
downloadpasserelle-imio-extra-fees-6bb00d4c721efbfc1c039772e879f8fd15d58eef.zip
passerelle-imio-extra-fees-6bb00d4c721efbfc1c039772e879f8fd15d58eef.tar.gz
passerelle-imio-extra-fees-6bb00d4c721efbfc1c039772e879f8fd15d58eef.tar.bz2
Make more generic Postage fees. Add 4 fields and 1 profile field to keep Namur like an 'exception'. Next step : Higher genericity.
-rw-r--r--passerelle_imio_extra_fees/migrations/0003_auto_20180305_1152.py50
-rw-r--r--passerelle_imio_extra_fees/models.py55
2 files changed, 103 insertions, 2 deletions
diff --git a/passerelle_imio_extra_fees/migrations/0003_auto_20180305_1152.py b/passerelle_imio_extra_fees/migrations/0003_auto_20180305_1152.py
new file mode 100644
index 0000000..60a7e87
--- /dev/null
+++ b/passerelle_imio_extra_fees/migrations/0003_auto_20180305_1152.py
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.core.validators
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('passerelle_imio_extra_fees', '0002_auto_20170921_1213'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='extrafees',
+ name='belgium_postage_fee',
+ field=models.DecimalField(default=0.0, verbose_name='Postage fees for belgium', max_digits=6, decimal_places=2),
+ ),
+ migrations.AddField(
+ model_name='extrafees',
+ name='europe_postage_fee',
+ field=models.DecimalField(default=0.0, verbose_name='Postage fees for europe', max_digits=6, decimal_places=2),
+ ),
+ migrations.AddField(
+ model_name='extrafees',
+ name='max_doc_in_letter',
+ field=models.PositiveSmallIntegerField(default=5, verbose_name='Maximum documents in one letter', validators=[django.core.validators.MaxValueValidator(100), django.core.validators.MinValueValidator(1)]),
+ ),
+ migrations.AddField(
+ model_name='extrafees',
+ name='profile',
+ field=models.CharField(default=b'Default', max_length=30, choices=[(b'DEFAULT', b'Default'), (b'NAMUR', b'Namur')]),
+ ),
+ migrations.AddField(
+ model_name='extrafees',
+ name='world_postage_fee',
+ field=models.DecimalField(default=0.0, verbose_name='Postage fees for rest of the world', max_digits=6, decimal_places=2),
+ ),
+ migrations.AlterField(
+ model_name='extrafees',
+ name='description',
+ field=models.TextField(verbose_name='Description'),
+ ),
+ migrations.AlterField(
+ model_name='extrafees',
+ name='title',
+ field=models.CharField(max_length=50, verbose_name='Title'),
+ ),
+ ]
diff --git a/passerelle_imio_extra_fees/models.py b/passerelle_imio_extra_fees/models.py
index 0e0c277..050b7f7 100644
--- a/passerelle_imio_extra_fees/models.py
+++ b/passerelle_imio_extra_fees/models.py
@@ -16,6 +16,8 @@
import json
+from django.core.validators import MaxValueValidator, MinValueValidator
+from django.db import models
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
@@ -25,6 +27,19 @@ from decimal import Decimal
class ExtraFees(BaseResource):
category = _('Misc')
+ PROFILE_CHOICES = (
+ ('DEFAULT','Default'),
+ ('NAMUR','Namur'),
+ )
+
+ profile = models.CharField(max_length=30, choices=PROFILE_CHOICES, default='Default')
+ max_doc_in_letter = models.PositiveSmallIntegerField(verbose_name=_('Maximum documents in one letter'),
+ default=5,
+ validators=[MaxValueValidator(100), MinValueValidator(1)])
+ belgium_postage_fee = models.DecimalField(decimal_places=2, max_digits=6, verbose_name=_('Postage fees for belgium'), default=0.00)
+ europe_postage_fee = models.DecimalField(decimal_places=2, max_digits=6, verbose_name=_('Postage fees for europe'), default=0.00)
+ world_postage_fee = models.DecimalField(decimal_places=2, max_digits=6, verbose_name=_('Postage fees for rest of the world'), default=0.00)
+ # free_postage_exeptions_list_doc ?
class Meta:
verbose_name = _('Extra Fees')
@@ -33,8 +48,28 @@ class ExtraFees(BaseResource):
def get_connector_slug(cls):
return 'extra-fees'
- @endpoint(methods=['post'])
- def compute(self, request, **kwargs):
+
+ # Minimum requirement in request to compute basket with postage fee
+ # nb_documents : Number of desired documents for one citizen posted form.
+ # postage_fee : THanks to citizen choice (Be/Eu/Wrld), Postage Fee value for this form.
+ def default_compute(self, request, **kwargs):
+ nb_documents = 0
+ nb_letter = 0
+ data = json.loads(request.body)
+ postage_fee = Decimal(self.belgium_postage_fee)
+ for basket_item in data['data']:
+ try:
+ nb_documents += int(basket_item['request_data']['nb_documents'])
+ if Decimal(basket_item['request_data']['postage_fee']) > postage_fee:
+ postage_fee = Decimal(basket_item['request_data']['postage_fee'])
+ except KeyError:
+ # basket item not associated with any document, no fee
+ pass
+ nb_letter = int(nb_documents / int(self.max_doc_in_letter)) + (((nb_documents % int(self.max_doc_in_letter)) > 0) and 1 or 0)
+ postage_fee = nb_letter * postage_fee
+ return {'data': [{'subject': force_text(_('Postage')), 'amount': str(postage_fee)}]}
+
+ def namur_compute(self, request, **kwargs):
data = json.loads(request.body)
# EXCEPTIONS :
# duplicata-de-livret-de-mariage (frais port 8 ou 13)
@@ -74,3 +109,19 @@ class ExtraFees(BaseResource):
if duplicata_exception == True:
postage_fee = duplicata_country_price if duplicata_country_price > postage_fee else postage_fee
return {'data': [{'subject': force_text(_('Postage')), 'amount': str(postage_fee)}]}
+
+ @endpoint(methods=['post'])
+ def compute(self, request, **kwargs):
+ if self.profile == 'NAMUR':
+ data = self.namur_compute(request, **kwargs)
+ else:
+ data = self.default_compute(request, **kwargs)
+ return data
+
+ @endpoint()
+ def destination_choices(self, request, q=None, **kwargs):
+ destination_choices = {'data':[{'id':'belgium', 'text':'En belgique', 'fee':self.belgium_postage_fee},
+ {'id':'europe', 'text':'En europe', 'fee':self.europe_postage_fee},
+ {'id':'world', 'text':'Dans le reste du monde', 'value':self.world_postage_fee}
+ ]}
+ return destination_choices