We are copying 3 stats buttons from the project view form (manufacturing orders, work orders and bills of materials) and we add them to the project update page. See task-2638359 closes odoo/odoo#76421 X-original-commit: 4572e710a5e755c0c75c95a6d033e44f0a2799f8 Related: odoo/enterprise#20811 Signed-off-by: LTU-Odoo <IT-Ideas@users.noreply.github.com>
63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models, _
|
|
|
|
|
|
class Project(models.Model):
|
|
_inherit = "project.project"
|
|
|
|
purchase_orders_count = fields.Integer('# Purchase Orders', compute='_compute_purchase_orders_count')
|
|
|
|
@api.depends('analytic_account_id')
|
|
def _compute_purchase_orders_count(self):
|
|
purchase_orders_data = self.env['purchase.order.line'].read_group([
|
|
('account_analytic_id', '!=', False),
|
|
('account_analytic_id', 'in', self.analytic_account_id.ids)
|
|
], ['account_analytic_id', 'order_id:count_distinct'], ['account_analytic_id'])
|
|
mapped_data = dict([(data['account_analytic_id'][0], data['order_id']) for data in purchase_orders_data])
|
|
for project in self:
|
|
project.purchase_orders_count = mapped_data.get(project.analytic_account_id.id, 0)
|
|
|
|
# ----------------------------
|
|
# Actions
|
|
# ----------------------------
|
|
|
|
def action_open_project_purchase_orders(self):
|
|
purchase_orders = self.env['purchase.order'].search([
|
|
('order_line.account_analytic_id', '!=', False),
|
|
('order_line.account_analytic_id', 'in', self.analytic_account_id.ids)
|
|
])
|
|
action_window = {
|
|
'name': _('Purchase Orders'),
|
|
'type': 'ir.actions.act_window',
|
|
'res_model': 'purchase.order',
|
|
'views': [[False, 'tree'], [False, 'form']],
|
|
'domain': [('id', 'in', purchase_orders.ids)],
|
|
'context': {
|
|
'create': False,
|
|
}
|
|
}
|
|
if len(purchase_orders) == 1:
|
|
action_window['views'] = [[False, 'form']]
|
|
action_window['res_id'] = purchase_orders.id
|
|
return action_window
|
|
|
|
# ----------------------------
|
|
# Project Updates
|
|
# ----------------------------
|
|
|
|
def _get_stat_buttons(self):
|
|
buttons = super(Project, self)._get_stat_buttons()
|
|
if self.user_has_groups('purchase.group_purchase_user'):
|
|
buttons.append({
|
|
'icon': 'credit-card',
|
|
'text': _('Purchase Orders'),
|
|
'number': self.purchase_orders_count,
|
|
'action_type': 'object',
|
|
'action': 'action_open_project_purchase_orders',
|
|
'show': self.purchase_orders_count > 0,
|
|
'sequence': 13,
|
|
})
|
|
return buttons
|