这个功能主要用于销售订单的反流程,比如采购、销售审核了订单,但这时你要申请修改,
修改的详细就要记录,提交去审核,采购、销售经理才知道你改了什么地方。
难点:
1. 你设置了一个字段记录销售单申请前核心字段的信息,用什么方式存
2. 当销售员在改单后,保存时,同时要对比改动了什么地方做记录,这里人强制刷缓存
否则在删除了销售明细时,你做记录时就报错,One of the documents you are trying to access has been deleted … 这是缓存引起的,若不记录,修改保存记录时,系统会自动刷新,可是保存的同时,你还要做记录修改详细,此时一个动作还没有完成,就无法自动刷新缓存
效果图:
基于反流程比较复杂,这里只讲如何保存修改记录
设置字段
class SaleOrder(models.Model):
_inherit = 'sale.order'
apply_modify_old = fields.Text(string='Apply Modify Old', readonly=True)
apply_modify_detail = fields.Text(string='Apply Modify Detail',readonly=True)
写提出申请时记录初始记录
@api.one
def action_apply_modify_cancel(self):
self.is_apply_modify = True
if self.apply_type =='modify' and self.apply_is_refused == False:
apply_modify_old ={
'name':self.name,
'partner_id': self.partner_id.id,
'partner_shipping_id':self.partner_shipping_id.id,
'date_order':self.date_order,
'pricelist_id':self.pricelist_id.id,
'order_policy':self.order_policy,
'picking_policy': self.picking_policy,
}
if self.order_line:
line_dict={}
for line in self.order_line:
line_dict[line.id] = {
'product_id': line.product_id.id,
'price_unit':line.price_unit,
'product_uom':line.product_uom.id,
'product_uom_qty':line.product_uom_qty,
'lot_id':line.lot_id.id,
'unlimited_dis':line.unlimited_dis,
'special_dis':line.special_dis,
'th_weight':line.th_weight
}
apply_modify_old['order_line'] = line_dict
self.apply_modify_old =json.dumps(apply_modify_old)
我这里把字典转为json放在数据中
下面给出销售员修改订单时方法记录:
def update_apply_modify_detail(self):
values = {}
apply_modify_detail = ''
order_policy_zh={'prepaid':u'基于发货前','manual':u'基于销售订单'}
picking_policy_zh = {'one': u'整批发货', 'direct': u'分批发货'}
old= self.apply_modify_old and json.loads(self.apply_modify_old) or None
if old:
if old.get('name') and old.get('name') !=self.name:
apply_modify_detail += '销售订单:' + old.get('name') + ' -> ' + self.name + ' '
if old.get('partner_id') and old.get('partner_id') !=self.partner_id.id:
apply_modify_detail += '客户:' + self.env['res.partner'].browse(
old.get('partner_id')).name_get()[0][1] + ' -> ' + self.partner_id.name_get()[0][1]+ ' '
if old.get('partner_shipping_id') and old.get('partner_shipping_id') !=self.partner_shipping_id.id:
apply_modify_detail += '发货地址:' + self.env['res.partner'].browse(
old.get('partner_shipping_id')).name_get()[0][1] + ' -> ' + self.partner_shipping_id.name_get()[0][1]+ ' '
if old.get('date_order') and old.get('date_order') !=self.date_order:
apply_modify_detail += '订单日期:' + old.get('date_order')+ ' -> ' + self.date_order + ' '
if old.get('pricelist_id') and old.get('pricelist_id') !=self.pricelist_id.id:
apply_modify_detail += '价格表:' + self.env['product.pricelist'].browse(
old.get('pricelist_id')).name_get()[0][1] + ' -> ' + self.pricelist_id.name_get()[0][1] + ' '
if old.get('order_policy') and old.get('order_policy') !=self.order_policy:
apply_modify_detail += '开票策略:' + order_policy_zh.get(
old.get('order_policy'))+ ' -> ' + order_policy_zh.get(self.order_policy) + ' '
if old.get('picking_policy') and old.get('picking_policy') !=self.picking_policy:
apply_modify_detail += '发货策略:' + picking_policy_zh.get(
old.get('picking_policy'))+ ' -> ' + picking_policy_zh.get(self.picking_policy) + ' '
order_line_old = old.get('order_line') and old.get('order_line') or {}
line_modify = ''
if self.order_line:
for line in self.order_line:
if order_line_old.get(str(line.id)):
modify=False
old_line = order_line_old.get(str(line.id))
if line.product_id.id != old_line.get('product_id'):
line_modify += "修改明细:" +str(line.id) +'[' + line.product_id.name_get()[0][1] + ']:'
line_modify += " | 产品:"+ self.env['product.product'].browse(old_line.get('product_id')).name_get()[0][1]+' -> '+line.product_id.name_get()[0][1]
modify = True
if line.price_unit != old_line.get('price_unit'):
if not modify:
line_modify += "修改明细:" + str(line.id) + '[' + line.product_id.name_get()[0][1] + ']:'
line_modify += " | 单价:" + str(old_line.get('price_unit')) + ' -> ' + str(line.price_unit)
modify = True
if line.product_uom.id != old_line.get('product_uom'):
if not modify:
line_modify += "修改明细:" + str(line.id) + '[' + line.product_id.name_get()[0][1] + ']:'
line_modify += " | 计量单位:" + self.env['product.uom'].browse(old_line.get(
'product_uom')).name_get()[0][1] + ' -> ' + line.product_uom.name_get()[0][1]
modify = True
if line.product_uom_qty != old_line.get('product_uom_qty'):
if not modify:
line_modify += "修改明细:" + str(line.id) + '[' + line.product_id.name_get()[0][1] + ']:'
line_modify += " | 数量:" + str(old_line.get('product_uom_qty')) + ' -> ' + str(line.product_uom_qty)
modify = True
if line.lot_id.id != old_line.get('lot_id'):
if not modify:
line_modify += "修改明细:" + str(line.id) + '[' + line.product_id.name_get()[0][1] + ']:'
line_modify += " | 序列号:" + self.env['stock.production.lot'].browse(old_line.get(
'lot_id')).name_get()[0][1] + ' -> ' + line.lot_id.name_get()[0][1]
modify = True
if line.unlimited_dis != old_line.get('unlimited_dis'):
if not modify:
line_modify += "修改明细:" + str(line.id) + '[' + line.product_id.name_get()[0][1] + ']:'
line_modify += " | 突破限价:" + str(old_line.get('unlimited_dis')) + ' -> ' + str(line.unlimited_dis)
modify = True
if line.special_dis != old_line.get('special_dis'):
if not modify:
line_modify += "修改明细:" + str(line.id) + '[' + line.product_id.name_get()[0][1] + ']:'
line_modify += " | 特殊折扣:" + str(old_line.get('special_dis')) + ' -> ' + str(line.special_dis)
modify = True
if line.th_weight != old_line.get('th_weight'):
if not modify:
line_modify += "修改明细:" + str(line.id) + '[' + line.product_id.name_get()[0][1] + ']:'
line_modify += " | 重量:" + str(old_line.get('th_weight')) + ' -> ' + str(line.th_weight)
if modify:
line_modify += ' '
order_line_old.pop(str(line.id))
else:
line_modify += "增加明细:" + str(line.id) + '[' + line.product_id.name_get()[0][1] + ']:' +
" 单价:"+str(line.price_unit)+" 数量:" +str(line.product_uom_qty)+' '
if order_line_old:
for key,val in order_line_old.items():
product=self.env['product.product'].browse(val.get('product_id'))
line_modify += "删除明细:" + str(key)+'[' + product.name_get()[0][1] +']:'+ " 单价:"+str(val.get('price_unit'))+" 数量:" +str(val.get('product_uom_qty'))+' '
if line_modify:
apply_modify_detail += line_modify
values['apply_modify_detail_flag']=1
values['apply_modify_detail'] = apply_modify_detail
if values:
self.write(values)
@api.one
def write(self, vals):
if vals.get('apply_modify_detail_flag') ==1:
apply_modify_detail_flag=1
vals.pop('apply_modify_detail_flag')
else:
apply_modify_detail_flag=0
res = super(SaleOrder, self).write(vals)
self.refresh()
if not apply_modify_detail_flag and self.is_apply_modify and self.apply_type == 'modify':
self.update_apply_modify_detail()
return res
重点要说明
1. self.refresh() 这个一定要加,否则删除销售订单明细,做记录报错,但你增加或修改时不报错,会让你摸不着头脑。它的功能是刷出缓存。让res = super(SaleOrder, self).write(vals) 这一句彻底完成,否则要等 write 方法全部完成,才完成,
2. res = super(SaleOrder, self).write(vals) 前面的那几句判断也很重要,若不设定,会让write方法进入死循环。