金蝶单据表单插件用python从临时单据体数据添加到子单据体

业务场景:

    从一个临时单据体“F_cc_Entity”,根据单据体“FEntity”的复选框“F_cc_CheckBox”勾选状态,循环添加到勾选行的子单据体“F_cc_xsm_entry_cf”。

其中有个位置至今还没搞懂为什么:

    在添加对基础资料字段赋值时候,为什么不能用newobject[“FBW”]=cfentityRow[“FBW2”],这样会造成保存时候实际上没保存到;不知道是不是因为内存地址都同时指向了临时单据体的DynamicObject原因造成的,但是SetValue又不会??

代码冗余比较多,听说python写的代码可以短点,如果有大神可以帮忙优化一下的话,请指点下小弟,感激!

import clrclr.AddReference(‘Kingdee.BOS.Core’)clr.AddReference(‘Kingdee.BOS.DataEntity’)from Kingdee.BOS.Core import *import Kingdee.BOS.Orm.DataEntity as dedef ButtonClick(e):#获取临时录入的成分数据cfentity=this.View.BusinessInfo.GetEntity(“F_cc_Entity”)#获取单据体entity=this.View.BusinessInfo.GetEntity(“FEntity”)#获取子单据体,此单据体是一个集合,根据每个集合的属性去找关联单据体subentity=this.View.BusinessInfo.GetEntity(“F_cc_xsm_entry_cf”)if e.Key.ToString()==”FBATHADD”:AddSubEntity(entity,subentity,cfentity)if e.Key.ToString()==”FBATHDEL”:DelSubEntity(entity,subentity,cfentity)#最后updateview刷新界面this.View.UpdateView(“F_cc_xsm_entry_cf”)#批量添加def AddSubEntity(entity,subentity,cfentity):#获取临时单据体数据航,DynamicObjectCollectioncfentityRows=cfentity.DynamicProperty.GetValue(this.Model.DataObject)#获取单据体数据航,DynamicObjectCollectionentityRows=entity.DynamicProperty.GetValue(this.Model.DataObject)if entityRows:#循环单据体for entityRow in entityRows:#是否勾选if entityRow[“F_cc_CheckBox”]==True:#根据单据体行获取关联子单据体集合subEntityRows = subentity.DynamicProperty.GetValue(entityRow)if cfentityRows:#循环临时单据体for cfentityRow in cfentityRows:#判断是否存在isExists=False#循环子单据体for subEntityRow in subEntityRows:if subEntityRow:if cfentityRow[“FBW2”] and subEntityRow[“FBW”] and cfentityRow[“FCF2”] and subEntityRow[“FCF”]:#组合条件,不存在则添加if cfentityRow[“FBW2”][“Number”]==subEntityRow[“FBW”][“Number”] and cfentityRow[“FCF2”][“Number”]==subEntityRow[“FCF”][“Number”]:isExists=Truebreakif not isExists:

                            #创建子单据体行结构newobject=de.DynamicObject(subEntityRows.DynamicCollectionItemPropertyType)this.View.Model.SetValue(this.View.BusinessInfo.GetField(“FBW”),newobject,cfentityRow[“FBW2”])this.View.Model.SetValue(this.View.BusinessInfo.GetField(“FVALUE”),newobject,cfentityRow[“FVALUE2”])this.View.Model.SetValue(this.View.BusinessInfo.GetField(“FCF”),newobject,cfentityRow[“FCF2”])subEntityRows.Add(newobject)#批量删除def DelSubEntity(entity,subentity,cfentity):#获取临时单据体数据航,DynamicObjectCollectioncfentityRows=cfentity.DynamicProperty.GetValue(this.Model.DataObject)#获取单据体数据航,DynamicObjectCollectionentityRows=entity.DynamicProperty.GetValue(this.Model.DataObject)if entityRows:#循环单据体for entityRow in entityRows:#是否勾选if entityRow[“F_cc_CheckBox”]==True:#根据单据体行获取关联子单据体集合subEntityRows = subentity.DynamicProperty.GetValue(entityRow)if cfentityRows:#循环临时单据体for cfentityRow in cfentityRows:removeList=[]#循环子单据体for subEntityRow in subEntityRows:if subEntityRow:if cfentityRow[“FBW2”] and subEntityRow[“FBW”] and cfentityRow[“FCF2”] and subEntityRow[“FCF”]:#组合条件,把要删除的数据添加进removeListif cfentityRow[“FBW2”][“Number”]==subEntityRow[“FBW”][“Number”] and cfentityRow[“FCF2”][“Number”]==subEntityRow[“FCF”][“Number”]:removeList.append(subEntityRow)#删除数据if removeList:for removedata in removeList:subEntityRows.Remove(removedata)