跳至内容
菜单
此问题已终结
3 回复
6466 查看

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 + 2 

it should be just :

[{'name': 'x', 'sold_qty': 12.0},{'name': 'y', 'sold_qty': 9.0}]
how that can be done 

thanks in advance

 
形象
丢弃
最佳答案

Hi @Mohamed Fouad

Try it like this:

    @api.multi
    def product_summary_test(self):
        product_summary_dict = {}
        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:
                        product_summary_dict[each_order_line.product_id.name] = product_summary_dict.get(each_order_line.product_id.name, 0.0) + each_order_line.qty
        data = [{"name": key, "sold_qty": value} for (key, value) in product_summary_dict.items()]
        if data:
            print(len(data))
            print(data)
            return data
        else:
            return {}

Hope it help you

形象
丢弃
最佳答案

Hi Mohamed:

You can use the dict itself to keep a track of the summary information like so. The update and get allow you to handle situations with missing keys in the dict gracefully.

product_summary_dict.update({each_order_line.product_id.name: {
"name": each_order_line.product_id.name,
"sold_qty": product_qty + product_summary_dict.get(each_order_line.product_id.name, {}).get("sold_qty", 0.0)
}})


形象
丢弃
编写者

i think it may be suitable, but can you explain it more or merge it in my code and paste it again

thanks dear

相关帖文 回复 查看 活动
2
8月 19
4104
0
5月 24
530
2
11月 21
32576
1
7月 20
3067
1
10月 18
9577