def add_moz_zero_charge(apps): mzn, _ = (apps.get_model("accounting", "Currency") if apps else Currency).objects.get_or_create(code="MZN") sms_gateway_fee_class = apps.get_model("smsbillables", "SmsGatewayFee") if apps else SmsGatewayFee sms_gateway_fee_criteria_class = ( apps.get_model("smsbillables", "SmsGatewayFeeCriteria") if apps else SmsGatewayFeeCriteria ) SmsGatewayFee.create_new( SQLSislogBackend.get_api_id(), INCOMING, Decimal("0"), country_code=None, prefix="", currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLSislogBackend.get_api_id(), OUTGOING, Decimal("0"), country_code=None, prefix="", currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Moz gateway default fees.")
def bootstrap_smsgh_gateway(apps=None): default_currency, _ = (apps.get_model("accounting", "Currency") if apps else Currency).objects.get_or_create( code=settings.DEFAULT_CURRENCY ) sms_gateway_fee_class = apps.get_model("smsbillables", "SmsGatewayFee") if apps else SmsGatewayFee sms_gateway_fee_criteria_class = ( apps.get_model("smsbillables", "SmsGatewayFeeCriteria") if apps else SmsGatewayFeeCriteria ) SmsGatewayFee.create_new( SMSGHBackend.get_api_id(), INCOMING, Decimal("0.0"), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SMSGHBackend.get_api_id(), OUTGOING, Decimal("0.0"), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated SMSGH gateway fees.")
def bootstrap_telerivet_gateway(orm): default_currency = (orm['accounting.Currency'] if orm else Currency).get_default() sms_gateway_fee_class = orm[ 'smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm[ 'smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria SmsGatewayFee.create_new( TelerivetBackend.get_api_id(), INCOMING, Decimal('0.0'), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( TelerivetBackend.get_api_id(), OUTGOING, Decimal('0.0'), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Telerivet gateway fees.")
def add_moz_zero_charge(apps): mzn, _ = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get_or_create(code='MZN') sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new( SQLSislogBackend.get_api_id(), INCOMING, Decimal('0'), country_code=None, prefix='', currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLSislogBackend.get_api_id(), OUTGOING, Decimal('0'), country_code=None, prefix='', currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Moz gateway default fees.")
def create_instance_gateway_fees(self): for direction, backend in self.instance_fees.items(): for backend_api_id, (backend_instance, amount) in backend.items(): SmsGatewayFee.create_new(backend_api_id, direction, amount, backend_instance=backend_instance)
def bootstrap_yo_gateway(orm): ugx, _ = (orm['accounting.Currency'] if orm else Currency).objects.get_or_create(code='UGX') sms_gateway_fee_class = orm[ 'smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm[ 'smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria SmsGatewayFee.create_new( 'YO', INCOMING, Decimal('110.0'), currency=ugx, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( 'HTTP', OUTGOING, Decimal('55.0'), backend_instance='95a4f0929cddb966e292e70a634da716', currency=ugx, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Yo gateway fees.")
def test_gateway_charge_for_country_code(self): """ Create a generic gateway fee and a specific gateway fee with country code """ specific_gateway_fee_amount = Decimal('0.01') generic_gateway_fee_amount = Decimal('0.005') expected_gateway_charge = specific_gateway_fee_amount self.msg = get_fake_sms(self.domain, self.backend.hq_api_id, self.backend.couch_id, short_text) self.gateway_fees += [ SmsGatewayFee.create_new(self.backend.hq_api_id, self.msg.direction, generic_gateway_fee_amount), SmsGatewayFee.create_new(self.backend.hq_api_id, self.msg.direction, specific_gateway_fee_amount, country_code=1), ] create_billable_for_sms(self.msg, delay=False) sms_billables = SmsBillable.objects.filter(domain=self.domain, log_id=self.msg.couch_id) self.assertEqual(sms_billables.count(), 1) self.billable = sms_billables[0] actual_gateway_charge = self.billable.gateway_charge self.assertEqual(expected_gateway_charge, actual_gateway_charge)
def add_moz_zero_charge(apps): mzn, _ = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get_or_create(code='MZN') sms_gateway_fee_class = apps.get_model( 'smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model( 'smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new( SQLSislogBackend.get_api_id(), INCOMING, Decimal('0'), country_code=None, prefix='', currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLSislogBackend.get_api_id(), OUTGOING, Decimal('0'), country_code=None, prefix='', currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Moz gateway default fees.")
def update_sislog_vodacom_mozambique_fees(apps, schema_editor): mzn, _ = apps.get_model('accounting', 'Currency').objects.get_or_create(code='MZN') sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') country_code = 258 for prefix in ['84', '85']: SmsGatewayFee.create_new( SQLSislogBackend.get_api_id(), INCOMING, Decimal('2.0'), country_code=country_code, prefix=prefix, currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLSislogBackend.get_api_id(), OUTGOING, Decimal('0.35'), country_code=country_code, prefix=prefix, currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, )
def handle(self, *args, **options): workbook = xlrd.open_workbook('corehq/apps/smsbillables/management/' 'commands/pricing_data/Syniverse_coverage_list_DIAMONDplus.xls') table = workbook.sheet_by_index(0) data = {} try: row = 7 while True: if table.cell_value(row, 6) == 'yes': country_code = int(table.cell_value(row, 0)) if not(country_code in data): data[country_code] = [] subscribers = table.cell_value(row,10).replace('.', '') try: data[country_code].append( (table.cell_value(row, 9), int(subscribers))) except ValueError: print 'Incomplete data for country code %d' % country_code row += 1 except IndexError: pass for country_code in data: total_subscribers = 0 weighted_price = 0 for price, subscribers in data[country_code]: total_subscribers += subscribers weighted_price += price * subscribers weighted_price = weighted_price / total_subscribers SmsGatewayFee.create_new(MachBackend.get_api_id(), OUTGOING, weighted_price, country_code=country_code, currency=Currency.objects.get(code="EUR")) print "Updated MACH/Syniverse gateway fees."
def bootstrap_apposit_gateway(apps=None): usd_currency, _ = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get_or_create(code="USD") sms_gateway_fee_class = apps.get_model( 'smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model( 'smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new( SQLAppositBackend.get_api_id(), INCOMING, Decimal('0.02'), currency=usd_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLAppositBackend.get_api_id(), OUTGOING, Decimal('0.02'), currency=usd_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Apposit gateway fees.")
def create_other_currency_fees(self): for direction, fees in self.least_specific_fees.items(): for backend_api_id, amount in fees.items(): SmsGatewayFee.create_new(backend_api_id, direction, amount, currency=self.other_currency)
def bootstrap_yo_gateway(apps): ugx, _ = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get_or_create(code='UGX') sms_gateway_fee_class = apps.get_model( 'smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model( 'smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new( SQLYoBackend.get_api_id(), INCOMING, Decimal('110.0'), currency=ugx, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLYoBackend.get_api_id(), OUTGOING, Decimal('55.0'), currency=ugx, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Yo gateway fees.")
def bootstrap_mach_gateway(orm): currency_class = orm['accounting.Currency'] if orm else Currency sms_gateway_fee_class = orm[ 'smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm[ 'smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria workbook = xlrd.open_workbook( 'corehq/apps/smsbillables/management/' 'commands/pricing_data/Syniverse_coverage_list_DIAMONDplus.xls') table = workbook.sheet_by_index(0) data = {} try: row = 7 while True: if table.cell_value(row, 6) == 'yes': country_code = int(table.cell_value(row, 0)) if not (country_code in data): data[country_code] = [] subscribers = table.cell_value(row, 10).replace('.', '') try: data[country_code].append( (table.cell_value(row, 9), int(subscribers))) except ValueError: logger.info('Incomplete data for country code %d' % country_code) row += 1 except IndexError: pass for country_code in data: total_subscribers = 0 weighted_price = 0 for price, subscribers in data[country_code]: total_subscribers += subscribers weighted_price += price * subscribers weighted_price = weighted_price / total_subscribers SmsGatewayFee.create_new( MachBackend.get_api_id(), OUTGOING, weighted_price, country_code=country_code, currency=currency_class.objects.get(code="EUR"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) # Fee for invalid phonenumber SmsGatewayFee.create_new( MachBackend.get_api_id(), OUTGOING, 0.0225, country_code=None, currency=currency_class.objects.get(code="EUR"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated MACH/Syniverse gateway fees.")
def bootstrap_smsgh_gateway(apps=None): default_currency, _ = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get_or_create( code=settings.DEFAULT_CURRENCY) sms_gateway_fee_class = apps.get_model( 'smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model( 'smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new( SQLSMSGHBackend.get_api_id(), INCOMING, Decimal('0.0'), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLSMSGHBackend.get_api_id(), OUTGOING, Decimal('0.0'), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated SMSGH gateway fees.")
def bootstrap_yo_gateway(orm): ugx, _ = (orm['accounting.Currency'] if orm else Currency).objects.get_or_create(code='UGX') sms_gateway_fee_class = orm['smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm['smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria SmsGatewayFee.create_new( 'YO', INCOMING, Decimal('110.0'), currency=ugx, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( 'HTTP', OUTGOING, Decimal('55.0'), backend_instance='95a4f0929cddb966e292e70a634da716', currency=ugx, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Yo gateway fees.")
def create_country_code_gateway_fees(self): for direction, backend in self.country_code_fees.items(): for backend_api_id, country in backend.items(): for country_code, amount in country.items(): SmsGatewayFee.create_new(backend_api_id, direction, amount, country_code=country_code)
def bootstrap_mach_gateway(apps): currency_class = apps.get_model("accounting", "Currency") if apps else Currency sms_gateway_fee_class = apps.get_model("smsbillables", "SmsGatewayFee") if apps else SmsGatewayFee sms_gateway_fee_criteria_class = ( apps.get_model("smsbillables", "SmsGatewayFeeCriteria") if apps else SmsGatewayFeeCriteria ) workbook = xlrd.open_workbook( "corehq/apps/smsbillables/management/" "commands/pricing_data/Syniverse_coverage_list_DIAMONDplus.xls" ) table = workbook.sheet_by_index(0) data = {} try: row = 7 while True: if table.cell_value(row, 6) == "yes": country_code = int(table.cell_value(row, 0)) if not (country_code in data): data[country_code] = [] subscribers = table.cell_value(row, 10).replace(".", "") try: data[country_code].append((table.cell_value(row, 9), int(subscribers))) except ValueError: logger.info("Incomplete data for country code %d" % country_code) row += 1 except IndexError: pass for country_code in data: total_subscribers = 0 weighted_price = 0 for price, subscribers in data[country_code]: total_subscribers += subscribers weighted_price += price * subscribers weighted_price = weighted_price / total_subscribers SmsGatewayFee.create_new( MachBackend.get_api_id(), OUTGOING, weighted_price, country_code=country_code, currency=currency_class.objects.get(code="EUR"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) # Fee for invalid phonenumber SmsGatewayFee.create_new( MachBackend.get_api_id(), OUTGOING, 0.0225, country_code=None, currency=currency_class.objects.get(code="EUR"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated MACH/Syniverse gateway fees.")
def create_most_specific_gateway_fees(self): for direction, backend in self.most_specific_fees.items(): for backend_api_id, country in backend.items(): for country_code, (backend_instance, amount) in country.items(): SmsGatewayFee.create_new(backend_api_id, direction, amount, country_code=country_code, backend_instance=backend_instance)
def bootstrap_mach_gateway(apps): currency_class = apps.get_model('accounting', 'Currency') if apps else Currency sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') \ if apps else SmsGatewayFeeCriteria filename = 'corehq/apps/smsbillables/management/pricing_data/Syniverse_coverage_list_PREMIUM_Sept2019.xlsx' workbook = openpyxl.load_workbook(filename, read_only=True, data_only=True) table = workbook.worksheets[0] data = {} for row in islice(table.rows, 7, None): if row[6].value == 'yes': country_code = int(row[0].value) if not(country_code in data): data[country_code] = [] subscribers = row[10].value.replace('.', '') try: data[country_code].append((row[9].value, int(subscribers))) except ValueError: log_smsbillables_info('Incomplete data for country code %d' % country_code) for country_code in data: total_subscribers = 0 weighted_price = 0 for price, subscribers in data[country_code]: total_subscribers += subscribers weighted_price += price * subscribers if total_subscribers == 0: continue weighted_price = weighted_price / total_subscribers SmsGatewayFee.create_new( SQLMachBackend.get_api_id(), OUTGOING, weighted_price, country_code=country_code, currency=currency_class.objects.get(code="EUR"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) # Fee for invalid phonenumber SmsGatewayFee.create_new( SQLMachBackend.get_api_id(), OUTGOING, 0.0225, country_code=None, currency=currency_class.objects.get(code="EUR"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated MACH/Syniverse gateway fees.")
def bootstrap_unicel_gateway(orm): currency = (orm['accounting.Currency'] if orm else Currency).objects.get(code="INR") sms_gateway_fee_class = orm['smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm['smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria SmsGatewayFee.create_new(UnicelBackend.get_api_id(), INCOMING, 0.50, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class) SmsGatewayFee.create_new(UnicelBackend.get_api_id(), OUTGOING, 0.50, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class) logger.info("Updated Unicel gateway fees.")
def bootstrap_unicel_gateway(apps): currency = (apps.get_model('accounting.Currency') if apps else Currency).objects.get(code="INR") sms_gateway_fee_class = apps.get_model('smsbillables.SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables.SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new(SQLUnicelBackend.get_api_id(), INCOMING, 0.50, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class) SmsGatewayFee.create_new(SQLUnicelBackend.get_api_id(), OUTGOING, 0.50, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class) log_smsbillables_info("Updated Unicel gateway fees.")
def create_prefix_gateway_fees(self): for direction, backend in self.prefix_fees.items(): for backend_api_id, country in backend.items(): for country_code, prfx in country.items(): for prefix, backend_instance_and_amount in prfx.items(): for backend_instance, amount in backend_instance_and_amount.items(): SmsGatewayFee.create_new( backend_api_id, direction, amount, country_code=country_code, prefix=prefix, backend_instance=backend_instance, )
def bootstrap_unicel_gateway(apps): currency = (apps.get_model('accounting.Currency') if apps else Currency).objects.get(code="INR") sms_gateway_fee_class = apps.get_model('smsbillables.SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables.SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new(SQLUnicelBackend.get_api_id(), INCOMING, 0.50, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class) SmsGatewayFee.create_new(SQLUnicelBackend.get_api_id(), OUTGOING, 0.50, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class) logger.info("Updated Unicel gateway fees.")
def handle(self, *args, **options): SmsGatewayFee.create_new(TropoBackend.get_api_id(), INCOMING, 0.01) rates_csv = open('corehq/apps/smsbillables/management/' 'pricing_data/tropo_international_rates_2013-12-19.csv', 'r') for line in rates_csv.readlines(): data = line.split(',') if data[1] == 'Fixed Line' and data[4] != '\n': SmsGatewayFee.create_new(TropoBackend.get_api_id(), OUTGOING, float(data[4].rstrip()), country_code=int(data[2])) rates_csv.close() print "Updated Tropo gateway fees."
def arbitrary_sms_billables_for_domain(domain, message_month_date, num_sms, direction=None, multipart_count=1): direction = direction or random.choice(DIRECTIONS) gateway_fee = SmsGatewayFee.create_new('MACH', direction, Decimal(0.5)) usage_fee = SmsUsageFee.create_new(direction, Decimal(0.25)) _, last_day_message = calendar.monthrange(message_month_date.year, message_month_date.month) billables = [] for _ in range(0, num_sms): sms_billable = SmsBillable( gateway_fee=gateway_fee, usage_fee=usage_fee, log_id=data_gen.arbitrary_unique_name()[:50], phone_number=data_gen.random_phonenumber(), domain=domain, direction=direction, date_sent=datetime.date(message_month_date.year, message_month_date.month, random.randint(1, last_day_message)), multipart_count=multipart_count, ) sms_billable.save() billables.append(sms_billable) return billables
def arbitrary_sms_billables_for_domain(domain, direction, message_month_date, num_sms): from corehq.apps.smsbillables.models import SmsBillable, SmsGatewayFee, SmsUsageFee from corehq.apps.smsbillables import generator as sms_gen gateway_fee = SmsGatewayFee.create_new('MACH', direction, sms_gen.arbitrary_fee()) usage_fee = SmsUsageFee.create_new(direction, sms_gen.arbitrary_fee()) _, last_day_message = calendar.monthrange(message_month_date.year, message_month_date.month) for _ in range(0, num_sms): sms_billable = SmsBillable( gateway_fee=gateway_fee, usage_fee=usage_fee, log_id=data_gen.arbitrary_unique_name()[:50], phone_number=data_gen.random_phonenumber(), domain=domain, direction=direction, date_sent=datetime.date(message_month_date.year, message_month_date.month, random.randint(1, last_day_message)), ) sms_billable.save()
def get_rate_response(self): gateway = self.data.get('gateway') try: backend_api_id = SQLMobileBackend.get_backend_api_id(gateway, is_couch_id=True) except Exception as e: log_smsbillables_error( "Failed to get backend for calculating an sms rate due to: %s" % e ) raise SMSRateCalculatorError("Could not obtain connection information.") country_code = self.data.get('country_code') if country_code == NONMATCHING_COUNTRY: country_code = None direction = self.data.get('direction') gateway_fee = SmsGatewayFee.get_by_criteria( backend_api_id, direction, backend_instance=gateway, country_code=country_code, ) usage_fee = SmsUsageFee.get_by_criteria(direction, self.request.domain) usd_gateway_fee = gateway_fee.amount / gateway_fee.currency.rate_to_default usd_total = usage_fee.amount + usd_gateway_fee return { 'rate': _("%s per 160 character SMS") % fmt_dollar_amount(usd_total), }
def test_gateway_charge_with_api_and_non_matching_gateway_fee(self): """ A backend with non-matching gateway_fee and an API should charge using the API 'Non-matching' means the message under test does not fit the gateway fee criteria """ api_fee = Decimal('0.01') gateway_fee_amount = Decimal('0.0075') # expect to return the api price expected_gateway_charge = api_fee self.msg = get_fake_sms(self.domain, self.backend.hq_api_id, self.backend.couch_id, short_text) self.gateway_fees += [ # the default number here has a country code of 1, so make this code different SmsGatewayFee.create_new(self.backend.hq_api_id, self.msg.direction, gateway_fee_amount, country_code=10), ] with patch( 'corehq.apps.smsbillables.models.SmsBillable.get_charge_details_through_api', return_value=(api_fee, 1)): create_billable_for_sms(self.msg, delay=False) sms_billables = SmsBillable.objects.filter(domain=self.domain, log_id=self.msg.couch_id) self.assertEqual(sms_billables.count(), 1) self.billable = sms_billables[0] actual_gateway_charge = self.billable.gateway_charge self.assertEqual(expected_gateway_charge, actual_gateway_charge)
def test_gateway_charge_with_api_and_non_matching_gateway_fee_multiple_messages( self): """ A backend with non-matching gateway_fee and an API should charge using the API 'Non-matching' means the message under test does not fit the gateway fee criteria ASSUMPTION: we assume that the API will return a value accounting for multiple messages if applicable This may be true for Twilio, but as we add other gateways that support an API we should check """ api_fee = Decimal('0.01') gateway_fee_amount = Decimal('0.0075') # expect to return the api fee as is expected_gateway_charge = api_fee self.msg = get_fake_sms(self.domain, self.backend.hq_api_id, self.backend.couch_id, long_text) self.gateway_fees += [ # the default number here has a country code of 1, so make this code different SmsGatewayFee.create_new(self.backend.hq_api_id, self.msg.direction, gateway_fee_amount, country_code=10), ] with patch( 'corehq.apps.smsbillables.models.SmsBillable.get_charge_details_through_api', return_value=(api_fee, 1)): create_billable_for_sms(self.msg, delay=False) sms_billables = SmsBillable.objects.filter(domain=self.domain, log_id=self.msg.couch_id) self.assertEqual(sms_billables.count(), 1) self.billable = sms_billables[0] actual_gateway_charge = self.billable.gateway_charge self.assertEqual(expected_gateway_charge, actual_gateway_charge)
def arbitrary_sms_billables_for_domain(domain, message_month_date, num_sms, direction=None, multipart_count=1): from corehq.apps.smsbillables.models import SmsBillable, SmsGatewayFee, SmsUsageFee direction = direction or random.choice(DIRECTIONS) gateway_fee = SmsGatewayFee.create_new('MACH', direction, Decimal(0.5)) usage_fee = SmsUsageFee.create_new(direction, Decimal(0.25)) _, last_day_message = calendar.monthrange(message_month_date.year, message_month_date.month) billables = [] for _ in range(0, num_sms): sms_billable = SmsBillable( gateway_fee=gateway_fee, usage_fee=usage_fee, log_id=data_gen.arbitrary_unique_name()[:50], phone_number=data_gen.random_phonenumber(), domain=domain, direction=direction, date_sent=datetime.date(message_month_date.year, message_month_date.month, random.randint(1, last_day_message)), multipart_count=multipart_count, ) sms_billable.save() billables.append(sms_billable) return billables
def get_rate_response(self): gateway = self.data.get('gateway') try: backend = SMSBackend.get(gateway) backend_api_id = get_backend_by_class_name( backend.doc_type).get_api_id() except Exception as e: logger.error("Failed to get backend for calculating an sms rate " "due to: %s" % e) raise SMSRateCalculatorError( "Could not obtain connection information.") country_code = self.data.get('country_code') if country_code == NONMATCHING_COUNTRY: country_code = None direction = self.data.get('direction') gateway_fee = SmsGatewayFee.get_by_criteria( backend_api_id, direction, backend_instance=gateway, country_code=country_code, ) usage_fee = SmsUsageFee.get_by_criteria(direction, self.request.domain) usd_gateway_fee = gateway_fee.amount * gateway_fee.currency.rate_to_default usd_total = usage_fee.amount + usd_gateway_fee return { 'rate': _("%s per 160 character SMS") % fmt_dollar_amount(usd_total), }
def test_gateway_charge_with_api_and_matching_gateway_fee_multiple_messages( self): """ A backend with non-matching gateway_fee and an API should charge using the API 'Matching' means the message under test fits with the criteria set for the gateway_fee If using the gateway fee we need to do the calculation for the cost of multiple messages """ api_fee = Decimal('0.01') gateway_fee_amount = Decimal('0.0075') # expect to return the gateway fee x 2 (long_text takes up 2 messages) expected_gateway_charge = gateway_fee_amount * 2 self.msg = get_fake_sms(self.domain, self.backend.hq_api_id, self.backend.couch_id, long_text) self.gateway_fees += [ SmsGatewayFee.create_new(self.backend.hq_api_id, self.msg.direction, gateway_fee_amount), ] with patch( 'corehq.apps.smsbillables.models.SmsBillable.get_charge_details_through_api', return_value=(api_fee, None)): create_billable_for_sms(self.msg, delay=False) sms_billables = SmsBillable.objects.filter(domain=self.domain, log_id=self.msg.couch_id) self.assertEqual(sms_billables.count(), 1) self.billable = sms_billables[0] actual_gateway_charge = self.billable.gateway_charge self.assertEqual(expected_gateway_charge, actual_gateway_charge)
def bootstrap_twilio_gateway_incoming(orm): currency_class = orm['accounting.Currency'] if orm else Currency sms_gateway_fee_class = orm['smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm['smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria # https://www.twilio.com/sms/pricing/us SmsGatewayFee.create_new( TwilioBackend.get_api_id(), INCOMING, 0.0075, country_code=None, currency=currency_class.objects.get(code="USD"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated INCOMING Twilio gateway fees.")
def bootstrap_twilio_gateway_incoming(apps): currency_class = apps.get_model('accounting', 'Currency') if apps else Currency sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria # https://www.twilio.com/sms/pricing/us SmsGatewayFee.create_new( SQLTwilioBackend.get_api_id(), INCOMING, 0.0075, country_code=None, currency=currency_class.objects.get(code="USD"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated INCOMING Twilio gateway fees.")
def _directed_fee(direction, backend_api_id, backend_instance_id): gateway_fee = SmsGatewayFee.get_by_criteria( backend_api_id, direction, backend_instance=backend_instance_id, country_code=country_code ) if not gateway_fee or gateway_fee.amount is None: return None usd_gateway_fee = gateway_fee.amount / gateway_fee.currency.rate_to_default usage_fee = SmsUsageFee.get_by_criteria(direction) return fmt_dollar_amount(usage_fee.amount + usd_gateway_fee)
def _bootstrap_gateway(apps, backend): sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee')\ if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria')\ if apps else SmsGatewayFeeCriteria default_currency, _ = (apps.get_model('accounting', 'Currency') if apps else Currency)\ .objects.get_or_create(code=settings.DEFAULT_CURRENCY) for direction in [INCOMING, OUTGOING]: SmsGatewayFee.create_new( backend.get_api_id(), direction, None, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, currency=default_currency, ) log_smsbillables_info(backend.get_api_id() + " - Updated gateway fees.")
def bootstrap_twilio_gateway_incoming(orm): currency_class = orm['accounting.Currency'] if orm else Currency sms_gateway_fee_class = orm[ 'smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm[ 'smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria # https://www.twilio.com/sms/pricing/us SmsGatewayFee.create_new( TwilioBackend.get_api_id(), INCOMING, 0.0075, country_code=None, currency=currency_class.objects.get(code="USD"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated INCOMING Twilio gateway fees.")
def _directed_fee(direction, backend_api_id, backend_instance_id): gateway_fee = SmsGatewayFee.get_by_criteria( backend_api_id, direction, backend_instance=backend_instance_id, country_code=country_code) if not gateway_fee or gateway_fee.amount is None: return None usd_gateway_fee = gateway_fee.amount / gateway_fee.currency.rate_to_default usage_fee = SmsUsageFee.get_by_criteria(direction) return fmt_dollar_amount(usage_fee.amount + usd_gateway_fee)
def add_moz_zero_charge(orm): mzn, _ = (orm['accounting.Currency'] if orm else Currency).objects.get_or_create(code='MZN') sms_gateway_fee_class = orm[ 'smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm[ 'smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria SmsGatewayFee.create_new( 'SISLOG', INCOMING, Decimal('0'), country_code=None, prefix='', currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) backend_id = '7ddf3301c093b793c6020ebf755adb6f' try: backend = HttpBackend.get(backend_id) SmsGatewayFee.create_new( backend.get_api_id(), OUTGOING, Decimal('0'), backend_instance=backend._id, country_code=None, prefix='', currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Moz gateway default fees.") except ResourceNotFound: logger.error( "[SMS-BILLING] Could not find HttpBackend %s - did not create outgoing Moz gateway default fees." " Please look into if this is on production, otherwise ignore." % backend_id)
def bootstrap_twilio_gateway_incoming(apps): currency_class = apps.get_model('accounting', 'Currency') if apps else Currency sms_gateway_fee_class = apps.get_model( 'smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model( 'smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria # https://www.twilio.com/sms/pricing/us SmsGatewayFee.create_new( SQLTwilioBackend.get_api_id(), INCOMING, 0.0075, country_code=None, currency=currency_class.objects.get(code="USD"), fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated INCOMING Twilio gateway fees.")
def bootstrap_grapevine_gateway(apps): currency_class = apps.get_model('accounting', 'Currency') if apps else Currency sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria relevant_backends = SQLGrapevineBackend.get_global_backends_for_this_class(SQLGrapevineBackend.SMS) currency = currency_class.objects.get_or_create(code="ZAR")[0] # any incoming message SmsGatewayFee.create_new( SQLGrapevineBackend.get_api_id(), INCOMING, Decimal('0.10'), currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Global Grapevine gateway fees.") # messages relevant to our Grapevine Backends for backend in relevant_backends: SmsGatewayFee.create_new( SQLGrapevineBackend.get_api_id(), INCOMING, Decimal('0.10'), currency=currency, backend_instance=backend.couch_id, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLGrapevineBackend.get_api_id(), OUTGOING, Decimal('0.22'), currency=currency, backend_instance=backend.couch_id, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Grapevine fees for backend %s" % backend.name)
def add_moz_zero_charge(apps): mzn, _ = (apps.get_model("accounting", "Currency") if apps else Currency).objects.get_or_create(code="MZN") sms_gateway_fee_class = apps.get_model("smsbillables", "SmsGatewayFee") if apps else SmsGatewayFee sms_gateway_fee_criteria_class = ( apps.get_model("smsbillables", "SmsGatewayFeeCriteria") if apps else SmsGatewayFeeCriteria ) SmsGatewayFee.create_new( "SISLOG", INCOMING, Decimal("0"), country_code=None, prefix="", currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) backend_id = "7ddf3301c093b793c6020ebf755adb6f" try: backend = HttpBackend.get(backend_id) SmsGatewayFee.create_new( backend.get_api_id(), OUTGOING, Decimal("0"), backend_instance=backend._id, country_code=None, prefix="", currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Moz gateway default fees.") except ResourceNotFound: logger.error( "[SMS-BILLING] Could not find HttpBackend %s - did not create outgoing Moz gateway default fees." " Please look into if this is on production, otherwise ignore." % backend_id )
def bootstrap_grapevine_gateway(orm): currency_class = orm['accounting.Currency'] if orm else Currency sms_gateway_fee_class = orm['smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm['smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria relevant_backends = get_global_backends_by_class(GrapevineBackend) currency = currency_class.objects.get_or_create(code="ZAR")[0] # any incoming message SmsGatewayFee.create_new( GrapevineBackend.get_api_id(), INCOMING, Decimal('0.10'), currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Global Grapevine gateway fees.") # messages relevant to our Grapevine Backends for backend in relevant_backends: SmsGatewayFee.create_new( GrapevineBackend.get_api_id(), INCOMING, Decimal('0.10'), currency=currency, backend_instance=backend.get_id, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( GrapevineBackend.get_api_id(), OUTGOING, Decimal('0.22'), currency=currency, backend_instance=backend.get_id, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Grapevine fees for backend %s" % backend.name)
def bootstrap_apposit_gateway(apps=None): usd_currency, _ = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get_or_create(code="USD") sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new( SQLAppositBackend.get_api_id(), INCOMING, Decimal('0.02'), currency=usd_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLAppositBackend.get_api_id(), OUTGOING, Decimal('0.02'), currency=usd_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Apposit gateway fees.")
def add_moz_zero_charge(orm): mzn, _ = (orm['accounting.Currency'] if orm else Currency).objects.get_or_create(code='MZN') sms_gateway_fee_class = orm['smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm['smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria SmsGatewayFee.create_new( 'SISLOG', INCOMING, Decimal('0'), country_code=None, prefix='', currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) backend_id = '7ddf3301c093b793c6020ebf755adb6f' try: backend = HttpBackend.get(backend_id) SmsGatewayFee.create_new( backend.get_api_id(), OUTGOING, Decimal('0'), backend_instance=backend._id, country_code=None, prefix='', currency=mzn, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Moz gateway default fees.") except ResourceNotFound: logger.error("[SMS-BILLING] Could not find HttpBackend %s - did not create outgoing Moz gateway default fees." " Please look into if this is on production, otherwise ignore." % backend_id)
def bootstrap_telerivet_gateway(apps): default_currency, _ = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get_or_create(code=settings.DEFAULT_CURRENCY) sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new( SQLTelerivetBackend.get_api_id(), INCOMING, Decimal('0.0'), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLTelerivetBackend.get_api_id(), OUTGOING, Decimal('0.0'), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Telerivet gateway fees.")
def bootstrap_telerivet_gateway(orm): default_currency = (orm['accounting.Currency'] if orm else Currency).get_default() sms_gateway_fee_class = orm['smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee sms_gateway_fee_criteria_class = orm['smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria SmsGatewayFee.create_new( TelerivetBackend.get_api_id(), INCOMING, Decimal('0.0'), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( TelerivetBackend.get_api_id(), OUTGOING, Decimal('0.0'), currency=default_currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) logger.info("Updated Telerivet gateway fees.")
def bootstrap_yo_gateway(apps): ugx, _ = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get_or_create(code='UGX') sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new( SQLYoBackend.get_api_id(), INCOMING, Decimal('110.0'), currency=ugx, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) SmsGatewayFee.create_new( SQLYoBackend.get_api_id(), OUTGOING, Decimal('55.0'), currency=ugx, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Yo gateway fees.")
def test_gateway_fee_after_creation(self): expected_fee = Decimal('0.005') self.msg = get_fake_sms(self.domain, self.backend.hq_api_id, self.backend.couch_id, short_text) self.gateway_fee = SmsGatewayFee.create_new(self.backend.hq_api_id, self.msg.direction, expected_fee) create_billable_for_sms(self.msg, delay=False) sms_billables = SmsBillable.objects.filter(domain=self.domain, log_id=self.msg.couch_id) self.assertEqual(sms_billables.count(), 1) self.billable = sms_billables[0] actual_fee = self.billable.gateway_fee.amount self.assertEqual(expected_fee, actual_fee)
def rows(self): rows = [] for criteria in self.sms_gateway_fee_criteria: gateway_fee = SmsGatewayFee.get_by_criteria_obj(criteria) rows.append([ criteria.backend_api_id, (criteria.backend_instance if criteria.backend_instance is not None else "Any"), criteria.direction, (criteria.country_code if criteria.country_code is not None else "Any"), "%(amount)s %(currency)s" % { 'amount': str(gateway_fee.amount), 'currency': gateway_fee.currency.code, }, ]) return rows
def bootstrap_tropo_gateway(apps): currency = (apps.get_model("accounting", "Currency") if apps else Currency).objects.get(code="USD") sms_gateway_fee_class = apps.get_model("smsbillables", "SmsGatewayFee") if apps else SmsGatewayFee sms_gateway_fee_criteria_class = ( apps.get_model("smsbillables", "SmsGatewayFeeCriteria") if apps else SmsGatewayFeeCriteria ) SmsGatewayFee.create_new( SQLTropoBackend.get_api_id(), INCOMING, 0.01, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) rates_csv = open( "corehq/apps/smsbillables/management/" "pricing_data/tropo_international_rates_2013-12-19.csv", "r" ) for line in rates_csv.readlines(): data = line.split(",") if data[1] == "Fixed Line" and data[4] != "\n": SmsGatewayFee.create_new( SQLTropoBackend.get_api_id(), OUTGOING, float(data[4].rstrip()), country_code=int(data[2]), currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) rates_csv.close() # Fee for invalid phonenumber SmsGatewayFee.create_new( SQLTropoBackend.get_api_id(), OUTGOING, 0.01, country_code=None, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Tropo gateway fees.")
def bootstrap_tropo_gateway(apps): currency = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get(code="USD") sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria SmsGatewayFee.create_new( SQLTropoBackend.get_api_id(), INCOMING, 0.01, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) rates_csv = open('corehq/apps/smsbillables/management/' 'pricing_data/tropo_international_rates_2013-12-19.csv', 'r', encoding='utf-8') for line in rates_csv.readlines(): data = line.split(',') if data[1] == 'Fixed Line' and data[4] != '\n': SmsGatewayFee.create_new( SQLTropoBackend.get_api_id(), OUTGOING, float(data[4].rstrip()), country_code=int(data[2]), currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) rates_csv.close() # Fee for invalid phonenumber SmsGatewayFee.create_new( SQLTropoBackend.get_api_id(), OUTGOING, 0.01, country_code=None, currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Tropo gateway fees.")
def handle(self, *args, **options): relevant_backends = get_global_backends_by_class(GrapevineBackend) currency = Currency.objects.get_or_create(code="ZAR")[0] # any incoming message SmsGatewayFee.create_new( GrapevineBackend.get_api_id(), INCOMING, Decimal('0.10'), currency=currency ) logger.info("Updated Global Grapevine gateway fees.") # messages relevant to our Grapevine Backends for backend in relevant_backends: SmsGatewayFee.create_new( GrapevineBackend.get_api_id(), INCOMING, Decimal('0.10'), currency=currency, backend_instance=backend.get_id ) SmsGatewayFee.create_new( GrapevineBackend.get_api_id(), OUTGOING, Decimal('0.22'), currency=currency, backend_instance=backend.get_id ) logger.info("Updated Grapevine fees for backend %s" % backend.name)
def handle(self, *args, **options): # iso -> provider -> rate def get_twilio_data(): twilio_file = open('corehq/apps/smsbillables/management/' 'commands/pricing_data/twilio-international-sms-rates.csv') twilio_csv = csv.reader(twilio_file) twilio_data = {} skip = 0 for row in twilio_csv: if skip < 4: skip += 1 continue else: try: iso = row[0].lower() provider = row[1].split('-')[1].lower().replace(' ', '') rate = float(row[2]) if not(iso in twilio_data): twilio_data[iso] = {} twilio_data[iso][provider] = rate except IndexError: logger.info("Twilio index error %s:" % row) twilio_file.close() return twilio_data # iso -> provider -> (country code, number of subscribers) def get_mach_data(): mach_workbook = xlrd.open_workbook('corehq/apps/smsbillables/management/' 'commands/pricing_data/Syniverse_coverage_list_DIAMONDplus.xls') mach_table = mach_workbook.sheet_by_index(0) mach_data = {} try: row = 7 while True: country_code = int(mach_table.cell_value(row, 0)) iso = mach_table.cell_value(row, 1) network = mach_table.cell_value(row, 5).lower().replace(' ', '') subscribers = 0 try: subscribers = int(mach_table.cell_value(row, 10).replace('.', '')) except ValueError: logger.info("Incomplete subscriber data for country code %d" % country_code) if not(iso in mach_data): mach_data[iso] = {} mach_data[iso][network] = (country_code, subscribers) row += 1 except IndexError: pass return mach_data twilio_data = get_twilio_data() mach_data = get_mach_data() for iso in twilio_data: if iso in mach_data: weighted_price = 0 total_subscriptions = 0 country_code = None calculate_other = False for twilio_provider in twilio_data[iso]: if twilio_provider == 'other': calculate_other = True else: for mach_provider in mach_data[iso]: try: if twilio_provider in mach_provider: country_code, subscriptions = mach_data[iso][mach_provider] weighted_price += twilio_data[iso][twilio_provider] * subscriptions total_subscriptions += subscriptions mach_data[iso][mach_provider] = country_code, 0 break except UnicodeDecodeError: pass if calculate_other: other_rate_twilio = twilio_data[iso]['other'] for _, subscriptions in mach_data[iso].values(): weighted_price += other_rate_twilio * subscriptions if country_code is not None: weighted_price = weighted_price / total_subscriptions SmsGatewayFee.create_new(TwilioBackend.get_api_id(), OUTGOING, weighted_price, country_code=country_code, currency=Currency.objects.get(code="USD")) else: logger.info("%s not in mach_data" % iso) # https://www.twilio.com/help/faq/sms/will-i-be-charged-if-twilio-encounters-an-error-when-sending-an-sms SmsGatewayFee.create_new(TwilioBackend.get_api_id(), OUTGOING, 0.00, country_code=None) logger.info("Updated Twilio gateway fees.")
def bootstrap_grapevine_gateway_update(apps): currency_class = apps.get_model('accounting', 'Currency') if apps else Currency sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria currency = currency_class.objects.get_or_create(code="ZAR")[0] # Incoming message to South Africa SmsGatewayFee.create_new( SQLGrapevineBackend.get_api_id(), INCOMING, Decimal('0.65'), country_code='27', currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) # Outgoing message from South Africa SmsGatewayFee.create_new( SQLGrapevineBackend.get_api_id(), OUTGOING, Decimal('0.22'), country_code='27', currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) # Explicitly include Lesotho fees for pricing table UI. # Incoming message to Lesotho SmsGatewayFee.create_new( SQLGrapevineBackend.get_api_id(), INCOMING, Decimal('0.90'), country_code='266', currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) # Outgoing message from Lesotho SmsGatewayFee.create_new( SQLGrapevineBackend.get_api_id(), OUTGOING, Decimal('0.90'), country_code='266', currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) # Incoming message to arbitrary country SmsGatewayFee.create_new( SQLGrapevineBackend.get_api_id(), INCOMING, Decimal('0.90'), currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) # Outgoing message from arbitrary country SmsGatewayFee.create_new( SQLGrapevineBackend.get_api_id(), OUTGOING, Decimal('0.90'), currency=currency, fee_class=sms_gateway_fee_class, criteria_class=sms_gateway_fee_criteria_class, ) log_smsbillables_info("Updated Global Grapevine gateway fees.")