#星空云诊所#星空项目中自动下推插件实现深度解析
现在随着智能会程度越来越高,有很多场景需要在执行某些操作的时候需要能自动执行下推,甚至自动保存和自动提交和审核,这样来减少一些重复工作,这些场景一般都是知道来源单号及需要下推的单据体项,有些可能是整单下推,有些可能是针对某一项进行下推,那下面就结合实际下推场景来深度解析我们二开来如何实现其自动下推。
一. 构建下推参数,具体实现如下:
/// <summary>
/// 创建下推参数
/// </summary>
/// <returns></returns>
private PushArgs CreatePushArgs(OperateOption pushOption, string OrderNo, string taskNO)
{
//得到转换规则
var convertRule = this.GetConvertRule();
//源单数据
var selectedRow = this.GetSelectedRow(pushOption, OrderNo, taskNO);
//单据下推参数
PushArgs pushArgs = new PushArgs(convertRule, selectedRow);
//目标单据主组织,可选参数,基础资料隔离,给没有住组织的目标数据包赋值,取当前登录组织即可
pushArgs.TargetOrgId = this.Context.CurrentOrganizationInfo.ID;
//目标单据类型,必填参数,除非下游单据类型不是必填的,对源单数据进行过滤,给目标单赋值
pushArgs.TargetBillTypeId = "ad0779a4685a43a08f08d2e42d7bf3e9";
return pushArgs;
}
//得到转换规则
private ConvertRuleElement GetConvertRule()
{
var convertRuleId = "SaleOrder-OutStock";
var convertRuleMeta = ConvertServiceHelper.GetConvertRule(this.Context, convertRuleId);
return convertRuleMeta.Rule;
}
注意点:1. TargetBillTypeId,为下游单据类型,获取方法:select FBILLTYPEID from t_bas_billtype where fnumber='XSCKD01_SYS',其中单据类型中可以查到编码,根据编码找到单据类型ID
2. convertRuleId 可以从单据转换里查找单据转换的唯一标识。
二. 调用下推方法,执行下推,并可获取到源单和目标单的数据包,如果还需要对目标单据赋值,则需要在目标数据包中进行赋值处理,具体实现如下:
var convertResult = ConvertServiceHelper.Push(this.Context, pushArgs, pushOption);//调用下推接口
var targetObjs = (from p in convertResult.TargetDataEntities select p.DataEntity).ToArray();//得到目标单据数据包
var targetBInfo = this.GetBusinessInfo(pushArgs.ConvertRule.TargetFormId, pushArgs.ConvertRule.TargetFormMetadata);
注意事项:
1. ConvertServiceHelper.Push(this.Context, pushArgs, pushOption)不能捕捉到下推时的错误信息,因此在调用时尽量使用 try{} catch{}, 否则会造成系统中断报错,别的也无法执行,特别是批量执行带进度条的。
三. 调用下推报错方法。
/// <summary>
/// 保存目标单据,需要修改目标单据的元数据仓库及出货量的赋值
/// </summary>
private bool SaveTargetBill(BusinessInfo targetBusinessInfo, DynamicObject[] targetBillObjs,string outStockQty,string stockFID,out string SaveerrorInfo)
{
bool SaveStatus = false;
SaveerrorInfo = "";
OperateOption saveOption = OperateOption.Create();
saveOption.SetIgnoreWarning(true);
saveOption.SetIgnoreInteractionFlag(true);
//DynamicObjectType dt = targetBillObjs.BusinessInfo.GetEntryEntity("FAuthentication").DynamicObjectType;
//DynamicObject obj = new DynamicObject(dt);
//单据体重新赋值
DynamicObjectCollection FEntity = targetBillObjs[0]["SAL_OUTSTOCKENTRY"] as DynamicObjectCollection;
targetBillObjs[0]["Date"] = DateTime.Now.ToShortDateString().ToString();//出库日期默认为导入日期
for (int i = 0; i < FEntity.Count; i++)
{
FEntity[i]["StockID_Id"] = stockFID;
}
DBServiceHelper.LoadReferenceObject(this.Model.Context, targetBillObjs, targetBusinessInfo.GetDynamicObjectType(), false);
//先保存下推数据,对于出库数量,由于单据中做了很多处理,需要模拟单据保存方法,构建数据进行保存
var saveResult = BusinessDataServiceHelper.Save(this.Model.Context, targetBusinessInfo, targetBillObjs, saveOption);
if (!saveResult.IsSuccess)
{
BusinessDataServiceHelper.Draft(this.Model.Context, targetBusinessInfo, targetBillObjs, saveOption);
SaveerrorInfo = string.Join(";", saveResult.ValidationErrors.Select(x => x.Message));
SaveerrorInfo = SaveerrorInfo.SubStr(0, 254);
}
else
{
SaveStatus = true;
string fid = saveResult.OperateResult[0].PKValue.ToString();
string number = saveResult.OperateResult[0].Number.ToString();
//获取元数据服务和构建视图,更新出货量,并触发值更新事件
FormMetadata meta = (FormMetadata)Kingdee.BOS.ServiceHelper.MetaDataServiceHelper.Load(this.Context, "SAL_OUTSTOCK");
IDynamicFormView OUTSTOCKview = CreateBillView(this.Context, "SAL_OUTSTOCK", meta, fid);
OUTSTOCKview.Model.SetValue("FRealQty", outStockQty, 0);//含税单价字段赋值,row为具体行号
OUTSTOCKview.InvokeFieldUpdateService("FRealQty", 0);//联动触发含税单价值更新事件,row为具体行号
OUTSTOCKview.InvokeFormOperation(Kingdee.BOS.Core.DynamicForm.FormOperationEnum.Save);
OUTSTOCKview.InvokeFormOperation(Kingdee.BOS.Core.DynamicForm.FormOperationEnum.Submit); //自动提交
OUTSTOCKview.InvokeFormOperation(Kingdee.BOS.Core.DynamicForm.FormOperationEnum.Audit); //自动提交和审核
//IOperationResult submitResult = BusinessDataServiceHelper.Submit(this.Context, IDynamicFormView, new object[] { fid }, "submit");//保存时自动提交
//List<KeyValuePair<object, object>> lstKeyValuePairs = new List<KeyValuePair<object, object>>();
//KeyValuePair<object, object> keyValuePair = new KeyValuePair<object, object>(fid, "");
//lstKeyValuePairs.Add(keyValuePair);
//IOperationResult auditResult = BusinessDataServiceHelper.SetBillStatus(this.Context, targetBusinessInfo, lstKeyValuePairs, null, "Audit"); //保存时自动审核
}
return SaveStatus;
}
#星空云诊所#星空项目中自动下推插件实现深度解析
本文2024-09-16 18:34:02发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-22987.html