OSDN Git Service

Merge branch 'feature/travis_ci'
[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     header = %w( 商品ID 商品コード 商品名 規格名称 商品型番 返品数 返品日時 )
131     csv_text = CSVUtil.make_csv_string(rows, header)
132     send_csv(csv_text, filename)
133   end
134     
135
136   private
137   def get_return_items
138     add_retailer_condition
139     @condition = ReturnItemSearchForm.new(params[:condition])
140     unless @condition.valid?
141       render :action => :index
142       return
143     end
144     @search_list = ReturnItemSearchForm.get_conditions(@condition)
145     @search_list << [ 'product_styles.deleted_at IS NULL' ]
146     find_options = {
147       :page => params[:page], 
148       :per_page => @condition.per_page || 10,
149       :conditions => flatten_conditions(@search_list),
150       :joins=> :product_style,
151       :include => [:product],
152       :order => "return_items.id"
153     }
154     @return_items = ReturnItem.paginate(find_options)
155   end
156   
157   def url_for_date(date)
158     url_for(:action => :csv, :id => date.strftime('%Y%m%d_%H%M%S'),:format => "csv")
159   end  
160
161   def add_retailer_condition
162     addparam = {'retailer_id' => session[:admin_user].retailer_id}
163     params[:condition].merge! addparam unless params[:condition].nil?
164   end
165
166   def get_csv_condition
167     condition = []
168     condition << ["products.retailer_id = ?", session[:admin_user].retailer_id]
169     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 "
170   end
171
172 end