Filter available partners for a new invoice in Odoo

Viewed 425

I would like to filter the available partner selection for 'Customer' when creating an invoice in Odoo. Specifically, I'd like to limit the partner contact records to those of type 'invoice address', i.e. a domain on res_partner of [('type','=','invoice')].

Can anyone point me in the right direction please?

I can see that res_partner._name_search will accept Args, and by the look of the code in models.py the Args could be the required domain, becoming a where clause. However, I can't see how to specify this in the xml (or anywhere else). The standard xml for the Customer drop-down is

<field name="partner_id" widget="res_partner_many2one" context="{'res_partner_search_mode': 'customer', 'show_address': 1, 'show_vat': True}" options='{"always_reload": True}'/>

res_partner_search_mode looked kind of promising, but seems to only be used in setting up customer_rank, so no help here. Maybe just override _name_search to filter the records?? I'd be glad of any help. Thanks!

2 Answers

Try something like that

<field name="partner_id" widget="res_partner_many2one" domain="[('type','=','invoice')]" ...```

Solved. Paxmees' suggestion didn't work (the domain in the XML didn't seem to flow through to anywhere in the python code). But the args in _name_search turn out to be a domain. So I made an override for _name_search:

    def _name_search(self, name, args, operator, limit, name_get_uid=None):
        restrict = self.env.context.get('restrict_types')
        if restrict == 'invoice':
            args.append(('type', '=', restrict))
        return super()._name_search(name, args, operator=operator, limit=limit, name_get_uid=name_get_uid)

and then added the controlling context in the xml for views where I want this to happen:

    <xpath expr="//field[@name='partner_id']" position="attributes">
          <attribute name="context">{'restrict_types': 'invoice'}</attribute>
    </xpath>

This works OK. Thanks for reading my question!

Related