I want to fetch sale order name, order_date from sale_order table and product name and price_total from sale_order_line table between two order_date and for a specific product_id.
Here is my code:
def _get_product_report_data(self, date_start, date_end, product_id):
product_data = []
sale_orders = self.env['sale.order'].search(
[('date_order', '>=', date_start),
('date_order', '<=', date_end)])
if sale_orders:
for order in sale_orders:
so_name = order.name
date_order = order.date_order
order_line = self.env['sale.order.line'].search([('order_id', '=', order.id),
('product_id', '=', product_id)])
amount = 0
if order_line:
for line in order_line:
amount += line.price_total
product_data.append({'so_name': so_name, 'order_date': date_order,
'total_amount': amount})
return product_data
How can I make this efficient? How can I grab desired data with only a single query?
def _get_product_report_data(self, date_start, date_end, product_id):
product_data = []
order_line = self.env['sale.order.line'].search([('product_id', '=', product_id),
('order_id.date_order', '>=', date_start),
('order_id.date_order', '<=', date_end)])
Search using this Method and based on the result you have to change the product_data list filling flow.
Thanks @Joby
It works!!