Python继承AbstractValidator实现校验器案例原创
金蝶云社区-邱育华
邱育华
6人赞赏了该文章 1300次浏览 未经作者许可,禁止转载编辑于2022年01月17日 15:53:51

【业务背景】

组装拆卸单成品跟子件物料编码相同时,设置相同仓库,批次不能相同才能保存,或者批次相同,但是不允许仓库相同


【需求分析】

从业务背景中分析得出,只有满足以下3种条件的任意一种,才允许保存

1、成品子件:仓库相同,批号不同

2、成品子件:批号相同,仓库不同

3、成品子件:仓库批号均不同


通常的做法,保存的服务端校验一般有两种方式:

1、使用BOS提供的校验规则

2、使用校验器进行校验


【插件实现】

import clr
clr.AddReference('System')
clr.AddReference('Kingdee.BOS')
clr.AddReference('Kingdee.BOS.Core')

from System import *
from Kingdee.BOS.Core import *
from Kingdee.BOS.Core.Validation import *
from Kingdee.BOS.Log import Logger

def OnAddValidators(e):
    validator = AssemblyStockLotExValidator()
    validator.EntityKey = "FBillHead"
    validator.AlwaysValidate = True
    e.Validators.Add(validator)
    
class AssemblyStockLotExValidator(AbstractValidator):
    def Validate(self, dataEntities, validateContext,ctx):
        for bill in dataEntities:
            products = bill["ProductEntity"]
            for product in products:
                productSeq = Convert.ToInt32(product["Seq"])
                subs = product["STK_ASSEMBLYSUBITEM"]
                stock = product["StockID"]
                lot = product["Lot"]
                material = product["MaterialID"]
                for sub in subs:
                    destStock = sub["FStockIDSETY"]
                    destLot = sub["LotSETY"]
                    destMaterial = sub["MaterialIDSETY"]
                    if str(material["Id"]) == str(destMaterial["Id"]): # and Convert.ToString(material["Number"]).StartsWith("03"):
                        if product["Lot_Text"] != '' and sub["LotSETY_Text"] != '' and not( (str(stock["Id"]) == str(destStock["Id"]) and product["Lot_Text"] != sub["LotSETY_Text"]) \
                                                                        or (str(stock["Id"]) != str(destStock["Id"]) and product["Lot_Text"] == sub["LotSETY_Text"]) \
                                                                        or (str(stock["Id"]) != str(destStock["Id"]) and product["Lot_Text"] != sub["LotSETY_Text"]) ):
                    
                            billId = str(bill["Id"])
                            msg = '''组装拆卸单{0}第{1}行物料{2}-{3}仓库或批号不符合保存条件,保存不成功'''.format(bill.BillNo, productSeq, Convert.ToString(material["Number"]), Convert.ToString(material["Name"]))
                            info = ValidationErrorInfo("",billId, bill.DataEntityIndex,productSeq,billId, msg,"表体校验",ErrorLevel.Error)
                            validateContext.AddError(None,info)



image.png


image.png


赞 6