def get_billable_domains(cls): marked_domains = SMSBillable.get_db().view('hqbilling/domains_marked_for_billing', reduce=False).all() prev_month, _ = HQMonthlyBill.get_default_start_end() recent = SMSBillable.get_db().view('hqbilling/domains_with_billables', startkey=[prev_month.year, prev_month.month], group=True, group_level=3).all() recent_counts = defaultdict(int) for r in recent: recent_counts[r['key'][-1]] += r['value'] for m in marked_domains: if m['key'] not in recent_counts.keys(): recent_counts[m['key']] = 0 all_time = SMSBillable.get_db().view('hqbilling/domains_with_billables', group=True, group_level=3).all() all_time_counts = defaultdict(int) for a in all_time: if a['key'][-1] not in recent_counts.keys(): all_time_counts[a['key'][-1]] += a['value'] sorted_recent = sorted(recent_counts.iteritems(), key=operator.itemgetter(1), reverse=True) sorted_all_time = sorted(all_time_counts.iteritems(), key=operator.itemgetter(1), reverse=True) return [Domain.get_by_name(r[0]) for r in sorted_recent if r[0]] + \ [Domain.get_by_name(a[0]) for a in sorted_all_time if a[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"
def get_marked_domains(cls, as_names=False): marked_domains = SMSBillable.get_db().view('hqbilling/domains_marked_for_billing', reduce=False).all() marked_domains = [m['key'] for m in marked_domains] return marked_domains if as_names else [Domain.get_by_name(d) for d in marked_domains]
def setUp(self): for domain in Domain.get_all(): domain.delete() start_date, _ = HQMonthlyBill.get_default_start_end() sms_date = start_date + datetime.timedelta(days=7) all_billables = SMSBillable.get_all() # all_billables contains duplicates; only delete each doc once for b_id in set(b._id for b in all_billables): SMSBillable.get_db().delete_doc(b_id) self.domain = Domain() self.domain.name = "domain_with_sms" self.domain.is_active = True self.domain.date_created = sms_date self.domain.save() # Incoming billables self.tropo_bill = TropoSMSBillable() self.tropo_bill.billable_date = sms_date self.tropo_bill.billable_amount = 2 self.tropo_bill.conversion_rate = 1 self.tropo_bill.dimagi_surcharge = 0.002 self.tropo_bill.rate_id = "INCOMING_RATE_TROPO" self.tropo_bill.log_id = "INCOMING_LOG_TROPO" self.tropo_bill.domain = self.domain.name self.tropo_bill.direction = INCOMING self.tropo_bill.phone_number = "+15551234567" self.tropo_bill.tropo_id = "TROPO_ID" self.tropo_bill.save() # Outgoing billables self.mach_bill = MachSMSBillable() self.mach_bill.billable_date = sms_date self.mach_bill.contacted_mach_api = sms_date self.mach_bill.mach_delivered_date = sms_date self.mach_bill.billable_amount = 0.01 self.mach_bill.conversion_rate = 1.2 self.mach_bill.dimagi_surcharge = 0.002 self.mach_bill.rate_id = "OUTGOING_MACH_RATE" self.mach_bill.log_id = "OUTGOING_MACH_LOG" self.mach_bill.domain = self.domain.name self.mach_bill.direction = OUTGOING self.mach_bill.phone_number = "+15551234567" self.mach_bill.mach_delivery_status = "delivered" self.mach_bill.mach_id = "MACH_MESSAGE_ID" self.mach_bill.save() self.unicel_bill = UnicelSMSBillable() self.unicel_bill.billable_date = sms_date self.unicel_bill.billable_amount = 2 self.unicel_bill.conversion_rate = 1 self.unicel_bill.dimagi_surcharge = 0.002 self.unicel_bill.rate_id = "OUTGOING_UNICEL_RATE" self.unicel_bill.log_id = "OUTGOING_UNICEL_LOG" self.unicel_bill.domain = self.domain.name self.unicel_bill.direction = OUTGOING self.unicel_bill.phone_number = "+15551234567" self.unicel_bill.unicel_id = "UNICEL_ID" self.unicel_bill.save()