Correct way to create a domain with in condition and multiple & (AND) conditions?

Hi guys,

I've made some custom code that needs to filter out particular purchase invoices.
The domain has to look at multiple conditions:

  • The invoice has to be a purchase invoice, not a sales invoice (controlled by in_invoice or out_invoice on the type field from the model)

  • The state has to be either open or paid, not concept.

  • The create_date has to be bigger, or equal, than the start_date, which is a custom field I made.

  • The create_date has to be smaller, or equal, than the end_date, which is a custom field I made.

When I write a domain for getting only the purchase invoices that are only in the state open or paid it'll work and it looks like this:

invoices = self.env['account.invoice'].search((['&',('type', 'in', ['in_invoice', 'in_refund']),('state', 'in', ['open', 'paid'])]))

When I write a domain to only get purchase invoices that are created after start_date and that are created before the end_date it will work fine too:

invoices =['&',('create_date', '>=', self.start_date), ('create_date', '<=', self.end_date)])

However, when I now want to combine those two in one statement I get incorrect results. My domain with the combination of both domains merged:

invoices = self.env['account.invoice'].search(['&', '&', ('type', 'in', ['in_invoice', 'in_refund']),('state', 'in', ['open', 'paid']), ('create_date', '>=', self.start_date), ('create_date', '<=', self.end_date)])

However when I do this the domain does not seem to look at the start_date and end_date anymore.
So, what is wrong with this domain? I thought that the polish annotation read from left to right, meaning that my first '&' tells Odoo to both comply with the invoice state AND the dates, but this does not seem to be the case?
Could anybody explain me why this is the case and what I did wrong in this domain?


4 Risposte
Migliore risposta

try this,

invoices = self.env['account.invoice'].search([('type', 'in', ['in_invoice', 'in_refund']),

                                                                           ('state', 'in', ['open', 'paid']),

                                                                           ('create_date', '>=', self.start_date),

                                                                           ('create_date', '<=', self.end_date)])

1 Commento

This works perfectly, thanks Juan! I actually didn't knew I didn't even need the '&' operators.

Migliore risposta

Hi Yenthe, by default Odoo uses AND operator, so if you are not using OR operator, you should not add the operators in front, but if are doing, first operator is for last 2 conditions and so on, here you have 2 operators and 4 4 conditions you should have 3 operators...

2 Commenti

I've converted your comment to an answer since the reason why is also very important. I didn't even know by default '&' was enforced for multiple domains like this. +1

NextERP Romania SRL

Yenthe, here is an old topiuc describing more the search operators, please see