From 3ce6b70e99056633a219b19466b9ccd2f2a88b7b Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Tue, 12 Jul 2016 22:33:53 +0200 Subject: [PATCH] commit initial customorder support --- shop/management/commands/invoice-worker.py | 22 +++++-- shop/models.py | 68 +++++++++++++++++----- shop/templates/pdf/custominvoice.html | 57 ++++++++++++++++++ 3 files changed, 125 insertions(+), 22 deletions(-) create mode 100644 shop/templates/pdf/custominvoice.html diff --git a/shop/management/commands/invoice-worker.py b/shop/management/commands/invoice-worker.py index 4fe2ea34..5c8ab654 100644 --- a/shop/management/commands/invoice-worker.py +++ b/shop/management/commands/invoice-worker.py @@ -4,7 +4,7 @@ from django.conf import settings from django.utils import timezone from shop.pdf import generate_pdf_letter from shop.email import send_invoice_email, send_creditnote_email -from shop.models import Order, Invoice, CreditNote +from shop.models import Order, CustomOrder, Invoice, CreditNote from time import sleep from decimal import Decimal @@ -19,11 +19,17 @@ class Command(BaseCommand): def handle(self, *args, **options): self.output('Invoice worker running...') while True: - # check if we need to generate any invoices + # 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 Invoice.objects.create(order=order) - self.output('Generated Invoice object for order %s' % order) + self.output('Generated Invoice object for %s' % order) + + # check if we need to generate any invoices for custom orders + for customorder in CustomOrder.objects.filter(paid=True, invoice__isnull=True): + # generate invoice for this CustomOrder + Invoice.objects.create(customorder=customorder) + self.output('Generated Invoice object for %s' % customorder) # check if we need to generate any pdf invoices for invoice in Invoice.objects.filter(pdf=''): @@ -34,9 +40,13 @@ class Command(BaseCommand): # generate the pdf try: + if invoice.customorder: + template='pdf/custominvoice.html' + else: + template='pdf/invoice.html' pdffile = generate_pdf_letter( filename=invoice.filename, - template='pdf/invoice.html', + template=template, formatdict=formatdict, ) self.output('Generated pdf for invoice %s' % invoice) @@ -54,8 +64,8 @@ class Command(BaseCommand): invoice.save() ############################################################### - # check if we need to send out any invoices (only where pdf has been generated) - for invoice in Invoice.objects.filter(sent_to_customer=False).exclude(pdf=''): + # check if we need to send out any invoices (only for shop orders, and only where pdf has been generated) + for invoice in Invoice.objects.filter(order__isnull=False, sent_to_customer=False).exclude(pdf=''): # send the email if send_invoice_email(invoice=invoice): self.output('OK: Invoice email sent to %s' % invoice.order.user.email) diff --git a/shop/models.py b/shop/models.py index 87a22985..97ee9128 100644 --- a/shop/models.py +++ b/shop/models.py @@ -15,8 +15,34 @@ from decimal import Decimal from datetime import timedelta -class Order(CreatedUpdatedModel): +class CustomOrder(CreatedUpdatedModel): + camp = models.ForeignKey( + 'camps.Camp', + verbose_name=_('Camp'), + help_text=_('The camp this custom order is for.'), + ) + text = models.TextField() + + amount = models.IntegerField( + help_text=_('Amount of this custom order (in DKK, including VAT).') + ) + + paid = models.BooleanField( + verbose_name=_('Paid?'), + help_text=_('Whether this custom order has been paid.'), + default=False, + ) + + def __str__(self): + return 'custom order id #%s' % self.pk + + @property + def vat(self): + return Decimal(self.amount*Decimal(0.2)) + + +class Order(CreatedUpdatedModel): class Meta: unique_together = ('user', 'open') ordering = ['-created'] @@ -29,26 +55,26 @@ class Order(CreatedUpdatedModel): user = models.ForeignKey( 'auth.User', verbose_name=_('User'), - help_text=_('The user this order belongs to.'), + help_text=_('The user this shop order belongs to.'), related_name='orders', ) paid = models.BooleanField( verbose_name=_('Paid?'), - help_text=_('Whether this order has been paid.'), + help_text=_('Whether this shop order has been paid.'), default=False, ) open = models.NullBooleanField( verbose_name=_('Open?'), - help_text=_('Whether this order is open or not. "None" means closed.'), + help_text=_('Whether this shop order is open or not. "None" means closed.'), default=True, ) camp = models.ForeignKey( 'camps.Camp', verbose_name=_('Camp'), - help_text=_('The camp this order is for.'), + help_text=_('The camp this shop order is for.'), ) CREDIT_CARD = 'credit_card' @@ -84,7 +110,7 @@ class Order(CreatedUpdatedModel): objects = OrderQuerySet.as_manager() def __str__(self): - return 'order id #%s' % self.pk + return 'shop order id #%s' % self.pk def get_number_of_items(self): return self.products.aggregate( @@ -225,7 +251,7 @@ class Product(CreatedUpdatedModel, UUIDModel): slug = models.SlugField() price = models.IntegerField( - help_text=_('Price of the product (in DKK).') + help_text=_('Price of the product (in DKK, including VAT).') ) description = models.TextField() @@ -326,19 +352,29 @@ class CreditNote(CreatedUpdatedModel): class Invoice(CreatedUpdatedModel): - order = models.OneToOneField('shop.Order') + order = models.OneToOneField('shop.Order', null=True, blank=True) + customorder = models.OneToOneField('shop.CustomOrder', null=True, blank=True) pdf = models.FileField(null=True, blank=True, upload_to='invoices/') sent_to_customer = models.BooleanField(default=False) def __str__(self): - return 'invoice#%s - order %s - %s - total %s DKK (sent to %s: %s)' % ( - self.id, - self.order.id, - self.order.created, - self.order.total, - self.order.user.email, - self.sent_to_customer, - ) + if self.order: + return 'invoice#%s - shop order %s - %s - total %s DKK (sent to %s: %s)' % ( + self.id, + self.order.id, + self.order.created, + self.order.total, + self.order.user.email, + self.sent_to_customer, + ) + elif self.customorder: + return 'invoice#%s - custom order %s - %s - amount %s DKK (customer: %s)' % ( + self.id, + self.customorder.id, + self.customorder.created, + self.customorder.amount, + self.customorder.customer, + ) @property def filename(self): diff --git a/shop/templates/pdf/custominvoice.html b/shop/templates/pdf/custominvoice.html new file mode 100644 index 00000000..31df124f --- /dev/null +++ b/shop/templates/pdf/custominvoice.html @@ -0,0 +1,57 @@ +{% load static from staticfiles %} +{% load shop_tags %} + + + + + + + +
  +

+ {{ invoice.created|date:"b jS, Y" }}
+ Order CU#{{ invoice.customorder.pk }}
+ Invoice #{{ invoice.pk }} +

+
+

Customer: {{ invoice.customorder.customer }}

+
+

INVOICE

+ + + + + +
+ Text + + Quantity + + Price + + Total +
+ {{ invoice.customorder.text }} + + 1 + + {{ invoice.customorder.amount|currency }} + + {{ invoice.customorder.amount|currency }} + +
+ + Danish VAT (25%) + + {{ invoice.customorder.vat|currency }} + +
+ + Total + + {{ invoice.customorder.amount|currency }} +
+
+

+Payment should be made by bank transfer to Arbejdernes Landsbank reg. 5371 account no. 0244504 within two weeks from {{ invoice.created|date:"b jS, Y" }} please. Thank you! +