Skip to content

Commit 3e238be

Browse files
[ADD] time spent on invoice (need refacto!)
1 parent 8755671 commit 3e238be

File tree

6 files changed

+98
-8
lines changed

6 files changed

+98
-8
lines changed

contract_timesheet_monitoring/__manifest__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"category": "Sales",
99
"author": "Coop IT Easy SC, Odoo Community Association (OCA)",
1010
"website": "https://github.com/coopiteasy/addons",
11-
"depends": ["contract", "hr_timesheet"],
11+
"depends": ["contract", "hr_timesheet", "contract_invoice_start_end_dates"],
1212
"development_status": "Production/Stable",
1313
"data": [
1414
"views/contract.xml",
15+
"views/account_move.xml",
16+
"views/account_invoice_report.xml",
1517
"views/contract_portal_templates.xml",
1618
],
1719
"license": "AGPL-3",
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import contract_line
2+
from . import account_move_line
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

contract_timesheet_monitoring/models/contract_line.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ def get_time_spent(self, analytic_distribution, start_date, end_date=None):
3131
return total_time_spent
3232

3333
def _compute_time_spent(self):
34-
if self.analytic_distribution:
35-
period_start_date = self.last_date_invoiced or self.date_start
36-
self.time_spent = self.get_time_spent(
37-
self.analytic_distribution, period_start_date
38-
)
39-
else:
40-
self.time_spent = False
34+
for line in self:
35+
if line.analytic_distribution:
36+
period_start_date = line.last_date_invoiced or line.date_start
37+
line.time_spent = line.get_time_spent(
38+
line.analytic_distribution, period_start_date
39+
)
40+
else:
41+
line.time_spent = False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<!-- Copyright 2020 Tecnativa - Víctor Martínez
3+
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
4+
<odoo>
5+
<template id="report_invoice_document" inherit_id="account.report_invoice_document">
6+
<xpath
7+
expr="//table[@name='invoice_line_table']/thead/tr/th[@name='th_quantity']"
8+
position="after"
9+
>
10+
<th name="th_time_spent" class="text-right">Time Spent</th>
11+
</xpath>
12+
<xpath expr="//span[@t-field='line.quantity']" position="after">
13+
<th
14+
name="td_time_spent"
15+
class="text-center"
16+
decoration-danger="line.time_spent &gt; line.quantity"
17+
>
18+
<span
19+
t-field="line.time_spent"
20+
t-attf-class="#{'text-danger' if line.time_spent &gt; line.quantity else 'text-success'}"
21+
/>
22+
</th>
23+
</xpath>
24+
25+
26+
</template>
27+
</odoo>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record id="account_move_form_view" model="ir.ui.view">
4+
<field name="name">account.move.form.view</field>
5+
<field name="model">account.move</field>
6+
<field name="inherit_id" ref="account.view_move_form" />
7+
<field name="arch" type="xml">
8+
<field name="quantity" position="after">
9+
<field name="time_spent" decoration-danger="time_spent &gt; quantity" />
10+
</field>
11+
</field>
12+
</record>
13+
</odoo>

0 commit comments

Comments
 (0)