Class: IntacctRB::JournalEntry

Inherits:
Base show all
Defined in:
lib/intacctrb/journal_entry.rb

Instance Attribute Summary

Attributes inherited from Base

#current_user, #data, #intacct_action, #object, #response, #sent_xml

Instance Method Summary collapse

Methods inherited from Base

#initialize, #intacct_id

Constructor Details

This class inherits a constructor from IntacctRB::Base

Instance Method Details

#createObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/intacctrb/journal_entry.rb', line 4

def create
  return false if object.intacct_id.present?
  send_xml('create') do |xml|
    xml.function(controlid: "f1") {
      xml.send("create") {
        xml.glbatch {
          je_xml xml
        }
      }
    }
  end

  successful?
end

#date_string(date) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/intacctrb/journal_entry.rb', line 187

def date_string(date)
  if date.is_a?(Date) || date.is_a?(DateTime)
    date.strftime('%Y-%m-%d')
  elsif date.is_a? String
    date
  end
end

#get(options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/intacctrb/journal_entry.rb', line 82

def get(options = {})
  send_xml('read') do |xml|
    xml.function(controlid: "f4") {
      xml.read {
        xml.object "glbatch"
        xml.keys object.try(:intacct_id) || options[:intacct_id]
        if options[:fields]
          xml.fields {
            fields.each do |field|
              xml.field field.to_s
            end
          }
        end
      }
    }
  end

  if successful?
    @data = []
    @response.xpath('//glbatch').each do |je|
      @data << OpenStruct.new({
        id: je.at("RECORDNO").content,
        batch_number: je.at("BATCHNO").content,
        journal_id: je.at("JOURNAL").content,
        date: Date.strptime(je.at("BATCH_DATE").content,'%Y-%m-%d'),
        modified_at: DateTime.strptime(je.at("WHENMODIFIED").content, '%Y-%m-%dT%H:%M:%S'),
        description: je.at("BATCH_TITLE").content,
        state: je.at("STATE").content,
        rows: get_rows(je)
      })
    end
    if @data.empty?
      false
    else
      @data
    end
  else
    false
  end
end

#get_date_at(xpath, object) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/intacctrb/journal_entry.rb', line 142

def get_date_at(xpath, object)
  year = object.at("#{xpath}/year").content
  month = object.at("#{xpath}/month").content
  day = object.at("#{xpath}/day").content
  if [year,month,day].any?(&:empty?)
    nil
  else
    Date.new(year.to_i,month.to_i,day.to_i)
  end
end

#get_list(options = {}) ⇒ Object

def delete

# return false unless object.payment.intacct_system_id.present?
send_xml('delete') do |xml|
  xml.function(controlid: "1") {
    xml.delete_bill(externalkey: "false", key: object.intacct_key)
  }
end

successful?

end



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/intacctrb/journal_entry.rb', line 45

def get_list(options = {})
  send_xml('readByQuery') do |xml|
    xml.function(controlid: "f4") {
      xml.readByQuery {
        xml.object "glbatch"
        xml.pagesize (options[:max_items] || 0)
        xml.query options[:filter]
        if options[:fields]
          xml.fields {
            fields.each do |field|
              xml.field field.to_s
            end
          }
        end
      }
    }
  end

  if successful?
    @data = []
    @response.xpath('//glbatch').each do |invoice|
      @data << OpenStruct.new({
        id: invoice.at("RECORDNO").content,
        batch_number: invoice.at("BATCHNO").content,
        journal_id: invoice.at("JOURNAL").content,
        date: Date.strptime(invoice.at("BATCH_DATE").content,'%m/%d/%Y'),
        modified_at: DateTime.strptime(invoice.at("MODIFIED").content, '%m/%d/%Y %H:%H:%S'),
        description: invoice.at("description").content,
        state: invoice.at("STATE").content
      })
    end
    @data
  else
    false
  end
end

#get_rows(je) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/intacctrb/journal_entry.rb', line 123

def get_rows(je)
  rows = []
  je.xpath('//glentry').each do |row|
    rows << {
      type: row.at('TR_TYPE').content,
      amount: row.at('AMOUNT').content,
      account_number: (row.at('ACCOUNTNO') ? row.at('ACCOUNTNO').content : nil),
      memo: (row.at('DESCRIPTION') ? row.at('DESCRIPTION').content : nil),
      location_id: (row.at('LOCATION') ? row.at('LOCATION').content : nil),
      department_id: (row.at('DEPARTMENT') ? row.at('DEPARTMENT').content : nil),
      customer_id: (row.at('CUSTOMER') ? row.at('CUSTOMER').content : nil),
      employee_id: (row.at('EMPLOYEE') ? row.at('EMPLOYEE').content : nil),
      project_id: (row.at('PROJECT') ? row.at('PROJECT').content : nil),
      item_id: (row.at('ITEM') ? row.at('ITEM').content : nil)
    }
  end
  rows
end

#intacct_object_idObject



153
154
155
# File 'lib/intacctrb/journal_entry.rb', line 153

def intacct_object_id
  object.intacct_key
end

#je_item_fields(xml) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/intacctrb/journal_entry.rb', line 167

def je_item_fields xml
  xml.entries {
    object.rows.each do |row|
      xml.glentry {
        xml.tr_type row[:type]
        xml.amount row[:amount]
        xml.accountno row[:account_number]
        xml.description row[:memo]
        xml.location row[:location_id] if row[:location_id]
        xml.department row[:department_id] if row[:department_id]
        xml.customer row[:customer_id] if row[:customer_id]
        xml.employee row[:employee_id] if row[:employee_id]
        xml.projectid row[:project_id] if row[:project_id]
        xml.item row[:item_id] if row[:itemid]
        xml.classid row[:class_id] if row[:class_id]
      }
    end
  }
end

#je_xml(xml) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/intacctrb/journal_entry.rb', line 157

def je_xml xml
  xml.recordno object.intacct_id if object.intacct_id
  xml.journal object.journal_id
  xml.batch_date date_string(object.date) if object.date
  xml.reverse_date date_string(object.reverse_date) if object.reverse_date
  xml.batch_title object.description
  xml.referenceno object.reference_number
  je_item_fields(xml)
end

#updateObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/intacctrb/journal_entry.rb', line 19

def update
  return false unless object.intacct_id.present?
  send_xml('update') do |xml|
    xml.function(controlid: "f1") {
      xml.send("update") {
        xml.glbatch {
          je_xml xml
        }
      }
    }
  end

  successful?
end