python - 根据产品类别拆分采购订单
问题描述
我想根据产品类别拆分采购订单。
到目前为止我的代码:
_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 种产品不仅属于产品类别(见下图)。
输出:
但我想要这种输出:
有什么解决办法吗?
解决方案
我试图忽略您的代码示例,因为这对我来说很难理解。如果你想试试我的尝试:
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
推荐阅读
- dynamics-crm - 在自定义网站中包含的 iframe 中托管 CRM Dynamics,自定义网站充当父级
- ios - React Native - 从 Expo 迁移到另一种启动应用程序的方式 - 为 andorid 和 ios 设置模拟器
- ruby-on-rails - RailsEventStore 中的持久关系投影
- mobile - NoSuchMethodError (NoSuchMethodError: Class '_InternalLinkedHashMap
' 没有具有匹配参数的实例方法 'cast' - python - 对列使用 groupby 后计算重复值的实例
- java - 字节数组在替换字符串变量时给出不同的输出
- css - 如果变量存在,SASS @each 跳过特定行
- python - 如何使用 numpy.mean 在特定维度上进行平均?
- excel - 文档文件夹中的“另存为”对话框默认设置
- wpf - 得到奇怪的边界