Module: Twinfield::Request::Read

Extended by:
Read
Included in:
Read
Defined in:
lib/twinfield/request/read.rb

Instance Method Summary collapse

Instance Method Details

#browse(options) ⇒ Object

Twinfield::Request::Read.browse( options: { code: “000” })



54
55
56
57
58
59
60
61
62
# File 'lib/twinfield/request/read.rb', line 54

def browse(options)
  options = options.merge(office: Twinfield.configuration.company)

  xml_wrap(read(:browse, options))

  {
    status: 0
  }
end

#debtor(options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/twinfield/request/read.rb', line 10

def debtor(options)
  xml = xml_wrap(read(:dimensions, options.merge(dimtype: "DEB")))

  if xml.at_css("dimensions").attributes["result"]&.value == "1"
    []
  elsif xml.at_css("dimension").attributes["result"].value == "1"
    {
      status: 1,
      country: xml.at_css("country").content,
      city: xml.at_css("city").content,
      postcode: xml.at_css("postcode").content,
      address: xml.at_css("field2").content,
      duedays: xml.at_css("duedays").content
    }
  else
    {
      status: 0
    }
  end
end

#office(options) ⇒ Object



6
7
8
# File 'lib/twinfield/request/read.rb', line 6

def office(options)
  xml_wrap(read(:office, options))
end

#sales_invoice(invoicenumber, invoicetype: "FACTUUR") ⇒ Object

Twinfield::Request::Read.sales_invoice({})



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/twinfield/request/read.rb', line 32

def sales_invoice(invoicenumber, invoicetype: "FACTUUR")
  options = {office: Twinfield.configuration.company, code: "FACTUUR", invoicenumber: invoicenumber}

  xml = xml_wrap(read(:salesinvoice, options))

  if xml.at_css("dimension").attributes["result"].value == "1"
    {
      status: 1,
      country: xml.at_css("country").content,
      city: xml.at_css("city").content,
      postcode: xml.at_css("postcode").content,
      address: xml.at_css("field2").content,
      duedays: xml.at_css("duedays").content
    }
  else
    {
      status: 0
    }
  end
end

#xml_to_json(xml) ⇒ Object



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
# File 'lib/twinfield/request/read.rb', line 64

def xml_to_json(xml)
  rv = {}
  nokogiri = xml.is_a?(String) ? Nokogiri::XML(xml) : xml

  nokogiri.children.each do |node|
    twig = [xml_to_json(node)]
    twig_keys = twig.map { |a| a.keys }.flatten
    uniq_twig_keys = twig_keys.uniq
    if uniq_twig_keys.count == 1 && "#{uniq_twig_keys.first}s" == node.name
      twig = twig.map { |a| a.values }.flatten
    elsif node.is_a?(Nokogiri::XML::Element) && node.children.count == 1 && node.children.first.is_a?(Nokogiri::XML::Text)
      twig = node.text
      if /\A\d*$/.match?(twig)
        twig = twig.to_i
      end
    elsif node.text.strip == ""
      twig = nil
    end

    # do not unnecesarily wrap a hash with uniq keys in an array
    if twig.is_a?(Array) && !node.name.end_with?("s") && twig.count == 1
      twig = twig[0]
    end

    rv[node.name.to_sym] = twig if twig
  end
  rv
end