Repair Invoice model

This commit is contained in:
Yohann D'ANELLO 2020-03-22 15:24:54 +01:00
parent f6027e9edd
commit 15bfdd9f96
8 changed files with 35 additions and 71 deletions

View File

@ -8,7 +8,7 @@ from .models import Invoice, Product
@admin.register(Invoice) @admin.register(Invoice)
class InvoiceAdmin(admin.ModelAdmin): class InvoiceAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'subject', 'acquitted', ) list_display = ('id', 'name', 'object', 'acquitted', )
@admin.register(Product) @admin.register(Product)

View File

@ -1,6 +1,8 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import datetime
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from django import forms from django import forms
@ -8,6 +10,14 @@ from .models import Invoice, Product
class InvoiceForm(forms.ModelForm): class InvoiceForm(forms.ModelForm):
date = forms.DateField(
initial=datetime.date.today,
widget=forms.TextInput(attrs={'type': 'date'})
)
def clean_date(self):
self.instance.date = self.data.get("date")
class Meta: class Meta:
model = Invoice model = Invoice
fields = '__all__' fields = '__all__'

View File

@ -25,9 +25,9 @@ class Invoice(models.Model):
verbose_name=_("BDE"), verbose_name=_("BDE"),
) )
subject = models.CharField( object = models.CharField(
max_length=255, max_length=255,
verbose_name=_("Subject"), verbose_name=_("Object"),
) )
description = models.TextField( description = models.TextField(
@ -52,56 +52,6 @@ class Invoice(models.Model):
verbose_name=_("Acquitted"), verbose_name=_("Acquitted"),
) )
place = models.CharField(
max_length=255,
default="Cachan",
verbose_name=_("Place"),
)
my_name = models.CharField(
max_length=255,
default="BDE ENS Cachan",
verbose_name=_("My name"),
)
my_address_street = models.CharField(
max_length=255,
default="61 avenue du Président Wilson",
verbose_name=_("My street address"),
)
my_city = models.CharField(
max_length=255,
default="94230 Cachan",
verbose_name=_("My city"),
)
bank_code = models.IntegerField(
default=30003,
verbose_name=_("Bank code"),
)
desk_code = models.IntegerField(
default=3894,
verbose_name=_("Desk code"),
)
account_number = models.IntegerField(
default=37280662,
verbose_name=_("Account number"),
)
rib_key = models.SmallIntegerField(
default=14,
verbose_name=_("RIB Key")
)
bic = models.CharField(
max_length=16,
default="SOGEFRPP",
verbose_name=_("BIC Code")
)
class Product(models.Model): class Product(models.Model):
invoice = models.ForeignKey( invoice = models.ForeignKey(

View File

@ -29,4 +29,4 @@ class InvoiceTable(tables.Table):
} }
model = Invoice model = Invoice
template_name = 'django_tables2/bootstrap4.html' template_name = 'django_tables2/bootstrap4.html'
fields = ('id', 'name', 'subject', 'acquitted', 'invoice',) fields = ('id', 'name', 'object', 'acquitted', 'invoice',)

View File

@ -47,9 +47,9 @@ class InvoiceCreateView(LoginRequiredMixin, CreateView):
kwargs = {} kwargs = {}
for key in self.request.POST: for key in self.request.POST:
value = self.request.POST[key] value = self.request.POST[key]
if key.endswith("amount"): if key.endswith("amount") and value:
kwargs[key] = str(int(100 * float(value))) kwargs[key] = str(int(100 * float(value)))
else: elif value:
kwargs[key] = value kwargs[key] = value
formset = ProductFormSet(kwargs, instance=form.instance) formset = ProductFormSet(kwargs, instance=form.instance)
@ -87,6 +87,7 @@ class InvoiceUpdateView(LoginRequiredMixin, UpdateView):
form = context['form'] form = context['form']
form.helper = FormHelper() form.helper = FormHelper()
form.helper.form_tag = False form.helper.form_tag = False
form.fields['date'].initial = form.instance.date
form_set = ProductFormSet(instance=form.instance) form_set = ProductFormSet(instance=form.instance)
context['formset'] = form_set context['formset'] = form_set
context['helper'] = ProductFormSetHelper() context['helper'] = ProductFormSetHelper()
@ -100,26 +101,22 @@ class InvoiceUpdateView(LoginRequiredMixin, UpdateView):
kwargs = {} kwargs = {}
for key in self.request.POST: for key in self.request.POST:
value = self.request.POST[key] value = self.request.POST[key]
if key.endswith("amount"): if key.endswith("amount") and value:
kwargs[key] = str(int(100 * float(value))) kwargs[key] = str(int(100 * float(value)))
else: elif value:
kwargs[key] = value kwargs[key] = value
formset = ProductFormSet(kwargs, instance=form.instance) formset = ProductFormSet(kwargs, instance=form.instance)
saved = [] saved = []
print(formset.errors)
if formset.is_valid(): if formset.is_valid():
for f in formset: for f in formset:
if f.is_valid() and f.instance.designation: if f.is_valid() and f.instance.designation:
if type(f.instance.pk) == 'number' and f.instance.pk <= 0:
f.instance.pk = None
f.save() f.save()
f.instance.save() f.instance.save()
saved.append(f.instance.pk) saved.append(f.instance.pk)
else: else:
f.instance = None f.instance = None
Product.objects.filter(~Q(pk__in=saved), invoice=form.instance).delete()
Product.objects.filter(~Q(pk__in=saved), invoice=form.instance).delete()
return ret return ret
@ -137,8 +134,18 @@ class InvoiceRenderView(LoginRequiredMixin, View):
invoice = Invoice.objects.get(pk=pk) invoice = Invoice.objects.get(pk=pk)
products = Product.objects.filter(invoice=invoice).all() products = Product.objects.filter(invoice=invoice).all()
invoice.description = invoice.description.replace("\n", "\\newline\n") invoice.place = "Cachan"
invoice.address = invoice.address.replace("\n", "\\newline\n") invoice.my_name = "BDE ENS Cachan"
invoice.my_address_street = "61 avenue du Président Wilson"
invoice.my_city = "94230 Cachan"
invoice.bank_code = 30003
invoice.desk_code = 3894
invoice.account_number = 37280662
invoice.rib_key = 14
invoice.bic = "SOGEFRPP"
invoice.description = invoice.description.replace("\r", "").replace("\n", "\\\\ ")
invoice.address = invoice.address.replace("\r", "").replace("\n", "\\\\ ")
tex = render_to_string("treasury/invoice_sample.tex", dict(obj=invoice, products=products)) tex = render_to_string("treasury/invoice_sample.tex", dict(obj=invoice, products=products))
try: try:
os.mkdir(BASE_DIR + "/tmp") os.mkdir(BASE_DIR + "/tmp")
@ -155,9 +162,6 @@ class InvoiceRenderView(LoginRequiredMixin, View):
error = subprocess.Popen( error = subprocess.Popen(
["pdflatex", "invoice-{}.tex".format(pk)], ["pdflatex", "invoice-{}.tex".format(pk)],
cwd=tmp_dir, cwd=tmp_dir,
stdin=open(os.devnull, "r"),
stderr=open(os.devnull, "wb"),
stdout=open(os.devnull, "wb")
).wait() ).wait()
if error: if error:

View File

@ -505,7 +505,7 @@ msgid "BDE"
msgstr "" msgstr ""
#: apps/treasury/models.py:30 #: apps/treasury/models.py:30
msgid "Subject" msgid "Object"
msgstr "" msgstr ""
#: apps/treasury/models.py:34 #: apps/treasury/models.py:34

View File

@ -505,7 +505,7 @@ msgid "BDE"
msgstr "BDE" msgstr "BDE"
#: apps/treasury/models.py:30 #: apps/treasury/models.py:30
msgid "Subject" msgid "Object"
msgstr "Objet" msgstr "Objet"
#: apps/treasury/models.py:34 #: apps/treasury/models.py:34

View File

@ -85,7 +85,7 @@
\def\FactureAcquittee {% if obj.acquitted %} {oui} {% else %} {non} {% endif %} % Facture acquittée : oui/non \def\FactureAcquittee {% if obj.acquitted %} {oui} {% else %} {non} {% endif %} % Facture acquittée : oui/non
\def\FactureLieu {{"{"}}{{ obj.place }}} % Lieu de l'édition de la facture \def\FactureLieu {{"{"}}{{ obj.place }}} % Lieu de l'édition de la facture
\def\FactureDate {{"{"}}{{ obj.date }}} % Date de l'édition de la facture \def\FactureDate {{"{"}}{{ obj.date }}} % Date de l'édition de la facture
\def\FactureObjet {{"{"}}{{ obj.subject|safe }} } % Objet du document \def\FactureObjet {{"{"}}{{ obj.object|safe }} } % Objet du document
% Description de la facture % Description de la facture
\def\FactureDescr {{"{"}}{{ obj.description|safe }}} \def\FactureDescr {{"{"}}{{ obj.description|safe }}}