Skip to content

Commit

Permalink
[IMP] hr_timesheet_report: Add day unit to the choices
Browse files Browse the repository at this point in the history
Allow to display times in days in the reports
  • Loading branch information
maisim committed Jan 21, 2025
1 parent c7f4e8f commit 9ea69ad
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
2 changes: 1 addition & 1 deletion hr_timesheet_report/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Features:

* Select reported fields
* Select and reorder report line grouping
* Configure time format (HH:MM, HH:MM:SS, or decimal)
* Configure time format : Hours (HH:MM, HH:MM:SS, or decimal) or Days (decimal)
* View in browser, export in PDF and XLSX formats

**Table of contents**
Expand Down
2 changes: 1 addition & 1 deletion hr_timesheet_report/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ Features:

* Select reported fields
* Select and reorder report line grouping
* Configure time format (HH:MM, HH:MM:SS, or decimal)
* Configure time format : Hours (HH:MM, HH:MM:SS, or decimal) or Days (decimal)
* View in browser, export in PDF and XLSX formats
17 changes: 15 additions & 2 deletions hr_timesheet_report/report/hr_timesheet_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class HrTimesheetReport(models.TransientModel):
selection=lambda self: self._selection_time_format(),
required=True,
)
uom = fields.Many2one("uom.uom", compute="_compute_uom", store=True)
group_ids = fields.One2many(
string="Groups",
comodel_name="hr.timesheet.report.group",
Expand All @@ -81,6 +82,7 @@ def _selection_time_format(self):
("hh_mm", "Hours, minutes"),
("hh_mm_ss", "Hours, minutes, seconds"),
("decimal", "Decimal"),
("days", "Days"),
]

@api.model
Expand Down Expand Up @@ -165,6 +167,14 @@ def _get_group_values(self, grouped_lines):
"scope": ustr(grouped_lines["__domain"]),
}

@api.depends("time_format")
def _compute_uom(self):
for report in self:
if report.time_format == "days":
report.uom = self.env.ref("uom.product_uom_day")
else:
report.uom = self.env.ref("uom.product_uom_hour")

@api.depends("group_ids.total_unit_amount")
def _compute_total_unit_amount(self):
for report in self:
Expand Down Expand Up @@ -404,6 +414,7 @@ class HrTimesheetReportEntry(models.TransientModel):
compute="_compute_total_unit_amount",
store=True,
)
uom = fields.Many2one("uom.uom", related="group_id.report_id.uom")

@api.depends("scope")
def _compute_any_line_id(self):
Expand All @@ -418,16 +429,16 @@ def _compute_any_line_id(self):
@api.depends("scope")
def _compute_total_unit_amount(self):
AccountAnalyticLine = self.env["account.analytic.line"]
uom_hour = self.env.ref("uom.product_uom_hour")

for entry in self:

total_unit_amount = 0.0
line_ids = AccountAnalyticLine.search(
safe_eval(entry.scope) if entry.scope else TRUE_DOMAIN
)
for line_id in line_ids:
total_unit_amount += line_id.product_uom_id._compute_quantity(
line_id.unit_amount, uom_hour
line_id.unit_amount, entry.uom
)
entry.total_unit_amount = total_unit_amount

Expand Down Expand Up @@ -644,6 +655,8 @@ def _get_amount_num_format(self, report):
return "[h]:mm"
elif report.time_format == "hh_mm_ss":
return "[h]:mm:ss"
elif report.time_format == "days":
return "0.00"

Check warning on line 659 in hr_timesheet_report/report/hr_timesheet_report.py

View check run for this annotation

Codecov / codecov/patch

hr_timesheet_report/report/hr_timesheet_report.py#L659

Added line #L659 was not covered by tests

@api.model
def _convert_amount_num_format(self, report, amount):
Expand Down
20 changes: 17 additions & 3 deletions hr_timesheet_report/report/hr_timesheet_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<t t-call="web.html_container">
<t t-foreach="docs" t-as="report">
<t t-call="web.external_layout">
<t t-set="uom_hour" t-value="env.ref('uom.product_uom_hour')" />
<div class="page">
<div class="oe_structure" />
<div class="row">
Expand All @@ -32,7 +31,12 @@
t-out="report.total_unit_amount"
t-options="{'widget': 'float', 'precision': 2}"
/>
<span t-field="uom_hour.name" />
<span
t-elif="report.time_format == 'days'"
t-esc="report.total_unit_amount"
t-options="{'widget': 'float', 'precision': 2}"
/>
<span t-esc="report.uom.name" />
</span>
</h2>
</div>
Expand Down Expand Up @@ -152,7 +156,7 @@
<span t-field="field.field_title" />
</th>
<th class="text-right">
<span t-field="uom_hour.name" />
<span t-esc="report.uom.name" />
</th>
</tr>
<tr t-foreach="group.entry_ids" t-as="entry">
Expand Down Expand Up @@ -182,6 +186,11 @@
t-field="entry.total_unit_amount"
t-options="{'widget': 'float', 'precision': 2}"
/>
<span
t-elif="report.time_format == 'days'"
t-field="entry.total_unit_amount"
t-options="{'widget': 'float', 'precision': 2}"
/>
</td>
</tr>
<tr class="o_subtotal">
Expand Down Expand Up @@ -210,6 +219,11 @@
t-out="group.total_unit_amount"
t-options="{'widget': 'float', 'precision': 2}"
/>
<strong
t-elif="report.time_format == 'days'"
t-esc="group.total_unit_amount"
t-options="{'widget': 'float', 'precision': 2}"
/>
</td>
</tr>
</t>
Expand Down
14 changes: 8 additions & 6 deletions hr_timesheet_report/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand All @@ -9,10 +8,11 @@

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code .ln { color: gray; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -376,7 +376,7 @@ <h1 class="title">Task Logs Timesheet Report</h1>
<ul class="simple">
<li>Select reported fields</li>
<li>Select and reorder report line grouping</li>
<li>Configure time format (HH:MM, HH:MM:SS, or decimal)</li>
<li>Configure time format : Hours (HH:MM, HH:MM:SS, or decimal) or Days (decimal)</li>
<li>View in browser, export in PDF and XLSX formats</li>
</ul>
</blockquote>
Expand Down Expand Up @@ -441,7 +441,9 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down
20 changes: 17 additions & 3 deletions hr_timesheet_report/tests/test_hr_timesheet_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def setUpClass(cls):
"name": "Time Entry",
"employee_id": cls.employee.id,
"date": cls.today,
"unit_amount": 1,
"unit_amount": 8, # 8 hours = 1 day
}
)

Expand Down Expand Up @@ -78,6 +78,20 @@ def test_no_grouping(self):
self.assertTrue(wizard.entry_field_ids)
report = self._create_report_from_wizard(wizard)
self.assertEqual(len(report.group_ids), 1)
self.assertEqual(report.total_unit_amount, 8)

@mute_logger("odoo.models.unlink")
def test_uom_day(self):
wizard_form = Form(self.Wizard)
wizard_form.date_from = self.today
wizard_form.date_to = self.today
wizard_form.employee_ids.add(self.employee)
wizard_form.time_format = "days"
wizard = wizard_form.save()
self.assertTrue(wizard.grouping_field_ids)
self.assertTrue(wizard.entry_field_ids)
report = self._create_report_from_wizard(wizard)
self.assertEqual(report.uom.id, self.env.ref("uom.product_uom_day").id)
self.assertEqual(report.total_unit_amount, 1)


Expand Down Expand Up @@ -109,7 +123,7 @@ def test_multi_project_01(self):
self.assertEqual(len(report.line_ids), 2)
self.assertIn(self.timesheet_1, report.line_ids)
self.assertIn(self.timesheet_2, report.line_ids)
self.assertEqual(report.total_unit_amount, 3)
self.assertEqual(report.total_unit_amount, 10) # 8 + 2

@mute_logger("odoo.models.unlink")
def test_multi_project_02(self):
Expand All @@ -122,4 +136,4 @@ def test_multi_project_02(self):
self.assertTrue(wizard.entry_field_ids)
report = self._create_report_from_wizard(wizard)
self.assertEqual(len(report.group_ids), 2)
self.assertEqual(report.total_unit_amount, 3)
self.assertEqual(report.total_unit_amount, 10) # 8 + 2

0 comments on commit 9ea69ad

Please sign in to comment.