user7575848
user7575848

Reputation:

Search domain : multiple condition in odoo

How to convert this expression : ((A & B) | ( C & D)) | (( E & F) | (G & H)) in OpenERP domain syntax

PostgreSQL query is :

SELECT
  *
FROM
   calendar_event
WHERE
  (
    (start_datetime <= '2020-01-07 09:00:00' and '2020-01-07 09:00:00' <= stop_datetime) or
    (start_datetime <= '2020-01-07 11:00:00' and '2020-01-07 11:00:00' <= stop_datetime)
  ) or (
    ('2020-01-07 09:00:00' <= start_datetime and start_datetime <= '2020-01-07 11:00:00') or
    ('2020-01-07 09:00:00' <= stop_datetime and stop_datetime <= '2020-01-07 11:00:00')
  )

I try to make like this but is not correct, where i'm wrong please?

   inParams1.push([
         "|",
         "&",
         ["start_datetime", "<",  '2020-01-07 09:00:00'],
         ['2020-01-07 09:00:00', "<=", "stop_datetime"],
         "&",
         ["start_datetime", "<=", '2020-01-07 11:00:00'],
         ['2020-01-07 11:00:00', "<=", "stop_datetime"],

         "|",
         "&",
         ['2020-01-07 09:00:00', "<=", " start_datetime"],
         ["start_datetime", "<=",'2020-01-07 11:00:00'],
         "&",
         ['2020-01-07 09:00:00', "<=",  "stop_datetime"],
     ["stop_datetime", "<=",'2020-01-07 11:00:00']
]);

Please i need your help, i have many days working and i can't find a solution :(

Upvotes: 2

Views: 2539

Answers (2)

Pyae
Pyae

Reputation: 519

I find this answer very helpful for complicated domain. https://stackoverflow.com/a/57853916/8211573

According to that answer, you can use odoo.osv.expression.

In [6]: from odoo.osv.expression import AND,OR                                                                                                                                     

In [7]: a,b,c,d,e,f,g,h = ([("field_" + x, "=", "value_" + x)] for x in "ABCDEFGH")                                                                                                

In [8]: OR([OR([AND([a,b]),AND([c,d])]),OR([AND([e,f]),AND([g,h])])])                                                                                                              
Out[8]: 
['|',
 '|',
 '&',
 ('field_A', '=', 'value_A'),
 ('field_B', '=', 'value_B'),
 '&',
 ('field_C', '=', 'value_C'),
 ('field_D', '=', 'value_D'),
 '|',
 '&',
 ('field_E', '=', 'value_E'),
 ('field_F', '=', 'value_F'),
 '&',
 ('field_G', '=', 'value_G'),
 ('field_H', '=', 'value_H')]

Upvotes: 1

Charif DZ
Charif DZ

Reputation: 14751

You complicated the filter by parentheses:

(
    (start_datetime <= '2020-01-07 09:00:00' and '2020-01-07 09:00:00' <= stop_datetime) or
    (start_datetime <= '2020-01-07 11:00:00' and '2020-01-07 11:00:00' <= stop_datetime)
  ) or (
    ('2020-01-07 09:00:00' <= start_datetime and start_datetime <= '2020-01-07 11:00:00') or
    ('2020-01-07 09:00:00' <= stop_datetime and stop_datetime <= '2020-01-07 11:00:00')
  )

Is the same as this:

    (start_datetime <= '2020-01-07 09:00:00' and '2020-01-07 09:00:00' <= stop_datetime)
     or
    (start_datetime <= '2020-01-07 11:00:00' and '2020-01-07 11:00:00' <= stop_datetime)
    or 
    ('2020-01-07 09:00:00' <= start_datetime and start_datetime <= '2020-01-07 11:00:00') 
    or
    ('2020-01-07 09:00:00' <= stop_datetime and stop_datetime <= '2020-01-07 11:00:00')

So just try this:

    [    '|'
         "&",
         ["start_datetime", "<",  '2020-01-07 09:00:00'],
         ["stop_datetime", ">=", '2020-01-07 09:00:00'],
         '|',
         "&",
         ["start_datetime", "<=", '2020-01-07 11:00:00'],
         ["stop_datetime", ">=", '2020-01-07 11:00:00'],
         '|',
         "&",
         ["start_datetime", ">=", '2020-01-07 09:00:00'],
         ["start_datetime", "<=",'2020-01-07 11:00:00'],
         "&",
         ["stop_datetime", ">=",  '2020-01-07 09:00:00'],
         ["stop_datetime", "<=",'2020-01-07 11:00:00']
     ] 

let me know if it doesn't work for you.

Upvotes: 0

Related Questions