Class: XeroGateway::ManualJournal

Inherits:
Object
  • Object
show all
Includes:
Dates
Defined in:
lib/xero_gateway/manual_journal.rb

Constant Summary collapse

GUID_REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
STATUSES =
{
  'DRAFT' 	=> 'Draft Manual Journal',
  'POSTED' 	=> 'Posted Manual Journal',
  'DELETED' => 'Deleted Draft Manual Journal',
  'VOIDED'	=> 'Voided Posted Manual Journal'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dates

included

Constructor Details

#initialize(params = {}) ⇒ ManualJournal

Returns a new instance of ManualJournal.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xero_gateway/manual_journal.rb', line 26

def initialize(params = {})
  @errors ||= []
  @payments ||= []

  # Check if the line items have been downloaded.
  @journal_lines_downloaded = (params.delete(:journal_lines_downloaded) == true)

  params.each do |k,v|
    self.send("#{k}=", v)
  end

  @journal_lines ||= []
end

Instance Attribute Details

#dateObject

accessible fields



24
25
26
# File 'lib/xero_gateway/manual_journal.rb', line 24

def date
  @date
end

#errorsObject (readonly)

Any errors that occurred when the #valid? method called.



18
19
20
# File 'lib/xero_gateway/manual_journal.rb', line 18

def errors
  @errors
end

#gatewayObject

Xero::Gateway associated with this invoice.



15
16
17
# File 'lib/xero_gateway/manual_journal.rb', line 15

def gateway
  @gateway
end

#journal_linesObject

If line items are not downloaded, then attempt a download now (if this record was found to begin with).



24
25
26
# File 'lib/xero_gateway/manual_journal.rb', line 24

def journal_lines
  @journal_lines
end

#journal_lines_downloadedObject

Represents whether the journal lines have been downloaded when getting from GET /API.XRO/2.0/ManualJournals



21
22
23
# File 'lib/xero_gateway/manual_journal.rb', line 21

def journal_lines_downloaded
  @journal_lines_downloaded
end

#manual_journal_idObject

accessible fields



24
25
26
# File 'lib/xero_gateway/manual_journal.rb', line 24

def manual_journal_id
  @manual_journal_id
end

#narrationObject

accessible fields



24
25
26
# File 'lib/xero_gateway/manual_journal.rb', line 24

def narration
  @narration
end

#show_on_cash_basis_reportsObject

accessible fields



24
25
26
# File 'lib/xero_gateway/manual_journal.rb', line 24

def show_on_cash_basis_reports
  @show_on_cash_basis_reports
end

#statusObject

accessible fields



24
25
26
# File 'lib/xero_gateway/manual_journal.rb', line 24

def status
  @status
end

#urlObject

accessible fields



24
25
26
# File 'lib/xero_gateway/manual_journal.rb', line 24

def url
  @url
end

Class Method Details

.from_xml(manual_journal_element, gateway = nil, options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/xero_gateway/manual_journal.rb', line 136

def self.from_xml(manual_journal_element, gateway = nil, options = {})
  manual_journal = ManualJournal.new(options.merge({:gateway => gateway}))
  manual_journal_element.children.each do |element|
    case(element.name)
      when "ManualJournalID" then manual_journal.manual_journal_id = element.text
      when "Date" then manual_journal.date = parse_date(element.text)
      when "Status" then manual_journal.status = element.text
      when "Narration" then manual_journal.narration = element.text
      when "JournalLines" then element.children.each {|journal_line| manual_journal.journal_lines_downloaded = true; manual_journal.journal_lines << JournalLine.from_xml(journal_line) }
      when "Url" then manual_journal.url = element.text
    end
  end
  manual_journal
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/xero_gateway/manual_journal.rb', line 40

def ==(other)
  ['narration', 'status', 'journal_lines', 'show_on_cash_basis_reports'].each do |field|
    return false if send(field) != other.send(field)
  end

  ["date"].each do |field|
    return false if send(field).to_s != other.send(field).to_s
  end
  return true
end

#add_journal_line(params = {}) ⇒ Object

from_xml



151
152
153
154
155
156
157
158
159
160
# File 'lib/xero_gateway/manual_journal.rb', line 151

def add_journal_line(params = {})
  journal_line = nil
  case params
    when Hash 			 then  journal_line = JournalLine.new(params)
    when JournalLine then  journal_line = params
    else             raise InvalidLineItemError
  end
  @journal_lines << journal_line
  journal_line
end

#journal_lines_downloaded?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/xero_gateway/manual_journal.rb', line 94

def journal_lines_downloaded?
  @journal_lines_downloaded
end

#to_xml(b = Builder::XmlMarkup.new) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/xero_gateway/manual_journal.rb', line 121

def to_xml(b = Builder::XmlMarkup.new)
  b.ManualJournal {
    b.ManualJournalID manual_journal_id if manual_journal_id
    b.Narration narration        
    b.JournalLines {
      self.journal_lines.each do |journal_line|
        journal_line.to_xml(b)
      end
    }
    b.Date ManualJournal.format_date(date || Date.today)
    b.Status status if status
    b.Url url if url
  }
end

#valid?Boolean

Validate the ManualJournal record according to what will be valid by the gateway.

Usage:

manual_journal.valid?     # Returns true/false

Additionally sets manual_journal.errors array to an array of field/error.

Returns:

  • (Boolean)


57
58
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
# File 'lib/xero_gateway/manual_journal.rb', line 57

def valid?
  @errors = []

  if !manual_journal_id.nil? && manual_journal_id !~ GUID_REGEX
    @errors << ['manual_journal_id', 'must be blank or a valid Xero GUID']
  end

  if narration.blank?
  	@errors << ['narration', "can't be blank"]
  end

  unless date
    @errors << ['date', "can't be blank"]
  end

  # Make sure all journal_items are valid.
  unless journal_lines.all? { | journal_line | journal_line.valid? }
    @errors << ['journal_lines', "at least one journal line invalid"]
  end

  # make sure there are at least 2 journal lines
  unless journal_lines.length > 1
  	@errors << ['journal_lines', "journal must contain at least two individual journal lines"]
  end

  if journal_lines.length > 100
  	@errors << ['journal_lines', "journal must contain less than one hundred journal lines"]
  end

  unless journal_lines.sum(&:line_amount).to_f == 0.0
  	@errors << ['journal_lines', "the total debits must be equal to total credits"]
  end

  @errors.size == 0
end