[FIX] point_of_sale, project: Smart buttons to count on partner
Unify the behavior for smart buttons on res.partner view form Let's consider generic records R on a specific model M - All the R of the children and archived children of a contact C must be counted in smart button of M in the form of C - Clicking on the smart button of a contact C must display all R of the children and archived children of C closes odoo/odoo#61241 Signed-off-by: Simon Goffin (sig) <sig@openerp.com>
This commit is contained in:
@@ -15,10 +15,33 @@ class ResPartner(models.Model):
|
||||
pos_order_ids = fields.One2many('pos.order', 'partner_id', readonly=True)
|
||||
|
||||
def _compute_pos_order(self):
|
||||
partners_data = self.env['pos.order'].read_group([('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
|
||||
mapped_data = dict([(partner['partner_id'][0], partner['partner_id_count']) for partner in partners_data])
|
||||
for partner in self:
|
||||
partner.pos_order_count = mapped_data.get(partner.id, 0)
|
||||
# retrieve all children partners and prefetch 'parent_id' on them
|
||||
all_partners = self.with_context(active_test=False).search([('id', 'child_of', self.ids)])
|
||||
all_partners.read(['parent_id'])
|
||||
|
||||
pos_order_data = self.env['pos.order'].read_group(
|
||||
domain=[('partner_id', 'in', all_partners.ids)],
|
||||
fields=['partner_id'], groupby=['partner_id']
|
||||
)
|
||||
|
||||
self.pos_order_count = 0
|
||||
for group in pos_order_data:
|
||||
partner = self.browse(group['partner_id'][0])
|
||||
while partner:
|
||||
if partner in self:
|
||||
partner.pos_order_count += group['partner_id_count']
|
||||
partner = partner.parent_id
|
||||
|
||||
def action_view_pos_order(self):
|
||||
'''
|
||||
This function returns an action that displays the pos orders from partner.
|
||||
'''
|
||||
action = self.env.ref('point_of_sale.action_pos_pos_form').read()[0]
|
||||
if self.is_company:
|
||||
action['domain'] = [('partner_id.commercial_partner_id.id', '=', self.id)]
|
||||
else:
|
||||
action['domain'] = [('partner_id.id', '=', self.id)]
|
||||
return action
|
||||
|
||||
@api.model
|
||||
def create_from_ui(self, partner):
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
<field name="groups_id" eval="[(4, ref('group_pos_user'))]"/>
|
||||
<field name="arch" type="xml">
|
||||
<div name="button_box" position="inside">
|
||||
<button class="oe_stat_button" type="action" name="%(point_of_sale.action_pos_pos_form)d"
|
||||
context="{'search_default_partner_id': active_id,'default_partner_id': active_id}"
|
||||
<button class="oe_stat_button" type="object" name="action_view_pos_order"
|
||||
context="{'default_partner_id': active_id}"
|
||||
attrs="{'invisible': [('pos_order_count', '=', 0)]}"
|
||||
icon="fa-shopping-bag">
|
||||
<field string="PoS Orders" name="pos_order_count" widget="statinfo"/>
|
||||
|
||||
@@ -12,7 +12,19 @@ class ResPartner(models.Model):
|
||||
task_count = fields.Integer(compute='_compute_task_count', string='# Tasks')
|
||||
|
||||
def _compute_task_count(self):
|
||||
fetch_data = self.env['project.task'].read_group([('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
|
||||
result = dict((data['partner_id'][0], data['partner_id_count']) for data in fetch_data)
|
||||
for partner in self:
|
||||
partner.task_count = result.get(partner.id, 0) + sum(c.task_count for c in partner.child_ids)
|
||||
# retrieve all children partners and prefetch 'parent_id' on them
|
||||
all_partners = self.with_context(active_test=False).search([('id', 'child_of', self.ids)])
|
||||
all_partners.read(['parent_id'])
|
||||
|
||||
task_data = self.env['project.task'].read_group(
|
||||
domain=[('partner_id', 'in', all_partners.ids)],
|
||||
fields=['partner_id'], groupby=['partner_id']
|
||||
)
|
||||
|
||||
self.task_count = 0
|
||||
for group in task_data:
|
||||
partner = self.browse(group['partner_id'][0])
|
||||
while partner:
|
||||
if partner in self:
|
||||
partner.task_count += group['partner_id_count']
|
||||
partner = partner.parent_id
|
||||
|
||||
Reference in New Issue
Block a user