bookyt_projects 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ class Activity < ActiveRecord::Base
2
2
  # Associations
3
3
  belongs_to :project
4
4
  belongs_to :person
5
+ belongs_to :work_day
5
6
  validates :project, :presence => true, :allow_blank => false
6
7
  validates :person, :presence => true, :allow_blank => false
7
8
 
@@ -13,6 +14,8 @@ class Activity < ActiveRecord::Base
13
14
 
14
15
  validates_date :date, :allow_nil => false, :allow_blank => false
15
16
 
17
+ before_save :create_work_day
18
+
16
19
  def duration=(value)
17
20
  if value.match(/:/)
18
21
  hours, minutes = value.split(':')
@@ -25,4 +28,13 @@ class Activity < ActiveRecord::Base
25
28
  def to_s
26
29
  "%s: %0.2fh" % [project.name, duration]
27
30
  end
31
+
32
+ private
33
+
34
+ def create_work_day
35
+ work_day = WorkDay.where(:person_id => person_id, :date => date).first
36
+ work_day ||= WorkDay.create(:person_id => person_id, :date => date)
37
+
38
+ self.work_day = work_day
39
+ end
28
40
  end
@@ -1,11 +1,27 @@
1
- class WorkDay
2
- attr_accessor :date, :employee
1
+ class WorkDay < ActiveRecord::Base
3
2
 
4
- def initialize(employee, date)
5
- @date = date
6
- @employee = employee
3
+ belongs_to :person
4
+ has_many :activities
5
+
6
+ before_create :current_daily_workload
7
+
8
+ def current_daily_workload
9
+ unless person && person.employments.current && person.employments.current.daily_workload
10
+ self.daily_workload = 0.0
11
+ else
12
+ self.daily_workload = person.employments.current(self.date).daily_workload
13
+ end
14
+ end
15
+
16
+ def self.create_for_current_employment(employee)
17
+ if employee.employments.current
18
+ range = employee.employments.current.duration_from..Date.today
19
+ range.each do |date|
20
+ WorkDay.create(:person => employee, :date => date)
21
+ end
22
+ end
7
23
  end
8
-
24
+
9
25
  # Get WorkDay instances for a range
10
26
  #
11
27
  # Returns an array of WorkDay instances. You probably want
@@ -15,7 +31,15 @@ class WorkDay
15
31
  # :employee: Employee to build WorkDay instances for
16
32
  # :range: Date range giving first and last day
17
33
  def self.for_range(employee, range)
18
- range.to_a.map{|day| WorkDay.new(employee, day)}
34
+ self.create_for_current_employment(employee)
35
+
36
+ range.inject([]) do |out, day|
37
+ work_day = WorkDay.where(:person_id => employee.id, :date => day).first
38
+ work_day ||= WorkDay.create(:person => employee, :date => day)
39
+ out << work_day
40
+
41
+ out
42
+ end
19
43
  end
20
44
 
21
45
  # Get WorkDay instances for a month
@@ -33,16 +57,6 @@ class WorkDay
33
57
  self.for_range(employee, start_date..end_date)
34
58
  end
35
59
 
36
- # Helper to access daily workload
37
- #
38
- # Returns 0.0 if no current daily_workload can be determined
39
- def daily_workload
40
- # Guard
41
- return 0.0 unless employee && employee.employments.current && employee.employments.current.daily_workload
42
-
43
- employee.employments.current.daily_workload
44
- end
45
-
46
60
  # Working hours for this day
47
61
  def hours_due
48
62
  case date.wday
@@ -60,7 +74,7 @@ class WorkDay
60
74
  # Calculates hours worked by summing up duration of all logged
61
75
  # activities.
62
76
  def hours_worked
63
- employee.activities.where(:date => date).to_a.sum(&:duration)
77
+ activities.where(:date => date).to_a.sum(&:duration)
64
78
  end
65
79
 
66
80
  # Overtime
@@ -69,4 +83,8 @@ class WorkDay
69
83
  def overtime
70
84
  hours_worked - hours_due
71
85
  end
86
+
87
+ def overall_overtime
88
+ WorkDay.where(:person_id => person.id).where('date <= ?', date).to_a.sum(&:overtime)
89
+ end
72
90
  end
@@ -8,13 +8,12 @@
8
8
  - overall_overtime = 0
9
9
  - WorkDay.for_month(@employee).each do |day|
10
10
  - overall_overtime += day.overtime
11
- - activities = @employee.activities.where(:date => day.date)
12
11
  - tr_params = {:class => work_day_classes(day)}
13
- - tr_params.merge!(:rel => 'popover', 'data-content' => h(render 'timesheets/activities_popover', :activities => activities), 'data-original-title' => 'Aktivitäten', 'data-html' => 'true') unless activities.empty?
12
+ - tr_params.merge!(:rel => 'popover', 'data-content' => h(render 'timesheets/activities_popover', :activities => day.activities), 'data-original-title' => 'Aktivitäten', 'data-html' => 'true') unless day.activities.empty?
14
13
  %tr{tr_params}
15
14
  %td= link_to l(day.date, :format => '%a, %d.%m.%Y'), person_activities_path(@employee, :by_date => day.date.to_s(:db)), 'data-href-container' => 'tr'
16
15
  %td.number
17
16
  %span.strong= "%0.2f" % day.hours_worked
18
17
  = "(%0.2f)" % day.hours_due
19
18
  %td.number.overtime.strong= "%0.2f" % day.overtime
20
- %td.number.overall-overtime.strong= "%0.2f" % overall_overtime
19
+ %td.number.overall-overtime.strong= "%0.2f" % day.overall_overtime
@@ -0,0 +1,11 @@
1
+ class CreateWorkDays < ActiveRecord::Migration
2
+ def change
3
+ create_table :work_days do |t|
4
+ t.integer :person_id
5
+ t.date :date
6
+ t.decimal :daily_workload, :precision => 10, :scale => 2
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ class AddActivitiesToWorkDay < ActiveRecord::Migration
2
+ def change
3
+ add_column :activities, :work_day_id, :integer
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module BookytPos
2
- VERSION = '0.13.0'
2
+ VERSION = '0.14.0'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bookyt_projects
3
3
  version: !ruby/object:Gem::Version
4
- hash: 43
4
+ hash: 39
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 13
8
+ - 14
9
9
  - 0
10
- version: 0.13.0
10
+ version: 0.14.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Roman Simecek (CyT)
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-01-17 00:00:00 Z
19
+ date: 2012-01-18 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: rails
@@ -142,6 +142,8 @@ files:
142
142
  - db/migrate/20120117070747_rename_comment_columns_to_remarks.rb
143
143
  - db/migrate/20120117075541_use_single_duration_field.rb
144
144
  - db/migrate/20120117080212_use_decimal_for_activity_duration.rb
145
+ - db/migrate/20120118131814_create_work_days.rb
146
+ - db/migrate/20120118140839_add_activities_to_work_day.rb
145
147
  - lib/bookyt_projects.rb
146
148
  - lib/bookyt_projects/navigation.rb
147
149
  - lib/bookyt_projects/railtie.rb