Class: LEWT::SimpleReports

Inherits:
Extension show all
Defined in:
lib/extensions/simple-reports.rb

Overview

The Reports LEWT Extension processes ledger data into a brief report.

Instance Attribute Summary

Attributes inherited from Extension

#command_name, #customers, #enterprise, #lewt_settings, #lewt_stash, #options

Instance Method Summary collapse

Methods inherited from Extension

#get_matched_customers, #lewt_extensions

Constructor Details

#initializeSimpleReports

Registers this extension.



12
13
14
# File 'lib/extensions/simple-reports.rb', line 12

def initialize
  super({:cmd => "report"})
end

Instance Method Details

#calculate_income_tax(income, tax) ⇒ Object



76
77
78
# File 'lib/extensions/simple-reports.rb', line 76

def calculate_income_tax (income, tax)
  
end

#make_report(targets, data) ⇒ Object

This method handles the bulk of the calculation required in compiling the report.

targets [Hash]

The target client(s) to operate on

data [LEWTBook]

The data in LEWTBook format



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
74
# File 'lib/extensions/simple-reports.rb', line 27

def make_report ( targets, data )
  report = {
    "date_created" => DateTime.now.strftime("%d/%m/%y"),
    "included_customers" => targets,
    "revenue" => 0,
    "expenses" => 0,
    "income" => 0,
    "taxes" => Array.new,
    "hours" => 0
  }
  data.each do |row|        
    if row[:category].downcase.match /income/
      report["revenue"] += row[:total]
      # check if category Hourly Income. If so add quantity to our 'hours' counter.
      if row[:category].downcase.match /hourly/
        report["hours"] += row[:quantity]
      end
    elsif row[:category].downcase.match /expense/
      report["expenses"] += row[:total]
    end 
  end
  
  # remember expenses is a negative amount to begin with so don't subtract it!
  report["income"] = report["revenue"] + report["expenses"]
  tax_levees = enterprise["tax_levees"]
  tax_total = 0

  if tax_levees != nil
    tax_levees.each do |tax|
      if tax["applies_to"] == "income"
        # do income tax
        if report["income"] > tax["lower_threshold"]
          taxable = [ report["income"], tax["upper_threshold"]].min - tax["lower_threshold"]
          damage = (taxable * tax["rate"] + ( tax["flatrate"] || 0 )).round(2)
          tax_total += damage
          report["taxes"].push({ "amount" => damage, "name" => tax["name"], "rate" => tax["rate"] })
        end
      elsif tax["applies_to"] == "revenue"
        # do GST's
        damage = report["revenue"] * tax["rate"] + ( tax["flatrate"] || 0 )
        tax_total += damage
        report["taxes"].push({ "amount" => damage, "name" => tax["name"], "rate" => tax["rate"] })
      end
    end
  end
  report["bottom_line"] = (report["income"] - tax_total).round(2)
  return report
end

#process(options, data) ⇒ Object

Called on Lewt process cycle and uses the ledger data to compose a report.

options [Hash]

The options hash passed to this function by the Lewt program.

data [LEWTBook]

The data in LEWTBook format



19
20
21
22
# File 'lib/extensions/simple-reports.rb', line 19

def process ( options, data )
  targets = get_matched_customers( options[:target] )
  return make_report(targets, data)
end