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_time_type_cost_rule #688

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
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
88 changes: 88 additions & 0 deletions hr_timesheet_time_type_cost_rule/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
=================================
Cost Rules in Time Type Timesheet
=================================

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

.. |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_time_type_cost_rule
: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_time_type_cost_rule
: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|

This module extends the functionality of project time type, providing the ability to apply cost rules to timesheet entries.

**Table of contents**

.. contents::
:local:

Configuration
=============

To configure the Timesheet Time Type cost Rules:

1. Go to Project > Configuration > Time Type
2. Create or Select a Time Type.
3. Mark the check "Apply Cost Rules" to display the rules information.
4. Click on the "Add a line" button to add a new rule.
5. Select the rule type.
6. Define a employee domain to apply the rule.

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_time_type_cost_rule%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
~~~~~~~

* Binhex

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

* Christian Ramos <[email protected]>

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.

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

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 4 additions & 0 deletions hr_timesheet_time_type_cost_rule/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (C) 2024 Binhex
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
19 changes: 19 additions & 0 deletions hr_timesheet_time_type_cost_rule/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (C) 2024 Binhex
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Cost Rules in Time Type Timesheet",
"summary": "Ability to add cost rules based on ratios or fixed price in time type.",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "Binhex, Odoo Community Association (OCA)",
"category": "Timesheet",
"website": "https://github.com/OCA/timesheet",
"depends": ["hr_timesheet_time_type"],
"data": [
"security/ir.model.access.csv",
"views/project_time_type_view.xml",
"views/project_time_type_view_cost_rule.xml",
],
"installable": True,
}
5 changes: 5 additions & 0 deletions hr_timesheet_time_type_cost_rule/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (C) 2024 Binhex
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import project_time_type
from . import account_analytic_line
39 changes: 39 additions & 0 deletions hr_timesheet_time_type_cost_rule/models/account_analytic_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (C) 2024 Binhex
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


class AccountAnalyticLine(models.Model):
_inherit = "account.analytic.line"

def _timesheet_postprocess_values(self, values):
"""Override to compute the amount based on the cost rules of the time type."""
result = super()._timesheet_postprocess_values(values)
sudo_self = (
self.sudo()
) # this creates only one env for all operation that required sudo()
# (re)compute the amount (depending on time_type_id)
if "time_type_id" in values:
for timesheet in sudo_self:
cost = timesheet._hourly_cost()
amount = -timesheet.unit_amount * cost
amount_converted = timesheet.employee_id.currency_id._convert(
amount,
timesheet.account_id.currency_id or timesheet.currency_id,
self.env.company,
timesheet.date,
)
if values["time_type_id"]:
amount_converted = (
self.env["project.time.type"]
.browse(values["time_type_id"])
._apply_cost_rules(timesheet, amount_converted)
)

result[timesheet.id].update(
{
"amount": amount_converted,
}
)
return result
99 changes: 99 additions & 0 deletions hr_timesheet_time_type_cost_rule/models/project_time_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Copyright (C) 2024 Binhex
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import ast

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class ProjectTimeType(models.Model):
_inherit = "project.time.type"

apply_cost_rules = fields.Boolean()
cost_rule_ids = fields.One2many(
"project.time.type.cost.rule",
"time_type_id",
)

def _apply_cost_rules(self, timesheet, amount):
"""Apply cost rules to the amount based on the employee and the time type."""
if not self.apply_cost_rules:
return amount
for rule in self.cost_rule_ids:
amount = rule.apply_rule(timesheet, amount)
return amount


class ProjectTimeTypeCostRule(models.Model):
_name = "project.time.type.cost.rule"
_description = "Project Time Type Cost Rule"
_order = "sequence, id"

def apply_rule(self, timesheet, amount):
if self.domain:
if not self.apply_domain(timesheet):
return amount

method_name = f"apply_rule_{self.cost_rule_type}"
if hasattr(self, method_name):
return getattr(self, method_name)(timesheet, amount)
else:
raise ValueError(

Check warning on line 42 in hr_timesheet_time_type_cost_rule/models/project_time_type.py

View check run for this annotation

Codecov / codecov/patch

hr_timesheet_time_type_cost_rule/models/project_time_type.py#L42

Added line #L42 was not covered by tests
f"There is no method to apply the rule type: {self.cost_rule_type}"
)

def apply_domain(self, timesheet):
domain = ast.literal_eval(self.domain)
return timesheet.employee_id.filtered_domain(domain)

def apply_rule_fixed(self, timesheet, amount):
return timesheet.unit_amount * self.fixed_cost

def apply_rule_ratio(self, timesheet, amount):
return amount * self.ratio_value

@api.constrains("fixed_cost", "ratio_value")
def _check_fixed_cost_ratio_value(self):
for record in self:
if record.cost_rule_type == "fixed" and record.fixed_cost > 0:
raise ValidationError(_("Fixed cost must be negative"))
if record.cost_rule_type == "ratio" and record.ratio_value < 0:
raise ValidationError(_("Ratio value must be positive"))

def _compute_display_value(self):
for record in self:
if record.cost_rule_type == "fixed":
record.display_value = record.fixed_cost
else:
record.display_value = record.ratio_value

sequence = fields.Integer(
required=True,
)
time_type_id = fields.Many2one(
"project.time.type",
ondelete="cascade",
required=True,
)
cost_rule_type = fields.Selection(
[("fixed", "Fixed"), ("ratio", "Ratio")],
required=True,
)
fixed_cost = fields.Float(
help="Fixed cost for this rule, this will replace"
" the employee cost or previus rule value"
)
ratio_value = fields.Float(
help="Ratio value for this rule, this will be multiplied"
" by the employee cost or previus rule value"
)
display_value = fields.Float(
string="Value",
compute="_compute_display_value",
help="Value to display in the timesheet line",
)
domain = fields.Text(
help="Domain to apply this rule base on the employee, leave"
" empty to apply to all employees"
)
8 changes: 8 additions & 0 deletions hr_timesheet_time_type_cost_rule/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To configure the Timesheet Time Type cost Rules:

1. Go to Project > Configuration > Time Type
2. Create or Select a Time Type.
3. Mark the check "Apply Cost Rules" to display the rules information.
4. Click on the "Add a line" button to add a new rule.
5. Select the rule type.
6. Define a employee domain to apply the rule.
1 change: 1 addition & 0 deletions hr_timesheet_time_type_cost_rule/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Christian Ramos <[email protected]>
1 change: 1 addition & 0 deletions hr_timesheet_time_type_cost_rule/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module extends the functionality of project time type, providing the ability to apply cost rules to timesheet entries.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you expand on what a "cost rule" is and what it is useful for (an example use case)?

Copy link

@arielbarreiros96 arielbarreiros96 Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A “cost rule” is a way to define costs per hour type. In Odoo it is fairly easy to define the hourly cost per employee, but this only takes into account cases where the hourly cost is fixed per employee. It is also true that you can set an hourly cost related to an SO line, but this is only useful if the timesheet is related to a sales order line, and it is only possible to define one hourly cost per employee per SO line. Let's imagine this scenario:

A company that repairs heavy machinery for ships, industry, etc, 24/7 with field service, etc. Its employees work in shifts, and the shifts can vary so a particular employee does not always take the same shifts each week. The cost to the company varies from employee to employee, and from shift to shift. They may need to record timesheets and record costs based on the shift (regular hour, holidays, night shift, etc.) and the employee who worked at it, because employees will work on whatever machine (lets consider one project/analytic account per machine) is there when they start their shift.

3 changes: 3 additions & 0 deletions hr_timesheet_time_type_cost_rule/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_project_time_type_cost_rule,project.time.type.cost.rule,model_project_time_type_cost_rule,base.group_user,1,0,0,0
access_project_time_type_cost_rule_manager,project.time.type.cost.rule,model_project_time_type_cost_rule,project.group_project_manager,1,1,1,1
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading