Class: LEWT::SimpleMilestones

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

Overview

Extracts milestone payment data from a CSV file.

Row Indexes:

0

Id

1

Date

2

Description

3

Context

4

Amount

The key milstones_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

#initializeSimpleMilestones

Sets up this extension and regsters its run-time options.



24
25
26
27
# File 'lib/extensions/simple-milestones.rb', line 24

def initialize
  @category = "Milestone Income"
  super({:cmd => "milestones"})
end

Instance Method Details

#extract(options) ⇒ Object

Extracts data from the milestones CSV file.

options [Hash]

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



31
32
33
34
35
36
37
38
# File 'lib/extensions/simple-milestones.rb', line 31

def extract( options )
  matchData = get_matched_customers( options[:target] )
  @dStart =  options[:start].to_date
  @dEnd = options[:end].to_date
  @targets = self.get_matched_customers(options[:target])
  exFile = lewt_settings["milestones_filepath"] || File.expand_path(File.join(File.dirname(__FILE__), "../../../tests/milestones.csv"))
  return get_milestones ( exFile )
end

#get_milestones(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.



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-milestones.rb', line 42

def get_milestones ( filepath )
  # ROWS:
  # [0]Id [1]Date [2]Description [3]Context [4]Amount
  count = 0
  data = LEWT::LEWTBook.new

  CSV.foreach(filepath) do |row|
    if count > 0
      id = row[0]
      date = Time.parse( row[1] )
      desc = row[2]
      context = row[3]
      amount = row[4].to_f

      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 => amount
                                   })
        data.push(row_data)
      end
    end
    # increment our row index counter
    count += 1
  end
  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)


78
79
80
81
82
83
84
85
86
87
# File 'lib/extensions/simple-milestones.rb', line 78

def is_target_context?(context)
  match = false
  @targets.each do |t|
    reg = [ t['alias'], t['name'] ]
    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)


92
93
94
95
96
97
98
99
# File 'lib/extensions/simple-milestones.rb', line 92

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