問題

Odoo系統根據單據更新發送消息通知相關人員。

舉例:
生產訂單下發,庫管人員就會得到需要發貨的消息。

相關的設計模式有:

  • 重寫動作函數.
  • 自動化動作
  • 定制開發,根據單據狀態,發送消息

 

下面就,討論不同模式的實現方法,和利弊。

重寫動作函數

odoo v8 中 確認生產訂單 通過workflow完成,
workflow 調用的確認函數是:

def action_confirm(self, cr, uid, ids, context=None):
        """ Confirms production order.
        @return: Newly generated Shipment Id.
        """
        user_lang = self.pool.get('res.users').browse(cr, uid, [uid]).partner_id.lang
        context = dict(context, lang=user_lang)
        uncompute_ids = filter(lambda x: x, [not x.product_lines and x.id or False for x in self.browse(cr, uid, ids, context=context)])
        self.action_compute(cr, uid, uncompute_ids, context=context)
        for production in self.browse(cr, uid, ids, context=context):
            self._make_production_produce_line(cr, uid, production, context=context)

            stock_moves = []
            for line in production.product_lines:
                if line.product_id.type != 'service':
                    stock_move_id = self._make_production_consume_line(cr, uid, line, context=context)
                    stock_moves.append(stock_move_id)
                else:
                    self._make_service_procurement(cr, uid, line, context=context)
            if stock_moves:
                self.pool.get('stock.move').action_confirm(cr, uid, stock_moves, context=context)
            production.write({'state': 'confirmed'})
        return 0

 

重寫函數, 增加以下代碼實現。

SUPER(mrp_prodduction, self).action_confirm(cr, uid, ids, context=None)
self.message_post(cr, uid, ids, body=_("Order %s confirmed. Please Send Material") % self._description, context=context)

 

修改結果


rewrite_function.png

優點: 簡單直接
缺點: 需要找到代碼函數,重寫函數。

定制,根據狀態變化

此方法需要修改代碼,在單據狀態變化的時候,自動推送消息。

依賴代碼部分
模塊集成

    _inherit = ['mail.thread', 'ir.needaction_mixin']

 

定義狀態, 增加track_visibility屬性

'state': fields.selection(
            [('draft', 'New'), ('cancel', 'Cancelled'), ('confirmed', 'Awaiting Raw Materials'),
                ('ready', 'Ready to Produce'), ('in_production', 'Production Started'), ('done', 'Done')],
            string='Status', readonly=True,
            track_visibility='onchange', copy=False,

 

定義_trace 字段以及參數
ps. mrp.production 中未定義_track, 故狀態更新 不會推送消息通知。

# Automatic logging system if mail installed
    # _track = {
    #   'field': {
    #       'module.subtype_xml': lambda self, cr, uid, obj, context=None: obj[state] == done,
    #       'module.subtype_xml2': lambda self, cr, uid, obj, context=None: obj[state] != done,
    #   },
    #   'field2': {
    #       ...
    #   },
    # }
    # where
    #   :param string field: field name
    #   :param module.subtype_xml: xml_id of a mail.message.subtype (i.e. mail.mt_comment)
    #   :param obj: is a browse_record
    #   :param function lambda: returns whether the tracking should record using this subtype

 

其中 module.subtype_xml 需要在xml中定義消息類型。 例如 account_voucher 的跟蹤消息類型

 <!-- Voucher-related subtypes for messaging / Chatter -->
        <record id="mt_voucher_state_change" model="mail.message.subtype">
            <field name="name">Status Change</field>
            <field name="res_model">account.voucher</field>
            <field name="default" eval="False"/>
            <field name="description">Status changed</field>
        </record>

  

優點:根據狀態或其他字段自動推送消息。
缺點:定義復雜。

自動化動作

創建自動話動作,定義對象和條件


Automatci_action.png

定義動作: 更改負責人 或增加關注者(本例中可以增加倉庫人員)


set_action1.png

或 更復雜動作,用服務器動作定義


create_server_action.png

優點 : 用戶可配置
缺點: server action 需要寫python代碼

總結

以上三種方法,都是使用message_post方法發送消息給關注者,如需使用其他發送消息方法,需要在mail thread尋找新的方法。
方法三,可以自定義配置條件,也可以增加關注者,也可以增加復雜動作,靈活。
方法一,對開發者來說更直接。