因公司业务需求,想在产生仓库物料调拨的时候,同时产生一笔快递单(因为公司不同的仓库不在同一个地方),调拨单是在计算物料需求时产生的,所以我就想覆写计算物料需求的方法,在执行完原来的业务逻辑以后,再根据内部需求处理后面的逻辑。
开始写的方法如下:
class rhwl_order(osv.osv): _inherit = "procurement.order" def run_scheduler(self, cr, uid, use_new_cursor=False, company_id=False, context=None): super(rhwl_order,self).run_scheduler(cr,uid,use_new_cursor,company_id,context) move_obj = self.pool.get("stock.move") move_ids = move_obj.search(cr,uid,[('state','not in',['done','cancel']),('express_no','=',False)],context=context)
后来跟踪调用发现此方法是系统另外进程来处理的,与当前操作的进程不一样,代码可参考addons\procurement\wizard\schedulers_all.py中的procure_calculation方法。
threaded_calculation = threading.Thread(target=self._procure_calculation_all, args=(cr, uid, ids, context)) threaded_calculation.start()
参考其它方法的说明以后,重新修改代码如下:
class rhwl_order(osv.osv): _inherit = "procurement.order" def run_scheduler(self, cr, uid, use_new_cursor=False, company_id=False, context=None): super(rhwl_order,self).run_scheduler(cr,uid,use_new_cursor,company_id,context) try: if use_new_cursor: cr = openerp.registry(cr.dbname).cursor() move_obj = self.pool.get("stock.move")#('warehouse_id','=',1),('rule_id','>',0), move_ids = move_obj.search(cr,uid,[('state','not in',['done','cancel']),('express_no','=',False)],context=context) #内部业务逻辑处理 if use_new_cursor: cr.commit() finally: if use_new_cursor: try: cr.close() except Exception: pass return {}