如何按返回未排序的听写的日期对词典进行排序?

2024-06-28 20:57:28 发布

您现在位置:Python中文网/ 问答频道 /正文

这里的方法返回我想要排序的未排序字典。我还添加了屏幕截图以澄清问题,我不知道如何排序,因此我正在搜索一些帮助来完成此操作。任何帮助都将得到通知

def get_sale_details_product_wise(self, date_start=False, date_stop=False, configs=False):

    if not configs:
        configs = self.env['pos.config'].search([])

    orders = self.env['pos.order'].search([
        ('date_order', '>=', date_start),
        ('date_order', '<=', date_stop),
        ('state', 'in', ['paid', 'invoiced', 'done']),
        ('config_id', 'in', configs.ids)])

    products_sold = {}
    for order in orders:
        jour = ''
        jour_list = []
        for statement in order.statement_ids:
            if statement.journal_id.name not in jour_list:
                jour_list.append(statement.journal_id.name)
                jour += statement.journal_id.name + ','
        for line in order.lines:
            seller_name = self.env["res.partner"].search(
                [('id', '=', line.order_id.partner_id.id)]).name

            if products_sold.get(line.product_id.id):
                products_sold[line.product_id.id] = {
                    'order_date': order.date_order,
                    'product_id': products_sold[line.product_id.id].get('product_id'),
                    'product_name': products_sold[line.product_id.id].get('product_name'),
                    'quantity': products_sold[line.product_id.id].get('quantity', 0) + line.qty,
                    'product_cost': products_sold[line.product_id.id].get('product_cost'),
                    'price_unit': products_sold[line.product_id.id].get('price_unit'),
                    'remaining_qty': line.product_id.qty_available,
                    'seller_name': seller_name,
                    'jour': jour
                }
            else:
                products_sold[line.product_id.id] = {
                    'order_date': order.date_order,
                    'product_id': line.product_id.id,
                    'product_name': line.product_id.name,
                    'quantity': line.qty,
                    'product_cost': line.product_id.standard_price,
                    'price_unit': line.price_unit,
                    'remaining_qty': line.product_id.qty_available,
                    'seller_name': seller_name,
                    'jour': jour
                }

    print("1111111111",{'products': products_sold})
    return {'products': products_sold}


Tags: nameinidgetdatelineorderproduct