This question has been flagged
4 Replies
21600 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

Avatar
Discard
Best Answer

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


Avatar
Discard
Author

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

Best Answer

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...

Avatar
Discard
Author

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, 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