the dictionary on itself it has unique keys, but if I'm appending the dictionaries into a list, every dictionary is being separated with its unique values and it may be duplicated keys, every key in the separate dictionary into the list,
in my case I'm working product summary report for POS orders, I make a method that loop over all pos order lines and retrieve data from there, the final result should be unique product name with the sum of sold quantities to this product overall orders, I did it, but the output returns non-unique products and the length of the list is the summation of the order lines, I need to remove all duplicated product and set only the last one which has the maximum quantity, here is my code
@api.multi
def product_summary_test(self):
product_summary_dict = {}
data = []
if self.date_from and self.date_to:
order_detail = self.env['pos.order'].search([('date_order', '>=', self.date_from),
('date_order', '<=', self.date_to)])
if order_detail:
for each_order in order_detail:
for each_order_line in each_order.lines:
if each_order_line.product_id.name in product_summary_dict:
product_qty = product_summary_dict[each_order_line.product_id.name]
product_qty += each_order_line.qty
res1 = {
"name": each_order_line.product_id.name,
"sold_qty": product_qty,
}
data.append(res1)
else:
product_qty = each_order_line.qty
res2 = {
"name": each_order_line.product_id.name,
"sold_qty": product_qty,
}
data.append(res2)
product_summary_dict[each_order_line.product_id.name] = product_qty;
if data:
print(len(data))
print(data)
return data
else:
return {}
the output be like
[{'name': 'x', 'sold_qty': 2.0}, {'name': 'x', 'sold_qty': 8.0},{'name': 'x', 'sold_qty': 12.0}, {'name': 'y', 'sold_qty': 5.0}, {'name': 'y', 'sold_qty': 7.0, {'name': 'y', 'sold_qty': 9.0}]
its overwrite the same product and add the new quantity in sold_qty 2 + 6 + 4 as in x
it should be just :
[{'name': 'x', 'sold_qty': 12.0},{'name': 'y', 'sold_qty': 9.0}]
how that can be done ?!
thanks in advance