Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
2 Besvarelser
1826 Visninger

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


Avatar
Kassér
Bedste svar

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

Avatar
Kassér
Forfatter Bedste svar

Thanks cybrosys Techno now working smothly


Avatar
Kassér
Related Posts Besvarelser Visninger Aktivitet
1
okt. 23
1737
2
okt. 23
3305
3
dec. 24
5447
1
jan. 24
1187
0
sep. 23
1492