服务器动作

简介

服务器动作(Server Action)是OpenErp 5.0以来推出的一项新的功能。这个功能对于满足客户需求非常有用。 它可以为客户常见的一些业务要求提供简单而又快速的配置界面。 比如确认销售订单或发票时发送电子邮件、记录发票的开具和作废、或者在确认订单和发票的时候运行向导。

第一步:服务器动作的定义

下面列举了服务器动作的动作类型:

  • 客户端动作 Client Action
  • 无动作 Dummy
  • 迭代器 Iteration
  • Python 代码
  • 触发器 Trigger
  • 邮件 Email
  • 短信 SMS
  • 创建对象 Create Object
  • 修改对象 Write Object
  • 多个动作 Multi Action

每种类型的动作具有不同的功能和不同的配置参数。我们接下来将看到每种类型的动作, 如何配置,以及影响系统的参数。

客户端动作(Client Action)

这一动作在客户端执行,可以用来执行一个向导或报表。例如,一个客户端动作可以先打印一张发票随后运行付款向导。 从技术上来讲我们能够运行所有客户端执行的动作。包括ir.actions.report.custom, ir.actions.report.xml, ir.actions.act_window, ir.actions.wizard, 和 ir.actions.url. 下面这个例子向我们配置一个客户端动作来确认发票后立即打印.

../../_images/client_action.png

要关注的字段:

Object:动作相关工作流对应的对象
Client Action:选择要执行的动作,这些动作必须是如下种类中的一个:
  • ir.actions.report.custom
  • ir.actions.report.xml
  • ir.actions.act_window
  • ir.actions.wizard
  • ir.actions.url

循环

输入一个python变量作为循环表达式,然后选择要在这个表达式的结果集上遍历执行的服务端动作。 例如,确认一张入库单以后,每个移库行都要记录一下状态的变更。可以把 object.move_lines 作为表达式,然后创建另一个服务器动作来实现记录状态变更的功能。

Python 代码

这个动作类型是执行一个多行的Python代码. 返回的值是 action``变量的值, 缺省值是 ``{}. 仅在你需要弹出特定内容的特定窗体的时候需要返回值,一般来说返回空即可.

记住: 这些代码用Python的 exec 函数来执行. 这个函数有一些内置的变量可用, 如: object, time, cr, uid, ids.

触发器Trigger

任何工作流的迁移都可以用这个来触发。你需要设定的参数是:

Object:动作相关工作流对应的对象.
Workflow on:要触发的工作流对应的对象.
Trigger on:源对象中指向目标对象的字段名.
Trigger Name:是用来触发迁移的信号signal。下拉列表框列出了数据库中所有信号. 注意:这里可选的是所有信号,但实际上你应该选择目标对象对应的才有意义。下拉选项括号内给出的是对象名.

下图演示了如何配置实现自动触发确认发票的动作:

../../_images/trigger_action.png

邮件动作 Email Action

发邮件是所有业务处理的通用需求,比如通过邮件确认销售订单、采购订单、发票、付款、发货等.

不需要自己搭建邮件服务器,你可以使用现有的免费邮件服务账号,比如Gmail、Yahoo等等。

邮件服务配置

启用OpenErp服务时,我们提供下列参数:

--email-from=<sender_email@address>
--smtp=<smtp server name or IP address>
--smtp-port=<smtp server port>
--smtp-user=<smtp user name, if required>
--smtp-password=<smtp user password, if required>
--smtp-ssl=<true if the server requires SSL for sending email, else false>

下面以为发票确认后自动发送一封邮件为例, 来配置一个服务器动作:

../../_images/email_action.png

关注以下字段:

Object:动作相关的工作流相关的对象
Contact:告诉系统我们要使用的收件人地址从对象的哪个字段来
Message:这个邮件模版里可以使用对象的字段作为占位符,在发送邮件时它会被字段值替换。用的格式与我们用来设计rml报表一样, 用[[ object.partner_id.name ]]来表示变量,还可以使用HTML标记来格式化。object就是我们上面指定的对象,可以使用其中的任意字段。

完成上面的服务器动作配置后, 随便你啥时候确认发票的时候,都会自动发送类似下面的Email:

../../_images/email_confirm.png

创建对象

This type of action can be used to emulate the Event history feature currently available on Partners, which logs sales orders issued by a partner, on other objects which do not natively support this feature, such as invoices:

../../_images/create_object.png

Create Object action configuration can be tricky, since it is currently necessary to remember the field names (or to check them out from the source code itself). There are plans to provide an expression builder inside OpenERP in the future, which will be useful to build complex expressions.

重点字段:

Object:the object affected by the workflow on for which we want to run the action
Model:the target model for the object to be created. If empty, it refers to the current object and allows to select the fields from it. It is recommended to provide a model in all cases.
Fields Mapping:Need to provide 3 values:
  1. Destination: any of the fields from the target model
  2. Type: the type of the mapping. Allowed values are value or formula
  3. Value: provide the value or expression the expression. The object refers to the current object.

你必须选择该 对象 的所有必填字段

Record Id:the field in which the id of the new record is stored. This is used to refer to the same object in future operations (see below)

编辑对象

The configuration is very similar to the Create Object actions. The following example writes ‘Additional Information’ on the same object

../../_images/write_object.png

重点字段:

same as the Create Object

多重组合动作

这个动作允许在同一个商务活动中执行多个服务端动作. 例如, 可以打印 发送一封邮件确认发票. 这需要创建三个服务端动作:

  • 打印发票
  • 发票确认邮件
  • 多重动作

这个动作有一点限制的地方: 可以在服务端执行多个动作, 但是只有一个客户端动作. 这也是为什么不能在执行打印报表的同时无法打开 珍上向导的原因.

../../_images/multi_action.png

重点字段:

Object:这个对象受到我们想要执行动作的影响
Other Actions:服务动作清单.任意数量的动作都可以被选择,但是小心上面提到的限制: 如果你选择的客户端动作数量超过一个,那么只有第一个动作会被执行.

第二步:给工作流定义服务端动作

服务端的动作除非在工作流中触发,否则是无用的

工作流可以在: 设置(Administration) ‣ 自定义(Customization) ‣ 工作流(Workflow Definitions) ‣ 工作流(Workflows) 中访问. 打开相应的 工作流,编辑要被触发的服务端动作. 选择要执行的动作.

The following example shows how to associate the Print invoice action to the Open state of the Invoice workflow:

../../_images/link_workflow.png