Class: XeroGateway::ManualJournal

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

Defined Under Namespace

Classes: Error, NoGatewayError

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/xero_gateway/manual_journal.rb', line 30

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



27
28
29
# File 'lib/xero_gateway/manual_journal.rb', line 27

def date
  @date
end

#errorsObject (readonly)

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



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

def errors
  @errors
end

#gatewayObject

Xero::Gateway associated with this invoice.



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

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).



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/xero_gateway/manual_journal.rb', line 103

def journal_lines
  if journal_lines_downloaded?
    @journal_lines

  elsif manual_journal_id =~ GUID_REGEX && @gateway
    # There is a manual_journal_id so we can assume this record was loaded from Xero.
    # Let's attempt to download the journal_line records (if there is a gateway)

    response = @gateway.get_manual_journal(manual_journal_id)
    raise ManualJournalNotFoundError, "Manual Journal with ID #{manual_journal_id} not found in Xero." unless response.success? && response.manual_journal.is_a?(XeroGateway::ManualJournal)

    @journal_lines = response.manual_journal.journal_lines
    @journal_lines_downloaded = true

    @journal_lines

  # Otherwise, this is a new manual journal, so return the journal_lines reference.
  else
    @journal_lines
  end
end

#journal_lines_downloadedObject

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



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

def journal_lines_downloaded
  @journal_lines_downloaded
end

#manual_journal_idObject

accessible fields



27
28
29
# File 'lib/xero_gateway/manual_journal.rb', line 27

def manual_journal_id
  @manual_journal_id
end

#narrationObject

accessible fields



27
28
29
# File 'lib/xero_gateway/manual_journal.rb', line 27

def narration
  @narration
end

#show_on_cash_basis_reportsObject

accessible fields



27
28
29
# File 'lib/xero_gateway/manual_journal.rb', line 27

def show_on_cash_basis_reports
  @show_on_cash_basis_reports
end

#statusObject

accessible fields



27
28
29
# File 'lib/xero_gateway/manual_journal.rb', line 27

def status
  @status
end

#urlObject

accessible fields



27
28
29
# File 'lib/xero_gateway/manual_journal.rb', line 27

def url
  @url
end

Class Method Details

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/xero_gateway/manual_journal.rb', line 140

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/xero_gateway/manual_journal.rb', line 44

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



155
156
157
158
159
160
161
162
163
164
# File 'lib/xero_gateway/manual_journal.rb', line 155

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)


98
99
100
# File 'lib/xero_gateway/manual_journal.rb', line 98

def journal_lines_downloaded?
  @journal_lines_downloaded
end

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/xero_gateway/manual_journal.rb', line 125

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)


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

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