首页 > 解决方案 > 通过 Odoo 中的销售订单通知购买

问题描述

我们在一家工业工厂工作,采购负责人必须向 CEO 告知任何为销售订单产品采购制造材料的采购。

例如,我们有一个带有 product的销售订单 ,它有自己的,也有和路由。SO00005[856A3779G02] PULLER – PLATFORM, FAN BLADEBoMMTOManufacture

系统创建一个新的PO,负责编辑并处理路由,在确认时,我们必须向 CEO 发送如下消息:

SO00005 adlı [856A3779G02] PULLER – PLATFORM, FAN BLADE projesi için toplam maliyet 100.0₺'dir。

SO00005 adlı [856A3779G02] PULLER – 平台,风扇叶片 şindiye kadarki toplam maliyet 1175.0₺。

第一个表示当前材料价格,第二个表示每个销售订单的总价。

我们该如何处理呢?

标签: pythonpython-3.xodooodoo-11

解决方案


首先,我们将从通知流程开始。我们可以在此处使用自动操作,以使该操作尽可能简单。

自动操作

我们去Settings > Technical > Automation > Automated Actions(不要忘记安装base_automation模块),我们使用以下参数创建一个新操作:

模型purchase.order,我们希望在CONFIRM ORDER单击按钮时通知,将当前的采购订单 状态更改为购买触发条件:在更新时,我们希望在确认采购订单时发送此通知,这意味着当采购订单更改其状态purchase更新之前的域:因此,之前的域将是所有当前草稿,以批准或通过电子邮件发送的采购订单,["|","|",["state","=","draft"],["state","=","sent"],["state","=","to approve"]]. 申请日期:当采购订单的状态更改为时purchase,我们将发送此邮件[["state","=","purchase"]]待办事项: 最后,我们将应用python代码来获取所需的信息并将其发送给老板。

Python代码

我们决定使用python代码,因为在代码中迭代对象更容易;我们的意思是,我们也可以将电子邮件作为待办事项发送,但在这种情况下,我们需要采用purchase.order.line小时模型,这可能会更难。

现在,最简单的方法是向 Odoo 内部的通道发送消息,在系统中保留尽可能多的程序。因此,在创建此操作之前,我们必须在讨论模块中创建一个私人或公共频道。

继续编写代码,我们将获得用于向其发送消息的通道:

channel = env['mail.channel'].search([('name', '=', 'purchases-notify')])
channel.ensure_one()

另外,我们需要CEO经理合伙人:

boss = env['res.partner'].search([('function', '=', 'CEO')])

现在,我们将遍历处理信息的采购订单中的每一;我们这里有选项,默认情况下,在Odoo中跟踪采购和其他费用使用会计模块和流程,这意味着我们必须在采购订单的每一行添加分析账户,如果采购订单有很多不同的产品,这可能很烦人。我们将使用其他方法。

制造订单和销售订单根据采购规则生成采购组采购组具有独特的销售订单来源,对我们来说是双重优势;第一个是我们可以通过这个关系得到产品的名字;这个PG也有SO名字作为它的名字。

默认情况下, Odoo不会按PG组拆分每条采购行,如果产品产品变体uom相同,它只会合并行,而且我们无法知道该行的PG来源;为了解决这个问题,我们必须从OCA安装purchase_line_procurement_group模块。

然后我们有下一个代码:

for line in record.order_line:
  procurement_group = line.procurement_group_id
  product = env['sale.order'].search([('id', '=', procurement_group.sale_id.id)]).order_line[0].name
  sub_total = line.price_subtotal

从现场获取生产线的产品成本price_subtotal

但是我们想获得SO的总成本:我们首先获得与当前行的PG相关的所有采购订单行,然后,我们对它们进行迭代,仅对也确认了PO的行求和:

purchase_order_lines_list = env['purchase.order.line'].search([('procurement_group_id','=',procurement_group.id)])
  total = 0
  for line in purchase_order_lines_list:
    if line.order_id.state == 'purchase':
      total += line.price_subtotal

第二个好处,正如我们在上面的代码中看到的那样:由于每个PG只有一个SO来源,我们不一定要使用procurement_group_id.sale_id.id字段搜索,因为PG ID 将只与一个SO相关联,而不会与其他 SO 相关联。

我们拥有所需的所有信息,然后,我们将为PO中的每一行发送一条新消息:

post_vars = {
    'subject': "Purchase {}".format(record.name),
    'body': '''<p>Mr. <strong>{0}</strong>,</p>
        <p><strong>{1}</strong> adlı <strong>{2}</strong> projesi için toplam maliyet <strong>{3}{4}</strong>'dir.</p>
        <p><strong>{1}</strong> adlı <strong>{2}</strong> şindiye kadarki toplam maliyet <strong>{5}{4}</strong>.</p>'''.format(boss.name, procurement_group.name, product, sub_total, line.currency_id.symbol, total)
  }
  channel.message_post(type="notification", subtype="mt_comment", **post_vars)

我们必须添加货币符号,作为字段存在于行中,即line.currency_id.symbol.

最后,我们的完整代码将是:

channel = env['mail.channel'].search([('name', '=', 'purchases-notify')])
channel.ensure_one()


boss = env['res.partner'].search([('function', '=', 'CEO')])

for line in record.order_line:
  procurement_group = line.procurement_group_id
  product = env['sale.order'].search([('id', '=', procurement_group.sale_id.id)]).order_line[0].name
  sub_total = line.price_subtotal
  purchase_order_lines_list = env['purchase.order.line'].search([('procurement_group_id','=',procurement_group.id)])
  total = 0
  for line in purchase_order_lines_list:
    if line.order_id.state == 'purchase':
      total += line.price_subtotal
  post_vars = {
    'subject': "Purchase {}".format(record.name),
    'body': '''<p>Mr. <strong>{0}</strong>,</p>
        <p><strong>{1}</strong> adlı <strong>{2}</strong> projesi için toplam maliyet <strong>{3}{4}</strong>'dir.</p>
        <p><strong>{1}</strong> adlı <strong>{2}</strong> şindiye kadarki toplam maliyet <strong>{5}{4}</strong>.</p>'''.format(boss.name, procurement_group.name, product, sub_total, line.currency_id.symbol, total)
  }
  channel.message_post(type="notification", subtype="mt_comment", **post_vars)

每次确认PO时,我们都会得到下一个: 在此处输入图像描述


推荐阅读