0

Hi guys i was using Odoo 10, Is there a way to search partner using vat number instead of partner name when creating invoice?

Thanks in advance

  • is there any vat number field in account.invoice? – Alpesh Valaki Aug 02 '17 at 11:31
  • No there is no vat number field... – war10ck6001 Aug 02 '17 at 12:01
  • then put vat number field in invoice and add it in search view – Alpesh Valaki Aug 02 '17 at 12:47
  • I think you misunderstood the question... what i want is when i create sales to invoice there is a customer field which let's you search customer/ partner using name of the partner then it automatically populate other fields depending on then customer property, so instead of searching by name i want to search the customer using partners vat number... I found a module (https://github.com/adhoc-dev/odoo-addons/tree/8.0/partner_search_by_vat) but it only works for odoo v8 – war10ck6001 Aug 02 '17 at 13:10

1 Answers1

2

Yes. There is a way to do that. You can try my solution

  1. Add a context in the partner field to set, that you want to search by vat. Here you can use xpath.
context="{'search_by_vat': True}"
  1. Overwrite the function name_search in res.partner:
class Partner(models.Model):
_inherit = 'res.partner'

    @api.model
    def name_search(self, name='', args=None, operator='ilike', limit=100):
        if self._context.get('search_by_vat', False):
            if name:
                args = args If i Want to search in both name and vat what should i do?if args else []
                args.append(['vat', 'ilike', name])
                name = ''
        return super(Partner, self).name_search(name=name, args=args, operator=operator, limit=limit)

If i Want to search in both name and vat what should i do?

You can use ['name', 'ilike', name] or ['vat', 'ilike', name]

class Partner(models.Model):
_inherit = 'res.partner'

    @api.model
    def name_search(self, name='', args=None, operator='ilike', limit=100):
        if self._context.get('search_by_vat', False):
            if name:
                args = args if args else []
                args.extend(['|', ['name', 'ilike', name], ['vat', 'ilike', name]])
                name = ''
        return super(Partner, self).name_search(name=name, args=args, operator=operator, limit=limit)
qvpham
  • 1,896
  • 9
  • 17
  • Yes, you should create a custom module. – qvpham Aug 02 '17 at 15:29
  • Hi i tried it your way but it's not working... I removed the if statement since i want it to work all the time Here is the error [ NameError: global name 'Partner' is not defined ] – war10ck6001 Aug 03 '17 at 11:55
  • You must use the class name in your module. `Partner` is the class name in my module. You can read more about it hier: (https://stackoverflow.com/questions/30712300/inheritance-in-openerp-odoo)[https://stackoverflow.com/questions/30712300/inheritance-in-openerp-odoo] – qvpham Aug 03 '17 at 12:29
  • The link doesn't work.... here is my class [ class res_partner(models.Model): _inherit = 'res.partner' ] so should i use res_partner instead of Partner – war10ck6001 Aug 03 '17 at 13:02
  • exactly. try it... Again the usefully link: [https://stackoverflow.com/questions/30712300/inheritance-in-openerp-odoo](https://stackoverflow.com/questions/30712300/inheritance-in-openerp-odoo) – qvpham Aug 03 '17 at 13:26
  • I tried and the error goes away but still the functionality is not working... when i insert the vat number in the customer field it doesn't know the customer it asks me to create a new customer... – war10ck6001 Aug 03 '17 at 13:33
  • Hey sorry. I have checked the code. The `search_key` isn't store in `display_name`. It's stored in `name`. Can you please try my correction? – qvpham Aug 03 '17 at 13:48
  • Hi i tried the changes but it's still doesn't work, please see the question to see the code... – war10ck6001 Aug 03 '17 at 14:25
  • HI, i saw your code. Indentation is false. Please look my code again. Don't forget to restart the server and update the module again. If you didn't set `context` in your view, you must take the `if` out `if self._context.get('search_by_vat', False):` – qvpham Aug 03 '17 at 14:42
  • Hey, sorry i mistakenly screwed up the indentation when i posted the code so the indentation is true here... and i already took out the if statement on the context and i restarted the server every time i update then module but still it's not working (check the question for the new code) – war10ck6001 Aug 03 '17 at 14:58
  • i don't know. Somewhere is wrong. It works now on my env. Did't you wrote a module before? – qvpham Aug 03 '17 at 15:10
  • Finally it works... there was a database error and i tried in a fresh install of odoo and it works... Thank you Very much – war10ck6001 Aug 04 '17 at 07:42
  • If i Want to search in both name and vat what should i do? – war10ck6001 Aug 04 '17 at 09:09
  • Then you should remove this line `name = ''` – qvpham Aug 04 '17 at 09:13
  • Ah right. That's `AND` not `OR`. You can use `['|', ['name', 'ilike', name], ['vat', 'ilike', name]]` – qvpham Aug 21 '17 at 08:45
  • Still not working... can u update the answer including the new code... thanks – war10ck6001 Sep 04 '17 at 12:30
  • I am getting a this error... type error unhashable type list... have u tried the code in ur end... – war10ck6001 Sep 04 '17 at 14:30
  • sorry, i missed a `]` in the end of the domain – qvpham Sep 04 '17 at 14:34
  • I already added ] at the end of the domain... but the error is still there – war10ck6001 Sep 04 '17 at 14:39
  • For the second code i used `extend` instead `append`. Did you added it too? – qvpham Sep 04 '17 at 14:44