diff --git a/kfet/forms.py b/kfet/forms.py index 4ba5f7c1..2c01c3c2 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -155,7 +155,6 @@ class KPsulOperationForm(forms.ModelForm): "Un achat nécessite un article et une quantité") if article_nb < 1: raise ValidationError("Impossible d'acheter moins de 1 article") - self.cleaned_data['amount'] = article.price * article_nb self.cleaned_data['is_checkout'] = True elif type_ope and type_ope in [Operation.DEPOSIT, Operation.WITHDRAW]: if not amount or article or article_nb: @@ -168,5 +167,5 @@ class KPsulOperationForm(forms.ModelForm): KPsulOperationFormSet = modelformset_factory( Operation, form = KPsulOperationForm, - extra = 0, + extra = 1, min_num = 1, validate_min = True) diff --git a/kfet/migrations/0011_auto_20160807_1720.py b/kfet/migrations/0011_auto_20160807_1720.py new file mode 100644 index 00000000..97525676 --- /dev/null +++ b/kfet/migrations/0011_auto_20160807_1720.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('kfet', '0010_auto_20160806_2343'), + ] + + operations = [ + migrations.AlterField( + model_name='operation', + name='amount', + field=models.DecimalField(decimal_places=2, max_digits=6, default=0, blank=True), + ), + ] diff --git a/kfet/views.py b/kfet/views.py index b3eee1f2..7c801b9b 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -402,15 +402,24 @@ def kpsul_perform_operations(request): if 'errors' in data: return JsonResponse(data, status=400) - # Pre-saving + # Pre-saving (no commit) operationgroup = operationgroup_form.save(commit = False) operations = operation_formset.save(commit = False) + # Calculating amount of each PURCHASE operations + # and total amount for operation group for operation in operations: + if operation.type == Operation.PURCHASE: + operation.amount = - operation.article.price * operation.article_nb operationgroup.amount += operation.amount - operationgroup.save() + # Filling cof status for statistics + operationgroup.is_cof = operationgroup.on_acc.is_cof + # Saving operation group + operationgroup.save() data['operationgroup'] = operationgroup.pk + + # Filling operationgroup id for each operations and saving for operation in operations: operation.group = operationgroup operation.save()