[MERGE][IMP] project, *_timesheet: link analytic account and project

Impacted modules: hr_timesheet, project, sale_timesheet and sale_timesheet_purchase

Currently, the link between the Analytic Account and the Project is non existant if
Timesheets is not installed, and we track any cost linked to the AA set on the Project,
but we only track the revenues linked to Timesheets.

The purpose of this merge is to allow simple project (no timesheet installed or
activated) to track their costs and revenues. This is done through the analytic account; indeed, all
purchase order, sales order, invoices and vendor bills can generate analytic entries. It could
then be interessting for user to see the costs and revenues for a project.
We decided to redirect user from the project dashboard to the "costs/revenues" action of the analytic
account itself, or (if timesheet is allowed on project) to the overview.
This commit also integrates purchase orders on the project overview.

Implementation:
This PR aims to improve the link between Project and Analytic Accounting, by several actions:
* Move all the analytic account business to project
* Add an Other Revenues field in Project Overview,  which represent the positive analytic entries
from the AA set on the Project AND that are not linked
to a Timesheet
* Add two smart buttons on Project Overview to Purchase Orders and Vendor Bill of the project's AA
* Add the results (Sales Orders and Timesheets) linked to the project's AA in the respective smart
buttons of Project Overview
* Add a "Profitability" link on the Kanban card of the Project bringing to the Costs & Revenues of
the AA set on the Project to allow profitability tracking when there are no Timesheets.

To do so, it was required to move back the analytic dependency from hr_timesheet to project, like
before 3dd3e26483 (but without the inheritS).

Limitation:
This will only make sense for project having a "one-to-one" relation with analytic account. If 2 projects
are linked to the same AA, we have no way to deduce from which project the cost entry is linked.

Task-1975022

closes odoo/odoo#33237

Signed-off-by: Jérome Maes (jem) <jem@openerp.com>
This commit is contained in:
Robot Odoo
2019-05-24 14:15:52 +02:00
committed by GitHub
17 changed files with 158 additions and 66 deletions
-1
View File
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import analytic_account
from . import hr
from . import hr_timesheet
from . import ir_http
+4 -29
View File
@@ -9,9 +9,6 @@ class Project(models.Model):
_inherit = "project.project"
allow_timesheets = fields.Boolean("Allow timesheets", default=True, help="Enable timesheeting on the project.")
analytic_account_id = fields.Many2one('account.analytic.account', string="Analytic Account", copy=False, ondelete='set null',
help="Analytic account to which this project is linked for financial management."
"Use an analytic account to record cost and revenue on your project.")
@api.onchange('partner_id')
def _onchange_partner_id(self):
@@ -47,12 +44,7 @@ class Project(models.Model):
"""
allow_timesheets = values['allow_timesheets'] if 'allow_timesheets' in values else self.default_get(['allow_timesheets'])['allow_timesheets']
if allow_timesheets and not values.get('analytic_account_id'):
analytic_account = self.env['account.analytic.account'].create({
'name': values.get('name', _('Unknown Analytic Account')),
'company_id': values.get('company_id', self.env.company_id.id),
'partner_id': values.get('partner_id'),
'active': True,
})
analytic_account = self._create_analytic_account_from_values(values)
values['analytic_account_id'] = analytic_account.id
return super(Project, self).create(values)
@@ -66,31 +58,14 @@ class Project(models.Model):
result = super(Project, self).write(values)
return result
@api.multi
def unlink(self):
""" Delete the empty related analytic account """
analytic_accounts_to_delete = self.env['account.analytic.account']
for project in self:
if project.analytic_account_id and not project.analytic_account_id.line_ids:
analytic_accounts_to_delete |= project.analytic_account_id
result = super(Project, self).unlink()
analytic_accounts_to_delete.unlink()
return result
# ---------------------------------------------------
# Business Methods
# ---------------------------------------------------
@api.model
def _init_data_analytic_account(self):
self.search([('analytic_account_id', '=', False), ('allow_timesheets', '=', True)])._create_analytic_account()
def _create_analytic_account(self):
for project in self:
analytic_account = self.env['account.analytic.account'].create({
'name': project.name,
'company_id': project.company_id.id,
'partner_id': project.partner_id.id,
'active': True,
})
project.write({'analytic_account_id': analytic_account.id})
class Task(models.Model):
_inherit = "project.task"
@@ -2,10 +2,6 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_account_analytic_line_user,analytic.account.analytic.line.timesheet.user,analytic.model_account_analytic_line,hr_timesheet.group_hr_timesheet_user,1,1,1,1
access_account_analytic_line_portal,access_timesheets_portal,analytic.model_account_analytic_line,base.group_portal,1,0,0,0
access_account_analytic_user,analytic.account.analytic.timesheet.user,analytic.model_account_analytic_account,hr_timesheet.group_hr_timesheet_user,1,1,0,0
access_account_analytic_account_user,account.analytic.account,analytic.model_account_analytic_account,project.group_project_user,1,0,0,0
access_account_analytic_account_manager,account.analytic.account,analytic.model_account_analytic_account,project.group_project_manager,1,1,1,1
access_account_analytic_account_portal,account_analytic_account,analytic.model_account_analytic_account,base.group_portal,1,0,0,0
access_account_analytic_line_project,account.analytic.line project,analytic.model_account_analytic_line,project.group_project_manager,1,1,1,1
access_uom_uom_hr_timesheet,uom.uom.timesheet.user,uom.model_uom_uom,hr_timesheet.group_hr_timesheet_user,1,0,0,0
access_project_project,project.project.timesheet.user,model_project_project,hr_timesheet.group_hr_timesheet_user,1,0,0,0
access_project_task,project.task.timesheet.user,model_project_task,hr_timesheet.group_hr_timesheet_user,1,1,0,0
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_account_analytic_line_user analytic.account.analytic.line.timesheet.user analytic.model_account_analytic_line hr_timesheet.group_hr_timesheet_user 1 1 1 1
3 access_account_analytic_line_portal access_timesheets_portal analytic.model_account_analytic_line base.group_portal 1 0 0 0
4 access_account_analytic_user analytic.account.analytic.timesheet.user analytic.model_account_analytic_account hr_timesheet.group_hr_timesheet_user 1 1 0 0
access_account_analytic_account_user account.analytic.account analytic.model_account_analytic_account project.group_project_user 1 0 0 0
access_account_analytic_account_manager account.analytic.account analytic.model_account_analytic_account project.group_project_manager 1 1 1 1
access_account_analytic_account_portal account_analytic_account analytic.model_account_analytic_account base.group_portal 1 0 0 0
access_account_analytic_line_project account.analytic.line project analytic.model_account_analytic_line project.group_project_manager 1 1 1 1
5 access_uom_uom_hr_timesheet uom.uom.timesheet.user uom.model_uom_uom hr_timesheet.group_hr_timesheet_user 1 0 0 0
6 access_project_project project.project.timesheet.user model_project_project hr_timesheet.group_hr_timesheet_user 1 0 0 0
7 access_project_task project.task.timesheet.user model_project_task hr_timesheet.group_hr_timesheet_user 1 1 0 0
@@ -1,24 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<!--
Analytic Accounts with project
-->
<record id="account_analytic_account_view_form_inherit" model="ir.ui.view">
<field name="name">account.analytic.account.form.inherit</field>
<field name="model">account.analytic.account</field>
<field name="inherit_id" ref="analytic.view_account_analytic_account_form"/>
<field eval="18" name="priority"/>
<field name="arch" type="xml">
<xpath expr="//button[@name='toggle_active']" position="before">
<button class="oe_stat_button" type="object" name="action_view_projects"
icon="fa-puzzle-piece" attrs="{'invisible': [('project_count', '=', 0)]}">
<field string="Projects" name="project_count" widget="statinfo"/>
</button>
</xpath>
</field>
</record>
<!-- Timesheet root menus -->
<menuitem id="timesheet_menu_root"
+3 -3
View File
@@ -45,9 +45,6 @@
<xpath expr="//group[@name='extra_settings']" position="inside">
<field name="allow_timesheets"/>
</xpath>
<xpath expr="//field[@name='partner_id']" position="after">
<field name="analytic_account_id" groups="analytic.group_analytic_accounting" />
</xpath>
</field>
</record>
@@ -178,6 +175,9 @@
</div>
</a>
</xpath>
<xpath expr="//a[@name='action_view_account_analytic_line']" position="attributes">
<attribute name="t-if">record.analytic_account_id.raw_value and !record.allow_timesheets.raw_value</attribute>
</xpath>
</field>
</record>
+2
View File
@@ -9,6 +9,7 @@
'sequence': 10,
'summary': 'Organize and schedule your projects ',
'depends': [
'analytic',
'base_setup',
'mail',
'portal',
@@ -23,6 +24,7 @@
'security/project_security.xml',
'security/ir.model.access.csv',
'report/project_report_views.xml',
'views/analytic_views.xml',
'views/digest_views.xml',
'views/rating_views.xml',
'views/project_views.xml',
+1
View File
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import analytic_account
from . import project
from . import res_config_settings
from . import res_partner
@@ -9,7 +9,6 @@ class AccountAnalyticAccount(models.Model):
_inherit = 'account.analytic.account'
_description = 'Analytic Account'
company_uom_id = fields.Many2one('uom.uom', related='company_id.project_time_mode_id', string="Company UOM", readonly=False)
project_ids = fields.One2many('project.project', 'analytic_account_id', string='Projects')
project_count = fields.Integer("Project Count", compute='_compute_project_count')
+50 -7
View File
@@ -80,13 +80,6 @@ class Project(models.Model):
values['alias_defaults'] = {'project_id': self.id}
return values
@api.multi
def unlink(self):
for project in self:
if project.tasks:
raise UserError(_('You cannot delete a project containing tasks. You can either archive it or first delete all of its tasks.'))
return super(Project, self).unlink()
def _compute_attached_docs_count(self):
Attachment = self.env['ir.attachment']
for project in self:
@@ -180,6 +173,9 @@ class Project(models.Model):
partner_id = fields.Many2one('res.partner', string='Customer', auto_join=True, tracking=True)
company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.company_id)
currency_id = fields.Many2one('res.currency', related="company_id.currency_id", string="Currency", readonly=True)
analytic_account_id = fields.Many2one('account.analytic.account', string="Analytic Account", copy=False, ondelete='set null',
help="Analytic account to which this project is linked for financial management. "
"Use an analytic account to record cost and revenue on your project.")
favorite_user_ids = fields.Many2many(
'res.users', 'project_favorite_user_rel', 'project_id', 'user_id',
@@ -324,6 +320,21 @@ class Project(models.Model):
project.message_subscribe(project.partner_id.ids)
return res
@api.multi
def unlink(self):
# Check project is empty
for project in self:
if project.tasks:
raise UserError(_('You cannot delete a project containing tasks. You can either archive it or first delete all of its tasks.'))
# Delete the empty related analytic account
analytic_accounts_to_delete = self.env['account.analytic.account']
for project in self:
if project.analytic_account_id and not project.analytic_account_id.line_ids:
analytic_accounts_to_delete |= project.analytic_account_id
result = super(Project, self).unlink()
analytic_accounts_to_delete.unlink()
return result
@api.multi
def message_subscribe(self, partner_ids=None, channel_ids=None, subtype_ids=None):
""" Subscribe to all existing active tasks when subscribing to a project """
@@ -365,6 +376,13 @@ class Project(models.Model):
action = self.env['ir.actions.act_window'].for_xml_id('project', 'act_project_project_2_project_task_all')
return dict(action, context=ctx)
def action_view_account_analytic_line(self):
""" return the action to see all the analytic lines of the project's analytic account """
action = self.env.ref('analytic.account_analytic_line_action').read()[0]
action['context'] = {'default_account_id': self.analytic_account_id.id}
action['domain'] = [('account_id', '=', self.analytic_account_id.id)]
return action
@api.multi
def action_view_all_rating(self):
""" return the action to see all the rating of the project, and activate default filters """
@@ -383,6 +401,31 @@ class Project(models.Model):
action_context.pop('group_by', None)
return dict(action, context=action_context)
# ---------------------------------------------------
# Business Methods
# ---------------------------------------------------
@api.model
def _create_analytic_account_from_values(self, values):
analytic_account = self.env['account.analytic.account'].create({
'name': values.get('name', _('Unknown Analytic Account')),
'company_id': values.get('company_id', self.env.user.company_id.id),
'partner_id': values.get('partner_id'),
'active': True,
})
return analytic_account
@api.multi
def _create_analytic_account(self):
for project in self:
analytic_account = self.env['account.analytic.account'].create({
'name': project.name,
'company_id': project.company_id.id,
'partner_id': project.partner_id.id,
'active': True,
})
project.write({'analytic_account_id': analytic_account.id})
# ---------------------------------------------------
# Rating business
# ---------------------------------------------------
+5 -1
View File
@@ -19,4 +19,8 @@ access_project_tags_all,project.project_tags_all,model_project_tags,,1,0,0,0
access_project_tags_manager,project.project_tags_manager,model_project_tags,project.group_project_manager,1,1,1,1
access_project_tags_portal,project_tags_portal,project.model_project_tags,base.group_portal,1,0,0,0
access_mail_alias,mail.alias,mail.model_mail_alias,project.group_project_manager,1,1,1,1
access_mail_activity_type_project_manager,mail.activity.type.project.manager,mail.model_mail_activity_type,project.group_project_manager,1,1,1,1
access_mail_activity_type_project_manager,mail.activity.type.project.manager,mail.model_mail_activity_type,project.group_project_manager,1,1,1,1
access_account_analytic_account_user,account.analytic.account,analytic.model_account_analytic_account,project.group_project_user,1,0,0,0
access_account_analytic_account_manager,account.analytic.account,analytic.model_account_analytic_account,project.group_project_manager,1,1,1,1
access_account_analytic_account_portal,account_analytic_account,analytic.model_account_analytic_account,base.group_portal,1,0,0,0
access_account_analytic_line_project,account.analytic.line project,analytic.model_account_analytic_line,project.group_project_manager,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
19 access_project_tags_manager project.project_tags_manager model_project_tags project.group_project_manager 1 1 1 1
20 access_project_tags_portal project_tags_portal project.model_project_tags base.group_portal 1 0 0 0
21 access_mail_alias mail.alias mail.model_mail_alias project.group_project_manager 1 1 1 1
22 access_mail_activity_type_project_manager mail.activity.type.project.manager mail.model_mail_activity_type project.group_project_manager 1 1 1 1
23 access_account_analytic_account_user account.analytic.account analytic.model_account_analytic_account project.group_project_user 1 0 0 0
24 access_account_analytic_account_manager account.analytic.account analytic.model_account_analytic_account project.group_project_manager 1 1 1 1
25 access_account_analytic_account_portal account_analytic_account analytic.model_account_analytic_account base.group_portal 1 0 0 0
26 access_account_analytic_line_project account.analytic.line project analytic.model_account_analytic_line project.group_project_manager 1 1 1 1
+21
View File
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!--
Analytic Accounts with project
-->
<record id="account_analytic_account_view_form_inherit" model="ir.ui.view">
<field name="name">account.analytic.account.form.inherit</field>
<field name="model">account.analytic.account</field>
<field name="inherit_id" ref="analytic.view_account_analytic_account_form"/>
<field eval="18" name="priority"/>
<field name="arch" type="xml">
<xpath expr="//button[@name='toggle_active']" position="before">
<button class="oe_stat_button" type="object" name="action_view_projects"
icon="fa-puzzle-piece" attrs="{'invisible': [('project_count', '=', 0)]}">
<field string="Projects" name="project_count" widget="statinfo"/>
</button>
</xpath>
</field>
</record>
</odoo>
+7
View File
@@ -140,6 +140,7 @@
</group>
<group>
<field name="partner_id" string="Customer"/>
<field name="analytic_account_id" context="{'default_partner_id': partner_id}" groups="analytic.group_analytic_accounting"/>
<label for="rating_status" groups="project.group_project_rating"/>
<div groups="project.group_project_rating">
<field name="rating_status" widget="radio"/>
@@ -303,6 +304,7 @@
<field name="is_favorite"/>
<field name="rating_percentage_satisfaction"/>
<field name="rating_status"/>
<field name="analytic_account_id"/>
<templates>
<t t-name="kanban-box">
<div t-attf-class="#{kanban_color(record.color.raw_value)} oe_kanban_global_click o_has_icon">
@@ -359,6 +361,11 @@
<span class="o_label"><t t-esc="record.label_tasks.value"/></span>
</div>
</a>
<a t-if="record.analytic_account_id.raw_value" class="o_project_kanban_box o_project_timesheet_box" name="action_view_account_analytic_line" type="object" groups="analytic.group_analytic_accounting">
<div>
<span class="o_label">Profitability</span>
</div>
</a>
</div>
</div>
</t>
@@ -71,7 +71,7 @@
<td class="o_timesheet_plan_dashboard_cell">
<t t-esc="dashboard['profit']['invoiced']" t-options='{"widget": "monetary", "display_currency": currency}'/>
</td>
<td>Invoiced</td>
<td>Time invoiced</td>
</tr>
<tr>
<td class="o_timesheet_plan_dashboard_cell">
@@ -85,7 +85,7 @@
<td class="o_timesheet_plan_dashboard_cell">
<t t-esc="dashboard['profit']['to_invoice']" t-options='{"widget": "monetary", "display_currency": currency}'/>
</td>
<td>To invoice</td>
<td>Time to invoice</td>
</tr>
<tr>
<td class="o_timesheet_plan_dashboard_cell">
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import models
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name': 'Sales Timesheet Purchase',
'category': 'Hidden',
'summary': 'Bridge module between Sales Timesheet and Purchase',
'description': """
Allows to access purchase orders from Project Overview
======================================================
""",
'depends': ['sale_timesheet', 'purchase'],
'data': [],
'demo': [],
'auto_install': True,
'uninstall_hook': 'uninstall_hook',
}
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import project_overview
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
from odoo import _, models
from odoo.addons.sale_timesheet.models.project_overview import _to_action_data
class Project(models.Model):
_inherit = 'project.project'
def _plan_get_stat_button(self):
stat_buttons = super(Project, self)._plan_get_stat_button()
if self.env.user.has_group('purchase.group_purchase_user'):
accounts = self.mapped('analytic_account_id.id')
purchase_order_lines = self.env['purchase.order.line'].search([('account_analytic_id', 'in', accounts)])
purchase_orders = purchase_order_lines.mapped('order_id')
if purchase_orders:
stat_buttons.append({
'name': _('Purchase Orders'),
'count': len(purchase_orders),
'icon': 'fa fa-shopping-cart',
'action': _to_action_data('purchase.order',
domain=[('id', 'in', purchase_orders.ids)],
context={'create': False, 'edit': False, 'delete': False}
)
})
account_invoice_lines = self.env['account.invoice.line'].search([('account_analytic_id', 'in', accounts)])
account_invoices = account_invoice_lines.mapped('invoice_id')
if account_invoices:
stat_buttons.append({
'name': _('Vendor Bills'),
'count': len(account_invoices),
'icon': 'fa pencil-square-o',
'action': _to_action_data(
action=self.env.ref('purchase.action_invoice_pending'),
domain=[('id', 'in', account_invoices.ids)],
context={'create': False, 'edit': False, 'delete': False}
)})
return stat_buttons