Class: TextInvoice::Totals

Inherits:
Object
  • Object
show all
Defined in:
lib/text-invoice/totals.rb

Instance Method Summary collapse

Instance Method Details

#process(invoice) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/text-invoice/totals.rb', line 3

def process(invoice)
    # parse
    parsed = YAML.load(invoice)
            
    # total accumulator
    total = 0
       
    # process line items
    if not parsed["items"] == nil
        for item in parsed["items"]
            quantity = item["quantity"]
            unit = item["unit"]
            subtotal = quantity * unit
            item["subtotal"] = subtotal
            total += subtotal
        end
    end

    # update totals
    parsed["total"] = total

    if parsed["paid"]
        parsed["due"] = total - parsed["paid"]
    else
        parsed["due"] = total
    end

    parsed.to_yaml
end