from odoo import models, fields, api, _ from odoo.exceptions import UserError class PurchaeOrderMultiBills(models.TransientModel): _name = 'purchase.order.multi_bills' _description = 'Create bills for Multiple purchases Orders' def queue_job(self): po_ids = self._context.get('po_ids', []) purchase_orders = self.env['purchase.order'].browse(po_ids) for purchase in purchase_orders: purchase.bill_date = fields.Date.today() queue_job = self.env['queue.job'].search([('res_id', '=', purchase.id), ('method_name', '=', 'action_create_invoice'), ('state', '!=', 'error')], limit=1) if queue_job: continue self.env['queue.job'].create({ 'name': f'Create Bills {purchase.name}', 'model_name': 'purchase.order', 'method_name': 'action_create_invoice', 'res_id': purchase.id, }) # def create_bills(self): # # Get SO IDs from context # po_ids = self._context.get('po_ids', []) # if not po_ids: # raise UserError(_("No purchases orders selected!")) # # Browse all selected purchases orders # purchase_orders = self.env['purchase.order'].browse(po_ids) # created_bills = self.env['account.move'] # # Create one invoice per SO (even if partner is the same) # for order in purchase_orders: # # Create invoice for this SO only # invoice = order.with_context(default_invoice_origin=order.name)._create_bills(final=True) # invoice.action_post() # created_bills += invoice # # Link the invoice to the SO # order.invoice_ids += invoice # # Return action to view created bills # if len(created_bills) > 1: # action = { # 'name': _('Created bills'), # 'type': 'ir.actions.act_window', # 'res_model': 'account.move', # 'view_mode': 'tree,form', # 'domain': [('id', 'in', created_bills.ids)], # } # elif created_bills: # action = { # 'name': _('Created Invoice'), # 'type': 'ir.actions.act_window', # 'res_model': 'account.move', # 'view_mode': 'form', # 'res_id': created_bills.id, # } # else: # action = {'type': 'ir.actions.act_window_close'} # return action