1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
from odoo import models, fields, api, _
from odoo.exceptions import UserError
class SaleOrderMultiInvoices(models.TransientModel):
_name = 'sale.order.multi_invoices'
_description = 'Create Invoices for Multiple Sales Orders'
def create_invoices(self):
# Get SO IDs from context
so_ids = self._context.get('so_ids', [])
if not so_ids:
raise UserError(_("No sales orders selected!"))
# Browse all selected sales orders
sale_orders = self.env['sale.order'].browse(so_ids)
created_invoices = self.env['account.move']
# Create one invoice per SO (even if partner is the same)
for order in sale_orders:
# Create invoice for this SO only
invoice = order.with_context(default_invoice_origin=order.name)._create_invoices(final=True)
created_invoices += invoice
# Link the invoice to the SO
order.invoice_ids += invoice
# Return action to view created invoices
if len(created_invoices) > 1:
action = {
'name': _('Created Invoices'),
'type': 'ir.actions.act_window',
'res_model': 'account.move',
'view_mode': 'tree,form',
'domain': [('id', 'in', created_invoices.ids)],
}
elif created_invoices:
action = {
'name': _('Created Invoice'),
'type': 'ir.actions.act_window',
'res_model': 'account.move',
'view_mode': 'form',
'res_id': created_invoices.id,
}
else:
action = {'type': 'ir.actions.act_window_close'}
return action
|