This question has been flagged
1 Reply
3787 Views

I am trying to duplicate a purchase order line, so I could split up lines that have more than 1 product_qty, to multiple lines of 1 unit.

This is how I tried to duplicate the line:

classPurchaseOrderLine(models.Model):
    _inherit ='purchase.order.line'
    @api.onchange('product_qty')
    defx_onchange_product_qty(self):
        self.copy()

But that only creates an empty line bellow. How do I do this correctly?
Avatar
Discard
Best Answer

hi

Onchange fuction is not a good way, try this , it will help you.


class PurchaseOrderLine(models.Model):
    _inherit = 'purchase.order.line'
    @api.model
    def create(self, vals):
        if vals.get('product_qty') and vals['product_qty'] > 1:
            qty = int(vals['product_qty']) - 1
            print("VVVVV", vals)
            if qty:
                for q in range(qty):
                    vals['product_qty'] = 1
                    self.create(vals)
        res = super(PurchaseOrderLine, self).create(vals)
        return res

    @api.multi
    def write(self, vals):
        if vals.get('product_qty') and vals['product_qty'] > 1:
            qty = int(vals['product_qty']) - 1
            if qty:
                for q in range(qty):
                    vals = {'product_id': self.product_id.id,
                            'product_qty': 1,
                            'name': self.name,
                            'date_planned': self.date_planned,
                            'price_unit': self.price_unit,
                            'product_uom': self.product_uom.id,
                            'order_id': self.order_id.id,
                            }
                    self.create(vals)
        res = super(PurchaseOrderLine, self).write(vals)
        return res

If you have any further questions, please do not hesitate to comment.

Regards,
Nikhilkrishnan

Avatar
Discard