Class: IntacctRB::Invoice

Inherits:
Base show all
Defined in:
lib/intacctrb/invoice.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/invoice.rb', line 3

def customer_data
  @customer_data
end

Instance Method Details

#createObject



43
44
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/invoice.rb', line 43

def create
  return false if object.invoice.intacct_system_id.present?

  # Need to create the customer if one doesn't exist
  intacct_customer = Intacct::Customer.new object.customer
  unless object.customer.intacct_system_id.present?
    unless intacct_customer.create
      raise 'Could not create customer'
    end
  end

  if intacct_customer.get
    object.customer = intacct_customer.object
    @customer_data = intacct_customer.data
  else
    raise 'Could not grab Intacct customer data'
  end

  # Create vendor if we have one and not in Intacct
  if object.vendor and object.vendor.intacct_system_id.blank?
    intacct_vendor = Intacct::Vendor.new object.vendor
    if intacct_vendor.create
      object.vendor = intacct_vendor.object
    else
      raise 'Could not create vendor'
    end
  end

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

  successful?
end

#deleteObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/intacctrb/invoice.rb', line 82

def delete
  return false unless object.invoice.intacct_system_id.present?

  send_xml('delete') do |xml|
    xml.function(controlid: "1") {
      xml.delete_invoice(externalkey: "false", key: object.invoice.intacct_key)
    }
  end

  successful?
end

#delete_intacct_keyObject



121
122
123
# File 'lib/intacctrb/invoice.rb', line 121

def delete_intacct_key
  object.invoice.intacct_key = nil
end

#delete_intacct_system_idObject



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

def delete_intacct_system_id
  object.invoice.intacct_system_id = nil
end

#get_list(*fields) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/intacctrb/invoice.rb', line 6

def get_list *fields
  # return false unless object.intacct_system_id.present?

  fields = [
    :customerid,
    :name,
    :termname
  ] if fields.empty?

  send_xml('get_list') do |xml|
    xml.function(controlid: "f4") {
      xml.get_list(object: "invoice", maxitems: "10", showprivate:"false") {
        # xml.fields {
        #   fields.each do |field|
        #     xml.field field.to_s
        #   end
        # }
      }
    }
  end

  if successful?
    @data = []
    @response.xpath('//invoice').each do |invoice|
      @data << Invoice.new({
         id: invoice.at("//customerid").content,
         total: invoice.at("//totalamount").content,
         total_paid: invoice.at("//totalpaid").content,
         termname: invoice.at("//termname").content
       })
    end
    @data
  else
    false
  end
end

#intacct_object_idObject



94
95
96
# File 'lib/intacctrb/invoice.rb', line 94

def intacct_object_id
  "#{intacct_invoice_prefix}#{object.invoice.id}"
end

#invoice_xml(xml) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/intacctrb/invoice.rb', line 98

def invoice_xml xml
  xml.customerid "#{object.customer.intacct_system_id}"
  xml.datecreated {
    xml.year object.invoice.created_at.strftime("%Y")
    xml.month object.invoice.created_at.strftime("%m")
    xml.day object.invoice.created_at.strftime("%d")
  }

  termname = customer_data.termname
  xml.termname termname.present?? termname : "Net 30"

  xml.invoiceno intacct_object_id
  run_hook :custom_invoice_fields, xml
end

#set_date_time(type) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/intacctrb/invoice.rb', line 125

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

#set_intacct_system_idObject



113
114
115
# File 'lib/intacctrb/invoice.rb', line 113

def set_intacct_system_id
  object.invoice.intacct_system_id = intacct_object_id
end