Skip to content

Commit

Permalink
[FIX] hr_timesheet: Widget for planned hours
Browse files Browse the repository at this point in the history
Before this commit:
The field `planned_hours` and `subtask_planned_hours` are present
twice. This causes problems with widget: different widgets are put
on different fields, but only the last on was put on all of them.
Code to edit label with hours/days is not flexible, we need to
write each possibilities.

After this commit:
All fields are present only once in the view. A new widget is
created to manage days with a factor. In this new widget, instead
of having a toggle button, we can insert a float.
When the widget timesheet_uom is set on a field, we modify his string
with hours/days.

closes odoo#57557

Taskid: 2326216
X-original-commit: 5b38044
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
  • Loading branch information
jbm-odoo committed Sep 11, 2020
1 parent ef8806d commit 90b46ce
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
11 changes: 4 additions & 7 deletions addons/hr_timesheet/models/project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import re
from lxml import etree

from odoo import models, fields, api, _
Expand Down Expand Up @@ -222,13 +222,10 @@ def _fields_view_get(self, view_id=None, view_type='form', toolbar=False, submen
def _apply_time_label(self, view_arch):
doc = etree.XML(view_arch)
encoding_uom = self.env.company.timesheet_encode_uom_id
for node in doc.xpath("//field[@widget='timesheet_uom'][not(@string)] | //field[@widget='timesheet_uom_no_toggle'][not(@string)]"):
name_with_uom = re.sub(_('Hours') + "|Hours", encoding_uom.name or '', self._fields[node.get('name')]._description_string(self.env), flags=re.IGNORECASE)
node.set('string', name_with_uom)

for node in doc.xpath("//field[@name='planned_hours'][@widget='timesheet_uom'][not(@string)]"):
node.set('string', _('Planned %s') % encoding_uom.name or '')
for node in doc.xpath("//field[@name='effective_hours'][@widget='timesheet_uom'][not(@string)]"):
node.set('string', _('%s Spent') % encoding_uom.name or '')
for node in doc.xpath("//field[@name='remaining_hours'][@widget='timesheet_uom'][not(@string)]"):
node.set('string', _('Remaining %s') % encoding_uom.name or '')
return etree.tostring(doc, encoding='unicode')

def unlink(self):
Expand Down
22 changes: 22 additions & 0 deletions addons/hr_timesheet/static/src/js/timesheet_uom.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ if (widgetName === 'float_toggle') {
}
fieldRegistry.add('timesheet_uom', FieldTimesheetUom);

// widget timesheet_uom_no_toggle is the same as timesheet_uom but without toggle.
// We can modify easly huge amount of days.
let FieldTimesheetUomWithoutToggle = null;
if (widgetName === 'float_toggle') {
FieldTimesheetUomWithoutToggle = FieldTimesheetFactor;
} else {
FieldTimesheetUomWithoutToggle = FieldTimesheetTime;
}
fieldRegistry.add('timesheet_uom_no_toggle', FieldTimesheetUomWithoutToggle);


// bind the formatter and parser method, and tweak the options
const _tweak_options = function(options) {
Expand All @@ -132,6 +142,18 @@ fieldUtils.parse.timesheet_uom = function(value, field, options) {
return parser(value, field, options);
};

fieldUtils.format.timesheet_uom_no_toggle = function(value, field, options) {
options = _tweak_options(options || {});
const formatter = fieldUtils.format[FieldTimesheetUom.prototype.formatType];
return formatter(value, field, options);
};

fieldUtils.parse.timesheet_uom_no_toggle = function(value, field, options) {
options = _tweak_options(options || {});
const parser = fieldUtils.parse[FieldTimesheetUom.prototype.formatType];
return parser(value, field, options);
};

return {
FieldTimesheetUom,
FieldTimesheetFactor,
Expand Down
26 changes: 10 additions & 16 deletions addons/hr_timesheet/views/project_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,16 @@
<page string="Timesheets" id="timesheets_tab" attrs="{'invisible': [('allow_timesheets', '=', False)]}">
<group>
<group>
<label for="planned_hours" string="Initially Planned Hours" attrs="{'invisible': [('encode_uom_in_days', '=', True)]}"/>
<field name="planned_hours" widget="float_time" attrs="{'invisible': [('encode_uom_in_days', '=', True)]}" nolabel="1" />
<label for="planned_hours" string="Initially Planned Days" attrs="{'invisible': [('encode_uom_in_days', '=', False)]}"/>
<field name="planned_hours" widget="timesheet_factor" attrs="{'invisible': [('encode_uom_in_days', '=', False)]}" nolabel="1" />
<label for="subtask_planned_hours" groups="project.group_subtask_project" string="Sub-tasks Planned Hours"
attrs="{'invisible': ['|', '|', ('allow_subtasks', '=', False), ('subtask_count', '=', 0), ('encode_uom_in_days', '=', True)]}"/>
<span class="o_row" groups="project.group_subtask_project"
attrs="{'invisible': ['|', '|', ('allow_subtasks', '=', False), ('subtask_count', '=', 0), ('encode_uom_in_days', '=', True)]}">
<field name="subtask_planned_hours" widget="float_time"/>
</span>
<label for="subtask_planned_hours" groups="project.group_subtask_project" string="Sub-tasks Planned Days"
attrs="{'invisible': ['|', '|', ('allow_subtasks', '=', False), ('subtask_count', '=', 0), ('encode_uom_in_days', '=', False)]}"/>
<div class="o_row" groups="project.group_subtask_project"
attrs="{'invisible': ['|', '|', ('allow_subtasks', '=', False), ('subtask_count', '=', 0), ('encode_uom_in_days', '=', False)]}">
<field name="subtask_planned_hours" widget="timesheet_uom"/>
<div class="o_td_label">
<label for="planned_hours" string="Initially Planned Hours" attrs="{'invisible': [('encode_uom_in_days', '=', True)]}"/>
<label for="planned_hours" string="Initially Planned Days" attrs="{'invisible': [('encode_uom_in_days', '=', False)]}"/>
</div>
<field name="planned_hours" widget="timesheet_uom_no_toggle" nolabel="1"/>
<div class="o_td_label" groups="project.group_subtask_project" attrs="{'invisible': ['|', ('allow_subtasks', '=', False), ('subtask_count', '=', 0)]}">
<label for="subtask_planned_hours" string="Sub-tasks Planned Hours" attrs="{'invisible': [('encode_uom_in_days', '=', True)]}"/>
<label for="subtask_planned_hours" string="Sub-tasks Planned Days" attrs="{'invisible': [('encode_uom_in_days', '=', False)]}"/>
</div>
<field name="subtask_planned_hours" widget="timesheet_uom_no_toggle" nolabel="1" groups="project.group_subtask_project" attrs="{'invisible': ['|', ('allow_subtasks', '=', False), ('subtask_count', '=', 0)]}"/>
</group>
<group>
<field name="progress" widget="progressbar"/>
Expand Down Expand Up @@ -213,7 +207,7 @@
<field name="arch" type="xml">
<field name="company_id" position="after">
<field name="allow_subtasks" invisible="1"/>
<field name="planned_hours" widget="timesheet_uom" sum="Initially Planned Hours" optional="hide"/>
<field name="planned_hours" widget="timesheet_uom_no_toggle" sum="Initially Planned Hours" optional="hide"/>
<field name="effective_hours" widget="timesheet_uom" sum="Effective Hours" optional="show"/>
<field name="remaining_hours" widget="timesheet_uom" sum="Remaining Hours" optional="hide" decoration-danger="progress &gt;= 100" decoration-warning="progress &gt;= 80 and progress &lt; 100"/>
<field name="subtask_effective_hours" widget="timesheet_uom" attrs="{'invisible' : [('allow_subtasks', '=', False)]}" optional="hide"/>
Expand Down
2 changes: 1 addition & 1 deletion addons/project/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def _read_group_stage_ids(self, stages, domain, order):
compute='_compute_project_id', store=True, readonly=False,
index=True, tracking=True, check_company=True, change_default=True)
planned_hours = fields.Float("Initially Planned Hours", help='Time planned to achieve this task (including its sub-tasks).', tracking=True)
subtask_planned_hours = fields.Float("Sub-tasks Planned Hours", compute='_compute_subtask_planned_hours', help="Sum of the planned hours of all the sub-tasks linked to this task. Usually less or equal to the initially planned hours of this task.")
subtask_planned_hours = fields.Float("Sub-tasks Planned Hours", compute='_compute_subtask_planned_hours', help="Sum of the time planned of all the sub-tasks linked to this task. Usually less or equal to the initially time planned of this task.")
user_id = fields.Many2one('res.users',
string='Assigned to',
default=lambda self: self.env.uid,
Expand Down

0 comments on commit 90b46ce

Please sign in to comment.