Class: Twinfield::Transaction

Inherits:
AbstractModel show all
Extended by:
Helpers::Parsers
Includes:
Helpers::TransactionMatch
Defined in:
lib/twinfield/transaction.rb

Defined Under Namespace

Classes: Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Parsers

parse_date, parse_datetime, parse_float

Methods included from Helpers::TransactionMatch

#match!, #to_match_xml, #to_transaction_match_line_xml

Constructor Details

#initialize(code:, office: nil, currency: "EUR", date: Date.today, period: nil, destiny: :final, lines: [], number: nil) ⇒ Transaction

Returns a new instance of Transaction.



58
59
60
61
62
63
64
65
66
67
# File 'lib/twinfield/transaction.rb', line 58

def initialize(code:, office: nil, currency: "EUR", date: Date.today, period: nil, destiny: :final, lines: [], number: nil)
  self.office = office || Twinfield.configuration.company
  self.code = code
  self.currency = currency
  self.date = date
  self.period = period || "#{date.year}/#{"%02d" % date.month}"
  self.lines = lines.collect { |a| a.is_a?(Line) ? a : Line.new(**a) }
  self.destiny = destiny
  self.number = number
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



56
57
58
# File 'lib/twinfield/transaction.rb', line 56

def code
  @code
end

#currencyObject

Returns the value of attribute currency.



56
57
58
# File 'lib/twinfield/transaction.rb', line 56

def currency
  @currency
end

#dateObject

Returns the value of attribute date.



56
57
58
# File 'lib/twinfield/transaction.rb', line 56

def date
  @date
end

#destinyObject

Returns the value of attribute destiny.



56
57
58
# File 'lib/twinfield/transaction.rb', line 56

def destiny
  @destiny
end

#linesObject

Returns the value of attribute lines.



56
57
58
# File 'lib/twinfield/transaction.rb', line 56

def lines
  @lines
end

#numberObject

Returns the value of attribute number.



56
57
58
# File 'lib/twinfield/transaction.rb', line 56

def number
  @number
end

#officeObject

Returns the value of attribute office.



56
57
58
# File 'lib/twinfield/transaction.rb', line 56

def office
  @office
end

#periodObject

Returns the value of attribute period.



56
57
58
# File 'lib/twinfield/transaction.rb', line 56

def period
  @period
end

Instance Method Details

#saveObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/twinfield/transaction.rb', line 73

def save
  response = Twinfield::Api::Process.request do
    to_xml
  end

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

  if xml.at_css("transaction").attributes["result"].value == "1"
    self.number = xml.at_css("header number").content
    self
  else
    raise Twinfield::Create::Error.new(xml.css("[msg]").map { |x| x.attributes["msg"].value }.join(" "), object: self)
  end
end

#to_xmlObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/twinfield/transaction.rb', line 88

def to_xml
  Nokogiri::XML::Builder.new do |xml|
    xml.transaction(destiny: "final") do
      xml.header do
        xml.office office
        xml.code code
        xml.number number if number
        xml.currency currency
        xml.date date.strftime("%Y%m%d")
        xml.period period
      end
      xml.lines do
        lines.select(&:total?).each do |line|
          xml << line.to_xml
        end

        lines.select(&:detail?).each do |line|
          xml << line.to_xml
        end
      end
    end
  end.doc.root.to_xml
end

#valueObject



69
70
71
# File 'lib/twinfield/transaction.rb', line 69

def value
  0.0 - lines.select { |l| l.credit? && l.detail? }.map(&:value).sum
end