入库类单据可以实时管控到库存是否超出上限(超出最大库存)原创
54人赞赏了该文章
220次浏览
编辑于2024年05月16日 19:53:01
需求:控制库存不能超过设置的最大库存。
实现思路:在单据保存或者审核操作上进行校验,获取即时库存以及本单数量与最大库存进行对比,如果超出了最大库存就不能保存或审核。
Python脚本如下(注册在保存或者审核操作上):
import clr clr.AddReference('System') clr.AddReference('Kingdee.BOS') clr.AddReference('Kingdee.BOS.Core') clr.AddReference('Kingdee.BOS.ServiceHelper') from System import * from System import StringComparison from Kingdee.BOS.Core import * from Kingdee.BOS.Core.Metadata.EntityElement import * from Kingdee.BOS.Core.Validation import * from Kingdee.BOS.Log import Logger from Kingdee.BOS.ServiceHelper import * def OnAddValidators(e): validator = StockMaxQtyValidator() validator.AlwaysValidate = True validator.EntityKey = "FBillHead" e.Validators.Add(validator) class StockMaxQtyValidator(AbstractValidator): def Validate(self, dataEntities, validateContext, ctx): if len(dataEntities) == 0: return for bill in dataEntities: billId = bill["Id"] sqlText = '''select T1.* from (select a.FSTOCKORGID,b.FMATERIALID,b.FNUMBER,c.FMAXSTOCK,SUM(a.FBASEQTY+ts.FBASEUNITQTY) FBASEQTY from T_STK_INVENTORY a join T_BD_MATERIAL b on b.FMASTERID=a.FMATERIALID and b.FUSEORGID=a.FSTOCKORGID join T_BD_MATERIALSTOCK c on c.FMATERIALID=b.FMATERIALID join T_STK_INSTOCK t on t.FSTOCKORGID=a.FSTOCKORGID --对于采购入库单,其他单据需要修改表名 join T_STK_INSTOCKENTRY ts on ts.FID=t.FID and ts.FMATERIALID=b.FMATERIALID --对于采购入库单,其他单据需要修改表名 where c.FISENABLEMAXSTOCK='1' and t.FID={0} group by a.FSTOCKORGID,b.FMATERIALID,b.FNUMBER,c.FMAXSTOCK) T1 where T1.FMAXSTOCK<=T1.FBASEQTY'''.format(billId); dyObjs = DBServiceHelper.ExecuteDynamicObject(this.Context, sqlText) if dyObjs.Count >0: errorMsg = "单据{0}存在物料库存大于最大库存,不允许入库" info = ValidationErrorInfo("",str(bill["Id"]), bill.DataEntityIndex, bill.RowIndex, "最大库存校验", errorMsg.format(str(bill["BillNo"])), str(bill["BillNo"]), ErrorLevel.Error) validateContext.AddError(None,info)
推荐阅读