#
#= productsコントローラー
# Authors:: Sumiyo Yamamoto
# Copyright:: Copyright (C) OrbusNeich Medical K.K.  2010.
#--
# date        name                   note
# 2010.11.5   Sumiyo Yamamoto        新規作成
#-------------------------------------------------------------------------------
#++
class ProductsController < Comm::BaseController::PermanentMaster
  include Comm::Module::Controller::UnifiedIF
  #== コンストラクタ
  #-----------------------------------------------------------------#++
  def initialize
    @mcls = Product
    @multiple_id_cols = {'product1_tag_id'=>true,'product2_tag_id'=>true,'product_set1_tag_id'=>true,'product_set2_tag_id'=>true}
    @search_cls = ProductSearch
  end
  
  # 単価設定のためのOverRide
  # modelのcreate_mng/update_mngに渡されるのでちゃんと処理しましょう...
  def get_table_params
    @table_params = {:main=>params[:product], :sub=>params[:products_price], :flag=>params[:price_edit_flag], :pp_id=>params[:pp_id] }
  end
  
  def find_all
    ss = ProductSearch.new
    ss.table_alias = {'products' => 'a',
                      'products_prices' => 'a'}
    return ss.search(FLAG_ON, ProductsPrice, [], params)
  end
  
  class ProductSearch < Comm::Tool::SqlSearch
    def get_columns_and_tables(tab, join_lists, params, str_vals)
      invalid_vals = (!params || params[:with_invalid]==STR_TRUE) ? '0,1' : '0'
      str_cols = ' * '
      str_tab =  " FROM ( SELECT p.*, pp.id AS pp_id, pp.price AS price, IF(IFNULL(pp2.price_cnt,0) <= 1 AND pp2.stock_price_type_code IS NULL, 1, 0) AS price_edit_flag, ps.product1_tag_id AS product_set1_tag_id, ps.product2_tag_id  AS product_set2_tag_id, ps.supplier_id AS product_set_supplier_id,
                          ps.ps_spec1,
                          ps.ps_spec2,
                          ps.ps_spec3,
                          ps.ps_spec4,
                          ps.ps_spec5,
                          ps.ps_spec6,
                          ps.ps_spec7,
                          ps.ps_spec8,
                          ps.ps_spec9,
                          ps.ps_spec10
                          FROM master_app_production.products AS p 
                          LEFT JOIN master_app_production.product_sets AS ps ON p.product_set_id=ps.id
                          LEFT JOIN (SELECT id, price, product_id FROM products_prices WHERE stock_price_type_code IS NULL GROUP BY product_id) AS pp ON pp.product_id=p.id
                          LEFT JOIN (SELECT count(*) AS price_cnt, max(stock_price_type_code) AS stock_price_type_code, product_id FROM products_prices GROUP BY product_id) AS pp2 ON pp2.product_id=p.id
                          WHERE p.invalid_flag_code in (#{invalid_vals}) ) AS a "
                          
      return str_cols, str_tab, str_vals
    end
  end
  # show 関連
  def add_to_extra_table_columns(merge_h, result)
    arrange_result(merge_h, result, Product)
    arrange_result(merge_h, result, ProductsPrice)
    merge_h['price_edit_flag'] = result['price_edit_flag']
    merge_h['pp_id'] = result['pp_id']
  end
  
  def get_show_where_clause
    int_id = params[:id].to_i
    case int_id
    when -1
      condition = " (SELECT min(id) FROM master_app_production.products) "
    when -2
      condition = " (SELECT max(id) FROM master_app_production.products) "
    else
      condition = int_id.to_s
    end
    return "WHERE a.id = "+condition
  end
  
  def find_one_using_sqlsearch
    str_cols, str_tab, str_vals = @search_cls.new.get_columns_and_tables(nil, nil, nil, nil)
    str_sql = ['SELECT']
    str_sql << str_cols
    str_sql << add_show_select_sentence if respond_to?(:add_show_select_sentence)
    str_sql << str_tab
    str_sql << get_show_where_clause
    result = ProductsPrice.find_by_sql(str_sql.join(' '))
    return result
  end
  
  def count_all
    result = 0
    ret = ProductsPrice.connection.execute('SELECT FOUND_ROWS()')
    ret.each {|x| result = x.first }
    return result
  end
end
