Class: Twinfield::Create::Debtor

Inherits:
Object
  • Object
show all
Defined in:
lib/twinfield/create/debtor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Debtor

Returns a new instance of Debtor.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/twinfield/create/debtor.rb', line 9

def initialize(hash = {})
  # Escape all the things.
  hash.each do |k, v|
    val = if v.is_a?(String)
      CGI.escapeHTML(v)
    elsif v.is_a?(Hash)
      v.each_with_object({}) { |(k1, v1), h|
        h[k1] = CGI.escapeHTML(v1)
      }
    else
      v
    end

    send(:"#{k}=", val)
  end
end

Instance Attribute Details

#bank_biccodeObject

Returns the value of attribute bank_biccode.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def bank_biccode
  @bank_biccode
end

#bank_countryObject

Returns the value of attribute bank_country.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def bank_country
  @bank_country
end

#bank_descriptionObject

Returns the value of attribute bank_description.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def bank_description
  @bank_description
end

#bank_ibanObject

Returns the value of attribute bank_iban.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def bank_iban
  @bank_iban
end

#codeObject

Returns the value of attribute code.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def code
  @code
end

#countryObject

Returns the value of attribute country.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def country
  @country
end

#financials_duedaysObject

Returns the value of attribute financials_duedays.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def financials_duedays
  @financials_duedays
end

#ibanObject

Returns the value of attribute iban.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def iban
  @iban
end

#invoice_addressObject

Returns the value of attribute invoice_address.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def invoice_address
  @invoice_address
end

#invoice_cityObject

Returns the value of attribute invoice_city.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def invoice_city
  @invoice_city
end

#invoice_contact_nameObject

Returns the value of attribute invoice_contact_name.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def invoice_contact_name
  @invoice_contact_name
end

#invoice_countryObject

Returns the value of attribute invoice_country.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def invoice_country
  @invoice_country
end

#invoice_nameObject

Returns the value of attribute invoice_name.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def invoice_name
  @invoice_name
end

#invoice_zipcodeObject

Returns the value of attribute invoice_zipcode.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def invoice_zipcode
  @invoice_zipcode
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def name
  @name
end

#shortnameObject

Returns the value of attribute shortname.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def shortname
  @shortname
end

#vatcodeObject

Returns the value of attribute vatcode.



4
5
6
# File 'lib/twinfield/create/debtor.rb', line 4

def vatcode
  @vatcode
end

Instance Method Details

#saveObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/twinfield/create/debtor.rb', line 26

def save
  response = Twinfield::Api::Process.request do
    %(
      <dimension>
        <office>#{Twinfield.configuration.company}</office>
        <type>DEB</type>
        <code>#{code}</code>
        <name>#{name}</name>
        <shortname>#{shortname}</shortname>
        <financials>
          <duedays>#{financials_duedays}</duedays>
        </financials>
        <addresses>
          <address type="invoice">
            <name>#{invoice_name}</name>
            <country>#{invoice_country}</country>
            <city>#{invoice_city}</city>
            <postcode>#{invoice_zipcode}</postcode>
            <field1>#{invoice_contact_name}</field1>
            <field2>#{invoice_address}</field2>
            <field4>#{vatcode}</field4>
          </address>
        </addresses>
        #{bank_xml}
      </dimension>
    )
  end

  xml = Nokogiri::XML(response.body[:process_xml_string_response][:process_xml_string_result])

  if xml.at_css("dimension").attributes["result"].value == "1"
    {
      code: code,
      status: 1
    }
  else
    {
      code: code,
      status: 0,
      messages: xml.css("[msg]").map { |x| x.attributes["msg"].value }
    }
  end
end