Skip to content

Instantly share code, notes, and snippets.

@airspeed
Last active August 29, 2015 14:09
Show Gist options
  • Save airspeed/5b4978ff8f1e24969816 to your computer and use it in GitHub Desktop.
Save airspeed/5b4978ff8f1e24969816 to your computer and use it in GitHub Desktop.
Bestellungen nach Uhr.
# string product_code - ["CLXB5S1F", "CLXB5S2F", "CLXB5S1Q", "CLXB5S2Q", "CLXB6S1F", "CLXB6S2F", "CLXF3S1F", "CLXF4S1F", "CLXF5S1F", "CLXF6S1F"]
# Date start_date - mit in den Report eingeschlossen
# Date end_date - mit in den Report eingeschlossen
# string path - der User muss schreibberechtigt darauf sein
def make_happy_hour_stats( product_code, start_date, end_date, path )
timespan = ( start_date .. end_date )
header = timespan.map( &:to_s ).unshift( "Uhr" )
CSV.open( path, "w" ) do | csv |
csv << [product_code]
csv << header
OrderItem.where( :product_code => product_code, :created_at => start_date .. end_date + 1.day ).sort_by{ | w | w.created_at.hour }.group_by{ | w | w.created_at.hour }.each do | k, v |
row = timespan.map{ | d | v.select{ | i | i.created_at.to_date === d }.map( &:quantity ).sum }
row.unshift( k )
csv << row
end
end
end
# Usage:
# make_happy_hour_stats( "CLXB5S1F", Date.parse( '2014-10-01' ), Date.parse( '2014-10-31' ), "/Users/fpugl/Downloads/stats.csv" )
# string start_date - mit in den Report eingeschlossen, bitte MYSQL-Format jjjj-mm-tt einhalten
# string end_date - mit in den Report eingeschlossen, bitte MYSQL-Format jjjj-mm-tt einhalten
# string basepath - der User muss schreibberechtigt darauf sein
def auto_happy_hour_stats( start_date, end_date, basepath = "/home/deploy/" )
product_code = [ "CLXB5S1F", "CLXB5S2F", "CLXB5S1Q", "CLXB5S2Q", "CLXB6S1F", "CLXB6S2F", "CLXF3S1F", "CLXF4S1F", "CLXF5S1F", "CLXF6S1F" ]
product_code.each do | c |
make_happy_hour_stats( c, Date.parse( start_date ), Date.parse( end_date ), Pathname.new( basepath ).join( "#{ c }_#{ start_date }_#{ end_date }.csv" ).to_s )
end
end
# Usage:
# auto_happy_hour_stats( '2014-10-01', '2014-10-31', "/Users/fpugl/Downloads/" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment