Last active
August 29, 2015 14:14
-
-
Save airspeed/282696124e2a458cb7e2 to your computer and use it in GitHub Desktop.
Erstellt die Statistiken über die täglich bestellten Produkte in einem bestimmten Zeitraum.
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
def make_stats( from_date, to_date = Date.today.to_s, products = [ 'CLXB5S1Q', 'CLXB5S2Q' ] ) | |
stats = [] | |
t0 = Date.parse( from_date ) | |
t1 = Date.parse( to_date ) | |
dt = t0 .. t1 | |
dt.each do | datum | | |
sdatum = I18n.l( datum, :locale => 'de' ) | |
os = Order.where( :created_at => datum .. datum + 1, :state => :printed.to_s ).map( &:id ) # Alle gedruckten Order im Zeitraum | |
ois = OrderItem.where( :order_id => os ) # Alle gedruckten OrderItem im Zeitraum | |
is = ois.where( :product_code => products ) # Alle gedruckten OrderItem mit Squarebüchern im Zeitraum | |
fis = [] | |
mis = [] | |
fmis = [] | |
is.each do | i | | |
fis << i if i && i.order && i.order.user && i.order.id === i.order.user.orders.first.id # first order for this user | |
mis << i if i && i.order && i.order.user && ( i.quantity > 1 || i.order.order_items.count > 1 || ois.select{ | w | w.user_id === i.user_id }.count > 1 ) # more than one book ordered in dt | |
fmis << i if i && i.order && i.order.user && fis.include?( i ) && mis.include?( i ) # first order for this user AND more than one book ordered in dt | |
end | |
count = is.sum( &:quantity ) | |
fcount = fis.sum( &:quantity ) | |
mcount = mis.sum( &:quantity ) | |
fmcount = fmis.sum( &:quantity ) | |
fdetail = fis.map( &:user_id ).uniq | |
mdetail = mis.map( &:user_id ).uniq | |
fmdetail = fmis.map( &:user_id ).uniq | |
stats << [ sdatum, count, fcount, mcount, fmcount, fdetail, mdetail, fmdetail ] | |
end | |
stats.each do | s | | |
puts "Datum: #{ s[ 0 ] } | Stückzahl: #{ s[ 1 ] } | Davon von Erstbestellern: #{ s[ 2 ] } | Davon von Mehrfachbestellern ( im gesamten Zeitraum ): #{ s[ 3 ] } | Davon von Erstbestellern, die gleichzeitig auch Mehrfachbesteller ( im gesamten Zeitraum ) sind: #{ s[ 4 ] }" | |
end | |
puts "Gesamt: #{ stats.map{ | s | s[ 1 ] }.sum }" | |
puts "Gesamt von Erstbestellern: #{ stats.map{ | s | s[ 2 ] }.sum }" | |
puts "Gesamt von Mehrfachbestellern: #{ stats.map{ | s | s[ 3 ] }.sum }" | |
puts "Gesamt von Erstbestellern, die gleichzeitig auch Mehrfachbesteller ( im gesamten Zeitraum ) sind: #{ stats.map{ | s | s[ 4 ] }.sum }" | |
puts "Anzahl Erstbesteller: #{ stats.map{ | s | s[ 5 ] }.flatten.uniq.count }" | |
puts "Anzahl Mehrfachbesteller: #{ stats.map{ | s | s[ 6 ] }.flatten.uniq.count }" | |
puts "Anzahl Erstbesteller, die gleichzeitig auch Mehrfachbesteller ( im gesamten Zeitraum ) sind: #{ stats.map{ | s | s[ 7 ] }.flatten.uniq.count }" | |
end | |
# Usage | |
# make_stats( '2015-01-19', '2015-01-30', [ 'CLXB5S1Q', 'CLXB5S2Q' ] ) | |
# make_stats( '2015-01-01' ) # seit Jahresanfang |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment