|
| 1 | +# Copyright 2016 Tecnativa - Carlos Dauden |
| 2 | +# Copyright 2018 ACSONE SA/NV. |
| 3 | +# Copyright 2020 Tecnativa - Pedro M. Baeza |
| 4 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 5 | + |
| 6 | +from odoo import fields, models |
| 7 | + |
| 8 | + |
| 9 | +class AccountMoveLine(models.Model): |
| 10 | + _inherit = "account.move.line" |
| 11 | + |
| 12 | + time_spent = fields.Float( |
| 13 | + string="Time Spent this Period", compute="_compute_time_spent" |
| 14 | + ) |
| 15 | + |
| 16 | + def get_time_spent(self, analytic_distribution, start_date, end_date=None): |
| 17 | + total_time_spent = 0 |
| 18 | + for analytic_account, percentage in analytic_distribution.items(): |
| 19 | + analytic_account_id = int(analytic_account) |
| 20 | + analytic_account_lines = ( |
| 21 | + self.env["account.analytic.account"] |
| 22 | + .browse(analytic_account_id) |
| 23 | + .line_ids |
| 24 | + ) |
| 25 | + timesheets = analytic_account_lines.filtered( |
| 26 | + # keep only timesheets |
| 27 | + # ensure the uom is the same as the one configure for the project |
| 28 | + # timesheets (hours or day) |
| 29 | + lambda x: (x.encoding_uom_id == x.project_id.timesheet_encode_uom_id) |
| 30 | + ) |
| 31 | + if timesheets: |
| 32 | + time_spent_on_account = timesheets.filtered( |
| 33 | + lambda x: (x.date >= start_date) |
| 34 | + ).mapped("unit_amount") |
| 35 | + total_time_spent_on_account = sum(time_spent_on_account) |
| 36 | + total_time_spent += total_time_spent_on_account * percentage / 100 |
| 37 | + return total_time_spent |
| 38 | + |
| 39 | + def _compute_time_spent(self): |
| 40 | + for line in self: |
| 41 | + if line.analytic_distribution and line.start_date: |
| 42 | + line.time_spent = line.get_time_spent( |
| 43 | + line.analytic_distribution, line.start_date |
| 44 | + ) |
| 45 | + else: |
| 46 | + line.time_spent = False |
0 commit comments