Class: LEWT::SimpleExpenses

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

Overview

The Simple Expenses LEWT Extensions allows you to manage you expenses in a CSV file. The CSV file itself must conform to the following specification:

Row Indexes:

0

Date

1

Description

2

Context

3

Cost

The key expenses_filepath can be added to your settings file to change the location where this extension looks for the CSV.

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

#initializeSimpleExpenses

Registers this extension



24
25
26
27
28
29
30
31
32
33
# File 'lib/extensions/simple-expenses.rb', line 24

def initialize
  options = {
    :include_own => {
      :definition => "toggles including own business expenses from csv file",
      :default => false,
      :short_flag => "-i"
    }
  }
  super({:cmd => "expenses", :options => options })
end

Instance Method Details

#extract(options) ⇒ Object

Extracts data from the expenses CSV file.

options [Hash]

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



37
38
39
40
41
42
43
44
45
# File 'lib/extensions/simple-expenses.rb', line 37

def extract( options )
  @targets = get_matched_customers( options[:target] )
  @dStart =  options[:start]
  @dEnd = options[:end]
  @category = 'Expenses'
  @include_own_expenses = options[:include_own]
  exFile = lewt_settings["expenses_filepath"] || File.expand_path(File.join(File.dirname(__FILE__), "../../../tests/expenses.csv"))
  return get_expenses ( exFile )
end

#get_expenses(filepath) ⇒ Object

Read file at filepath and parses it expecting the format presented in this classes header.

filepath [String]

The CSV filepath as a string.



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
80
81
# File 'lib/extensions/simple-expenses.rb', line 50

def get_expenses ( filepath )
  # ROWS:
  # [0]Date [1]Description [2]Context [3]Cost
  count = 0
  data = LEWT::LEWTBook.new
  CSV.foreach(filepath) do |row|
    if count > 0
      date = Time.parse(row[0])
      desc = row[1]
      context = row[2]
      cost = row[3].to_f * -1
      if self.is_target_date?( date ) == true && self.is_target_context?(context) == true
        # create ledger entry and append to books
        row_data = LEWT::LEWTLedger.new({
                                     :date_start => date, 
                                     :date_end => date, 
                                     :category => @category, 
                                     :entity => context,
                                     :description => desc,
                                     :quantity => 1, 
                                     :unit_cost => cost
                                   })
        data.push(row_data)       
      end
    end
    # increment our row index counter
    count += 1
  end

  # return our data as per specification!
  return data
end

#is_target_context?(context) ⇒ Boolean

Checks if the context field in the CSV matches any of our target clients names or alias’

context [String]

The context field as a string.

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/extensions/simple-expenses.rb', line 97

def is_target_context?(context)
  match = false
  @targets.each do |t|
    reg = [ t['alias'], t['name'] ]
    if @include_own_expenses == true
      reg.concat [ @enterprise["alias"], @enterprise["name"] ]
    end
    regex = Regexp.new( reg.join("|"), Regexp::IGNORECASE )
    match = regex.match(context) != nil ? true : false;
    break if match != false
  end
  return match
end

#is_target_date?(date) ⇒ Boolean

Checks whether event date is within target range

date [DateTime]

The date to check

returns: Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
# File 'lib/extensions/simple-expenses.rb', line 86

def is_target_date? ( date ) 
  d = date.to_date
  check = false
  if d >= @dStart.to_date && d <= @dEnd.to_date
    check = true
  end
  return check
end