When i generate this report from sales its shows the product data from date wise but here this report same category product was sale from another date range its show the value 0. but i want to show only the product contain value. I do not want to show the product which value is 0.
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):
domain = []
date_from = self.date_from
if date_from:
domain += [('create_date', '>=', date_from)]
date_to = self.date_to
if date_to:
domain += [('create_date', '<=', date_to)]
print("Domain.......", domain)
sale_date = self.env['sale.order.line'].search_read(domain)
data = {
'sale_date': sale_date,
'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['product.template'].search([])
sale_date = self.env['sale.order.line'].search([])
categ_list_groupby_dict = {}
date_list_groupby_dict = {}
for categories in self.categ_ids:
filtered_product_list = list(filter(lambda x: x.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,
sale_date))
elif self.date_from:
filtered_by_date = list(filter(lambda x: x.create_date >= self.date_from,
sale_date))
elif self.date_to:
filtered_by_date = list(filter(lambda x: x.create_date <= self.date_to,
sale_date))
print(filtered_by_date)
categ_list_groupby_dict[categories.name] = filtered_by_date and filtered_product_list
final_dist = {}
for categories in categ_list_groupby_dict.keys():
product_data = []
for products in categ_list_groupby_dict[categories]:
if products.sales_count < 0:
# sales = self.sudo().env['sale.order.line'].search([('product_template_id', '=', products.id)])
total_net_sale = 0
total_qty = 0
temp_data = []
temp_data.append(products.name)
for j in filtered_by_date:
if j.product_id.name == products.name:
total_qty = total_qty + j.qty_invoiced
total_net_sale = total_net_sale + j.price_subtotal
temp_data.append(total_qty)
temp_data.append(products.uom_id.name)
temp_data.append(total_net_sale)
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)
# 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):
# domain = []
# date_from = self.date_from
# if date_from:
# domain += [('create_date', '>=', date_from)]
# date_to = self.date_to
# if date_to:
# domain += [('create_date', '<=', date_to)]
# print("Domain.......", domain)
# sale_date = self.env['sale.order.line'].search_read(domain)
#
# data = {
# 'sale_date': sale_date,
# '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['product.template'].search([])
# sale_date = self.env['sale.order.line'].search([])
# categ_list_groupby_dict = {}
# date_list_groupby_dict = {}
# for categories in self.categ_ids:
# filtered_product_list = list(filter(lambda x: x.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,
# sale_date))
#
# elif self.date_from:
# filtered_by_date = list(filter(lambda x: x.create_date >= self.date_from,
# sale_date))
# elif self.date_to:
# filtered_by_date = list(filter(lambda x: x.create_date <= self.date_to,
# sale_date))
# print(filtered_by_date)
# categ_list_groupby_dict[categories.name] = filtered_by_date and filtered_product_list
# final_dist = {}
# for categories in categ_list_groupby_dict.keys():
# product_data = []
# if filtered_by_date:
# for products in categ_list_groupby_dict[categories]:
# if products.sales_count > 0:
# sales = self.sudo().env['sale.order.line'].search([('product_template_id', '=', products.id)])
# total_net_sale = 0
# total_qty = 0
# temp_data = []
# temp_data.append(products.name)
# if products.name == filtered_by_date:
# for j in filtered_by_date:
# total_qty = total_qty + j.qty_invoiced
# total_net_sale = total_net_sale + j.price_subtotal
# temp_data.append(total_qty)
# temp_data.append(products.uom_id.name)
# temp_data.append(total_net_sale)
# 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)
#
#
# class SaleOrderLine(models.Model):
# _inherit = 'sale.order.line'
#
# sales_count = fields.Float('sales_count')