Example #1
0
    def rows(self):
        rows = []
        totals = [0,0,0]

        show_dupes = self.request.GET.get(SelectBillableDuplicateFilter.slug) == 'all'
        for domain in self.domains:
            if self.direction:
                billables = SMSBillable.by_domain_and_direction(domain.name, self.direction,
                    start=self.datespan.startdate_param_utc, end=self.datespan.enddate_param_utc)
            else:
                billables = SMSBillable.by_domain(domain.name,
                    start=self.datespan.startdate_param_utc, end=self.datespan.enddate_param_utc)
            # yes, I know I'm lazy...but this view is going away soon anyway
            billables.reverse()
            last_billable = None
            for billable in billables:
                if last_billable and last_billable.log_id == billable.log_id and not show_dupes:
                    continue
                totals[0] += billable.converted_billable_amount
                totals[1] += billable.dimagi_surcharge
                totals[2] += billable.total_billed
                row = [
                    self.table_cell(
                        billable.billable_date.isoformat(),
                        billable.billable_date.strftime("%Y-%m-%d %H:%M:%S")
                    ),
                    self.table_cell(
                        billable.modified_date.isoformat(),
                        billable.modified_date.strftime("%Y-%m-%d %H:%M:%S")
                    ),
                    self._format_client(domain),
                    billable.log_id,
                    domain.name,
                    SMS_DIRECTIONS.get(billable.direction),
                    billable.api_name(),
                    render_to_string("hqbilling/partials/billing_status_details.html", {
                        'has_error': billable.has_error,
                        'error_msg': billable.error_message,
                        'billable_type': billable.api_name(),
                        'billed_date': billable.billable_date.strftime("%d %b %Y at %H.%M UTC"),
                        'billable_id': billable._id,
                    }),
                    self._format_bill_amount(billable.converted_billable_amount),
                    self._format_bill_amount(billable.dimagi_surcharge),
                    self._format_bill_amount(billable.total_billed)
                ]
                rows.append(row)
                last_billable = billable
        self.total_row = ["", "", "", "", "", "", "", "Total Billed:"] + ["%.2f" % t for t in totals]
        return rows
    def handle(self, *args, **options):
        if len(args) < 2:
            raise CommandError("year and month are required")

        if len(args) > 2:
            domains = [Domain.get_by_name(args[2])]
        else:
            domains = SelectSMSBillableDomainsFilter.get_billable_domains()

        first_day, last_day = month_span(int(args[0]), int(args[1]))
        print "\nRecalculating SMS Billables\n----\n"
        for domain in domains:
            billables_for_domain = SMSBillable.by_domain(domain.name,
                start=first_day.isoformat(), end=last_day.isoformat()).all()
            print "Found %d SMS Billables for domain %s" % (len(billables_for_domain), domain.name)
            for billable in billables_for_domain:
                rate_doc = SMSRate.get_db().get(billable.rate_id)
                rate_class = to_function("hqbilling.models.%s" % rate_doc.get('doc_type', 'SMSRate'))
                rate_item = rate_class.get(rate_doc['_id'])
                billable.calculate_rate(rate_item, real_time=False)
                billable.save()
                sys.stdout.write(".")
                sys.stdout.flush()
            print "\n"