将销售订单合并到一张发票,从服务器对 stock.picking 的操作

Combine salesorders to one invoice from server action on stock.picking

提问人:Pelingier 提问时间:11/11/2023 更新时间:11/14/2023 访问量:21

问:

我怎样才能调整以下代码,以便dat odoo将来自同一客户的销售订单合并到一张发票温就像您在sales.orders的树视图中所做的那样。

for record in records:
    if record.picking_type_code == 'outgoing':
        sale_order = record.sale_id
        if sale_order:
            sale_order.action_invoice_create()
            invoices = sale_order.invoice_ids
            if invoices:
                last_invoice = invoices.sorted(key=lambda i: i.id, reverse=True)[0]
                
                last_invoice.action_invoice_open()
                error_message='FACTUUR OKE: ('+ str(last_invoice.number) +')'
                record.write({'x_error': error_message})
                
            else:
                record.write({'x_error': 'No invoices found for the sale order'})
        else:
            record.write({'x_error': 'No sale order associated with the record'})

上面的代码是模型 stock.picking 上的服务器操作。它工作正常,但为每个交付/销售订单开具单独的发票。如果可能的话,我还想调用一个 id 为 1183 的自定义服务器操作以在 last_invoice 上运行

odoo-10 服务器操作

评论


答:

0赞 CZoellner 11/13/2023 #1

您必须首先收集所有订单并调用它们。Odoo将在此过程中对它们进行分组。这与使用列表功能非常相似。action_invoice_create

例如:

orders = self.env["sale.order"]
for picking in records:
    if picking.picking_type_code == "outgoing":
        orders |= picking.sale_id
orders.action_invoice_create()

for order in orders:
    # do you error stuff here
0赞 Pelingier 11/14/2023 #2
sale_ids = []
for picking in records:
    if picking.sale_id:
        sale_ids.append(picking.sale_id.id)

if sale_ids:
    # Create Invoices
    invoices = env['sale.order'].browse(sale_ids).action_invoice_create()

    # Confirm Invoices
    if invoices:
        env['account.invoice'].browse(invoices).action_invoice_open()

        # Update x_error in stock.picking
        for picking in records:
            if picking.sale_id and picking.sale_id.id in sale_ids:
                last_invoice = picking.sale_id.invoice_ids.sorted(key=lambda i: i.id, reverse=True)[0]
                error_message='FACTUUR OKE: ('+ str(last_invoice.number) +')'
                record.write({'x_error': error_message})