#
#= payablesコントローラー
# Authors:: Sumiyo Yamamoto
# Copyright:: Copyright (C) OrbusNeich Medical K.K.  2010.
#--
# date        name                   note
# 2010.12.6   Sumiyo Yamamoto        新規作成
#-------------------------------------------------------------------------------
#++
class PayablesController < Comm::BaseController::Basic
  include CommLogistics::Modules::Print::Controller
  #== コンストラクタ
  #-----------------------------------------------------------------#++
  def initialize
    @pdf_cls = PayablePdf
    @mcls = Payable
    @pdf_sort_column = 'cutoff_date'
    @multiple_id_cols = {'cutoff_day_code'=>true}
  end
  
  def print_sheet
    logger.debug('print_sheet @ list')
    params[:sort] = @pdf_sort_column
    params[:record_list] = find_all
    print_sheet_base
  end
  
  # override basic controller
  def find_all(calc_rows_flag = FLAG_OFF)
    ss = PayablesSearch.new
    ss.table_alias = {'payables' => 't',
                      'suppliers' => 't'}
    return ss.search(calc_rows_flag, @mcls, @join_mcls, params)
  end
  
  class PayablesSearch < Comm::Tool::SqlSearch
    def get_columns_and_tables(tab, join_lists, params, str_vals)
      params[:start_target_date]=Date.parse(params[:start_target_date]).beginning_of_month.to_s
      str_cols = " * "
      str_tab =  " FROM ( SELECT
               a.id,
               a.supplier_id,
               a.supplier_group_id,
               a.cutoff_day_code,
               a.cutoff_date,
               a.payment_date,
               a.payment_type_code,
               a.trade_sheet_cnt,
               a.created_at,
               a.updated_at,
               a.total,
               IF(a.cutoff_date<\'#{params[:end_target_date]}\', 0, a.total_price ) AS total_price,
               IF(a.cutoff_date<\'#{params[:end_target_date]}\', 0, a.total_duty ) AS total_duty,
               IF(a.cutoff_date<\'#{params[:end_target_date]}\', 0, a.total_payment ) AS total_payment,
               IF(a.cutoff_date<\'#{params[:end_target_date]}\', 0, a.total_carry ) AS total_carry,
               IF(a.cutoff_date<\'#{params[:end_target_date]}\', 0, a.total_carry_duty ) AS total_carry_duty,
               IF(a.cutoff_date<\'#{params[:end_target_date]}\', a.total , a.pre_total) AS pre_total,
               s.kana_name AS kana_name
            FROM ( SELECT * 
              FROM (
                    SELECT * FROM payables WHERE cutoff_date <= \'#{params[:end_target_date]}\' ORDER BY cutoff_date DESC
                   ) AS tmp_a 
             GROUP BY tmp_a.supplier_id) AS a
             LEFT JOIN master_app_production.suppliers AS s ON a.supplier_id=s.id
             WHERE a.cutoff_date BETWEEN \'#{params[:start_target_date]}\' AND \'#{params[:end_target_date]}\' OR a.total != 0
           ) AS t "
      return str_cols, str_tab, str_vals
    end
    #空実装
    def set_target_date_to_where(tab, params, str_where)
    end
  end
  # 買掛金一覧
  class PayablePdf < CommLogistics::Modules::Print::Controller::PdfList
    def initialize(params, mcls=nil)
      @pdf_basename = 'payables'
      @total_columns = ['total_price','total_duty','total_payment','total_carry','total_carry_duty','pre_total','total','trade_sheet_cnt']
      @paging_column = 'cutoff_date'
      @total_title_column = 'supplier_dn'
      super
    end
  end
end
