Last active
August 29, 2015 14:23
-
-
Save airspeed/5aba5b4cc75e36a3874f to your computer and use it in GitHub Desktop.
#4956
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
# 2. Wieviele Polapix wurden im Schnitt bestellt? | |
# Note that / has higher precedence than |, @see also http://stackoverflow.com/q/21060234 | |
Voucher.all.map{ | g | [ g.code, Order.where( :voucher_id => g.id, :state => :printed.to_s ).map{ | w | w.order_items.map( &:quantity ).sum }.sum / ( Order.where( :voucher_id => g.id, :state => :printed.to_s ).count | 1 ) ] } | |
# 3. Wieviele Bilder wurden pro Kategorie (gemäß Aufsplittung von Julien - 1-3 bestellte Fotos, 4-6 bestellte Fotos, etc.) bestellt? | |
def class_voucher_detail( voucher_id, classes ) | |
results = [] | |
classes.each do | range | | |
n = Order.where( :state => :printed.to_s, :voucher_id => voucher_id ).select{ | w | range.include?( w.order_items.map( &:quantity ).sum ) }.count | |
results << "#{ range }: #{ n }" | |
end | |
results | |
end | |
def classes_voucher_auto( vouchers ) | |
results = [] | |
classes = [ | |
1 .. 3, | |
4 .. 8, | |
9 .. 11, | |
12 .. 16, | |
17 .. 23, | |
24 .. 30, | |
31 .. 36, | |
36 .. 10000 | |
] | |
vouchers.each do | g | | |
results << [ g.code, class_voucher_detail( g.id, classes ) ] | |
end | |
puts results | |
end | |
# Usage: | |
# classes_voucher_auto( Voucher.all ) | |
# 4. Wieviele Kunden, die über einen Gutschein bestellt haben, haben danach nochmals Polapix bestellt? | |
Voucher.all.map{ | g | [ g.code, Order.where( :state => :printed.to_s, :voucher_id => g.id ).reject{ | w | w.user.nil? }.reject{ | w | w.user.orders.last.voucher_id == g.id }.map{ | w | w.user_id }.uniq.count ] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment