We store amounts in cents but users enter euros

This commit is contained in:
Yohann D'ANELLO 2020-03-21 18:59:13 +01:00
parent a220ca7549
commit 1c6d69ab9d
3 changed files with 45 additions and 6 deletions

View File

@ -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)

View File

@ -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:

View File

@ -1,10 +1,10 @@
{% extends "base.html" %}
{% load static %}
{% load i18n %}
{% load crispy_forms_tags %}
{% load crispy_forms_tags pretty_money %}
{% block content %}
<p><a class="btn btn-default" href="{% url 'treasury:billing' %}">{% trans "Billings list" %}</a></p>
<form method="post" action="" class="form-horizontal">
<form method="post" action="">
{% csrf_token %}
{% crispy form %}
{{ formset.management_form }}
@ -21,7 +21,16 @@
<tr class="row-formset">
<td>{{ form.designation }}</td>
<td>{{ form.quantity }} </td>
<td>{{ form.amount }}</td>
<td>
<div class="input-group">
<input type="number" name="product_set-{{ forloop.counter0 }}-amount" min="0" step="0.01"
id="id_product_set-{{ forloop.counter0 }}-amount"
value="{{ form.instance.amount|cents_to_euros }}">
<div class="input-group-append">
<span class="input-group-text"></span>
</div>
</div>
</td>
{{ form.billing }}
{{ form.id }}
</tr>
@ -45,7 +54,15 @@
<tr class="row-formset">
<td>{{ formset.empty_form.designation }}</td>
<td>{{ formset.empty_form.quantity }} </td>
<td>{{ formset.empty_form.amount }}</td>
<td>
<div class="input-group">
<input type="number" name="product_set-__prefix__-amount" min="0" step="0.01"
id="id_product_set-__prefix__-amount">
<div class="input-group-append">
<span class="input-group-text"></span>
</div>
</div>
</td>
{{ formset.empty_form.billing }}
{{ formset.empty_form.id }}
</tr>