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
|
|
|
|
from shop.email import send_invoice_email
|
2016-05-30 15:33:02 +00:00
|
|
|
from shop.models import Order, Invoice
|
|
|
|
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-05-30 14:58:55 +00:00
|
|
|
help = 'Send out 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 14:58:55 +00:00
|
|
|
|
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=''):
|
2016-05-30 14:58:55 +00:00
|
|
|
# put the dict with data for the pdf together
|
|
|
|
formatdict = {
|
|
|
|
'invoice': invoice,
|
|
|
|
}
|
2016-05-30 16:09:21 +00:00
|
|
|
|
2016-05-30 14:58:55 +00:00
|
|
|
# generate the pdf
|
2016-05-17 13:31:41 +00:00
|
|
|
try:
|
2016-05-30 14:58:55 +00:00
|
|
|
pdffile = generate_pdf_letter(
|
|
|
|
filename=invoice.filename,
|
2016-05-31 05:42:01 +00:00
|
|
|
template='pdf/invoice.html',
|
2016-05-30 14:58:55 +00:00
|
|
|
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
|
|
|
|
2016-05-30 14:58:55 +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
|
|
|
|
|
2016-05-30 17:49:32 +00:00
|
|
|
# update invoice object with the file
|
|
|
|
invoice.pdf.save(invoice.filename, File(pdffile))
|
2016-05-30 14:58:55 +00:00
|
|
|
invoice.save()
|
|
|
|
|
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=''):
|
2016-05-30 14:58:55 +00:00
|
|
|
# send the email
|
2016-05-30 18:30:52 +00:00
|
|
|
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-05-30 19:09:07 +00:00
|
|
|
# pause for a bit
|
2016-05-17 13:31:41 +00:00
|
|
|
sleep(60)
|
|
|
|
|