[IMP] sale_{project,timesheet}: Add SO stat button to projects

This commit adds a "Sales Order" stat button in the project form view to ease the creation of a SO from a project.
This stat button is displayed if the project is billable and has a partner.

Task-3278895
This commit is contained in:
Vincent Larcin
2023-07-20 07:58:41 +02:00
committed by Xavier Bol (xbo)
parent 647b9de23b
commit b12ebdfd4e
4 changed files with 81 additions and 3 deletions
+3 -3
View File
@@ -121,10 +121,10 @@ class Project(models.Model):
action_window = {
"type": "ir.actions.act_window",
"res_model": "sale.order",
'name': _("%(name)s's Sales Order", name=self.name),
"context": {"create": False, "show_sale": True},
'name': _("%(name)s's Sales Orders", name=self.name),
"context": {"create": self.env.context.get('create_for_project_id'), "show_sale": True},
}
if len(all_sale_orders) == 1:
if len(all_sale_orders) <= 1:
action_window.update({
"res_id": all_sale_orders.id,
"views": [[False, "form"]],
+13
View File
@@ -4,6 +4,7 @@
from collections import defaultdict
from odoo import api, fields, models, _
from odoo.exceptions import UserError
from odoo.tools.safe_eval import safe_eval
@@ -203,6 +204,18 @@ class SaleOrder(models.Model):
}
}
@api.model_create_multi
def create(self, vals_list):
created_records = super().create(vals_list)
project = self.env['project.project'].browse(self.env.context.get('create_for_project_id'))
if project:
service_sol = next((sol for sol in created_records.order_line if sol.is_service), False)
if not service_sol:
raise UserError(_('This Sales Order must contain at least one product of type "Service".'))
if not project.sale_line_id:
project.sale_line_id = service_sol
return created_records
def write(self, values):
if 'state' in values and values['state'] == 'cancel':
self.project_id.sudo().sale_line_id = False
@@ -49,6 +49,65 @@
<field name="model">project.project</field>
<field name="inherit_id" ref="project.edit_project"/>
<field name="arch" type="xml">
<div name="button_box" position="inside">
<field name="display_sales_stat_buttons" invisible="1"/>
<button
class="oe_stat_button"
type="object"
name="action_view_sos"
icon="fa-dollar"
attrs="{
'invisible': [
'|',
('display_sales_stat_buttons', '=', False),
('sale_order_count', '=', 0),
]
}"
groups="sales_team.group_sale_salesman_all_leads"
context="{
'create_for_project_id': active_id,
'default_project_id': active_id,
'default_partner_id': partner_id
}"
>
<div class="o_field_widget o_stat_info">
<span class="o_stat_value">
<field name="sale_order_count" nolabel="1"/>
</span>
<span class="o_stat_text">
Sales Orders
</span>
</div>
</button>
<button
class="oe_stat_button"
type="object"
name="action_view_sos"
icon="fa-dollar"
attrs="{
'invisible': [
'|',
('display_sales_stat_buttons', '=', False),
('sale_order_count', '!=', 0),
],
}"
groups="sales_team.group_sale_salesman_all_leads"
context="{
'create_for_project_id': active_id,
'default_project_id': active_id,
'default_partner_id': partner_id
}"
>
<div class="o_field_widget o_stat_info">
<span class="o_stat_text">
<span class="o_stat_value">0</span> Sales Order
</span>
<span class="o_stat_text">
Make Billable
</span>
</div>
</button>
</div>
<xpath expr="//header" position="inside">
<field name="has_any_so_to_invoice" invisible="1"/>
<field name="has_any_so_with_nothing_to_invoice" invisible="1"/>
@@ -6,6 +6,12 @@
<field name="model">project.project</field>
<field name="inherit_id" ref="hr_timesheet.project_invoice_form"/>
<field name="arch" type="xml">
<xpath expr="//button[@name='action_view_sos'][1]" position="attributes">
<attribute name="context">{'create_for_project_id': active_id, 'default_project_id': active_id, 'default_partner_id': partner_id, 'default_analytic_account_id': analytic_account_id}</attribute>
</xpath>
<xpath expr="//button[@name='action_view_sos'][2]" position="attributes">
<attribute name="context">{'create_for_project_id': active_id, 'default_project_id': active_id, 'default_partner_id': partner_id, 'default_analytic_account_id': analytic_account_id}</attribute>
</xpath>
<xpath expr="//page[@name='settings']//field[@name='analytic_account_id']" position="before">
<field name="sale_line_id" groups="!sales_team.group_sale_salesman" options="{'no_create': True, 'no_edit': True, 'delete': False, 'no_open': True}" attrs="{'invisible': ['|', ('allow_billable', '=', False), ('partner_id', '=', False)]}"/>
<field name="sale_line_id" groups="sales_team.group_sale_salesman" options="{'no_create': True, 'no_edit': True, 'delete': False}" attrs="{'invisible': ['|', ('allow_billable', '=', False), ('partner_id', '=', False)]}"/>