# -*- coding: utf-8 -*-
class Mobile::SaleDetailsController < CommLogistics::Base::Controller::MobileController
  def index
    find_sale
    @title_date = date_to_title(params[:start_target_date], params[:end_target_date])

    # params で指定されたヘッダを含むSQLの生成
    # see also) mobile_contorller#params_to_scopes
    @sassigned = params_to_scopes

    # params で指定されたヘッダを
    # ビューにの先頭に追加するための情報を生成
    # see also) mobile_controller#col_descriptions
    @results_header = Array.new
    for sa in @sassigned do
      @results_header << col_descriptions(sa)
    end


    # @all から params で指定されたカラム(@sassigned)を引いたカラムを出力する
    # lot_number 毎の本数集計を出力するので、:lot_number は @all に含まない
    # product_category_id は product_set を見ればわかるので含めない
    @all = [:sales_area_id, :user_position_id, :warehouse_id, :operate_date, :customer_id, :product_set_id, :product_id]

    # ビューの先頭に追加した情報以外は
    # 全て出力の対象とする
    print_fields_tmp = (@all - @sassigned)
    @print_fields = Array.new
    for sa in print_fields_tmp do
      @print_fields << col_descriptions(sa)
    end

    @results = @arobj.period({:start => params[:start_target_date], :end => params[:end_target_date]}).
      find(:all,
           :joins => :sale_details,
           :select => "sales_area_id, user_position_id, warehouse_id, operate_date, customer_id, sale_details.product_set_id, sale_details.product_id, sale_details.product_category_id, SUM(sale_details.quantity) as sum_quantity",
           :group  => "sales_area_id, user_position_id, warehouse_id, operate_date, customer_id, sale_details.product_set_id, sale_details.product_id, sale_details.product_category_id",
           :conditions => @conditions.join(" AND ")
           )
    @results = hashfy_and_add_disp_names(@results)
    @total_quantity = total_quantity(@results)
    
    respond_to do |format|
      format.html
    end
  end

end
