Class: Twinfield::Create::Transaction

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

Overview

Create a transaction

In Twinfield there is a clear distinction between sales invoices and sales transactions. Sales invoices are the invoices, which will be sent to customers. Sales transactions are the related financial transactions that will be posted in the accounting part of Twinfield.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Transaction

Returns a new instance of Transaction.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/twinfield/create/transaction.rb', line 23

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

#autobalancevatObject



19
20
21
# File 'lib/twinfield/create/transaction.rb', line 19

def autobalancevat
  @autobalancevat || true
end

#codeObject

Returns the value of attribute code.



9
10
11
# File 'lib/twinfield/create/transaction.rb', line 9

def code
  @code
end

#currencyObject

Returns the value of attribute currency.



9
10
11
# File 'lib/twinfield/create/transaction.rb', line 9

def currency
  @currency
end

#dateObject

Returns the value of attribute date.



9
10
11
# File 'lib/twinfield/create/transaction.rb', line 9

def date
  @date
end

#destinyObject



11
12
13
# File 'lib/twinfield/create/transaction.rb', line 11

def destiny
  @destiny || "temporary"
end

#duedateObject

Returns the value of attribute duedate.



9
10
11
# File 'lib/twinfield/create/transaction.rb', line 9

def duedate
  @duedate
end

#invoicenumberObject

Returns the value of attribute invoicenumber.



9
10
11
# File 'lib/twinfield/create/transaction.rb', line 9

def invoicenumber
  @invoicenumber
end

#linesObject

Returns the value of attribute lines.



9
10
11
# File 'lib/twinfield/create/transaction.rb', line 9

def lines
  @lines
end

#numberObject

Returns the value of attribute number.



9
10
11
# File 'lib/twinfield/create/transaction.rb', line 9

def number
  @number
end

#raisewarningObject



15
16
17
# File 'lib/twinfield/create/transaction.rb', line 15

def raisewarning
  @raisewarning || false
end

Instance Method Details

#generate_linesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/twinfield/create/transaction.rb', line 40

def generate_lines
  xml_lines = lines.map do |line|
    %(
      <line type="#{line[:type]}" id="#{line[:id]}" >
        <dim1>#{line[:dim1]}</dim1>
        <dim2>#{line[:dim2]}</dim2>
        <dim3>#{line[:dim3]}</dim3>
        <value>#{line[:value]}</value>
        #{"<vatvalue>#{line[:vatvalue]}</vatvalue>" if line[:vatvalue]}
        <debitcredit>#{line[:debitcredit]}</debitcredit>
        <description>#{CGI.escapeHTML(line[:description]) if line[:description]}</description>
        #{"<vatcode>#{line[:vatcode]}</vatcode>" if line[:vatcode]}
      </line>
    )
  end

  xml_lines.join("")
end

#saveObject



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
# File 'lib/twinfield/create/transaction.rb', line 59

def save
  response = Twinfield::Api::Process.request do
    %(
      <transaction destiny="#{destiny}" raisewarning="#{raisewarning}" autobalancevat="#{autobalancevat}">
        <header>
          <code>#{code}</code>
          #{"<number>#{number}</number>" if number}
          <currency>#{currency}</currency>
          <date>#{date.strftime("%Y%m%d")}</date>
          <duedate>#{duedate.strftime("%Y%m%d")}</duedate>
          <invoicenumber>#{invoicenumber}</invoicenumber>
          <office>#{Twinfield.configuration.company}</office>
        </header>
        <lines>
          #{generate_lines}
        </lines>
      </transaction>
    )
  end

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

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