Skip to content

Instantly share code, notes, and snippets.

@airspeed
Last active February 25, 2016 14:56
Show Gist options
  • Save airspeed/914f0cdf91b8742c6734 to your computer and use it in GitHub Desktop.
Save airspeed/914f0cdf91b8742c6734 to your computer and use it in GitHub Desktop.
def cs16
result = []
ps = [ 'PLBF6S1F', 'PLBF5S1F', 'PLBF4S1F', 'PLBF3S1F', 'PLBFXBX1']
is = OrderItem.where( :product_code => ps )
uids = is.map( &:user_id ).uniq
iids = is.map( &:id ).uniq # eigentlich unnötig, aber schadet ja nicht
oids = is.map( &:order_id ).uniq
result << [ 'User ID', 'User Email', 'Anzahl Bestellungen', 'Gesamtbestellwert', 'Bestellte Produkte' ]
uids.each do | uid |
u = User.where( :id => uid ).last or next
os = u.orders.where( :state => :printed.to_s, :id => oids )
is = OrderItem.where( :order_id => oids, :id => iids, :user_id => uid ).map( &:product_code ).uniq.join('|')
result << [ u.id, u.email, os.count, os.map( &:total_price ).sum, is ] unless os.count.zero?
end
result
end
# Usage:
# cs16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment