Module: TimeSheet::Time

Defined in:
lib/time_sheet/time.rb

Defined Under Namespace

Modules: Util Classes: Cmd, Entry, Exception, Parser

Class Method Summary collapse

Class Method Details

.averages(results, options) ⇒ Object



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
# File 'lib/time_sheet/time.rb', line 40

def self.averages(results, options)
  days = 1

  unless results['entries'].empty?
    time = (
      (options[:to] || Util.day_end) -
      (options[:from] || results['entries'].first[1].to_time)
    ).to_i
    days = (time.to_f / 60 / 60 / 24).round
  end

  weeks = days / 7.0
  months = days / 30.0
  workdays = weeks * 5.0
  worked = TimeSheet::Time::Util.hours(results['total'])

  results['averages'] = {
    'days' => days,
    'weeks' => weeks,
    'months' => months,
    'workdays' => workdays,
    'worked' => worked,
    'hours_per_day' => worked / days,
    'hours_per_workday' => worked / workdays,
    'hours_per_week' => worked / weeks,
    'hours_per_month' => worked / months,
  }
end

.invoice(options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/time_sheet/time.rb', line 69

def self.invoice(options)
  TimeSheet.options = options
  
  grouped = {}
  Parser.new(options[:location]).entries.each do |e|
    if e.matches?(options)
      grouped[[e.date, e.description]] ||= 0
      grouped[[e.date, e.description]] += e.duration.to_i
    end
  end
  rows = []
  grouped.each{|k, d| rows << [k.first, d, k.last]}
  packages = [[]]
  ptotal = 0

  while row = rows.shift
    if options[:package] > 0
      new_package_size = ptotal + row[1]

      if new_package_size > options[:package] * 60
        filler = row.dup
        filler[1] = options[:package] * 60 - ptotal
        packages.last << filler
        packages << []
        ptotal = 0

        # we re-queue the remainder of the current row so that it is picked
        # up by the next iteration
        row[1] -= filler[1]
        rows.unshift row
        next
      end

      if new_package_size == options[:package] * 60
        packages.last << row
        packages << []
        ptotal = 0
        next
      end
    end

    packages.last << row
    ptotal += row[1]
  end

  if packages.last.empty?
    packages.pop
  end

  if options[:petty]
    packages.map! do |package|
      petty = 0
      package.select! do |row|
        if row[1] < options[:petty]
          petty += row[1]
          next false
        end
        true
      end
      if petty > 0
        package << [nil, petty, 'misc']
      end
      package
    end
  end

  packages
end

.report(options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/time_sheet/time.rb', line 10

def self.report(options)
  TimeSheet.options = options

  results = {
    'entries' => [],
    'total' => 0.0,
    'projects' => {}
  }

  x = nil
  Parser.new(options[:location]).entries.each do |e|
    unless x
      x = true
    end
    if e.matches?(options)
      results['total'] += e.duration
      results['projects'][e.project] ||= {'total' => 0.0, 'activities' => {}}
      results['projects'][e.project]['total'] += e.duration
      results['projects'][e.project]['activities'][e.activity] ||= 0.0
      results['projects'][e.project]['activities'][e.activity] += e.duration

      results['entries'] << e.to_row
    end
  end

  averages(results, options)

  results
end