Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
2 Respostas
1737 Visualizações

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
Cancelar
Melhor resposta

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
Cancelar
Autor Melhor resposta

Thanks cybrosys Techno now working smothly


Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
1
out. 23
1647
2
out. 23
3213
3
dez. 24
5263
1
jan. 24
1100
0
set. 23
1414