#
#= warehousesコントローラー
# Authors:: Sumiyo Yamamoto
# Copyright:: Copyright (C) OrbusNeich Medical K.K.  2010.
#--
# date        name                   note
# 2010.11.5   Sumiyo Yamamoto        新規作成
#-------------------------------------------------------------------------------
#++
class WarehousesController < Comm::BaseController::PermanentMaster
  include Comm::Module::Controller::UnifiedIF
  #== コンストラクタ
  #-----------------------------------------------------------------#++
  def initialize
    @mcls = Warehouse
    @search_cls = WarehouseSearch
  end
  
  def find_all
    ss = WarehouseSearch.new
    ss.table_alias = {'warehouses' => 'a',
                      'sale_areas' => 'a',
                      'warehouses_customers' => 'a'}
    return ss.search(FLAG_ON, Warehouse, [], params)
  end
  
  class WarehouseSearch < 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 w.*,
                                 s.global_area_id,
                                 wc.customer_id,
                                 wc.shipment_customer_id,
                                 wc.send_customer_id,
                                 wc.delivery_id,
                                 wc.trucking_id, 
                                 wc.receiver_name, 
                                 wc.request_time, 
                                 wc.accept_order_trigger_code,
                                 IFNULL(wc.multi_cnt,0) AS multi_cnt,
                                 wc.note AS warehouses_customer_note
                          FROM master_app_production.warehouses AS w 
                          LEFT JOIN master_app_production.sales_areas AS s ON w.sales_area_id=s.id
                          LEFT JOIN (
                            SELECT warehouse_id, customer_id, shipment_customer_id, send_customer_id, delivery_id, trucking_id, receiver_name, request_time, note, accept_order_trigger_code, COUNT(*) AS multi_cnt 
                            FROM ( SELECT * FROM master_app_production.warehouses_customers ORDER BY id ) AS tmp_wc
                            GROUP BY warehouse_id
                        ) AS wc ON w.id=wc.warehouse_id 
                        WHERE w.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, WarehousesCustomer)
    merge_h['multi_cnt'] = result['multi_cnt']
    merge_h['warehouses_customer_note'] = result['warehouses_customer_note']
  end
  
  def get_show_where_clause
    int_id = params[:id].to_i
    case int_id
    when -1
      condition = " (SELECT min(id) FROM warehouses) "
    when -2
      condition = " (SELECT max(id) FROM warehouses) "
    else
      condition = int_id.to_s
    end
    return "WHERE a.id = "+condition
  end
end
