def populate_annual_billing(year): """ add annual_billing for given year. """ sql = """ Select id from services where active = true except select service_id from annual_billing where financial_year_start = :year """ services_without_annual_billing = db.session.execute(sql, {"year": year}) for row in services_without_annual_billing: latest_annual_billing = """ Select free_sms_fragment_limit from annual_billing where service_id = :service_id order by financial_year_start desc limit 1 """ free_allowance_rows = db.session.execute(latest_annual_billing, {"service_id": row.id}) free_allowance = [x[0] for x in free_allowance_rows] print("create free limit of {} for service: {}".format( free_allowance[0], row.id)) dao_create_or_update_annual_billing_for_year( service_id=row.id, free_sms_fragment_limit=free_allowance[0], financial_year_start=int(year))
def test_create_annual_billing(sample_service): dao_create_or_update_annual_billing_for_year(sample_service.id, 9999, 2016) free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, 2016) assert free_limit.free_sms_fragment_limit == 9999
def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service): new_limit = 9999 year = get_current_financial_year_start_year() dao_create_or_update_annual_billing_for_year(sample_service.id, new_limit, year) new_free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, year) assert new_free_limit.free_sms_fragment_limit == new_limit
def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, financial_year_start): current_year = get_current_financial_year_start_year() if not financial_year_start: financial_year_start = current_year dao_create_or_update_annual_billing_for_year(service_id, free_sms_fragment_limit, financial_year_start) # if we're trying to update historical data, don't touch other rows. # Otherwise, make sure that future years will get the new updated value. if financial_year_start >= current_year: dao_update_annual_billing_for_future_years(service_id, free_sms_fragment_limit, financial_year_start)
def get_free_sms_fragment_limit(service_id): financial_year_start = request.args.get('financial_year_start') annual_billing = dao_get_free_sms_fragment_limit_for_year( service_id, financial_year_start) if annual_billing is None: # An entry does not exist in annual_billing table for that service and year. If it is a past year, # we return the oldest entry. # If it is the current or future years, we create an entry in the db table using the newest record, # and return that number. If all fails, we return InvalidRequest. sms_list = dao_get_all_free_sms_fragment_limit(service_id) if not sms_list: raise InvalidRequest( 'no free-sms-fragment-limit entry for service {} in DB'.format( service_id), 404) else: if financial_year_start is None: financial_year_start = get_current_financial_year_start_year() if int(financial_year_start ) < get_current_financial_year_start_year(): # return the earliest historical entry annual_billing = sms_list[0] # The oldest entry else: annual_billing = sms_list[-1] # The newest entry annual_billing = dao_create_or_update_annual_billing_for_year( service_id, annual_billing.free_sms_fragment_limit, financial_year_start) return jsonify(annual_billing.serialize_free_sms_items()), 200