問題
系統(tǒng)根據(jù)單據(jù)更新發(fā)送消息通知相關(guān)人員。
舉例:
生產(chǎn)訂單下發(fā)观游,庫管人員就會(huì)得到需要發(fā)貨的消息。
相關(guān)的設(shè)計(jì)模式有:
- 重寫動(dòng)作函數(shù).
- 自動(dòng)化動(dòng)作
- 定制開發(fā)驮俗,根據(jù)單據(jù)狀態(tài)懂缕,發(fā)送消息
相關(guān)主題:odoo提醒
下面就,討論不同模式的實(shí)現(xiàn)方法王凑,和利弊搪柑。
重寫動(dòng)作函數(shù)
odoo v8 中 確認(rèn)生產(chǎn)訂單 通過workflow完成,
workflow 調(diào)用的確認(rèn)函數(shù)是:
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
重寫函數(shù), 增加以下代碼實(shí)現(xiàn)索烹。
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)
修改結(jié)果
優(yōu)點(diǎn): 簡單直接
缺點(diǎn): 需要找到代碼函數(shù)工碾,重寫函數(shù)。
定制百姓,根據(jù)狀態(tài)變化
此方法需要修改代碼渊额,在單據(jù)狀態(tài)變化的時(shí)候,自動(dòng)推送消息。
依賴代碼部分
模塊集成
_inherit = ['mail.thread', 'ir.needaction_mixin']
定義狀態(tài), 增加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 字段以及參數(shù)
ps. mrp.production 中未定義_track, 故狀態(tài)更新 不會(huì)推送消息通知旬迹。
# 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>
優(yōu)點(diǎn):根據(jù)狀態(tài)或其他字段自動(dòng)推送消息。
缺點(diǎn):定義復(fù)雜奔垦。
自動(dòng)化動(dòng)作
創(chuàng)建自動(dòng)話動(dòng)作屹耐,定義對象和條件
定義動(dòng)作: 更改負(fù)責(zé)人 或增加關(guān)注者(本例中可以增加倉庫人員)
或 更復(fù)雜動(dòng)作,用服務(wù)器動(dòng)作定義
優(yōu)點(diǎn) : 用戶可配置
缺點(diǎn): server action 需要寫python代碼
總結(jié)
以上三種方法椿猎,都是使用message_post方法發(fā)送消息給關(guān)注者惶岭,如需使用其他發(fā)送消息方法,需要在mail thread尋找新的方法犯眠。
方法三俗他,可以自定義配置條件,也可以增加關(guān)注者阔逼,也可以增加復(fù)雜動(dòng)作,靈活地沮。
方法一嗜浮,對開發(fā)者來說更直接。