def update_mach_billables(): """ Goes through all billable items tied to mach (SMS records) and tries to make sure that they are properly associated with rates from mach. """ mach_data = get_mach_data(days=3) try: # rateless billables are Mach Billables that do not have a delivered date rateless_billables = MachSMSBillable.get_rateless() for billable in rateless_billables: billable.sync_attempts.append(datetime.datetime.utcnow()) for data in mach_data: phone_number = data[3] if phone_number == billable.phone_number: mach_number = MachPhoneNumber.get_by_number(phone_number, data) rate_item = MachSMSRate.get_by_number(billable.direction, mach_number) billable.calculate_rate(rate_item) billable.save() billable.update_mach_delivery_status(data) billable.save() if billable.rate_id and billable.mach_delivered_date: break deal_with_delinquent_mach_billable(billable) except Exception as e: logging.error("There was an error updating mach billables: %s" % e)
def handle_noargs(self, *args, **options): mach_billables = MachSMSBillable.get_db().view( "hqbilling/sms_billables", startkey=["billable type date", "MachSMSBillable", "2013-08-01"], endkey=["billable type date", "MachSMSBillable", "2013-11-30"], reduce=False, ).all() mach_billables_ids = [billable['id'] for billable in mach_billables] billable_num = 0 total_billables = len(mach_billables_ids) for billable_doc in iter_docs(MachSMSBillable.get_db(), mach_billables_ids): billable_num += 1 billable = MachSMSBillable.wrap(billable_doc) message_log = MessageLog.get(billable.log_id) billable.billable_date = message_log.date billable.save() print "(%d/%d) Successfully restored billable date on Mach Billable %s to %s" \ % (billable_num, total_billables, billable._id, billable.billable_date)
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()
class TestMonthlyBillFewUsers(TestCase): 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() def tearDown(self): self.mach_bill.delete() self.unicel_bill.delete() self.tropo_bill.delete() self.domain.delete() def testSMSBilling(self): generate_monthly_bills() last_bill = HQMonthlyBill.get_bills(self.domain.name).first() if last_bill: self.assertEqual(self.tropo_bill.total_billed, last_bill.incoming_sms_billed) self.assertEqual(self.unicel_bill.total_billed + self.mach_bill.total_billed, last_bill.outgoing_sms_billed) else: raise Exception("Monthly Bill not successfully generated.")