update invoiceworker to reflect the new email system

Also removes some comments (for consistency) and some unused imports
This commit is contained in:
Stephan Telling 2017-04-30 12:36:03 +02:00
parent a8eb0ffe97
commit 098b6ea83c
No known key found for this signature in database
GPG key ID: D4892289F36ADA9B

View file

@ -1,10 +1,8 @@
from django.core.files import File from django.core.files import File
from django.utils import timezone
from shop.pdf import generate_pdf_letter from shop.pdf import generate_pdf_letter
from shop.email import send_invoice_email, send_creditnote_email from shop.email import send_invoice_email, send_creditnote_email
from shop.models import Order, CustomOrder, Invoice, CreditNote from shop.models import Order, CustomOrder, Invoice, CreditNote
from decimal import Decimal import logging
import logging, importlib
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('bornhack.%s' % __name__) logger = logging.getLogger('bornhack.%s' % __name__)
@ -16,23 +14,18 @@ def do_work():
that have no PDF. It also emails invoices for shop orders. that have no PDF. It also emails invoices for shop orders.
""" """
###############################################################
# check if we need to generate any invoices for shop orders # check if we need to generate any invoices for shop orders
for order in Order.objects.filter(paid=True, invoice__isnull=True): for order in Order.objects.filter(paid=True, invoice__isnull=True):
# generate invoice for this Order # generate invoice for this Order
Invoice.objects.create(order=order) Invoice.objects.create(order=order)
logger.info('Generated Invoice object for %s' % order) logger.info('Generated Invoice object for %s' % order)
###############################################################
# check if we need to generate any invoices for custom orders # check if we need to generate any invoices for custom orders
for customorder in CustomOrder.objects.filter(invoice__isnull=True): for customorder in CustomOrder.objects.filter(invoice__isnull=True):
# generate invoice for this CustomOrder # generate invoice for this CustomOrder
Invoice.objects.create(customorder=customorder) Invoice.objects.create(customorder=customorder)
logger.info('Generated Invoice object for %s' % customorder) logger.info('Generated Invoice object for %s' % customorder)
###############################################################
# check if we need to generate any pdf invoices # check if we need to generate any pdf invoices
for invoice in Invoice.objects.filter(pdf=''): for invoice in Invoice.objects.filter(pdf=''):
# generate the pdf # generate the pdf
@ -57,21 +50,15 @@ def do_work():
invoice.pdf.save(invoice.filename, File(pdffile)) invoice.pdf.save(invoice.filename, File(pdffile))
invoice.save() invoice.save()
###############################################################
# check if we need to send out any invoices (only for shop orders, and only where pdf has been generated) # 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=''): for invoice in Invoice.objects.filter(order__isnull=False, sent_to_customer=False).exclude(pdf=''):
logger.info("found unmailed Invoice object: %s" % invoice) logger.info("found unmailed Invoice object: %s" % invoice)
# send the email # add email to the outgoing email queue
if send_invoice_email(invoice=invoice): send_invoice_email(invoice=invoice)
invoice.sent_to_customer = True invoice.sent_to_customer = True
invoice.save() invoice.save()
logger.info('OK: Invoice email sent to %s' % invoice.order.user.email) logger.info('OK: Invoice email added to queue.')
else:
logger.error('Unable to send invoice email for order %s to %s' % (invoice.order.pk, invoice.order.user.email))
###############################################################
# check if we need to generate any pdf creditnotes? # check if we need to generate any pdf creditnotes?
for creditnote in CreditNote.objects.filter(pdf=''): for creditnote in CreditNote.objects.filter(pdf=''):
# generate the pdf # generate the pdf
@ -92,8 +79,6 @@ def do_work():
creditnote.pdf.save(creditnote.filename, File(pdffile)) creditnote.pdf.save(creditnote.filename, File(pdffile))
creditnote.save() creditnote.save()
###############################################################
# check if we need to send out any creditnotes (only where pdf has been generated) # 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=''): for creditnote in CreditNote.objects.filter(sent_to_customer=False).exclude(pdf=''):
# send the email # send the email