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



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 126

def bill_xml xml
  xml.recordno object.intacct_id
  xml.vendorid object.vendor_id
  xml.whenposted object.posted_at
  xml.whencreated object.invoice_date
  xml.whendue object.due_date
  xml.action object.action
  xml.recordid object.record_id
  xml.supdocid object.supdoc_id

  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.projectid line_item.project_id
      }
    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
21
22
23
24
# 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

  if !successful?
    raise IntacctRB::Exceptions::Bill.new(response.at('//error//description2'))
  end

  object.intacct_id
end

#deleteObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/intacctrb/bill.rb', line 46

def delete
  # return false unless object.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

#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



111
112
113
114
115
116
117
118
119
120
# File 'lib/intacctrb/bill.rb', line 111

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



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
105
106
107
108
109
# File 'lib/intacctrb/bill.rb', line 58

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



122
123
124
# File 'lib/intacctrb/bill.rb', line 122

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/intacctrb/bill.rb', line 26

def update
  raise '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

  if !successful?
    raise(response.at('//error//description2'))
  end

  object.intacct_id
end