close

DEV Community

harena-odoo
harena-odoo

Posted on

Odoo 18: How to filter analytic accounts in analytic_distribution widget programmatically?

I'm working on Odoo 18 Enterprise and I'm struggling to filter the analytic accounts displayed in the analytic_distribution widget on purchase order lines.

The Setup

Model A: global.budget contains a list of allowed account.analytic.account.

Model B: purchase.order has a field overall_budget_id pointing to Model A.

Goal: When clicking the analytic_distribution widget on a PO line, the "Analytic Account" search/selection should only show accounts defined in the linked budget.

The Problem

In Odoo 18, the analytic_distribution widget seems to bypass standard Python hooks for searching. I have tried the following without success:

What we tried

Context injection in XML:

<xpath expr="//sheet" position="inside">
       <field name="allowed_analytic_account_ids" invisible="1"/>
 </xpath>
<xpath expr="//field[@name='order_line']/list//field[@name='analytic_distribution']" position="attributes">
<attribute name="context">{
                'allowed_analytic_account_ids': parent.allowed_analytic_account_ids
            }</attribute>
 </xpath>

Enter fullscreen mode Exit fullscreen mode

Overriding _search and name_search on account.analytic.account: I added print statements in _search and name_search to catch the filter_budget_id from context. However, these methods are never triggered when searching for an account inside the distribution widget popup.

@api.model
def search(self, domain, offset=0, limit=None, order=None):
    allowed_ids = self.env.context.get('allowed_analytic_account_ids')
    if allowed_ids:
        domain = [('id', 'in', allowed_ids)] + list(domain)
    print("Search domain:", domain)
    return super().search(domain, offset=offset, limit=limit, order=order)

@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
    args = args or []
    allowed_ids = self.env.context.get('allowed_analytic_account_ids')
    if allowed_ids:
        args = [('id', 'in', allowed_ids)] + args
    print(args)
    return super().name_search(
        name=name, args=args, operator=operator, limit=limit
    )

Enter fullscreen mode Exit fullscreen mode

My Question

The analytic_distribution widget (which uses a specific OWL component in v18) seems to use a different RPC call or a specific route to fetch accounts.

Which method should I override in Odoo 18 to filter the accounts suggested by this specific widget?

Is there a specific options key in the XML to pass a domain to the sub-fields of the distribution widget?

Environment: Odoo 18.0 Enterprise.

Any help or pointer to the right method/hook to override would be greatly appreciated!

Thanks in advance.

Top comments (1)

Collapse
 
harenaodoo21 profile image
harena-odoo

You can see as well my post in stack-overflow here: stackoverflow post