comparison app/models/time_entry.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents 433d4f72a19b
children 622f24f53b42
comparison
equal deleted inserted replaced
1209:1b1138f6f55e 1338:25603efa57b5
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
40 validates_numericality_of :hours, :allow_nil => true, :message => :invalid 40 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
41 validates_length_of :comments, :maximum => 255, :allow_nil => true 41 validates_length_of :comments, :maximum => 255, :allow_nil => true
42 before_validation :set_project_if_nil 42 before_validation :set_project_if_nil
43 validate :validate_time_entry 43 validate :validate_time_entry
44 44
45 named_scope :visible, lambda {|*args| { 45 scope :visible, lambda {|*args| {
46 :include => :project, 46 :include => :project,
47 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args) 47 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
48 }} 48 }}
49 scope :on_issue, lambda {|issue| {
50 :include => :issue,
51 :conditions => "#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}"
52 }}
53 scope :on_project, lambda {|project, include_subprojects| {
54 :include => :project,
55 :conditions => project.project_condition(include_subprojects)
56 }}
57 scope :spent_between, lambda {|from, to|
58 if from && to
59 {:conditions => ["#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to]}
60 elsif from
61 {:conditions => ["#{TimeEntry.table_name}.spent_on >= ?", from]}
62 elsif to
63 {:conditions => ["#{TimeEntry.table_name}.spent_on <= ?", to]}
64 else
65 {}
66 end
67 }
49 68
50 safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values' 69 safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
51 70
52 def after_initialize 71 def initialize(attributes=nil, *args)
72 super
53 if new_record? && self.activity.nil? 73 if new_record? && self.activity.nil?
54 if default_activity = TimeEntryActivity.default 74 if default_activity = TimeEntryActivity.default
55 self.activity_id = default_activity.id 75 self.activity_id = default_activity.id
56 end 76 end
57 self.hours = nil if hours == 0 77 self.hours = nil if hours == 0
70 90
71 def hours=(h) 91 def hours=(h)
72 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) 92 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
73 end 93 end
74 94
95 def hours
96 h = read_attribute(:hours)
97 if h.is_a?(Float)
98 h.round(2)
99 else
100 h
101 end
102 end
103
75 # tyear, tmonth, tweek assigned where setting spent_on attributes 104 # tyear, tmonth, tweek assigned where setting spent_on attributes
76 # these attributes make time aggregations easier 105 # these attributes make time aggregations easier
77 def spent_on=(date) 106 def spent_on=(date)
78 super 107 super
79 if spent_on.is_a?(Time) 108 if spent_on.is_a?(Time)
86 115
87 # Returns true if the time entry can be edited by usr, otherwise false 116 # Returns true if the time entry can be edited by usr, otherwise false
88 def editable_by?(usr) 117 def editable_by?(usr)
89 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) 118 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
90 end 119 end
91
92 def self.earilest_date_for_project(project=nil)
93 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
94 if project
95 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
96 end
97 TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
98 end
99
100 def self.latest_date_for_project(project=nil)
101 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
102 if project
103 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
104 end
105 TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
106 end
107 end 120 end