Class: Agris::Api::Grain::Contract

Inherits:
Object
  • Object
show all
Includes:
XmlModel
Defined in:
lib/agris/api/grain/contract.rb

Defined Under Namespace

Classes: Schedule

Constant Summary collapse

ATTRIBUTE_NAMES =
%w(
  contract_location
  contract_number
  date_written
  integration_guid
  purchase_sales
  purchase_sales_description
  contract_location_description
  contract_name_id
  contract_name_description
  contract_type
  unique_id
  contract_type_description
  status
  status_description
  signed
  commodity
  commodity_description
  class
  variety
  transport_mode
  transport_description
  external_contract_no
  delivery_terms
  advance_percent
  contract_loads
  applied_loads
  remaining_loads
  contract_quantity
  applied_quantity
  remaining_quantity
  ordered_quantity
  unordered_quantity
  uom
  uom_description
  dp_table
  dp_table_description
  invoice_terms
  invoice_terms_description
  quantity_base
  quantity_base_description
  weight_base
  weight_base_description
  grade_base
  grade_base_description
  ship_to_from_id
  ship_to_from_description
  misc_id
  misc_description
  agent_broker_id
  agent_broker_description
  qty_uom
  qty_uom_description
  last_change_datetime
  delete
  last_change_user_id
  last_change_username
  remarks
).freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XmlModel

included

Constructor Details

#initialize(hash = {}) ⇒ Contract

Returns a new instance of Contract.



97
98
99
100
101
# File 'lib/agris/api/grain/contract.rb', line 97

def initialize(hash = {})
  super

  @schedules = []
end

Class Method Details

.add_transcodes_to_schedule(schedule) ⇒ Object

This adds the transcode attributes like salesperson into the schedule



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/agris/api/grain/contract.rb', line 104

def self.add_transcodes_to_schedule(schedule)
  if schedule['trancodes']
    trancodes = schedule['trancodes']['trancode']
    trancodes = [trancodes] unless trancodes.class == Array
    trancodes.each do |trancode|
      label_code = trancode['label'].downcase + 'code'
      schedule[label_code] = trancode['code']
      label_description = trancode['label'].downcase + 'description'
      schedule[label_description] = trancode['description']
    end
  end
  schedule
end

.from_json_hash(hash) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/agris/api/grain/contract.rb', line 85

def self.from_json_hash(hash)
  super.tap do |contract|
    if hash['schedules']
      contract.schedules.concat(
        hash['schedules'].map do |schedule|
          Schedule.from_json_hash(schedule)
        end
      )
    end
  end
end

.from_xml_hash(hash) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/agris/api/grain/contract.rb', line 70

def self.from_xml_hash(hash)
  super.tap do |document|
    if hash['schedules']
      document.schedules.concat(
        [hash['schedules']['schedule']]
          .flatten
          .map do |schedule|
            enhanced_schedule = add_transcodes_to_schedule(schedule)
            Schedule.from_xml_hash(enhanced_schedule)
          end
      )
    end
  end
end