Search code examples
pythonodooodoo-12

Split Purchase Order according to Product Category


I wanna split the Purchase Order according to Product Category.

My Code so far:

_inherit ='purchase.order.line'  
split = fields.Boolean(string='Split')


_inherit ='purchase.order'
def btn_split_rfq(self):
            flag = []
            for record in self: 
                if record.order_line:
                    for rec in record.order_line:
                        rec.split = True # oles tis eggrafes true
                        flag.append(rec.product_id.categ_id.id) # lista me ta categ ids
                        newlist=[ii for n,ii in enumerate(flag) if ii not in flag[:n]] # ta krata mono mia fora an uparxoun polles
                    for index in newlist: # gia 2 katigories 8a treksi 2 fores
                        quotation_id = self.copy()
                        for index in record.order_line:
                            if index.split:
                                self.env['purchase.order.line'].browse(index.id).unlink() 
                else:
                    raise ValidationError(_('Please Select Order Line To Split'))

The code so far, is split to multiple POs e.g. if i have 2 type of categories is making 2 POs but and the two POs is taking and the 4 products not only of product category(see image below).

Output:

enter image description here enter image description here enter image description here

But i want this kind of Output:

enter image description here enter image description here enter image description here

Any solution?


Solution

  • I found solution to my problem, i dont think is pretty but it does the job. Thanks @ CZoellner and @Charif DZ for the effort!!!

       def btn_split_rfq(self):
                flag =[]
                for record in self: 
                    if record.order_line:
                       for rec in record.order_line: #run for all products on purchase order
                            flag.append(rec.product_id.categ_id.id) # append product  category ids
                            categ_ids=[ii for n,ii in enumerate(flag) if ii not in flag[:n]] # filter list,keep only one time every product category id
                            categ_ids.sort() # sorting list
                       for index in categ_ids: # will run 2 times if there is 2 product categories
                               quotations_ids = [self.copy()]
                               for order_line in quotations_ids:
                                   prods = self.env['purchase.order.line'].search([('product_categ_id' ,'!=',index),('order_id','=',int(order_line))])
                                   for ids in prods:
                                       self.env['purchase.order.line'].browse(ids.id).unlink() 
                    else:
                         raise ValidationError(_('Not Available Purchase Order Lines'))