From 18ed1b2bb85b1a6ea28506a1b3c9aab00ea4b153 Mon Sep 17 00:00:00 2001 From: Victor Champonnois Date: Fri, 25 Oct 2024 10:37:02 +0200 Subject: [PATCH] [FIX] compute time spent based on last invoice date --- .../models/account_analytic_account.py | 9 +++++---- .../models/account_invoice_line.py | 18 +++++++++++++----- .../models/contract_line.py | 10 +++++++++- .../views/account_invoice.xml | 4 +++- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/contract_timesheet_monitoring/models/account_analytic_account.py b/contract_timesheet_monitoring/models/account_analytic_account.py index b456b3f3..85fb0abe 100644 --- a/contract_timesheet_monitoring/models/account_analytic_account.py +++ b/contract_timesheet_monitoring/models/account_analytic_account.py @@ -13,7 +13,8 @@ def get_time_spent_for_period(self, start_date, end_date=None): lambda x: (x.product_uom_id.measure_type == "time") ) if timesheets: - time_spent_on_account = timesheets.filtered( - lambda x: (x.date >= start_date) - ).mapped("unit_amount") - return sum(time_spent_on_account) + if start_date: + timesheets = timesheets.filtered( + lambda x: (x.date >= start_date) + ) + return sum(timesheets.mapped("unit_amount")) diff --git a/contract_timesheet_monitoring/models/account_invoice_line.py b/contract_timesheet_monitoring/models/account_invoice_line.py index 13654d98..d71665a8 100644 --- a/contract_timesheet_monitoring/models/account_invoice_line.py +++ b/contract_timesheet_monitoring/models/account_invoice_line.py @@ -3,7 +3,7 @@ # Copyright 2020 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import api, fields, models class AccountInvoiceLine(models.Model): @@ -13,14 +13,22 @@ class AccountInvoiceLine(models.Model): string="Time Spent", compute="_compute_time_spent" ) time_available = fields.Integer(related="product_id.hours_available") + + time_remaining = fields.Float( + string="Time Remaining", compute="_compute_time_remaining" + ) - - + @api.depends("account_analytic_id.line_ids") def _compute_time_spent(self): for line in self: - if line.analytic_account_id and line.start_date: - line.time_spent = line.analytic_account_id.get_time_spent_for_period( + if line.account_analytic_id and line.start_date: + line.time_spent = line.account_analytic_id.get_time_spent_for_period( line.start_date, line.end_date ) else: line.time_spent = False + + @api.depends("time_available", "time_spent") + def _compute_time_remaining(self): + for line in self: + line.time_remaining = line.time_available - line.time_spent diff --git a/contract_timesheet_monitoring/models/contract_line.py b/contract_timesheet_monitoring/models/contract_line.py index f79bc751..5ff3179c 100644 --- a/contract_timesheet_monitoring/models/contract_line.py +++ b/contract_timesheet_monitoring/models/contract_line.py @@ -12,12 +12,20 @@ class ContractLine(models.Model): time_remaining = fields.Float( string="Time Remaining", compute="_compute_time_remaining" ) + date_of_last_invoice = fields.Date(compute="_get_date_of_last_invoice") + + def _get_date_of_last_invoice(self): + invoices = self.contract_id._get_related_invoices().filtered(lambda x: (x.state != "draft")) + if any(invoices.mapped("date_invoice")): + return max(invoices.mapped("date_invoice")) + else: + return False @api.depends("analytic_account_id.line_ids") def _compute_time_spent(self): for line in self: if line.analytic_account_id: - period_start_date = line.last_date_invoiced or line.date_start + period_start_date = line._get_date_of_last_invoice() line.time_spent = line.analytic_account_id.get_time_spent_for_period( period_start_date ) diff --git a/contract_timesheet_monitoring/views/account_invoice.xml b/contract_timesheet_monitoring/views/account_invoice.xml index 261e1b77..f19453b9 100644 --- a/contract_timesheet_monitoring/views/account_invoice.xml +++ b/contract_timesheet_monitoring/views/account_invoice.xml @@ -6,7 +6,9 @@ - + + +