Created
February 10, 2016 12:19
-
-
Save airspeed/12a58ec3076318590ad1 to your computer and use it in GitHub Desktop.
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
# !#crm | |
def make_stats | |
emails = Set.new | |
stats = [] | |
Customer.find_each do | k | | |
emails.add?( k.email ) or next # customer already reported | |
all_cs = Customer.where( :email => k.email ) | |
all_os = all_cs.map( &:order_carts ) | |
all_os.select{ | order_list | order_list.count > 0 }.count > 1 or next # this customer is NOT a rebuyer -> skip | |
first_order_id = all_os.map{ | w | w.select(" MIN( id ) AS first_order_id ").group( :app_id ).map( &:first_order_id ) }.map( &:first ).min | |
first_order = OrderCart.find( first_order_id ) | |
all_os = all_os.map{ | w | w.group( :app_id ).count } # now in detail | |
b_os = all_os.select{ | h | h.keys.first == 1 }.first and b_os = b_os[1] or b_os = 0 | |
f_os = all_os.select{ | h | h.keys.first == 2 }.first and f_os = f_os[2] or f_os = 0 | |
px_os = all_os.select{ | h | h.keys.first == 4 }.first and px_os = px_os[4] or px_os = 0 | |
cl_os = all_os.select{ | h | h.keys.first == 5 }.first and cl_os = cl_os[5] or cl_os = 0 | |
stats << [ k.email, first_order.created_at.to_date.to_s, app_desc( first_order.app_id ), b_os, f_os, px_os, cl_os ] | |
end | |
stats | |
end | |
def app_desc( app_id ) | |
desc = case app_id | |
when 1 then 'Fotobuch' | |
when 2 then 'Flexiphoto' | |
when 3 then 'Quickboox' | |
when 4 then 'Polapix' | |
when 5 then 'Kalender' | |
else 'Unknown' | |
end | |
desc | |
end | |
# make_stats |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment