#
#= 日付パラメータ処理機能群
# Authors:: Sumiyo Yamamoto
# Copyright:: Copyright (C) OrbusNeich Medical K.K.  2010.
#--
# date        name                   note
# 2010.3.12   Sumiyo Yamamoto        新規作成
#-------------------------------------------------------------------------------
#++
module Comm
  module Module
    #= 日付パラメータ処理モジュール
    # 日付パラメータ処理に使えるメソッド群を備える。
    #------------------------------------------------------------------------#++
    module DateParams
    protected
      #== 日付の適正チェック
      #-----------------------------------------------------------------#++
      def is_valid_date?(date = nil)
        result = false
        
        unless date.blank?
          date_array = ParseDate.parsedate(date)
          if Date.valid_date?(date_array[0], date_array[1], date_array[2])
            result = true
          end
        end
        
        return result
      end

      #== 日付取得
      #-----------------------------------------------------------------#++
      def get_date(date = nil)
        if is_valid_date?(date)
          result = Date.parse(date)
        else
          result = Date.today
        end
        
        return result
      end

      #== 期間取得
      #-----------------------------------------------------------------#++
      def get_period
        period = {}
        
        # START
        period[:start] = get_date(params[:start_target_date])
        
        # END
        period[:end] = get_date(params[:end_target_date])
        
        return period
      end

      #== 歴週取得
      #-----------------------------------------------------------------#++
      def get_commercial
        if params[:cwyear] && params[:cweek]
          cwyear = params[:cwyear].to_i
          cweek = params[:cweek].to_i
        else
          d = Date.today
          cwyear = d.cwyear
          cweek = d.cweek
        end
        
        return cwyear, cweek
      end

      #== 歴週から月曜日付取得
      #-----------------------------------------------------------------#++
      def get_monday_by_commercial(cwyear = nil, cweek = nil)
        return Date.commercial(cwyear, cweek, 1)
      end
    end
  end
end
