def send_bookkeeper_email(month=None, year=None, emails=None): today = datetime.date.today() # now, make sure that we send out LAST month's invoices if we did # not specify a month or year. today = utils.get_previous_month_date_range(today)[0] month = month or today.month year = year or today.year from corehq.apps.accounting.interface import InvoiceInterface request = HttpRequest() params = urlencode(( ('report_filter_statement_period_use_filter', 'on'), ('report_filter_statement_period_month', month), ('report_filter_statement_period_year', year), )) request.GET = QueryDict(params) request.couch_user = FakeUser( domain="hqadmin", username="******", ) invoice = InvoiceInterface(request) invoice.is_rendered_as_email = True first_of_month = datetime.date(year, month, 1) email_context = { 'month': first_of_month.strftime("%B"), } email_content = render_to_string( 'accounting/bookkeeper_email.html', email_context) email_content_plaintext = render_to_string( 'accounting/bookkeeper_email_plaintext.html', email_context) format_dict = Format.FORMAT_DICT[Format.CSV] excel_attachment = { 'title': 'Invoices_%(period)s.%(extension)s' % { 'period': first_of_month.strftime('%B_%Y'), 'extension': format_dict['extension'], }, 'mimetype': format_dict['mimetype'], 'file_obj': invoice.excel_response, } emails = emails or settings.BOOKKEEPER_CONTACT_EMAILS for email in emails: send_HTML_email( "Invoices for %s" % datetime.date(year, month, 1).strftime("%B %Y"), email, email_content, email_from=settings.DEFAULT_FROM_EMAIL, text_content=email_content_plaintext, file_attachments=[excel_attachment], ) logger.info( "[BILLING] Sent Bookkeeper Invoice Summary for %(month)s " "to %(emails)s." % { 'month': first_of_month.strftime("%B %Y"), 'emails': ", ".join(emails) })
def post(self, request, *args, **kwargs): if 'adjust' in self.request.POST: if self.adjust_balance_form.is_valid(): self.adjust_balance_form.adjust_balance( web_user=self.request.user.username, ) return HttpResponseRedirect(request.META.get('HTTP_REFERER') or self.page_url) elif 'resend' in self.request.POST: if self.resend_email_form.is_valid(): try: self.resend_email_form.resend_email() return HttpResponseRedirect(self.page_url) except Exception as e: messages.error(request, "Could not send emails due to: %s" % e) elif SuppressInvoiceForm.submit_kwarg in self.request.POST: if self.suppress_invoice_form.is_valid(): self.suppress_invoice_form.suppress_invoice() if self.invoice.is_customer_invoice: return HttpResponseRedirect(CustomerInvoiceInterface.get_url()) else: return HttpResponseRedirect(InvoiceInterface.get_url()) elif HideInvoiceForm.submit_kwarg in self.request.POST: if self.hide_invoice_form.is_valid(): self.hide_invoice_form.hide_invoice() return self.get(request, *args, **kwargs)
def invoice_context(self): subscriber_domain = self.subscription.subscriber.domain invoice_report = InvoiceInterface(self.request) invoice_report.filters.update(subscription__subscriber__domain=subscriber_domain) # Tied to InvoiceInterface. encoded_params = urlencode({'subscriber': subscriber_domain}) invoice_report_url = "{}?{}".format(invoice_report.get_url(), encoded_params) invoice_export_url = "{}?{}".format(invoice_report.get_url(render_as='export'), encoded_params) return { 'invoice_headers': invoice_report.headers, 'invoice_rows': invoice_report.rows, 'invoice_export_url': invoice_export_url, 'invoice_report_url': invoice_report_url, 'adjust_balance_forms': invoice_report.adjust_balance_forms, }
def parent_pages(self): return [{"title": InvoiceInterface.name, "url": InvoiceInterface.get_url()}]
def parent_pages(self): return [{ 'title': InvoiceInterface.name, 'url': InvoiceInterface.get_url(), }]
def send_bookkeeper_email(month=None, year=None, emails=None): today = datetime.date.today() # now, make sure that we send out LAST month's invoices if we did # not specify a month or year. today = get_previous_month_date_range(today)[0] month = month or today.month year = year or today.year from corehq.apps.accounting.interface import InvoiceInterface request = HttpRequest() params = urlencode(( ('report_filter_statement_period_use_filter', 'on'), ('report_filter_statement_period_month', month), ('report_filter_statement_period_year', year), )) request.GET = QueryDict(params) request.couch_user = FakeUser( domain="hqadmin", username="******", ) invoice = InvoiceInterface(request) invoice.is_rendered_as_email = True first_of_month = datetime.date(year, month, 1) email_context = { 'month': first_of_month.strftime("%B"), } email_content = render_to_string('accounting/email/bookkeeper.html', email_context) email_content_plaintext = render_to_string( 'accounting/email/bookkeeper.txt', email_context) format_dict = Format.FORMAT_DICT[Format.CSV] excel_attachment = { 'title': 'Invoices_%(period)s.%(extension)s' % { 'period': first_of_month.strftime('%B_%Y'), 'extension': format_dict['extension'], }, 'mimetype': format_dict['mimetype'], 'file_obj': invoice.excel_response, } emails = emails or settings.BOOKKEEPER_CONTACT_EMAILS for email in emails: send_HTML_email( "Invoices for %s" % datetime.date(year, month, 1).strftime(USER_MONTH_FORMAT), email, email_content, email_from=settings.DEFAULT_FROM_EMAIL, text_content=email_content_plaintext, file_attachments=[excel_attachment], ) log_accounting_info( "Sent Bookkeeper Invoice Summary for %(month)s " "to %(emails)s." % { 'month': first_of_month.strftime(USER_MONTH_FORMAT), 'emails': ", ".join(emails) })