diff --git a/apps/note/templatetags/pretty_money.py b/apps/note/templatetags/pretty_money.py index 12530c6e..f821e819 100644 --- a/apps/note/templatetags/pretty_money.py +++ b/apps/note/templatetags/pretty_money.py @@ -18,5 +18,10 @@ def pretty_money(value): ) +def cents_to_euros(value): + return "{:.02f}".format(value / 100) if value else "" + + register = template.Library() register.filter('pretty_money', pretty_money) +register.filter('cents_to_euros', cents_to_euros) diff --git a/apps/treasury/views.py b/apps/treasury/views.py index 170a837d..a5d31d66 100644 --- a/apps/treasury/views.py +++ b/apps/treasury/views.py @@ -44,7 +44,15 @@ class BillingCreateView(LoginRequiredMixin, CreateView): def form_valid(self, form): ret = super().form_valid(form) - formset = ProductFormSet(self.request.POST, instance=form.instance) + kwargs = {} + for key in self.request.POST: + value = self.request.POST[key] + if key.endswith("amount"): + kwargs[key] = str(int(100 * float(value))) + else: + kwargs[key] = value + + formset = ProductFormSet(kwargs, instance=form.instance) if formset.is_valid(): for f in formset: if f.is_valid() and f.instance.designation: @@ -89,8 +97,17 @@ class BillingUpdateView(LoginRequiredMixin, UpdateView): def form_valid(self, form): ret = super().form_valid(form) - formset = ProductFormSet(self.request.POST, instance=form.instance) + kwargs = {} + for key in self.request.POST: + value = self.request.POST[key] + if key.endswith("amount"): + kwargs[key] = str(int(100 * float(value))) + else: + kwargs[key] = value + + formset = ProductFormSet(kwargs, instance=form.instance) saved = [] + print(formset.errors) if formset.is_valid(): for f in formset: if f.is_valid() and f.instance.designation: diff --git a/templates/treasury/billing_form.html b/templates/treasury/billing_form.html index 42f4201c..9c1416ed 100644 --- a/templates/treasury/billing_form.html +++ b/templates/treasury/billing_form.html @@ -1,10 +1,10 @@ {% extends "base.html" %} {% load static %} {% load i18n %} -{% load crispy_forms_tags %} +{% load crispy_forms_tags pretty_money %} {% block content %}

{% trans "Billings list" %}

-
+ {% csrf_token %} {% crispy form %} {{ formset.management_form }} @@ -21,7 +21,16 @@ {{ form.designation }} {{ form.quantity }} - {{ form.amount }} + +
+ +
+ +
+
+ {{ form.billing }} {{ form.id }} @@ -45,7 +54,15 @@ {{ formset.empty_form.designation }} {{ formset.empty_form.quantity }} - {{ formset.empty_form.amount }} + +
+ +
+ +
+
+ {{ formset.empty_form.billing }} {{ formset.empty_form.id }}