OSDN Git Service

在庫管理履歴プレビューが見られない不具合修正
[elecoma/elecoma.git] / app / controllers / admin / product_styles_controller.rb
1 # -*- coding: utf-8 -*-
2 class Admin::ProductStylesController < Admin::BaseController
3   before_filter :admin_permission_check_product,
4     :only => [:create, :new]
5   
6   def new
7     @product = Product.find_by_id(params[:id].to_i)
8     set_product_styles(params[:id].to_i)
9     set_style_category
10   end
11
12   def create_form
13     @product = Product.find_by_id(params[:id].to_i)
14     set_style_category
15     if @style1.nil? && ! @style2.nil?
16       @error_message = "規格1が無い状態で規格 2を登録出来ません。"
17     end
18     if @style1 && @style1 == @style2
19       @error_message = "規格1、規格2で同一規格の選択はできません"
20     end
21     render :layout => false
22   end
23
24   def confirm
25     set_product_styles
26     set_style_category
27     unless @save_flg
28       render :action => "new"
29     end
30   end
31
32   def create
33     set_product_styles
34     if @save_flg
35       @product.product_styles = @product_styles if @product.product_styles.empty?
36       @product.have_product_style = true
37       @product.save
38       # has_manyが正常に動かない時の対策
39       @product_styles.each do |ps|
40         ps.save
41       end
42       flash.now[:notice] = "保存しました"
43     else
44       flash.now[:error] = "保存に失敗しました"
45     end
46     redirect_to :controller => "products", :action => "index"
47   end
48   
49   #在庫管理履歴プレビュー
50   def stock_histories
51     product_style_id = params[:id].to_i
52     if !product_style_id.blank? && product_style_id.to_s =~ /^\d*$/
53       @product_style = ProductStyle.find_by_id(product_style_id.to_i)
54       if !@product_style.blank?
55         @stock_histories = @product_style.stock_histories
56       end
57     else
58       raise "Parameter Invalid"
59     end
60   end
61   
62   protected
63
64   def set_style_category
65     @product_product_styles ||= []
66     if params[:style_id1]
67       @style1 = Style.find_by_id(params[:style_id1].to_i) unless params[:style_id1].blank?
68       @style2 = Style.find_by_id(params[:style_id2].to_i) unless params[:style_id2].blank?
69       if @product_product_styles.blank?
70         @product.product_styles.each do | p_s |
71           @product_product_styles << p_s
72         end
73       end
74     else
75       unless @product.product_styles.empty?
76         @product.product_styles.each do | p_s |
77           @product_product_styles << p_s
78         end
79         #p @product_product_sytles
80         @style1 = @product.product_styles.first.style_category1 && @product.product_styles.first.style_category1.style
81         @style2 = @product.product_styles.first.style_category2 && @product.product_styles.first.style_category2.style
82       end
83     end
84     @style_category1 = @style1.style_categories if @style1
85     @style_category2 = @style2.style_categories if @style2
86     @style_category1 ||= [nil]
87     @style_category2 ||= [nil]
88
89     @product_style_flg = false
90     @product_styles = {}
91     @product_product_styles.each do |p_s|
92       @product_style_flg = true
93       @product_styles["#{p_s.style_category_id1}_#{p_s.style_category_id2}"] = p_s
94       logger.debug "#{p_s.style_category_id1}_#{p_s.style_category_id2}"
95     end
96   end
97
98   def set_product_styles(id = params[:product_id].to_i)
99     @product = Product.find_by_id(id)
100     if params[:product_styles]
101       @product_styles = []
102       @save_flg = true
103       params[:product_styles].each do |idx, value|
104         if value[:enable] == "on"
105           unless product_style = ProductStyle.find(:first,
106                                                    :conditions => ["style_category_id1 #{value[:style_category1].blank? ? "is" : "=" } :style_category_id1 and style_category_id2 #{value[:style_category2].blank? ? "is" : "=" } :style_category_id2 and product_id = :product_id",
107                                                      {:style_category_id1 => value[:style_category1].blank? ? nil : value[:style_category1] , :style_category_id2 => value[:style_category2].blank? ? nil : value[:style_category2], :product_id => @product.id }])
108             product_style = ProductStyle.new(:style_category_id1 => value[:style_category1],
109                                              :style_category_id2 => value[:style_category2], 
110                                              :product_id => @product.id)
111           end
112           if product_style[:id]
113             product_style.update_attributes({:sell_price=>value[:sell_price], 
114                                              :code=>value[:code],
115                                              :manufacturer_id=>value[:manufacturer_id]})
116           else
117             [:sell_price, :code ,:manufacturer_id].each do |column|  
118               product_style[column] = value[column]
119             end
120             product_style[:position] = idx.to_i + 1
121           end
122           @product_styles << product_style
123           unless product_style.valid?
124             @save_flg = false
125             @error_messages ||= ""
126             @error_messages += "#{idx.to_i + 1}行目が不正です。"
127           end
128         end
129       end
130     end
131   end
132
133 end