Class: PaperlessToXero::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/paperless_to_xero/converter.rb

Constant Summary collapse

VAT_RATE_CHANGE_2008_12_01 =
Date.parse('2008-12-01')
VAT_RATE_CHANGE_2010_01_01 =
Date.parse('2010-01-01')
VAT_RATE_CHANGE_2011_01_04 =
Date.parse('2011-01-04')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_path, output_path) ⇒ Converter

Returns a new instance of Converter.



13
14
15
# File 'lib/paperless_to_xero/converter.rb', line 13

def initialize(input_path, output_path)
  @input_path, @output_path = input_path, output_path
end

Instance Attribute Details

#input_pathObject (readonly)

Returns the value of attribute input_path.



11
12
13
# File 'lib/paperless_to_xero/converter.rb', line 11

def input_path
  @input_path
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



11
12
13
# File 'lib/paperless_to_xero/converter.rb', line 11

def output_path
  @output_path
end

Instance Method Details

#convert!Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/paperless_to_xero/converter.rb', line 75

def convert!
  # grab the input
  parse
  # open the output CSV
  CSV.open(output_path, 'w') do |writer|
    # Xero header row
    writer << ['ContactName','InvoiceNumber','InvoiceDate','DueDate','SubTotal',
               'TotalTax','Total','Description','Quantity','UnitAmount','AccountCode','TaxType','TaxAmount',
               'TrackingName1','TrackingOption1','TrackingName2','TrackingOption2']
    
    # body rows
    invoices.each do |invoice|
      invoice.serialise_to_csv(writer)
    end
  end
end

#invoicesObject



17
18
19
# File 'lib/paperless_to_xero/converter.rb', line 17

def invoices
  @invoices ||= []
end

#parseObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/paperless_to_xero/converter.rb', line 25

def parse
  input_csv = CSV.read(input_path)
  # verify Paperless header row
  verify_header_row!(input_csv.shift)
  
  input_csv.each_with_index do |row, index|
    line_number = index + 1
    date, merchant, paperless_currency, amount, vat, category, payment_method, notes_field, description, reference, status, *extras = row
    negative = amount.index('--') == 0
    category = category[0..2] unless category.nil?
    unless negative # negative stuff ought to be a credit note. not sure if that works...
      # process amounts for commas added by Paperless
      amount = amount.tr(',', '') unless amount.nil?
      vat = vat.tr(',', '') unless vat.nil?
      notes = extract_notes(notes_field)
      total_vat = vat.nil? ? "0.00" : vat
      invoice = PaperlessToXero::Invoice.new(extract_date(date), merchant, reference, amount, total_vat, inc_vat?(notes), extract_currency(notes))
      if extras.empty?
        begin
          invoice.add_item(description, amount, vat, category, extract_vat_note(invoice.date, vat, notes))
        rescue
          raise BadItem.new(line_number, row, "Couldn't process this item")
        end
      else
        raise IncorrectNumberOfColumns.new(line_number, row, "Extra items are badly formatted") unless extras.size % 6 == 0
        items = chunk_extras(extras)
        items.each do |item|
          begin
            description, paperless_currency, amount, unknown, category, notes_field = item
            category = category[0..2]
            notes = extract_notes(notes_field)
            vat_amount = extract_vat_amount(notes)
            vat_note = extract_vat_note(invoice.date, vat_amount, notes)
            invoice.add_item(description, amount, vat_amount, category, vat_note)
          rescue
            raise BadItem.new(line_number, row, "Couldn't process this item")
          end
        end
      end
      invoices << invoice
      
      # currency fudging
      # actual_currency_match = notes.nil? ? nil : notes.match(/(\$|€|DKK|USD|EUR)/)
      # actual_currency = actual_currency_match.nil? ? nil : actual_currency_match[1]
      # 
      # description = description + " (#{actual_currency})" unless actual_currency.nil?
    end
  end
end

#verify_header_row!(row) ⇒ Object

Raises:



21
22
23
# File 'lib/paperless_to_xero/converter.rb', line 21

def verify_header_row!(row)
  raise UnknownHeaderRow unless row == PaperlessToXero::PAPERLESS_HEADER_ROW
end