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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
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 queue_job(self):
so_ids = self._context.get('so_ids', [])
sale_orders = self.env['sale.order'].browse(so_ids)
for sale in sale_orders:
queue_job = self.env['queue.job'].search([('res_id', '=', sale.id), ('method_name', '=', 'create_invoices')], limit=1)
if queue_job:
continue
self.env['queue.job'].create({
'name': f'Create Invoice {sale.name}',
'model_name': 'sale.order',
'method_name': 'create_invoices',
'res_id': sale.id,
})
# 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)
# invoice.action_post()
# 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
|