bornhack-website/shop/management/commands/invoice-worker.py

110 lines
4.8 KiB
Python
Raw Normal View History

2016-05-17 13:31:41 +00:00
from django.core.management.base import BaseCommand
2016-05-30 18:39:54 +00:00
from django.core.files import File
2016-05-30 16:41:25 +00:00
from django.conf import settings
2016-05-30 21:05:33 +00:00
from django.utils import timezone
2016-05-17 13:31:41 +00:00
from shop.pdf import generate_pdf_letter
2016-06-18 18:51:53 +00:00
from shop.email import send_invoice_email, send_creditnote_email
from shop.models import Order, Invoice, CreditNote
2016-05-30 15:33:02 +00:00
from time import sleep
2016-05-31 04:51:30 +00:00
from decimal import Decimal
2016-05-17 13:31:41 +00:00
class Command(BaseCommand):
args = 'none'
2016-06-18 18:51:53 +00:00
help = 'Generate invoices and credit notes, and email invoices that have not been sent yet'
2016-05-17 13:31:41 +00:00
2016-05-30 21:05:33 +00:00
def output(self, message):
self.stdout.write('%s: %s' % (timezone.now().strftime("%Y-%m-%d %H:%M:%S"), message))
2016-05-17 13:31:41 +00:00
def handle(self, *args, **options):
2016-05-30 21:05:33 +00:00
self.output('Invoice worker running...')
2016-05-17 13:31:41 +00:00
while True:
2016-05-30 19:09:07 +00:00
# check if we need to generate any invoices
2016-05-17 13:31:41 +00:00
for order in Order.objects.filter(paid=True, invoice__isnull=True):
2016-05-30 19:09:07 +00:00
# generate invoice for this Order
2016-05-17 13:31:41 +00:00
Invoice.objects.create(order=order)
2016-05-30 21:05:33 +00:00
self.output('Generated Invoice object for order %s' % order)
2016-05-30 19:09:07 +00:00
# check if we need to generate any pdf invoices
2016-05-30 21:05:33 +00:00
for invoice in Invoice.objects.filter(pdf=''):
# put the dict with data for the pdf together
formatdict = {
'invoice': invoice,
}
2016-05-30 16:09:21 +00:00
# generate the pdf
2016-05-17 13:31:41 +00:00
try:
pdffile = generate_pdf_letter(
filename=invoice.filename,
2016-05-31 05:42:01 +00:00
template='pdf/invoice.html',
formatdict=formatdict,
)
2016-05-30 21:05:33 +00:00
self.output('Generated pdf for invoice %s' % invoice)
2016-05-17 13:31:41 +00:00
except Exception as E:
2016-05-30 21:05:33 +00:00
self.output('ERROR: Unable to generate PDF file for invoice #%s. Error: %s' % (invoice.pk, E))
2016-05-17 13:31:41 +00:00
continue
2016-05-30 16:09:21 +00:00
# so, do we have a pdf?
2016-05-17 13:31:41 +00:00
if not pdffile:
2016-05-30 21:05:33 +00:00
self.output('ERROR: Unable to generate PDF file for invoice #%s' % invoice.pk)
2016-05-17 13:31:41 +00:00
continue
# update invoice object with the file
invoice.pdf.save(invoice.filename, File(pdffile))
invoice.save()
2016-06-18 18:51:53 +00:00
###############################################################
2016-05-30 19:09:07 +00:00
# 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=''):
# send the email
if send_invoice_email(invoice=invoice):
2016-05-30 21:05:33 +00:00
self.output('OK: Invoice email sent to %s' % invoice.order.user.email)
2016-05-17 13:31:41 +00:00
invoice.sent_to_customer=True
invoice.save()
else:
2016-05-30 21:05:33 +00:00
self.output('ERROR: Unable to send invoice email for order %s to %s' % (invoice.order.pk, invoice.order.user.email))
2016-05-17 13:31:41 +00:00
2016-06-18 18:51:53 +00:00
###############################################################
# check if we need to generate any pdf creditnotes?
for creditnote in CreditNote.objects.filter(pdf=''):
# put the dict with data for the pdf together
formatdict = {
'creditnote': creditnote,
}
# generate the pdf
try:
pdffile = generate_pdf_letter(
filename=creditnote.filename,
template='pdf/creditnote.html',
formatdict=formatdict,
)
self.output('Generated pdf for creditnote %s' % creditnote)
except Exception as E:
self.output('ERROR: Unable to generate PDF file for creditnote #%s. Error: %s' % (creditnote.pk, E))
continue
# so, do we have a pdf?
if not pdffile:
self.output('ERROR: Unable to generate PDF file for creditnote #%s' % creditnote.pk)
continue
# update creditnote object with the file
creditnote.pdf.save(creditnote.filename, File(pdffile))
creditnote.save()
###############################################################
# check if we need to send out any creditnotes (only where pdf has been generated)
for creditnote in CreditNote.objects.filter(sent_to_customer=False).exclude(pdf=''):
# send the email
if send_creditnote_email(creditnote=creditnote):
self.output('OK: Creditnote email sent to %s' % creditnote.user.email)
creditnote.sent_to_customer=True
creditnote.save()
else:
self.output('ERROR: Unable to send creditnote email for creditnote %s to %s' % (creditnote.pk, creditnote.user.email))
2016-05-30 19:09:07 +00:00
# pause for a bit
2016-05-17 13:31:41 +00:00
sleep(60)