Class: NFE::RPS

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRPS

Returns a new instance of RPS.



5
6
7
8
9
# File 'lib/rps.rb', line 5

def initialize
    @header  = Header.new
    @footer  = Footer.new
    @details = Array.new
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



3
4
5
# File 'lib/rps.rb', line 3

def details
  @details
end

Returns the value of attribute footer.



3
4
5
# File 'lib/rps.rb', line 3

def footer
  @footer
end

#headerObject (readonly)

Returns the value of attribute header.



3
4
5
# File 'lib/rps.rb', line 3

def header
  @header
end

Instance Method Details

#add_detail(fields) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rps.rb', line 17

def add_detail fields
    detail = Detail.new
    detail << fields
    if detail.valid?
        @details << detail
    else
        raise Errors::InvalidDetailError, /Invalid Detail: #{detail.to_hash}/
    end
end

#add_header(fields) ⇒ Object



11
12
13
14
15
# File 'lib/rps.rb', line 11

def add_header fields
    @header << fields
    raise Errors::InvalidHeaderError, /Invalid Header: #{@header.to_hash}/      if !@header.valid?
    return @header.fields
end

#save_to_file(path = "./") ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/rps.rb', line 56

def save_to_file path = "./"
    filename = "#{path}/RPS_#{@details.first.to_hash[:issuing_date]}.txt"
    file = File.new(filename, "w")
    saved = file.write(self.to_s)
    file.close

    return filename if saved
end


27
28
29
30
31
32
33
34
35
36
# File 'lib/rps.rb', line 27

def set_footer
    total_amount     = 0
    total_tax_amount = 0
    @details.each do |detail|
        total_amount     += detail.to_hash[:amount].to_i
        total_tax_amount += detail.to_hash[:tax_amount].to_i
    end

    @footer << {total_detail_lines: @details.count.to_s, total_amount: total_amount.to_s, total_tax_amount: total_tax_amount.to_s}
end

#to_sObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rps.rb', line 42

def to_s
    if self.valid?
        string = @header.to_s
        @details.each do |detail|
            string += detail.to_s
        end
        string += @footer.to_s

        return string
    else
        raise Errors::InvalidRPSError
    end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
    return (@header.valid? and @details.count and @footer.valid?)
end