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 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 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 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 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 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_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 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 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 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): 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 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 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_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_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 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(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_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 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 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 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 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.")
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 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)