Skip to Content
Menu
This question has been flagged
1 Reply
1240 Views
from odoo import api, fields, models
from odoo.exceptions import UserError


class ProductCategoryReport(models.TransientModel):
_name = 'item.sales.wizard'
_description = "Print Item Wise Sales Report"

date_from = fields.Datetime('From', required=False)
date_to = fields.Datetime('To', default=lambda self: fields.datetime.now(), required=False)
categ_ids = fields.Many2many('product.category', string="Category", required=True)

def action_excel_report(self):
data = {
'date_from': self.date_from,
'date_to': self.date_to,
'categ_ids': self.categ_ids.ids
}

return self.env.ref('item_wise_sales_report.action_item_sales_report_xlsx').report_action(self,
data=data)

def action_report(self):
product_list = self.env['sale.order.line'].search([], order='product_id,create_date asc')
categ_list_groupby_dict = {}
for categories in self.categ_ids:
filtered_product_list = list(filter(lambda x: x.product_id.categ_id == categories, product_list))
if self.date_from and self.date_to:
filtered_by_date = list(
filter(lambda x: x.create_date >= self.date_from and x.create_date <= self.date_to,
filtered_product_list))
elif self.date_from:
filtered_by_date = list(filter(lambda x: x.create_date >= self.date_from,
filtered_product_list))
elif self.date_to:
filtered_by_date = list(filter(lambda x: x.create_date <= self.date_to,
filtered_product_list))
categ_list_groupby_dict[categories.name] = filtered_by_date
final_dist = {}
for categories in categ_list_groupby_dict.keys():
product_data = []
for products in categ_list_groupby_dict[categories]:
temp_data = []
temp_data.append(products.product_template_id.name)
temp_data.append(products.qty_invoiced)
temp_data.append(products.product_uom.name)
temp_data.append(products.price_subtotal)
product_data.append(temp_data)
final_dist[categories] = product_data
datas = {
'ids': self,
'model': 'product.category.wizard',
'form': final_dist,
'date_from': self.date_from,
'date_to': self.date_to,

}
return self.env.ref('item_wise_sales_report.action_item_sales_report').report_action([], data=datas)

Here sale order line how can i merge same product. here this report same product, quantity and price_subtotal are showing many time. But I want to show same product only one time How can I do that. Please help me. 

Avatar
Discard

Have you tried using set() instead of list() ?

Author

No brother

Author

Nothing change same result are shown

Best Answer

Just replace below code with last code:

final_dist = {}
for categories in categ_list_groupby_dict.keys():
product_data = []
for products in categ_list_groupby_dict[categories]:
temp_data = []
temp_data.append(products.product_template_id.name)
temp_data.append(products.qty_invoiced)
temp_data.append(products.product_uom.name)
temp_data.append(products.price_subtotal)
product_data.append(temp_data)
final_dist[categories] = product_data

Replace below code

final_dist = {}
for categories in categ_list_groupby_dict.keys():
product_data = {}
for products in categ_list_groupby_dict[categories]:
old_qty_invoiced = 0.0
old_subtotal = 0.0
if product_data.get(products.product_template_id.id):
old_qty_invoiced = product_data.get(products.product_template_id.id)[1]
old_subtotal = product_data.get(products.product_template_id.id)[3]

temp_data = []
temp_data.append(products.product_template_id.name)
temp_data.append(products.qty_invoiced + old_qty_invoiced)
temp_data.append(products.product_uom.name)
temp_data.append(products.price_subtotal + old_subtotal)
product_data[products.product_template_id.id] = temp_data
final_dist[categories] = product_data

in above code I have replace product_data =[] to product_data = {}, manage data by dictionary instead of list.

Thank you

Avatar
Discard
Author

Brother the data are showing wrongly correct data are not showing and the product name is not showing. The product name are showing like this 1