OSDN Git Service

テストケースがすべて通るように最適化(MySQLにも対応)
[elecoma/elecoma.git] / app / controllers / admin / return_items_controller.rb
1 # -*- coding: utf-8 -*-
2 # 返品処理コントローラ
3
4 require 'csv'
5
6 class Admin::ReturnItemsController < Admin::BaseController
7   resource_controller
8   before_filter :admin_permission_check_return_item
9   caches_page :csv
10
11   def index
12   end
13
14   new_action.before do
15     @product_style = ProductStyle.find_by_id(params[:id].to_i)
16   end
17
18   new_action.wants.html do
19     if @product_style.nil?
20       redirect_to :action => :index
21     elsif @product_style.product.retailer_id != session[:admin_user].retailer_id
22       redirect_to :action => :index
23     else
24       render :action => :new
25     end
26   end
27
28   edit.before do
29     ri = ReturnItem.find_by_id(params[:id].to_i)
30     @product_style = ProductStyle.find_by_id(ri.product_style_id) unless ri.nil?
31   end
32
33   edit.wants.html do
34     if @product_style.nil?
35       redirect_to :action => :history
36     elsif @product_style.product.retailer_id != session[:admin_user].retailer_id
37       redirect_to :action => :history
38     else
39       render :action => :edit
40     end
41   end
42
43   [create, update].each do |action|
44     action.before do
45       @product_style = ProductStyle.find_by_id(params[:return_item][:product_style_id].to_i)
46       @return_item.admin_user_id = session[:admin_user].id
47       raise ActiveRecord::RecordNotFound if @product_style.product.retailer_id != session[:admin_user].retailer_id
48     end
49     
50   end  
51   
52   create.wants.html do
53     flash[:return_item_update] = "データを保存しました。"
54     redirect_to :action => "index"
55   end
56
57   update.wants.html do
58     flash[:return_item_update] = "データを保存しました。"
59     redirect_to :action => "history"
60   end
61
62   destroy.wants.html do
63     flash[:return_item_update] = "データを削除しました。"
64     redirect_to :action => "history"
65   end
66
67   def history
68   end
69
70   def history_search
71     get_return_items
72   end
73
74   def search
75     add_retailer_condition
76     @condition = ReturnItemSearchForm.new(params[:condition])
77     unless @condition.valid?
78       render :action => :index
79       return
80     end
81     @condition, @search_list = Product.get_conditions(@condition, params, true)
82     find_options = {
83       :page => params[:page],
84       :per_page => @condition.per_page || 10,
85       :conditions => flatten_conditions(@search_list),
86       :joins => "LEFT JOIN products ON products.id = product_styles.product_id ",
87       :order => "product_styles.id"
88     }
89     @product_styles = ProductStyle.paginate(find_options)
90   end
91
92   def csv_index
93     pairs = CSVUtil.make_csv_index_pairs(params[:controller], page_cache_directory, page_cache_extension)
94     unless pairs
95       @dates = []
96       @urls = []
97       return
98     end
99     @dates = pairs.map do |_, time|
100       time
101     end
102     @urls = pairs.map do |id, _|
103       url_for(:action => :csv, :id => id,:format => "csv")
104     end
105   end
106   
107   def new_csv
108     redirect_to(url_for_date(DateTime.now))
109   end
110
111   def csv
112     # params[:id] はページキャッシュのキーにするだけで抽出条件にはしない
113     if params[:id].blank?
114       render :status => :not_found
115     end
116     condition, join = get_csv_condition
117     rows = ReturnItem.find(:all, :conditions => flatten_conditions(condition), :joins => join).map do |ri|
118       a = []
119       a << ri.product_id
120       a << ri.product_style.code
121       a << ri.product_style.product_name
122       a << ri.product_style.style_name
123       a << ri.product_style.manufacturer_id
124       a << ri.returned_count
125       a << ri.returned_at
126       a
127     end
128     name = params[:id]
129     filename = '%s.csv' % name
130     title = %w(商品ID 商品コード 商品名 規格名称 商品型番 返品数 返品日時)
131     
132     f = CSVUtil.make_csv_string(rows, title)
133     headers['Content-Type'] = "application/octet-stream; name=#{filename}"
134     headers['Content-Disposition'] = "attachment; filename=#{filename}"
135     render :text => Iconv.conv('cp932', 'UTF-8', f.string)    
136     
137   end
138     
139
140   private
141   def get_return_items
142     add_retailer_condition
143     @condition = ReturnItemSearchForm.new(params[:condition])
144     unless @condition.valid?
145       render :action => :index
146       return
147     end
148     @search_list = ReturnItemSearchForm.get_conditions(@condition)
149     @search_list << [ 'product_styles.deleted_at IS NULL' ]
150     find_options = {
151       :page => params[:page], 
152       :per_page => @condition.per_page || 10,
153       :conditions => flatten_conditions(@search_list),
154       :joins=> :product_style,
155       :include => [:product],
156       :order => "return_items.id"
157     }
158     @return_items = ReturnItem.paginate(find_options)
159   end
160   
161   def url_for_date(date)
162     url_for(:action => :csv, :id => date.strftime('%Y%m%d_%H%M%S'),:format => "csv")
163   end  
164
165   def add_retailer_condition
166     addparam = {'retailer_id' => session[:admin_user].retailer_id}
167     params[:condition].merge! addparam unless params[:condition].nil?
168   end
169
170   def get_csv_condition
171     condition = []
172     condition << ["products.retailer_id = ?", session[:admin_user].retailer_id]
173     return condition, "LEFT JOIN product_styles ON product_styles.id = return_items.product_style_id " + "LEFT JOIN products ON products.id = product_styles.product_id "
174   end
175
176 end