Skip to content

Instantly share code, notes, and snippets.

@rmeringe
Last active September 17, 2018 20:43
Show Gist options
  • Select an option

  • Save rmeringe/e23aea453636f6f68eaaa51cf2d79e5e to your computer and use it in GitHub Desktop.

Select an option

Save rmeringe/e23aea453636f6f68eaaa51cf2d79e5e to your computer and use it in GitHub Desktop.
Update Tax Rates to FA on active subscriptions
unequal = []
subs = SubscriptionEngine::Subscription.active
count = 0
subs.each do |sub|
count += 1
package_entry = SubscriptionEngine::FirstAssociatesPackageEntry.where(subscription_id: sub.id).last
original_tax_rate = package_entry.data['sales_tax_rate']
calculator = ::SubscriptionCalculators::RecurringPaymentCalculator.new(sub)
new_tax_amount = calculator.tax_subtotal.cents
new_tax_rate = calculator.tax_rate.to_s
puts "Sub Id: #{sub.id}", original_tax_rate, new_tax_rate
unequal.push({sub_id: sub.id, original: original_tax_rate, new: new_tax_rate, new_amount: new_tax_amount}) if original_tax_rate != new_tax_rate
puts count
end
output = []
last_pulled_date = '20180917' # Don't forget to update!
unequal.each do |item|
puts item
sub_id= item[:sub_id]
order = OrderEngine::Order.where(subscription_id: sub_id).first
tax_amount = item[:new_amount]
tax_rate = item[:new]
address = UserEngine::Person.where(id: order.person_id).first.current_address
latest_tax_rate = UserEngine::TaxRate.where(address_id: address.id).order(created_at: :desc)&.first
latest_tax_rate_before_last_pull = UserEngine::TaxRate.where(address_id: address.id).where('created_at < ?', last_pulled_date).order(created_at: :desc)&.first
next if latest_tax_rate.nil?
backdated_at = latest_tax_rate.created_at
next if backdated_at < last_pulled_date
# check if the tax rate has changed between the last pulled date or not
if latest_tax_rate_before_last_pull.present?
next if latest_tax_rate.rate == latest_tax_rate_before_last_pull.rate
end
next if tax_rate.nil?
puts "Tax rate", tax_rate
output.push({order_id: order.id, sales_tax_amount: tax_amount, sales_tax_rate: tax_rate, backdated_at: backdated_at&.strftime("%m/%d/%Y")})
end
# Copy the output of output locally and then run the following in your local rails console:
CSV.open("changing_tax_rates_09_17.csv", "wb") do |csv|
csv << ["Order Id", "Sales Tax Amount", "Sales Tax Rate", "Backdated At"]
lines.each do |line|
csv << [line[:order_id], line[:sales_tax_amount], line[:sales_tax_rate], line[:backdated_at]]
end
end
# Take this file and send it in an email to FA, and then update the last_pulled_date in this code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment