Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
1733 Widoki

i have a piece of code it work fine on local system while taking time on server ( loading and message too much memory consumed ) odoo15 E

The piece of code is below :

class CreateBillFromPicking(models.Model):
_inherit = 'stock.picking'

def button_validate(self):
res = super(CreateBillFromPicking, self).button_validate()
purchase_id = self.env['purchase.order'].search([('name', '=', self.origin)])
for rec in self:
for pi in purchase_id.picking_ids:
if rec.id == pi.id:
if rec.state == 'done':
print(rec.state, 'REC STATE')
purchase_id.action_create_invoice()
invoice_id = self.env['account.move'].search([('purchase_id', '=', purchase_id)])
print(invoice_id, 'Invoice ID')
invoice_id.invoice_date = fields.Date.today()
return res


Thanks advance


Awatar
Odrzuć
Najlepsza odpowiedź

Hi,

Nested loops in the code you provided could cause performance problems, especially if you have a lot of pickings and purchase orders. By removing nested loops and using Odoo's recordset operations, we can optimize the code as below.

from odoo import models, fields, api

class CreateBillFromPicking(models.Model):
    _inherit = 'stock.picking'

    def button_validate(self):
        res = super(CreateBillFromPicking, self).button_validate()
        purchase_orders = self.env['purchase.order'].search([('name', '=', self.origin)])
        done_pickings = self.filtered(lambda p: p.state == 'done')
        related_purchase_orders = purchase_orders.filtered(lambda po: done_pickings in po.picking_ids)
       
        for purchase_order in related_purchase_orders:
            purchase_order.action_create_invoice()
            invoices = self.env['account.move'].search([('purchase_id', '=', purchase_order.id)])
            invoices.write({'invoice_date': fields.Date.today()})
        return res


Hope it helps

Awatar
Odrzuć
Autor Najlepsza odpowiedź

Thanks cybrosys Techno now working smothly


Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
paź 23
1646
2
paź 23
3207
3
gru 24
5256
1
sty 24
1097
0
wrz 23
1408