summaryrefslogtreecommitdiff
path: root/fixco_custom/models/purchase_order_multi_bills.py
blob: db6a3d1603bd4b42d2cc32220f11db91cc0429da (plain)
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
63
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