Class: IntacctRB::Bill

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

Instance Attribute Summary collapse

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 Attribute Details

#customer_dataObject

Returns the value of attribute customer_data.



3
4
5
# File 'lib/intacctrb/bill.rb', line 3

def customer_data
  @customer_data
end

Instance Method Details

#bill_xml(xml) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/intacctrb/bill.rb', line 121

def bill_xml xml
  xml.recordno object.intacct_id
  xml.po_number object.po_number if object.po_number
  xml.vendorid object.vendor_id if object.vendor_id
  xml.whenposted object.posted_at if object.posted_at
  xml.whencreated object.invoice_date if object.invoice_date
  xml.whendue object.due_date if object.due_date
  xml.action object.action if object.action
  xml.recordid object.record_id if object.record_id
  xml.docnumber object.reference_number if object.reference_number
  xml.supdocid object.supdoc_id if object.supdoc_id
  custom_fields_xml(xml, object)
  if object.line_items
    xml.apbillitems {
      object.line_items.each do |line_item|
        xml.apbillitem {
          xml.accountno line_item.
          xml.amount line_item.amount
          xml.entrydescription line_item.memo
          xml.locationid line_item.location_id
          xml.departmentid line_item.department_id
          xml.projectid line_item.project_id
          xml.classid line_item.class_id
        }
      end
    }
  end
  run_hook :custom_bill_fields, xml
  run_hook :bill_item_fields, xml
end

#createObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/intacctrb/bill.rb', line 6

def create
  return false if object.intacct_system_id.present?

  send_xml('create') do |xml|
    xml.function(controlid: "f1") {
      xml.create {
        xml.apbill {
          bill_xml xml
        }
      }
    }
  end

  return_result(response)
end

#deleteObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/intacctrb/bill.rb', line 38

def delete
  return false unless object.intacct_id.present?

  send_xml('delete') do |xml|
    xml.function(controlid: "f1") {
      xml.delete {
        xml.object 'APBILL'
        xml.keys object.intacct_id
      }
    }
  end

  return_result(response)
end

#delete_intacct_keyObject



160
161
162
# File 'lib/intacctrb/bill.rb', line 160

def delete_intacct_key
  object.intacct_key = nil
end

#delete_intacct_system_idObject



156
157
158
# File 'lib/intacctrb/bill.rb', line 156

def delete_intacct_system_id
  object.intacct_system_id = nil
end

#get_date_at(xpath, object) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/intacctrb/bill.rb', line 106

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



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/intacctrb/bill.rb', line 53

def get_list(options = {})
  send_xml('get_list') do |xml|
    xml.function(controlid: "f4") {
      xml.get_list(object: "bill", maxitems: (options[:max_items] || 0),
        start: (options[:start] || 0), showprivate:"true") {
        if options[:filters]
          xml.filter {
            options[:filters].each do |filter|
              xml.expression do
                filter.each_pair do |k,v|
                  xml.send(k,v)
                end
              end
            end
          }
        end
        if options[:fields]
          xml.fields {
            fields.each do |field|
              xml.field field.to_s
            end
          }
        end
      }
    }
  end

  if successful?
    @data = []
    @response.xpath('//bill').each do |invoice|
      @data << OpenStruct.new({
        id: invoice.at("key").content,
        vendor_id: invoice.at("vendorid").content,
        bill_number: invoice.at("billno").content,
        po_number: invoice.at("ponumber").content,
        state: invoice.at("state").content,
        date_posted: get_date_at('dateposted', invoice),
        date_due: get_date_at('datedue', invoice),
        date_paid: get_date_at('datepaid', invoice),
        total: invoice.at("totalamount").content,
        total_paid: invoice.at("totalpaid").content,
        total_due: invoice.at("totaldue").content,
        termname: invoice.at("termname").content,
        description: invoice.at("description").content,
        modified_at: invoice.at("whenmodified").content
      })
    end
    @data
  else
    false
  end
end

#intacct_object_idObject



117
118
119
# File 'lib/intacctrb/bill.rb', line 117

def intacct_object_id
  "#{intacct_bill_prefix}#{object.id}"
end

#set_date_time(type) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/intacctrb/bill.rb', line 164

def set_date_time type
  if %w(create update delete).include? type
    if object.respond_to? :"intacct_#{type}d_at"
      object.send("intacct_#{type}d_at=", DateTime.now)
    end
  end
end

#set_intacct_system_idObject



152
153
154
# File 'lib/intacctrb/bill.rb', line 152

def set_intacct_system_id
  object.intacct_system_id = intacct_object_id
end

#updateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/intacctrb/bill.rb', line 22

def update
  raise IntacctRB::Exceptions::Bill.new('You must pass an id to update a bill') unless object.intacct_id.present?

  send_xml('update') do |xml|
    xml.function(controlid: "f1") {
      xml.update {
        xml.apbill(key: object.intacct_id) {
          bill_xml xml
        }
      }
    }
  end

  return_result(response)
end