Odoo , Domain, filter date and month
Solution 1
I find the solution, sale.report is not a real model, it's a view. For add field, you need to overwrite select method. so
class sale_report(osv.osv):
_inherit = 'sale.report'
date_order_month = fields.Char(string='Date Month')
date_order_day = fields.Char(string='Date Day')
def _select(self):
select_str = """
SELECT min(l.id) as id,
l.product_id as product_id,
t.uom_id as product_uom,
sum(l.product_uom_qty / u.factor * u2.factor) as product_uom_qty,
sum(l.product_uom_qty * l.price_unit * (100.0-l.discount) / 100.0) as price_total,
count(*) as nbr,
s.date_order as date,
date_part('month', s.date_order) as date_order_month,
date_part('day', s.date_order) as date_order_day,
s.date_confirm as date_confirm,
s.partner_id as partner_id,
s.user_id as user_id,
s.company_id as company_id,
extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay,
s.state,
t.categ_id as categ_id,
s.pricelist_id as pricelist_id,
s.project_id as analytic_account_id,
s.section_id as section_id
"""
return select_str
Solution 2
Here is example how you can do it. In this example i search for users with write_date (which is datetime) = yesterday.
yesterday = datetime.datetime.now() - datetime.timedelta(days = 2)
yesterday_beginning = datetime.datetime(yesterday.year, yesterday.month, yesterday.day,0,0,0,0)
yb = yesterday_beginning.strftime("%Y-%m-%d %I:%M:%S")
today = datetime.datetime.now()
today_beginning = datetime.datetime(today.year, today.month, today.day,0,0,0,0)
tb = today_beginning.strftime("%Y-%m-%d %I:%M:%S")
self.env['res.users'].search([('write_date','>=',yb),('write_date','<',tb)])
Solution 3
You need to take another fields to store order month and day if you want to filter data as you explain.
'order_month' : fields.char('Month')
'order_day' : fields.char('Day')
'order_year' : fields.char('Year')
Add all these fields in default.
'order_month' : lambda *a: str(time.strftime('%m')),
'order_day' : lambda *a: str(time.strftime('%d')),
'order_year' : lambda *a: str(time.strftime('%Y')),
Now you can directly add these fields in filtration.
Solution 4
Instead of implementing this via Python code, you can do this in xml like:
<filter string="Current Month" domain="[('payment_date','>=',context_today().strftime('%%Y-%%m-01')),('payment_date','<',(context_today()+relativedelta(months=1)).strftime('%%Y-%%m-01'))]"/>
<filter string="Next Month" domain="[('payment_date','>=',(context_today()+relativedelta(months=1)).strftime('%%Y-%%m-01')),('payment_date','<',(context_today()+relativedelta(months=2)).strftime('%%Y-%%m-01'))]"/>
Youri Le Barch
Updated on June 04, 2022Comments
-
Youri Le Barch almost 2 years
I try to create a filter domain in sale.report as : Month(order_date) = month(current_date) AND Day(order_date) <= Day(current_date).
filter string="This Month" domain="[(('date').strftime('%%m'),'=', ((context_today()).strftime('%%m'))),(('date').strftime('%%d'),'>=', ((context_today()).strftime('%%d')))]"/>
I have a problem in left side of domain : ('date'), System say: AttributeError: object has no attribute 'strftime'.
try many combination, but response it's same.
Have you an idea ? What is the type of object 'date' ?
Thanks
-
Youri Le Barch almost 9 yearsI think you are reason : left operand cannot be an object, juste a string. So i try your proposition but i can't add field in model sale_report. Fields are visible in UI in models but not in database Have you an idea ?
class sale_report(osv.osv): _inherit = 'sale.report' date_month = fields.Char(string='Date Month') date_day = fields.Char(string='Date Day')
I have not error message. -
Youri Le Barch almost 9 yearsThis not work. I think it's forbidden to use function in left operand of domain in filter
-
Chiru Constantin - Alexandru almost 9 yearsStore the result of the expression in a new field and you can use it.
-
Emipro Technologies Pvt. Ltd. almost 9 yearsYes on left side of operator there must be a database field name on which you can not apply any function. To update those fields into the database you must update that module.