From 60a6d744a1e00c1c12a2af61777dbc8657cc3cf3 Mon Sep 17 00:00:00 2001 From: Victor Champonnois Date: Fri, 25 Oct 2024 14:49:02 +0200 Subject: [PATCH] [FIX] use contract date rather than invoice date Invoice date might not the date of the invoiced period, because invoice are not validated and sent on the first day of the period. --- .../models/account_analytic_account.py | 2 -- .../models/contract_line.py | 17 ++++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/contract_timesheet_monitoring/models/account_analytic_account.py b/contract_timesheet_monitoring/models/account_analytic_account.py index 85fb0abe..dfcd1643 100644 --- a/contract_timesheet_monitoring/models/account_analytic_account.py +++ b/contract_timesheet_monitoring/models/account_analytic_account.py @@ -8,8 +8,6 @@ def get_time_spent_for_period(self, start_date, end_date=None): analytic_account_lines = self.line_ids timesheets = analytic_account_lines.filtered( # keep only timesheets - # ensure the uom is the same as the one configure for the project - # timesheets (hours or day) lambda x: (x.product_uom_id.measure_type == "time") ) if timesheets: diff --git a/contract_timesheet_monitoring/models/contract_line.py b/contract_timesheet_monitoring/models/contract_line.py index aa2c8be0..90d6d83a 100644 --- a/contract_timesheet_monitoring/models/contract_line.py +++ b/contract_timesheet_monitoring/models/contract_line.py @@ -1,4 +1,5 @@ from odoo import api, fields, models +from odoo.exceptions import UserError class ContractLine(models.Model): @@ -14,18 +15,20 @@ class ContractLine(models.Model): ) 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 + def _get_period_start_date(self): + if self.recurring_invoicing_type == 'post-paid': + start_date = self.recurring_next_dat + else: + start_date = self.recurring_next_date - self.get_relative_delta( + self.recurring_rule_type, self.recurring_interval + ) + return start_date @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._get_date_of_last_invoice() + period_start_date = line._get_period_start_date() line.time_spent = line.analytic_account_id.get_time_spent_for_period( period_start_date )