Created
April 30, 2019 23:22
-
-
Save debborafernandess/993bc90e4e2874d08ae803eac58c7290 to your computer and use it in GitHub Desktop.
Analise do comportamento dos clientes que cancelaram Plano Recorrente em 2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file = File.open('/tmp/cancel.csv', 'w') | |
file << "Mes de cancelamento, cliente, Modalidade, Unidade, Compras apos cancelar, Ultimo Plano comprado, Mes da ultima compra" | |
cancelled = Purchase.cancelled.recurrency.distinct(:person_id).joins(:plan).where("cancelled >= ?", Date.current.beginning_of_year) | |
cancelled.find_each do |purchase| | |
person = purchase.person | |
if person.purchases.where("created_at > ?", purchase.cancelled).exists? | |
new_purchases = person.purchases.where("created_at >= ?", purchase.cancelled) | |
qtd = new_purchases.count | |
last_purchase = new_purchases.last.plan.name | |
last_buy_at = new_purchases.last.created_at | |
end | |
file << "\n #{purchase.cancelled.try(:month)}, #{person.id}, #{purchase.microgym}, #{purchase.location.name}, #{qtd}, #{last_purchase}, #{last_buy_at.try(:month)}" | |
end | |
file.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment