Class: RVista::Invoice

Inherits:
Message show all
Defined in:
lib/rvista/invoice.rb

Overview

Represents a single Vista Invoice

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInvoice

creates a new RVista::Invoice object



14
15
16
17
# File 'lib/rvista/invoice.rb', line 14

def initialize
  @items = []
  @doc_date = nil
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



9
10
11
# File 'lib/rvista/invoice.rb', line 9

def currency
  @currency
end

#delivery_locationObject

Returns the value of attribute delivery_location.



9
10
11
# File 'lib/rvista/invoice.rb', line 9

def delivery_location
  @delivery_location
end

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/rvista/invoice.rb', line 8

def description
  @description
end

#doc_numberObject

Returns the value of attribute doc_number.



9
10
11
# File 'lib/rvista/invoice.rb', line 9

def doc_number
  @doc_number
end

#doc_typeObject

Returns the value of attribute doc_type.



8
9
10
# File 'lib/rvista/invoice.rb', line 8

def doc_type
  @doc_type
end

#itemsObject

Returns the value of attribute items.



10
11
12
# File 'lib/rvista/invoice.rb', line 10

def items
  @items
end

#receiver_idObject

Returns the value of attribute receiver_id.



8
9
10
# File 'lib/rvista/invoice.rb', line 8

def receiver_id
  @receiver_id
end

#sender_idObject

Returns the value of attribute sender_id.



8
9
10
# File 'lib/rvista/invoice.rb', line 8

def sender_id
  @sender_id
end

#total_gstObject

Returns the value of attribute total_gst.



11
12
13
# File 'lib/rvista/invoice.rb', line 11

def total_gst
  @total_gst
end

#total_qtyObject

Returns the value of attribute total_qty.



11
12
13
# File 'lib/rvista/invoice.rb', line 11

def total_qty
  @total_qty
end

#total_valueObject

Returns the value of attribute total_value.



11
12
13
# File 'lib/rvista/invoice.rb', line 11

def total_value
  @total_value
end

Class Method Details

.load_from_file(input) ⇒ Object

reads a vista invoice file into memory. input should be a string that specifies the file path

Raises:



21
22
23
24
25
# File 'lib/rvista/invoice.rb', line 21

def self.load_from_file(input)
  raise InvalidFileError, 'Invalid file' unless File.exist?(input)
  data = CSV.read(input, :quote_char => "`")
  return self.build_message(data)
end

.load_from_string(input) ⇒ Object

creates a RVista::Invoice object from a string. Input should be a complete vista file as a string



29
30
31
32
# File 'lib/rvista/invoice.rb', line 29

def self.load_from_string(input)
  data = CSV.parse(input, :quote_char => "`")
  return self.build_message(data)
end

Instance Method Details

#doc_dateObject



34
35
36
# File 'lib/rvista/invoice.rb', line 34

def doc_date
  vista_string_to_date(@doc_date)
end

#doc_date=(val) ⇒ Object



38
39
40
# File 'lib/rvista/invoice.rb', line 38

def doc_date=(val)
  @doc_date = process_date(val)
end

#to_sObject

print a string representation of this order that meets the spec



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
74
75
76
77
78
79
# File 'lib/rvista/invoice.rb', line 43

def to_s
  # message header
  msg = ""
  msg << "H,"
  msg << "#{sender_id.to_s[0,13]},"
  msg << "#{receiver_id.to_s[0,13]},"
  msg << "#{doc_type.to_s[0,4]},"
  msg << "#{description.to_s[0,30]},"
  msg << "#{doc_number.to_s[0,8]},"
  msg << "#{@doc_date},"
  msg << "#{delivery_location.to_s[0,10]},"
  msg << "#{currency.to_s[0,4]}\n"

  total_value = BigDecimal.new("0")
  total_qty   = BigDecimal.new("0")
  total_gst   = BigDecimal.new("0")

  # message line items
  @items.each do |item|
    msg << item.to_s << "\n"
    total_value += item.nett_value + item.gst
    total_qty   += item.qty
    total_gst   += item.gst
  end

  # message summary
  msg << "S,"
  msg << "#{@items.size.to_s},"
  msg << sprintf("%.0f", total_value)
  msg << ","
  msg << sprintf("%.0f", total_qty)
  msg << ","
  msg << sprintf("%.0f", total_gst)
  msg << "\n"

  return msg
end