Odoo Help

0

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

By
Yenthe
on 5/11/16, 5:27 AM 1,120 views

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 = invoices.search(['&',('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?


Yenthe

1
Juan Vicente Pascual
On 5/11/16, 6:56 AM

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)])


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

Yenthe
on 5/11/16, 9:49 AM
1
Fekete Mihai
On 5/11/16, 9:49 AM

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 conditions...at 4 conditions you should have 3 operators...

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

Yenthe
on 5/11/16, 9:50 AM

Yenthe, here is an old topiuc describing more the search operators, please see https://www.odoo.com/forum/help-1/question/full-list-of-search-operators-46716

Fekete Mihai
on 5/11/16, 11:09 PM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 5/11/16, 5:27 AM
Seen: 1120 times
Last updated: 5/11/16, 9:49 AM