OSDN Git Service

2.0開発:他店舗対応
[elecoma/elecoma.git] / app / controllers / admin / totals_controller.rb
1 require 'ostruct'
2 require 'totalizer'
3
4 class Admin::TotalsController < Admin::BaseController
5   before_filter :admin_permission_check_term
6
7   def index
8
9     params[:search] ||= {}
10     [:month, :date_from, :date_to, :sale_start_from, :sale_start_to].each do | key |
11       params[:search][key] = parse_date_select(params[:search], key)
12     end
13     @search = OpenStruct.new(params[:search])
14     params[:page] ||= 'term'
15     @agent = Totalizer.get_instance(params[:page])
16     if not @agent
17       params[:page] = 'term'
18       @agent = Totalizer.get_instance(params[:page])
19     end
20     @sale_start_enabled = (params[:page] == 'product')
21     params[:type] ||= @agent.default_type
22     @title = @agent.title
23     @list_view = @agent.columns
24     @links = @agent.links
25     @labels = @agent.labels
26     begin
27       @records = @agent.get_records(params)
28     rescue => e
29       logger.error e.message
30       e.backtrace.each{|bt|logger.error(bt)}
31     end
32     @total = @agent.total
33     begin
34       flash[:graph] = @agent.graph
35     rescue =>e
36       logger.error(e.message)
37       e.backtrace.each{|bt|logger.error(bt)}
38     end
39   end
40
41   def graph
42     if flash[:graph]
43       send_data flash[:graph], :type => 'image/png', :disposition => 'inline'
44     else
45       head :status => :not_found
46     end
47   end
48
49   def csv
50     totalizer = Object.const_get("#{params[:page]}_totalizer".classify)
51     csv_data, filename = totalizer.csv(params)
52     headers['Content-Type'] = "application/octet-stream; name=#{filename}"
53     headers['Content-Disposition'] = "attachment; filename=#{filename}"
54     render :text => Iconv.conv('cp932', 'UTF-8', csv_data)
55   end
56
57 end
58