Defect #29622

Adding a 'Custom Field' to 'Activities (time tracking)' prevents time entry on project change

Added by Ercan Demirel almost 4 years ago. Updated 7 months ago.

Status:NewStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:Time tracking
Target version:-
Resolution: Affected version:3.4.5

Description

Two projects have identical values for time tracking activity and custom field:

field_work_settings.png
consulting_settings.png

But if you try to move an issue from one project to the other and create a time entry with that time tracking activity, the following errors occur:

time_entry_error.png

The validation for the time entry should not fail, since both projects have the same activity with the same custom field value.

Our workflow dictates to create a time entry on every issue update, regardless of the content of the update. So moving the issue to another project, then creating a time entry is out of the question.

Seperate values for identical activities can be seen in the database, so inclusion validation always seems to fail:

 19 | QA                      |        3 | f          | TimeEntryActivity | t      |          3 |        14 | 
 18 | QA                      |        3 | f          | TimeEntryActivity | t      |          2 |        14 | 
 14 | QA                      |        3 | f          | TimeEntryActivity | t      |            |           | 

consulting_settings.png (37.9 KB) Ercan Demirel, 2018-09-17 10:28

field_work_settings.png (38 KB) Ercan Demirel, 2018-09-17 10:28

time_entry_error.png (42.8 KB) Ercan Demirel, 2018-09-17 10:28

project-time-tracking-settings.png (89.5 KB) Yuichi HARADA, 2021-12-07 08:54

29622.patch Magnifier (3.79 KB) Yuichi HARADA, 2021-12-07 09:16

History

#1 Updated by Yuichi HARADA 7 months ago

Ercan Demirel wrote:

But if you try to move an issue from one project to the other and create a time entry with that time tracking activity, the following errors occur:

time_entry_error.png

The validation for the time entry should not fail, since both projects have the same activity with the same custom field value.

There are two kinds of activities.
  • Administration > Enumerations > Activities (time tracking): These activities are system activities . The original of the activities updated in each project.
  • [project] > Settings > Time tracking: These activities are duplications of system activities. If you edit the value of the custom fields or switch between Active/Inactive, the checkmark of the system activity column disappears and it is re-registered as a project-specific activity.

Seperate values for identical activities can be seen in the database, so inclusion validation always seems to fail:

19 | QA                      |        3 | f          | TimeEntryActivity | t      |          3 |        14 | 
18 | QA                      |        3 | f          | TimeEntryActivity | t      |          2 |        14 | 
14 | QA                      |        3 | f          | TimeEntryActivity | t      |            |           | 

Perhaps I think the QA of Field Work and the QA of Consulting have different activity ids (18 or 19). When changing the issue's project, the time entry's activity must be replaced with a valid activity in the destination project.

#2 Updated by Yuichi HARADA 7 months ago

I created the following patch.

diff --git a/app/models/issue.rb b/app/models/issue.rb
index 5c1911eb8d..4faa063762 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -1679,8 +1679,28 @@ class Issue < ActiveRecord::Base
   end

   def after_project_change
-    # Update project_id on related time entries
-    TimeEntry.where({:issue_id => id}).update_all(["project_id = ?", project_id])
+    # Update project_id and activity_id on related time entries
+    activities = project.activities
+    time_entries.each do |time_entry|
+      curr_activity = time_entry.activity
+      curr_activity_id = (curr_activity.parent_id || curr_activity.id)
+      new_activity =
+        activities.detect do |activity|
+          activity.id == curr_activity_id || activity.parent_id == curr_activity_id
+        end
+
+      time_entry.project = project
+      time_entry.activity = new_activity unless curr_activity == new_activity
+      unless time_entry.save
+        errors.add(
+          :base,
+          l(:error_move_of_time_entries_not_possible,
+            :id => "##{time_entry.id}",
+            :errors => time_entry.errors.full_messages.join(", "))
+        )
+        raise ActiveRecord::Rollback
+      end
+    end

     # Delete issue relations
     unless Setting.cross_project_issue_relations?
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 2eb4af0e13..8693538ac1 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -229,6 +229,7 @@ en:
   error_no_tracker_allowed_for_new_issue_in_project: "The project doesn't have any trackers for which you can create an issue" 
   error_no_projects_with_tracker_allowed_for_new_issue: "There are no projects with trackers for which you can create an issue" 
   error_move_of_child_not_possible: "Subtask %{child} could not be moved to the new project: %{errors}" 
+  error_move_of_time_entries_not_possible: "Spent time %{id} could not be moved to the new project: %{errors}" 
   error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted: "Spent time cannot be reassigned to an issue that is about to be deleted" 
   warning_fields_cleared_on_bulk_edit: "Changes will result in the automatic deletion of values from one or more fields on the selected objects" 
   error_exceeds_maximum_hours_per_day: "Cannot log more than %{max_hours} hours on the same day (%{logged_hours} hours have already been logged)" 

Also available in: Atom PDF