Skip to content

Commit

Permalink
[ADD] time spent on invoice (need refacto!)
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-champonnois committed Mar 18, 2024
1 parent 8755671 commit 3e238be
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 8 deletions.
4 changes: 3 additions & 1 deletion contract_timesheet_monitoring/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"category": "Sales",
"author": "Coop IT Easy SC, Odoo Community Association (OCA)",
"website": "https://github.com/coopiteasy/addons",
"depends": ["contract", "hr_timesheet"],
"depends": ["contract", "hr_timesheet", "contract_invoice_start_end_dates"],
"development_status": "Production/Stable",
"data": [
"views/contract.xml",
"views/account_move.xml",
"views/account_invoice_report.xml",
"views/contract_portal_templates.xml",
],
"license": "AGPL-3",
Expand Down
1 change: 1 addition & 0 deletions contract_timesheet_monitoring/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import contract_line
from . import account_move_line
46 changes: 46 additions & 0 deletions contract_timesheet_monitoring/models/account_move_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2016 Tecnativa - Carlos Dauden
# Copyright 2018 ACSONE SA/NV.
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AccountMoveLine(models.Model):
_inherit = "account.move.line"

time_spent = fields.Float(
string="Time Spent this Period", compute="_compute_time_spent"
)

def get_time_spent(self, analytic_distribution, start_date, end_date=None):
total_time_spent = 0
for analytic_account, percentage in analytic_distribution.items():
analytic_account_id = int(analytic_account)
analytic_account_lines = (
self.env["account.analytic.account"]
.browse(analytic_account_id)
.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.encoding_uom_id == x.project_id.timesheet_encode_uom_id)
)
if timesheets:
time_spent_on_account = timesheets.filtered(
lambda x: (x.date >= start_date)
).mapped("unit_amount")
total_time_spent_on_account = sum(time_spent_on_account)
total_time_spent += total_time_spent_on_account * percentage / 100
return total_time_spent

def _compute_time_spent(self):
for line in self:
if line.analytic_distribution and line.start_date:
line.time_spent = line.get_time_spent(
line.analytic_distribution, line.start_date
)
else:
line.time_spent = False
15 changes: 8 additions & 7 deletions contract_timesheet_monitoring/models/contract_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def get_time_spent(self, analytic_distribution, start_date, end_date=None):
return total_time_spent

def _compute_time_spent(self):
if self.analytic_distribution:
period_start_date = self.last_date_invoiced or self.date_start
self.time_spent = self.get_time_spent(
self.analytic_distribution, period_start_date
)
else:
self.time_spent = False
for line in self:
if line.analytic_distribution:
period_start_date = line.last_date_invoiced or line.date_start
line.time_spent = line.get_time_spent(
line.analytic_distribution, period_start_date
)
else:
line.time_spent = False
27 changes: 27 additions & 0 deletions contract_timesheet_monitoring/views/account_invoice_report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2020 Tecnativa - Víctor Martínez
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<template id="report_invoice_document" inherit_id="account.report_invoice_document">
<xpath
expr="//table[@name='invoice_line_table']/thead/tr/th[@name='th_quantity']"
position="after"
>
<th name="th_time_spent" class="text-right">Time Spent</th>
</xpath>
<xpath expr="//span[@t-field='line.quantity']" position="after">
<th
name="td_time_spent"
class="text-center"
decoration-danger="line.time_spent &gt; line.quantity"
>
<span
t-field="line.time_spent"
t-attf-class="#{'text-danger' if line.time_spent &gt; line.quantity else 'text-success'}"
/>
</th>
</xpath>


</template>
</odoo>
13 changes: 13 additions & 0 deletions contract_timesheet_monitoring/views/account_move.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="account_move_form_view" model="ir.ui.view">
<field name="name">account.move.form.view</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml">
<field name="quantity" position="after">
<field name="time_spent" decoration-danger="time_spent &gt; quantity" />
</field>
</field>
</record>
</odoo>

0 comments on commit 3e238be

Please sign in to comment.