require "SAP4Rails"
class Exrate < SAP4Rails
# You must define a list of RFCs to preload
  @@funcs = [
       'BAPI_EXCHANGERATE_CREATE',
       'BAPI_EXCHRATE_CREATEMULTIPLE',
       'BAPI_EXCHRATE_GETCURRENTRATES',
       'BAPI_EXCHANGERATE_GETDETAIL',
       'BAPI_EXCHANGERATE_GETFACTORS',
       'BAPI_EXCHRATE_GETLISTRATETYPES',
       'BAPI_EXCHANGERATE_SAVEREPLICA',
       'BAPI_TRANSACTION_COMMIT',
  ]

# You must define a list of attribute accessors to preload
  @@params = [ 'from',
               'extype',
               'to',
               'rate',
               'validfrom',
  ]


  # User defined instance methods here
  attr_accessor :message

  def save()
    RAILS_DEFAULT_LOGGER.warn("[Exrate]#save what did we get: " + self.inspect)

    @errors.add("rate", "must be numeric") unless self.rate =~ /^[0-9\.]+$/
    @errors.add("extype", "must be 'M'") unless self.extype =~ /^M$/
    @errors.add("from", "must be ISO Currency code") unless self.from =~ /^\w{3}$/
    @errors.add("to", "must be ISO Currency code") unless self.to =~ /^\w{3}$/
    @errors.add("validfrom", "must be date format [YYYYMMDD]") unless self.validfrom =~ /^[0-9]{8}$/
    return nil if @errors.count > 0

    str = Exrate.BAPI_EXCHANGERATE_CREATE.exch_rate.structure
    str.rate_type.value   = self.extype
    str.from_curr.value   = self.from
    str.to_currncy.value  = self.to
    str.valid_from.value  = self.validfrom
    str.exch_rate.value   = self.rate
    str.from_factor.value = '1'
    str.to_factor.value   = '1'
    Exrate.BAPI_EXCHANGERATE_CREATE.exch_rate.value = str.value
    Exrate.BAPI_EXCHANGERATE_CREATE.upd_allow.value = 'X'
    Exrate.BAPI_EXCHANGERATE_CREATE.call()
    Exrate.BAPI_TRANSACTION_COMMIT.call()
    result = Exrate.BAPI_EXCHANGERATE_CREATE.return.value
    RAILS_DEFAULT_LOGGER.warn("[Exrate]#save result is: " + result.inspect)
    if result['TYPE'] == "I" and result['NUMBER'] == "005"
      @message = result['MESSAGE']
      return true
    else
      @errors.add("exrate", result['MESSAGE']) unless self.validfrom =~ /^[0-9]{8}$/
      return nil
    end
  end

  # user defined Class methods
  class << self
  
    def detail(id="")
      type, from, to = id.split(/:/)
      RAILS_DEFAULT_LOGGER.warn("[Exrate] id: " + id + " - #{type} : #{from} : #{to}")
      t = Date.today
      tday = sprintf("%04d%02d%02d", t.year.to_i, t.month.to_i, t.day.to_i)
      RAILS_DEFAULT_LOGGER.warn("[Exrate] date: " + tday)
      Exrate.BAPI_EXCHANGERATE_GETDETAIL.rate_type.value = type
      Exrate.BAPI_EXCHANGERATE_GETDETAIL.from_curr.value = from
      Exrate.BAPI_EXCHANGERATE_GETDETAIL.to_currncy.value = to
      Exrate.BAPI_EXCHANGERATE_GETDETAIL.date.value = tday
      Exrate.BAPI_EXCHANGERATE_GETDETAIL.call()
      return Exrate.BAPI_EXCHANGERATE_GETDETAIL.exch_rate.value()
    end

    def list
      t = Date.today
      tday = sprintf("%04d%02d%02d", t.year.to_i, t.month.to_i, t.day.to_i)
      RAILS_DEFAULT_LOGGER.warn("[Exrate] date: " + tday)
      Exrate.BAPI_EXCHRATE_GETCURRENTRATES.date.value = tday
      Exrate.BAPI_EXCHRATE_GETCURRENTRATES.call()
      return Exrate.BAPI_EXCHRATE_GETCURRENTRATES.exch_rate_list.rows()
    end
  end

end

