Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
3178 Zobrazení

At the top of a company/individual there is a tab that shows "XX Sales".  I'd like to use this field in reports but it doesn't seem to be an actual field on the object.  Is there a way (perhaps with Python) to define a computed field which would be a count of all related sales orders for this company?

Avatar
Zrušit
Nejlepší odpověď

Hi,

You can set store=True for compute functions then the value will be stored in the database.
Here you can redefine the sale order count field with store=True and also give sale_order_ids as depends on the compute function.

ie,

sale_order_count = fields.Integer(compute="_compute_sale_order_count",
string="Sale Order Count",
store=True)
@api.depends('sale_order_ids')
def _compute_sale_order_count(self):
# 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'])

sale_order_groups = self.env['sale.order'].read_group(
domain=[('partner_id', 'in', all_partners.ids)],
fields=['partner_id'], groupby=['partner_id']
)
partners = self.browse()
for group in sale_order_groups:
partner = self.browse(group['partner_id'][0])
while partner:
if partner in self:
partner.sale_order_count += group['partner_id_count']
partners |= partner
partner = partner.parent_id
(self - partners).sale_order_count = 0

Regards

Avatar
Zrušit