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