在控制方法(1)中(链接 https://vip.kingdee.com/article/484712447190676992?productLineId=1 )给出了委外倒冲控制的解决思路,并给出了存储过程SQL代码,以下是委外倒冲校验涉及的服务插件完整代码,其实包含了两个方案:(1)可以直接在审核按钮的执行前事件直接做判断 (2)在校验器中做判断。两种方案处理代码全部都在下面:
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Validation;
using Kingdee.BOS.Util;
using Kingdee.BOS.App.Data;
using Kingdee.BOS;
using System.ComponentModel;
using System.Collections.Generic;
using System.Data;
using System;
namespace WFHH.K3CLOUD.SCM.PUR.AuditService.Plugin
{
/// <summary>
/// 【服务插件】BeforeExecuteOperationTransaction
/// </summary>
[Description("【服务插件,委外入库审核校验能否倒冲】"), HotUpdate]
public class AuditServicePlugIn : AbstractOperationServicePlugIn
{
// *****方法一、不采用校验器,直接在审核前判断,如果倒冲有问题,则取消审核*****//
/*
public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
{
base.BeforeExecuteOperationTransaction(e);
string billNo = "";
if (e.SelectedRows != null)
{
billNo = string.Join(",", e.SelectedRows.Select(o => o.DataEntity["BillNo"]));
IEnumerator<string> bns = e.SelectedRows.Select(o => o.DataEntity["BillNo"].ToString()).GetEnumerator();
while (bns.MoveNext())
{
string bn = bns.Current;
List<SqlParam> sqlParams = new List<SqlParam>();
sqlParams.Add(new SqlParam("@billNo", KDDbType.String, bn));
sqlParams.Add(new SqlParam("@result", KDDbType.Double, 0) { Direction = ParameterDirection.Output });
List<SqlParam> sp = DBUtils.ExecuteStoreProcedure(this.Context, "DEMO_OUTSOURCING_VERIFY", sqlParams);
if ((double)sp[0].Value > 0)
{
e.CancelMessage = string.Format("库存充足,可以倒冲。比对结果={0},继续...", sp[0].Value.ToString());
e.Cancel = false;
}
else
{
e.CancelMessage = string.Format("库存不足,无法完成倒冲。比对结果={0}...请检查委外仓即时库存,编码、批号、数量是否匹配!", sp[0].Value.ToString());
e.Cancel = true;
}
}
}
}
*/
// ***方法二、采用校验器模式(目前采用此方案)****//
public override void OnAddValidators(AddValidatorsEventArgs e)
{
base.OnAddValidators(e);
var wwdcValidator = new WwdcValidator();
e.Validators.Add(wwdcValidator);
}
#region 自定义校验器
/// <summary>
/// 委外倒冲校验器
/// </summary>
public class WwdcValidator : AbstractValidator
{
public override void InitializeConfiguration(ValidateContext validateContext, Context ctx)
{
base.InitializeConfiguration(validateContext, ctx);
if (validateContext.BusinessInfo != null)
{
EntityKey = validateContext.BusinessInfo.GetEntity(0).Key;
}
}
public override void Validate(ExtendedDataEntity[] dataEntities, ValidateContext validateContext, Context ctx)
{
if (validateContext.IgnoreWarning)
{
return; // 警告已经被用户忽略,就不需要再次执行了
}
if (dataEntities == null || dataEntities.Length <= 0)
{
return;
}
// 循环校验每一个数据包(一个数据包对应一张单据)
foreach (var et in dataEntities)
{
bool hasError = false;
// 进行数据校验
List<SqlParam> sqlParams = new List<SqlParam>();
sqlParams.Add(new SqlParam("@billNo", KDDbType.String, et.BillNo));
sqlParams.Add(new SqlParam("@result", KDDbType.Double, 0) { Direction = ParameterDirection.Output });
sqlParams.Add(new SqlParam("@FNumber", KDDbType.String,"") { Direction = ParameterDirection.Output,Size=20 });
List<SqlParam> sp = DBUtils.ExecuteStoreProcedure(this.Context, "DEMO_OUTSOURCING_VERIFY", sqlParams);
if((double)sp[0].Value<0) hasError = true;//@result<0
// 如果校验不通过,抛出校验异常
if (hasError)
{
validateContext.AddError(et, new ValidationErrorInfo(
"", // 出错的字段Key,可以空
Convert.ToString(et.DataEntity[0]), // 数据包内码,必填
et.DataEntityIndex, // 出错的数据包在全部数据包中的顺序
et.RowIndex, // 出错的数据行在全部数据行中的顺序,如果校验基于单据头,此为0
"WW001", // 错误编码,可以任意设定一个字符,主要用于追查错误来源
"检查到委外库存不足,请核查委外即时库存中编码、批号、数量是否匹配,编码="+sp[1].Value+",缺料数量="+sp[0].Value.ToString(), // 错误的详细提示信息
"委外倒冲检查", // 错误的简明提示信息
ErrorLevel.Error // 错误级别:警告、错误
));
}
}
}
}
#endregion
///<summary>
///获取委外倒冲结果方法
///输入billNo单据编号,输出double型比对结果
/// </summary>
public double getWwdcResults(string billNo)
{
List<SqlParam> sqlParams = new List<SqlParam>();
sqlParams.Add(new SqlParam("@billNo", KDDbType.String, billNo));
sqlParams.Add(new SqlParam("@result", KDDbType.Double, 0) { Direction = ParameterDirection.Output });
List<SqlParam> sp = DBUtils.ExecuteStoreProcedure(this.Context, "DEMO_OUTSOURCING_VERIFY", sqlParams);
return (double)sp[0].Value;
}
}
}
代码中涉及的存储过程,请参考:https://vip.kingdee.com/article/484712447190676992?productLineId=1