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

45 views Asked by At

How can i adjust the following code so dat odoo will combine the sales orders from the same customer to one invoice like wen you do it from the tree view in 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'})

The above code is a server action on the model stock.picking. It works fine but makes individual invoices of each delivery / sale order. If possible i would also like to call a custom serveraction with id 1183 to run on last_invoice

2

There are 2 answers

0
CZoellner On BEST ANSWER

You have to gather all orders first and call action_invoice_create on them. Odoo will group them in the process. It is very similar to using the list functionality.

For example:

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 On
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})