按 categ_id 筛选行并计算数量
Filter lines by categ_id and count qty
我的目标是遍历具有相同 categ_id 的所有行并计算它们的总数量 if qty_categ_total < categ_id.qty_for_discount
(我将此字段添加到 'product.category' 而不是我需要 post 文本字段中的消息。问题是我的代码无法正常工作。
示例。
如果我有 2 行具有相同的 categ_id
,数量为 2 和 5,而我的 categ_id.qty_for_discount
是 10。消息应该说我需要再添加 3 个具有相同 [= 的产品13=] 获得折扣
更新
如果有不同类别的产品,我应该会收到每个类别的消息
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
discount_warning_message = fields.Text(string='Discount Warning', compute='discount_warning')
@api.depends('order_line.product_id', 'order_line.product_qty')
def discount_warning(self):
qty_categ_total = 0.0
for line in self.order_line:
qty_categ_total += line.product_qty
if qty_categ_total < line.product_id.categ_id.qty_for_discount:
message = (_("You can get discount if you add %s more %s\n")) % (qty_categ_total, line.product_id.categ_id.name)
self.discount_warning_message = message
您的代码似乎是正确的,但我会更改一些内容。首先要么使用 @api.one
让 odoo 自动循环遍历所有订单,要么为每个循环再添加一个 @api.multi
。其次,不止一个类别呢?
@api.multi
@api.depends('order_line.product_id', 'order_line.product_qty')
def discount_warning(self):
msg = _("You can get discount if you add %s more %s\n")
for order in self:
categ_qtys = {}
for line in order.order_line:
if line.product_id.categ_id not in categ_qtys:
categ_qtys[line.product_id.categ_id] = 0.0
categ_qtys[line.product_id.categ_id] += line.product_qty
msgs = []
for categ, qty in categ_qtys.iteritems():
if qty < categ.qty_for_discount:
msgs.append(msg % (qty, categ.name))
if msgs:
order.discount_warning_message = "".join(msgs)
一般建议:始终尝试调试您的方法。他们甚至被称为?如果不是,方法不是问题。
我的目标是遍历具有相同 categ_id 的所有行并计算它们的总数量 if qty_categ_total < categ_id.qty_for_discount
(我将此字段添加到 'product.category' 而不是我需要 post 文本字段中的消息。问题是我的代码无法正常工作。
示例。
如果我有 2 行具有相同的 categ_id
,数量为 2 和 5,而我的 categ_id.qty_for_discount
是 10。消息应该说我需要再添加 3 个具有相同 [= 的产品13=] 获得折扣
更新
如果有不同类别的产品,我应该会收到每个类别的消息
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
discount_warning_message = fields.Text(string='Discount Warning', compute='discount_warning')
@api.depends('order_line.product_id', 'order_line.product_qty')
def discount_warning(self):
qty_categ_total = 0.0
for line in self.order_line:
qty_categ_total += line.product_qty
if qty_categ_total < line.product_id.categ_id.qty_for_discount:
message = (_("You can get discount if you add %s more %s\n")) % (qty_categ_total, line.product_id.categ_id.name)
self.discount_warning_message = message
您的代码似乎是正确的,但我会更改一些内容。首先要么使用 @api.one
让 odoo 自动循环遍历所有订单,要么为每个循环再添加一个 @api.multi
。其次,不止一个类别呢?
@api.multi
@api.depends('order_line.product_id', 'order_line.product_qty')
def discount_warning(self):
msg = _("You can get discount if you add %s more %s\n")
for order in self:
categ_qtys = {}
for line in order.order_line:
if line.product_id.categ_id not in categ_qtys:
categ_qtys[line.product_id.categ_id] = 0.0
categ_qtys[line.product_id.categ_id] += line.product_qty
msgs = []
for categ, qty in categ_qtys.iteritems():
if qty < categ.qty_for_discount:
msgs.append(msg % (qty, categ.name))
if msgs:
order.discount_warning_message = "".join(msgs)
一般建议:始终尝试调试您的方法。他们甚至被称为?如果不是,方法不是问题。