def send_pdf_by_email(invoice_id): form = InvoiceEmail(request.form) if not form.validate(): return form.errors_as_json() invoice = Invoice.find_by_id(invoice_id) if not invoice: return abort(404) message = Message(form.subject.data.encode('utf-8'), sender=(g.member.display, g.member.get_email().encode('utf-8'))) message.add_recipient(sanitize_address((form.name.data, form.email.data))) message.body = form.message.data.encode('utf-8') invoice_key = amazons3.get_invoice(invoice) message.attach("Invoice_{0}.pdf".format(invoice.reference), "application/pdf", invoice_key.get_contents_as_string()) mail = Mail() mail.init_app(current_app) mail.send(message) history = InvoiceHistory() history.description = 'Sent email to {0}'.format(message.recipients[0]) history.pdf = invoice_key.key history.status = 'SENT' history.misc = "{0}\n\n{1}".format(message.subject, message.body) history.member_id = g.member.id history.invoice_id = invoice.id history.save() return 'sent', 200
def post_create(self, form, instance, invoice_id): invoice = Invoice.find_by_id(invoice_id) if not invoice: return abort(404) instance.invoice_id = invoice_id instance.created = datetime.datetime.utcnow() history = InvoiceHistory() history.created = instance.created history.description = 'Payment of {0} {1}'.format(instance.amount, invoice.currency) history.status = 'PAID' history.member_id = g.member.id history.invoice_id = invoice_id history.save()
def post_update(self, form, instance, invoice_id): invoice = Invoice.find_by_id(invoice_id) if not invoice: return abort(404) InvoiceHistory.query \ .filter(InvoiceHistory.status == 'PAID') \ .filter(InvoiceHistory.invoice_id == invoice_id) \ .filter(InvoiceHistory.created == instance.created) \ .delete() history = InvoiceHistory() history.created = instance.created history.description = 'Payment of {0} {1}'.format(instance.amount, invoice.currency) history.status = 'PAID' history.member_id = g.member.id history.invoice_id = invoice_id history.save()
def create_invoice_history(original_invoice_id, updated_by_user_id, changed_fields): """original_invoice_id, updated_by_user_id, changed_fields""" original_invoice = Invoice.objects.filter(id=original_invoice_id).first() created_by = original_invoice.created_by updated_by_user = User.objects.get(id=updated_by_user_id) changed_data = [(' '.join(field.split('_')).title()) for field in changed_fields] if len(changed_data) > 1: changed_data = ', '.join( changed_data[:-1]) + ' and ' + changed_data[-1] + ' have changed.' elif len(changed_data) == 1: changed_data = ', '.join(changed_data) + ' has changed.' else: changed_data = None if original_invoice.invoice_history.count() == 0: changed_data = 'Invoice Created.' if original_invoice: invoice_history = InvoiceHistory() invoice_history.invoice = original_invoice invoice_history.invoice_title = original_invoice.invoice_title invoice_history.invoice_number = original_invoice.invoice_number invoice_history.from_address = original_invoice.from_address invoice_history.to_address = original_invoice.to_address invoice_history.name = original_invoice.name invoice_history.email = original_invoice.email invoice_history.quantity = original_invoice.quantity invoice_history.rate = original_invoice.rate invoice_history.total_amount = original_invoice.total_amount invoice_history.currency = original_invoice.currency invoice_history.phone = original_invoice.phone invoice_history.updated_by = updated_by_user invoice_history.created_by = original_invoice.created_by invoice_history.amount_due = original_invoice.amount_due invoice_history.amount_paid = original_invoice.amount_paid invoice_history.is_email_sent = original_invoice.is_email_sent invoice_history.status = original_invoice.status invoice_history.details = changed_data invoice_history.due_date = original_invoice.due_date invoice_history.save() invoice_history.assigned_to.set(original_invoice.assigned_to.all())