|
| 1 | +# Copyright 2022 Akretion (https://www.akretion.com). |
| 2 | +# @author Sébastien BEAU <sebastien.beau@akretion.com> |
| 3 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 4 | + |
| 5 | + |
| 6 | +from odoo import fields, models |
| 7 | + |
| 8 | + |
| 9 | +class ProjectProject(models.Model): |
| 10 | + _inherit = "project.project" |
| 11 | + |
| 12 | + def _get_hour_domain(self): |
| 13 | + return [ |
| 14 | + ("category_id", "=", self.env.ref("uom.uom_categ_wtime").id), |
| 15 | + ("uom_type", "=", "smaller"), |
| 16 | + ] |
| 17 | + |
| 18 | + hour_uom_id = fields.Many2one( |
| 19 | + "uom.uom", |
| 20 | + "Hour Uom", |
| 21 | + help="Used for conversion between day and hours", |
| 22 | + domain=_get_hour_domain, |
| 23 | + ) |
| 24 | + |
| 25 | + def _get_hour_uom(self): |
| 26 | + # By default in Odoo the uom of reference is the day |
| 27 | + # so depending of your location and multicompany case |
| 28 | + # you can have a different unit for hours (7h per day, 8h per day...) |
| 29 | + if self.hour_uom_id: |
| 30 | + return self.hour_uom_id |
| 31 | + else: |
| 32 | + return self.env.ref("uom.product_uom_hour") |
| 33 | + |
| 34 | + def convert_hours_to_days(self, value): |
| 35 | + return self._convert_to(value, "hours2days") |
| 36 | + |
| 37 | + def convert_days_to_hours(self, value): |
| 38 | + return self._convert_to(value, "days2hours") |
| 39 | + |
| 40 | + def _convert_to(self, value, conversion): |
| 41 | + uom_day = self.env.ref("uom.product_uom_day") |
| 42 | + uom_hour = self._get_hour_uom() |
| 43 | + if conversion == "days2hours": |
| 44 | + return uom_day._compute_quantity(value, uom_hour) |
| 45 | + elif conversion == "hours2days": |
| 46 | + return uom_hour._compute_quantity(value, uom_day) |
0 commit comments