コード例 #1
0
ファイル: invoicing.py プロジェクト: pawelreise/commcare-hq
    def create(self):
        if self.subscription is None:
            raise InvoiceError("Cannot create an invoice without a subscription.")

        days_until_due = DEFAULT_DAYS_UNTIL_DUE
        if self.subscription.date_delay_invoicing is not None:
            td = self.subscription.date_delay_invoicing - self.date_end
            days_until_due = max(days_until_due, td.days)
        date_due = self.date_end + datetime.timedelta(days_until_due)

        invoice = Invoice(
            subscription=self.subscription, date_start=self.date_start, date_end=self.date_end, date_due=date_due
        )
        invoice.save()
        self.generate_line_items(invoice)
        invoice.update_balance()
        if invoice.balance == Decimal("0.0"):
            invoice.billingrecord_set.all().delete()
            invoice.lineitem_set.all().delete()
            invoice.delete()
            return None

        invoice.calculate_credit_adjustments()
        invoice.update_balance()
        # generate PDF
        invoice.save()

        billing_record = BillingRecord(invoice=invoice)
        invoice_pdf = InvoicePdf()
        invoice_pdf.generate_pdf(billing_record)

        return invoice
コード例 #2
0
ファイル: views.py プロジェクト: homck007/commcare-hq
    def get(self, request, *args, **kwargs):
        statement_id = kwargs.get('statement_id')
        if statement_id is None:
            raise Http404()
        try:
            invoice_pdf = InvoicePdf.get(statement_id)
        except ResourceNotFound:
            raise Http404()

        try:
            if not invoice_pdf.is_customer:
                raise NotImplementedError
            else:
                invoice = CustomerInvoice.objects.get(
                    pk=invoice_pdf.invoice_id)
        except (Invoice.DoesNotExist, WireInvoice.DoesNotExist,
                CustomerInvoice.DoesNotExist):
            raise Http404()

        filename = "%(pdf_id)s_%(edition)s_%(filename)s" % {
            'pdf_id': invoice_pdf._id,
            'edition': 'customer',
            'filename': invoice_pdf.get_filename(invoice),
        }
        try:
            data = invoice_pdf.get_data(invoice)
            response = HttpResponse(data, content_type='application/pdf')
            response['Content-Disposition'] = 'inline;filename="%s' % filename
        except Exception as e:
            log_accounting_error('Fetching invoice PDF failed: %s' % e)
            return HttpResponse(
                _("Could not obtain billing statement. "
                  "An issue has been submitted."))
        return response
コード例 #3
0
ファイル: views.py プロジェクト: kkrampa/commcare-hq
    def get(self, request, *args, **kwargs):
        statement_id = kwargs.get('statement_id')
        if statement_id is None:
            raise Http404()
        try:
            invoice_pdf = InvoicePdf.get(statement_id)
        except ResourceNotFound:
            raise Http404()

        try:
            if not invoice_pdf.is_customer:
                raise NotImplementedError
            else:
                invoice = CustomerInvoice.objects.get(pk=invoice_pdf.invoice_id)
        except (Invoice.DoesNotExist, WireInvoice.DoesNotExist, CustomerInvoice.DoesNotExist):
            raise Http404()

        filename = "%(pdf_id)s_%(edition)s_%(filename)s" % {
            'pdf_id': invoice_pdf._id,
            'edition': 'customer',
            'filename': invoice_pdf.get_filename(invoice),
        }
        try:
            data = invoice_pdf.get_data(invoice)
            response = HttpResponse(data, content_type='application/pdf')
            response['Content-Disposition'] = 'inline;filename="%s' % filename
        except Exception as e:
            log_accounting_error('Fetching invoice PDF failed: %s' % e)
            return HttpResponse(_("Could not obtain billing statement. "
                                  "An issue has been submitted."))
        return response
コード例 #4
0
    def handle(self, *args, **options):
        invoice_ids = WireBillingRecord.objects.values_list('pdf_data_id',
                                                            flat=True)
        db = InvoicePdf.get_db()

        with IterDB(db) as iter_db:
            for doc in iter_docs(db, invoice_ids):
                doc['is_wire'] = True
                iter_db.save(doc)

        if iter_db.saved_ids:
            print '{}/{} docs saved correctly!'.format(len(iter_db.saved_ids),
                                                       len(invoice_ids))
        if iter_db.error_ids:
            print 'There were {} errors. There were errors when saving the following:'.format(
                len(iter_db.error_ids))
            for error_id in iter_db.error_ids:
                print error_id