0

Haii frds...

I am not getting how to raise exception when same product is selected in one2many.

In sale order line--> Order_line when i selected one product for example Book&pen

In other time when i select the same product(Book&pen) its raise an exception at the same time. it is possible in when i save the sale order it is raise error. But want it at a time..

S.NO Product Qty UOM

1 [FINAL-0001] GDFB-H-035-XL 1.0000 Unit

2 [FINAL-0001] GDFB-H-035-XL 1.0000 Unit

Please any one help me to solve this issue ...... Thanks in advance...

  • 1
    Write a `onchange` method for `product_id` in the model of `order_line_ids` and check the selected product is exist in `order_line_ids`. If yes raise a warning. – KbiR May 05 '18 at 20:12

1 Answers1

0



you can do it in 2 ways, in sale.order.line model:

  1. using @api.constrains decorator.
  2. overriding write/create functions.

here's an example of a code for each way:

  1. With @api.constrains :
    from openerp.exceptions import ValidationError
        @api.constrains('product_id')
        def constr(self):
            a=0
            for rec in self.order_id.order_line:
                if (rec.product_id.id == self.product_id.id) and (rec.id !=  self.id):
                    a=a+1
            if a > 1:
                raise ValidationError(u"Duplicate lines \nthis line already exist!\ncheck your lines again please!")
  1. With overriding write/create functions:
@api.multi
def write(self,vals):
    if 'product_id' in vals :
        prod = self.env['product.product'].browse([vals.get('product_id')])
    else:
        prod = self.product_id

    for line in self.env['sale.order'].browse([self.order_id.id]).order_line:
        if (prod.id == line.product_id.id) and line.id !=  self.id:
            raise ValidationError(u"Duplicate lines \nthis line already exist!\ncheck your lines again please!")
    return super(sale_order_line,self).write(vals)

@api.model
def create(self,vals):
    for line in self.env['sale.order'].browse([vals['order_id']]).order_line:
        if (vals.get('product_id') == line.product_id.id) and line.id !=  self.id:
            raise ValidationError(u"Duplicate lines \nthis line already exist!\ncheck your lines again please!")
    return super(sale_order_line,self).create(vals)


thank you for your question
Best regards.

Reda Yahla
  • 11
  • 2