【问题标题】:Split Purchase Order according to Product Category根据产品类别拆分采购订单
【发布时间】:2020-01-27 17:42:35
【问题描述】:

我想根据产品类别拆分采购订单。

到目前为止我的代码:

_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'))

到目前为止,代码被拆分为多个 PO,例如如果我有 2 种类型的类别正在制作 2 个 PO,但是这两个 PO 正在接受并且 4 种产品不仅属于产品类别(见下图)。

输出

但我想要这种输出:

有什么办法吗?

【问题讨论】:

    标签: python odoo odoo-12


    【解决方案1】:

    我试图忽略您的代码示例,因为这对我来说很难理解。如果你想试试我的尝试:

    def button_split_by_prod_categ(self):
        self.ensure_one()
        groups = {}
        # group lines by product category
        for line in self.order_line:
            if line.product_id.categ_id not in groups:
                groups[line.product_id.categ_id] = line
            else:
                groups[line.product_id.categ_id] =| line
        skip = True
        orders = self
        for lines in groups.values():
            # skip first group
            if skip:
                skip = False
                continue
            # or create a new order without lines and connect
            # the group's lines with it
            else:
                default_values = {'order_line': []}
                new_order = self.copy(default=default_values)
                lines.write({'order_id': new_order.id})
                orders |= new_order
        # now you could return a list view with all orders
        # or just do 'nothing'
        return 
    

    【讨论】:

    • @OmaL 这个编辑是什么?在 for each 循环中不需要categ
    • orders |= self.copy({'order_line':[(6,0,lines.ids)]})我想这会在复制过程中自动添加行,同时将新订单添加到设置的订单中。
    • 您可以随时使用4[(4, line.id) for line in lines]
    • 是的,这是可能的,但我喜欢我的解决方案:-P
    • 这是一个很好的观点,我认为在这种情况下两种方式都是可能的,但你的方式更好。
    【解决方案2】:

    我找到了我的问题的解决方案,我认为这不是很好,但它确实可以完成工作。感谢@CZoellner 和@Charif DZ 的努力!!!

       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'))
    

    【讨论】:

    • 如果你在一个团队中工作,我认为你应该使用 CZollner 的代码比你的逻辑复杂。
    猜你喜欢
    • 2020-01-04
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2011-08-03
    • 1970-01-01
    相关资源
    最近更新 更多