0

How can I merge sale order line on a sale order in Odoo programmatically? I have duplicated products in sale order line, I want to remove the duplicated lines but merge the quantity. Thank you

Johan Pham
  • 23
  • 3
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 16 '22 at 07:16
  • The best way would be to deal with it at the origin: on sale order line creation : update quantity of an existing line having the same product instead of creating a new sale order line – sylvain May 18 '22 at 05:58

1 Answers1

0

The best way would be to deal with it at the origin: on sale order line creation : update quantity of an existing line having the same product instead of creating a new sale order line:

 class SaleOrderLine(models.Model):
    _inherit= 'sale.order.line'

    @api.model_create_multi
    def create(self, vals_list):
      vals_list_newproduct=[]
      for values in vals_list:
         # if one order line contains a product already existing in this order:
         existing_product_soline = self.search(
         [('order_id','=',values['order_id'],('product_id','=',values['product_id'])])
          if existing_product_soline:
             existing_product_soline[0].write({
                 'product_uom_qty': 
                 float(existing_product_soline[0]['product_uom_qty']) + float(values['product_uom_qty'])
             })
          else:
            #this order line contains a new product for the sale order
            vals_list_newproduct.append(values)

      
      #create one new order_line as usual for lines containing new products
      super(SaleOrderLine, self).create(vals_list_newproduct)
sylvain
  • 853
  • 1
  • 7
  • 20