Skip to content

Commit

Permalink
[FIX] compute time spent based on last invoice date
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-champonnois committed Oct 25, 2024
1 parent 55a466d commit 18ed1b2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
18 changes: 13 additions & 5 deletions contract_timesheet_monitoring/models/account_invoice_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
10 changes: 9 additions & 1 deletion contract_timesheet_monitoring/models/contract_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 3 additions & 1 deletion contract_timesheet_monitoring/views/account_invoice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<field name="inherit_id" ref="account.invoice_form" />
<field name="arch" type="xml">
<field name="quantity" position="after">
<field name="time_spent" decoration-danger="time_spent &gt; quantity" />
<field name="time_available" />
<field name="time_spent" decoration-danger="time_spent &gt; time_available" />
<field name="time_remaining" decoration-danger="time_spent &lt; 0" />
</field>
</field>
</record>
Expand Down

0 comments on commit 18ed1b2

Please sign in to comment.