Skip to content

Instantly share code, notes, and snippets.

@DavidEdwards1
Last active November 23, 2017 11:07
Show Gist options
  • Save DavidEdwards1/dc1a1a1d0370c9510180c4697ef5f6b8 to your computer and use it in GitHub Desktop.
Save DavidEdwards1/dc1a1a1d0370c9510180c4697ef5f6b8 to your computer and use it in GitHub Desktop.
The idea of this gist is to generate the number/proportion of companies that would have to get removed from the new banking rollout if it had gone out on 1st Jan 2017 until 1st Nov 2017. This means that we care about companies that were created before Jan 1st and were still users and any companies created since then.
def check_banking(company,first_compare_date)
# look at vat status on first_compare_date, currently and for the year so far
previous_vat_registration = company.is_vat_registered_on?(first_compare_date)
current_vat_registration = company.is_vat_registered?
vat_registered_this_year = company.is_vat_deregistered? && company.vat_deregistration_effective_date > first_compare_date
# mfa bank feeds, check currently enabled and currently disabled separately
still_enabled_previous_bank_feeds = company.bank_feeds.to_a.
select { |feed| feed.state == 'enabled' }.
select { |feed| feed.bank_service&.mfa? }.
select { |feed| feed.enabled_at < first_compare_date }.any?
disabled_previous_bank_feeds = company.bank_feeds.to_a.
select { |feed| feed.state == 'disabled' }.
select { |feed| feed.enabled_at.present? }.
select { |feed| feed.bank_service&.mfa? }.
select { |feed| feed.disabled_at > first_compare_date }.
select { |feed| feed.enabled_at < first_compare_date }.any?
previous_bank_feeds = still_enabled_previous_bank_feeds || disabled_previous_bank_feeds
current_bank_feeds = company.bank_feeds.to_a.
select { |feed| feed.state == 'enabled' }.
select { |feed| feed.bank_service.mfa? }.any?
bank_feeds_this_year = company.bank_feeds.to_a.
select { |feed| feed.state == 'disabled' }.
select { |feed| feed.enabled_at.present? }.
select { |feed| feed.bank_service&.mfa? }.
select { |feed| feed.enabled_at > first_compare_date }.any?
# ruth thinks that people having stock, and then deleting it with no transactions
# for anything other than 'testing/playing around' wil be rare
# check for stock items that existed as of Jan 1st
previous_stock_items = company.stock_items.any? { |stock| stock.created_at.to_date < first_compare_date }
# check for any added this year
stock_items_this_year = company.stock_items.any? { |stock| stock.created_at.to_date > first_compare_date }
starts_in_new_banking = ![previous_vat_registration, previous_vat_registration, previous_stock_items].any?
ends_in_new_banking = ![current_vat_registration,
vat_registered_this_year,
current_bank_feeds,
bank_feeds_this_year,
stock_items_this_year].any?
moves_out_of_new_banking = starts_in_new_banking && !ends_in_new_banking
company_data = {
"company_id": company.id.to_i,
"channel": company.subscription.channel.to_s,
"previous_vat": previous_vat_registration,
"previous_bank_feeds": previous_bank_feeds,
"previous_stock": previous_stock_items,
"current_vat": current_vat_registration || vat_registered_this_year,
"current_bank_feeds": current_bank_feeds || bank_feeds_this_year,
"current_stock": stock_items_this_year,
"starts_in_new_banking": starts_in_new_banking,
"moves_out_of_new_banking": moves_out_of_new_banking,
}
return company_data
end
switch_to_slave_db
models_to_include = [
:subscription,
:subscription_payments,
:bank_feeds,
{ bank_feeds: :bank_service },
:stock_items,
:setup_stage,
]
jan_first = Date.new(2017,1,1)
batch = 1
lower_limit = (batch-1)*50000
upper_limit = batch*50000
file = "/data/results/new_banking_rollout_#{batch}.csv"
CSV.open(file, "w") do |csv|
Company.includes(models_to_include).
where('type': 'UkLimitedCompany').
where('companies.id >= ?',lower_limit).where('companies.id < ?',upper_limit).
where('bank_services.yodlee_mfa_type is not null').
where('bank_feeds.enabled_at is not null').
where.not(:subscriptions => { 'status': ['Expired Free Trial','Live Free Trial'] }).
where('subscriptions.cancelled_at is null or subscriptions.cancelled_at > ?', jan_first).
find_each(batch_size: 10) do |company|
# look for first payment date to get subscription date
first_compare_date = company.subscription_payments.to_a.sort_by { |payment| payment&.dated_on }.first&.dated_on
# if the subscription date does not exist, grab the setup complete date for non Direct
if !first_compare_date && company.setup_stage.stage_type == 'SetupDone'
if company.subscription.channel != 'Direct'
first_compare_date = company.setup_stage.created_at.to_date
# also get this for a Complimentary direct company if they have never paid us
elsif company.subscription.status == 'Complimentary'
first_compare_date = company.setup_stage.created_at.to_date
end
end
# if no date found then skip this company since they never got to a point
# that they would be eligible for new banking
next unless first_compare_date
#finally set first compare date to be jan first if it is before that
first_compare_date = jan_first if first_compare_date < jan_first
data = check_banking(company,first_compare_date)
csv << data.keys if csv.lineno.zero?
csv << data.values
csv.flush
end
end
@DavidEdwards1
Copy link
Author

For companies created since Jan 1st we care about their status on their subscription date or set-up complete date when subscription date is not appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment