From effd900b6218506c2e3ff28f1470507f4e49ded2 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Tue, 9 Jul 2019 10:38:14 +0200 Subject: [PATCH] add proforma invoice support --- src/shop/invoiceworker.py | 22 +++++ src/shop/migrations/0058_order_pdf.py | 18 ++++ src/shop/models.py | 6 ++ src/shop/templates/order_detail.html | 27 ++++-- src/shop/templates/order_list.html | 17 +++- src/shop/templates/pdf/proforma_invoice.html | 95 ++++++++++++++++++++ src/shop/views.py | 36 ++++---- src/utils/pdf.py | 3 + 8 files changed, 194 insertions(+), 30 deletions(-) create mode 100644 src/shop/migrations/0058_order_pdf.py create mode 100644 src/shop/templates/pdf/proforma_invoice.html diff --git a/src/shop/invoiceworker.py b/src/shop/invoiceworker.py index bbc9df07..bf72b67d 100644 --- a/src/shop/invoiceworker.py +++ b/src/shop/invoiceworker.py @@ -14,8 +14,30 @@ def do_work(): The invoice worker creates Invoice objects for shop orders and for custom orders. It also generates PDF files for Invoice objects that have no PDF. It also emails invoices for shop orders. + It also generates proforma invoices for all closed orders. """ + # check if we need to generate any proforma invoices for shop orders + for order in Order.objects.filter(pdf="", open__isnull=True): + # generate proforma invoice for this Order + pdffile = generate_pdf_letter( + filename=order.filename, + template="pdf/proforma_invoice.html", + formatdict={ + "hostname": settings.ALLOWED_HOSTS[0], + "order": order, + "bank": settings.BANKACCOUNT_BANK, + "bank_iban": settings.BANKACCOUNT_IBAN, + "bank_bic": settings.BANKACCOUNT_SWIFTBIC, + "bank_dk_reg": settings.BANKACCOUNT_REG, + "bank_dk_accno": settings.BANKACCOUNT_ACCOUNT, + }, + ) + # update order object with the file + order.pdf.save(order.filename, File(pdffile)) + order.save() + logger.info("Generated proforma invoice PDF for order %s" % order) + # check if we need to generate any invoices for shop orders for order in Order.objects.filter(paid=True, invoice__isnull=True): # generate invoice for this Order diff --git a/src/shop/migrations/0058_order_pdf.py b/src/shop/migrations/0058_order_pdf.py new file mode 100644 index 00000000..6fd053c2 --- /dev/null +++ b/src/shop/migrations/0058_order_pdf.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.7 on 2019-07-08 21:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('shop', '0057_order_notes'), + ] + + operations = [ + migrations.AddField( + model_name='order', + name='pdf', + field=models.FileField(blank=True, null=True, upload_to='proforma_invoices/'), + ), + ] diff --git a/src/shop/models.py b/src/shop/models.py index d35030e6..d8f1480a 100644 --- a/src/shop/models.py +++ b/src/shop/models.py @@ -128,6 +128,8 @@ class Order(CreatedUpdatedModel): blank=True, ) + pdf = models.FileField(null=True, blank=True, upload_to="proforma_invoices/") + objects = OrderQuerySet.as_manager() def __str__(self): @@ -334,6 +336,10 @@ class Order(CreatedUpdatedModel): # nope return False + @property + def filename(self): + return "bornhack_proforma_invoice_order_%s.pdf" % self.pk + class ProductCategory(CreatedUpdatedModel, UUIDModel): class Meta: diff --git a/src/shop/templates/order_detail.html b/src/shop/templates/order_detail.html index 0444bebc..07c92b9f 100644 --- a/src/shop/templates/order_detail.html +++ b/src/shop/templates/order_detail.html @@ -84,15 +84,28 @@ {% endif %} {% if order.open %} - {% bootstrap_button "Update order" button_type="submit" button_class="btn-primary" name="update_order" %} - {% endif %} - {% if not order.paid %} - {% bootstrap_button "Cancel order" button_type="submit" button_class="btn-danger" name="cancel_order" %} - {% endif %} - {% if not order.paid %} - {% bootstrap_button "Review and pay" button_type="submit" button_class="btn btn-success btn-lg pull-right" name="review_and_pay" %} + {% bootstrap_button "Update order" button_type="submit" button_class="btn-primary btn-lg" name="update_order" icon="edit" %} {% endif %} + {% if not order.paid %} + {% bootstrap_button "Cancel order" button_type="submit" button_class="btn-danger btn-lg" name="cancel_order" icon="remove" %} + {% endif %} + + {% if not order.paid %} + {% bootstrap_button "Review and pay" button_type="submit" button_class="btn btn-success btn-lg pull-right" name="review_and_pay" icon="check" %} + {% endif %} + + {% if order.paid %} + {% if order.invoice.pdf %} + {% url 'shop:download_invoice' pk=order.pk as invoice_download_url %} + {% bootstrap_button "Invoice PDF" icon="save-file" href=invoice_download_url button_class="btn-primary btn-lg pull-right" %} + {% endif %} + {% else %} + {% if order.pdf %} + {% url 'shop:download_invoice' pk=order.pk as invoice_download_url %} + {% bootstrap_button "Proforma Invoice PDF" icon="save-file" href=invoice_download_url button_class="btn-primary btn-lg pull-right" %} + {% endif %} + {% endif %} {% if order.paid %}