Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][ADD] hr_timesheet_begin_end_no_end #691

Draft
wants to merge 6 commits into
base: 16.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .copier-answers.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Do NOT update manually; changes here will be overwritten by Copier
_commit: v1.21.1
_commit: '1.23'
_src_path: gh:oca/oca-addons-repo-template
ci: GitHub
convert_readme_fragments_to_markdown: false
Expand All @@ -17,6 +17,8 @@ org_name: Odoo Community Association (OCA)
org_slug: OCA
rebel_module_groups:
- sale_timesheet_rounded
- hr_timesheet_begin_end
- hr_timesheet_begin_end_no_end
repo_description: 'TODO: add repo description.'
repo_name: timesheet
repo_slug: timesheet
Expand Down
28 changes: 19 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,32 @@ jobs:
include:
- container: ghcr.io/oca/oca-ci/py3.10-odoo16.0:latest
include: "sale_timesheet_rounded"
makepot: "true"
name: test with Odoo
- container: ghcr.io/oca/oca-ci/py3.10-ocb16.0:latest
include: "sale_timesheet_rounded"
name: test with OCB
makepot: "true"
- container: ghcr.io/oca/oca-ci/py3.10-odoo16.0:latest
include: "hr_timesheet_begin_end"
name: test with Odoo
- container: ghcr.io/oca/oca-ci/py3.10-ocb16.0:latest
include: "hr_timesheet_begin_end"
name: test with OCB
makepot: "true"
- container: ghcr.io/oca/oca-ci/py3.10-odoo16.0:latest
exclude: "sale_timesheet_rounded"
include: "hr_timesheet_begin_end_no_end"
name: test with Odoo
- container: ghcr.io/oca/oca-ci/py3.10-ocb16.0:latest
include: "hr_timesheet_begin_end_no_end"
name: test with OCB
makepot: "true"
- container: ghcr.io/oca/oca-ci/py3.10-odoo16.0:latest
exclude: "sale_timesheet_rounded,hr_timesheet_begin_end,hr_timesheet_begin_end_no_end"
name: test with Odoo
- container: ghcr.io/oca/oca-ci/py3.10-ocb16.0:latest
exclude: "sale_timesheet_rounded"
exclude: "sale_timesheet_rounded,hr_timesheet_begin_end,hr_timesheet_begin_end_no_end"
name: test with OCB
makepot: "true"
services:
postgres:
image: postgres:12.0
Expand Down Expand Up @@ -79,9 +93,5 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Update .pot files
run:
oca_export_and_push_pot https://x-access-token:${{ secrets.GIT_PUSH_TOKEN
}}@github.com/${{ github.repository }}
if:
${{ matrix.makepot == 'true' && github.event_name == 'push' &&
github.repository_owner == 'OCA' }}
run: oca_export_and_push_pot https://x-access-token:${{ secrets.GIT_PUSH_TOKEN }}@github.com/${{ github.repository }}
if: ${{ matrix.makepot == 'true' && github.event_name == 'push' && github.repository_owner == 'OCA' }}
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ var/
*.egg
*.eggs

# Debian packages
*.deb

# Redhat packages
*.rpm

# MacOS packages
*.dmg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
Expand Down
95 changes: 69 additions & 26 deletions hr_timesheet_begin_end/models/account_analytic_line.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2015 Camptocamp SA - Guewen Baconnier
# Copyright 2017 Tecnativa, S.L. - Luis M. Ontalba
# Copyright 2024 Coop IT Easy SC - Carmen Bianca BAKKER
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from datetime import timedelta
Expand All @@ -15,16 +16,34 @@
time_start = fields.Float(string="Begin Hour")
time_stop = fields.Float(string="End Hour")

@api.constrains("time_start", "time_stop", "unit_amount")
def _check_time_start_stop(self):
for line in self:
value_to_html = self.env["ir.qweb.field.float_time"].value_to_html
start = timedelta(hours=line.time_start)
stop = timedelta(hours=line.time_stop)
if stop < start:
value_to_html(line.time_start, None)
value_to_html(line.time_stop, None)
# Override to be a computed field.
unit_amount = fields.Float(
compute="_compute_unit_amount",
store=True,
readonly=False,
# This default is a workaround for a bizarre situation: if a line is
# created with a time range but WITHOUT defining unit_amount, then you
# would expect unit_amount to be computed from the range. But this never
# happens, and it is instead set to default value 0. Subsequently the
# constraint _validate_unit_amount_equal_to_time_diff kicks in and
# raises an exception.
#
# By setting the default to None, the computation is correctly
# triggered. If nothing is computed, None falls back to 0.
default=None,
)

@api.depends("time_start", "time_stop", "project_id")
def _compute_unit_amount(self):
# Do not compute/adjust the unit_amount of non-timesheets.
lines = self.filtered(lambda line: line.project_id)
for line in lines:
line.unit_amount = line.unit_amount_from_start_stop()

def _validate_start_before_stop(self):
value_to_html = self.env["ir.qweb.field.float_time"].value_to_html
for line in self:
if line.time_stop < line.time_start:
raise exceptions.ValidationError(
_(
"The beginning hour (%(html_start)s) must "
Expand All @@ -35,7 +54,11 @@
"html_stop": value_to_html(line.time_stop, None),
}
)
hours = (stop - start).seconds / 3600

def _validate_unit_amount_equal_to_time_diff(self):
value_to_html = self.env["ir.qweb.field.float_time"].value_to_html
for line in self:
hours = line.unit_amount_from_start_stop()
rounding = self.env.ref("uom.product_uom_hour").rounding
if hours and float_compare(
hours, line.unit_amount, precision_rounding=rounding
Expand All @@ -50,16 +73,21 @@
"html_hours": value_to_html(hours, None),
}
)
# check if lines overlap
others = self.search(
[
("id", "!=", line.id),
("employee_id", "=", line.employee_id.id),
("date", "=", line.date),
("time_start", "<", line.time_stop),
("time_stop", ">", line.time_start),
]
)

def _overlap_domain(self):
self.ensure_one()
return [
("id", "!=", self.id),
("employee_id", "=", self.employee_id.id),
("date", "=", self.date),
("time_start", "<", self.time_stop),
("time_stop", ">", self.time_start),
]

def _validate_no_overlap(self):
value_to_html = self.env["ir.qweb.field.float_time"].value_to_html
for line in self:
others = self.search(line._overlap_domain())
if others:
message = _("Lines can't overlap:\n")
message += "\n".join(
Expand All @@ -74,13 +102,28 @@
)
raise exceptions.ValidationError(message)

@api.onchange("time_start", "time_stop")
def onchange_hours_start_stop(self):
start = timedelta(hours=self.time_start)
stop = timedelta(hours=self.time_stop)
@api.constrains("time_start", "time_stop", "unit_amount")
def _check_time_start_stop(self):
lines = self.filtered(lambda line: line.project_id)
lines._validate_start_before_stop()
lines._validate_unit_amount_equal_to_time_diff()
lines._validate_no_overlap()

@api.model
def _hours_from_start_stop(self, time_start, time_stop):
start = timedelta(hours=time_start)
stop = timedelta(hours=time_stop)
if stop < start:
return
self.unit_amount = (stop - start).seconds / 3600
# Invalid case, but return something sensible.
return 0
return (stop - start).seconds / 3600

def unit_amount_from_start_stop(self):
self.ensure_one()
# Don't handle non-timesheet lines.
if not self.project_id:
return 0

Check warning on line 125 in hr_timesheet_begin_end/models/account_analytic_line.py

View check run for this annotation

Codecov / codecov/patch

hr_timesheet_begin_end/models/account_analytic_line.py#L125

Added line #L125 was not covered by tests
return self._hours_from_start_stop(self.time_start, self.time_stop)

def merge_timesheets(self): # pragma: no cover
"""This method is needed in case hr_timesheet_sheet is installed"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the test to use timesheet lines instead of bare analytic lines.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactored the module to be more extensible.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed ``unit_amount`` into a computed (stored, writeable) field.
46 changes: 33 additions & 13 deletions hr_timesheet_begin_end/tests/test_timesheet_begin_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,51 @@ class TestBeginEnd(common.TransactionCase):
def setUp(self):
super(TestBeginEnd, self).setUp()
self.timesheet_line_model = self.env["account.analytic.line"]
self.analytic = self.env.ref("analytic.analytic_administratif")
self.user = self.env.ref("base.user_root")
self.project = self.env.ref("project.project_project_1")
self.employee = self.env.ref("hr.employee_qdp")
self.base_line = {
"name": "test",
"date": fields.Date.today(),
"time_start": 10.0,
"time_stop": 12.0,
"user_id": self.user.id,
"unit_amount": 2.0,
"account_id": self.analytic.id,
"amount": -60.0,
"project_id": self.project.id,
"employee_id": self.employee.id,
}

def test_onchange(self):
line = self.timesheet_line_model.new(
{"name": "test", "time_start": 10.0, "time_stop": 12.0}
)
line.onchange_hours_start_stop()
self.assertEqual(line.unit_amount, 2)
def test_compute_unit_amount(self):
line = self.base_line.copy()
del line["unit_amount"]
line_record = self.timesheet_line_model.create(line)
self.assertEqual(line_record.unit_amount, 2)
line_record.time_stop = 14.0
self.assertEqual(line_record.unit_amount, 4)

def test_compute_unit_amount_no_compute_if_no_times(self):
line = self.base_line.copy()
del line["time_start"]
del line["time_stop"]
line_record = self.timesheet_line_model.create(line)
self.assertEqual(line_record.unit_amount, 2.0)
line_record.unit_amount = 3.0
self.assertEqual(line_record.unit_amount, 3.0)

def test_compute_unit_amount_to_zero(self):
line = self.base_line.copy()
del line["unit_amount"]
line_record = self.timesheet_line_model.create(line)
self.assertEqual(line_record.unit_amount, 2)
line_record.write({"time_start": 0, "time_stop": 0})
self.assertEqual(line_record.unit_amount, 0)

def test_onchange_no_update(self):
def test_compute_unit_amount_to_zero_no_record(self):
# Cannot create/save this model because it breaks a constraint, so using
# .new().
line = self.timesheet_line_model.new(
{"name": "test", "time_start": 13.0, "time_stop": 12.0}
)
line.onchange_hours_start_stop()
self.assertEqual(line.unit_amount, 0)
line.time_stop = 10.0
self.assertEqual(line.unit_amount, 0)

def test_check_begin_before_end(self):
Expand Down
88 changes: 88 additions & 0 deletions hr_timesheet_begin_end_no_end/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
==========================================
Timesheet - Begin/End Hours with empty end
==========================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:6f9bf57eb39a36725b852081172b71f26e16fed507227ba22b894d0f1ed0d8e5
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Ftimesheet-lightgray.png?logo=github
:target: https://github.com/OCA/timesheet/tree/16.0/hr_timesheet_begin_end_no_end
:alt: OCA/timesheet
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/timesheet-16-0/timesheet-16-0-hr_timesheet_begin_end_no_end
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/timesheet&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

Allow to insert begin hour without end hour on the timesheet. The benefit of
doing this is that you can save your begin hour at the start of your day/task,
and come back later to save the end hour.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/timesheet/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/timesheet/issues/new?body=module:%20hr_timesheet_begin_end_no_end%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Coop IT Easy SC

Contributors
~~~~~~~~~~~~

* `Coop IT Easy SC <https://coopiteasy.be>`_:

* Carmen Bianca BAKKER

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

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.

.. |maintainer-carmenbianca| image:: https://github.com/carmenbianca.png?size=40px
:target: https://github.com/carmenbianca
:alt: carmenbianca

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-carmenbianca|

This module is part of the `OCA/timesheet <https://github.com/OCA/timesheet/tree/16.0/hr_timesheet_begin_end_no_end>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
5 changes: 5 additions & 0 deletions hr_timesheet_begin_end_no_end/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from . import models
18 changes: 18 additions & 0 deletions hr_timesheet_begin_end_no_end/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

{
"name": "Timesheet - Begin/End Hours with empty end",
"summary": (
"Allow to insert begin hour without end hour, which the user may fill"
" in later."
),
"version": "16.0.1.0.0",
"category": "Human Resources",
"website": "https://github.com/OCA/timesheet",
"author": "Coop IT Easy SC, Odoo Community Association (OCA)",
"maintainers": ["carmenbianca"],
"license": "AGPL-3",
"depends": ["hr_timesheet_begin_end"],
}
Loading
Loading