下推调用服务如下:
IConvertService :单据转换服务,获取转换规则,下推操作
ISaveService:单据保存服务,对目标单据进行保存操作
ISubmitService:单据提交服务
IAuditService:单据审核服务
插件中调用服务,使用Kingdee.BOS.App.ServiceHelper.GetService<>()泛型方法。如调用保存服务,ServiceHelper.GetService<ISaveService>().Save(...)。
下推操作步骤比较简单:根据源单FormId和目标单FormId,获取转换规则,将源单FID和分录ID,传递给转换服务,产生目标单据动态类型;获取目标单据FormMetadata,保存目标单据。
1、获取转换规则

2、下推参数,实例化PushArgs,传递两个参数:转换规则、源单数据ListSelectedRow[]。
ListSelectedRow主要三个参数:源单FID、源单分录行ID,源单FormId.
ListSelectedRowCollection selectRows = new ListSelectedRowCollection();selectRows.Add(new ListSelectedRow(100000, 100000, 0, "PRD_MO"));
3、下推,保存操作

4、提交,审核

由于目标单据有些必填项,如果没有默认值得,下推操作会失败。那就需要在下推完成时,先对数据修改,再保存。
下推操作返回值ConvertOperationResult类,属性TargetDataEntities,包含了产生的目标单据数据,如下操作可取得动态数据集:
DynamicObject[] collection = (from p in convertOperationResult.TargetDataEntitiesselect p.DataEntity).ToArray<DynamicObject>();
(DynamicObjectCollection)collection[0]["Entity"]可取得单据明细,collection[0]为单据头,根据需要对collection进行修改即可。
如下是封装好的方法

/// <summary>/// 获取单据转换规则/// </summary>public static ConvertRuleElement GetConvertRule(string src, string dest, Context ctx, string convertRuleKey = ""){var rules = ServiceHelper.GetService<IConvertService>().GetConvertRules(ctx, src, dest);if (string.IsNullOrEmpty(convertRuleKey))return rules.FirstOrDefault();return (from e in ruleswhere e.Id.Equals(convertRuleKey)select e).FirstOrDefault<ConvertRuleElement>();}/// <summary>/// 单据转换/// </summary>/// <param name="ctx">会话上下文</param>/// <param name="buildBillParam">下推参数</param>/// <param name="autoSubmit">是否自动提交</param>/// <param name="modifyAction">委托,对下推的单据执行修改</param>/// <returns></returns>public static IOperationResult Invoke(Context ctx, BuildBillParam buildBillParam, bool autoSubmit = false, Action<DynamicObject[]> modifyAction = null){IOperationResult result = new OperationResult();var ruleElement = GetConvertRule(buildBillParam.SourceFormId, buildBillParam.TargetFormId, ctx, buildBillParam.ConvertRuleKey);FormMetadata formMetadata = ServiceHelper.GetService<IMetaDataService>().Load(ctx, buildBillParam.TargetFormId) as FormMetadata;if (ruleElement != null && formMetadata != null && !buildBillParam.BizSelectRows.IsEmpty<ListSelectedRow>()){PushArgs pushArgs = new PushArgs(ruleElement, buildBillParam.BizSelectRows.ToArray());OperateOption operateOption = OperateOption.Create();operateOption.SetVariableValue("ValidatePermission", true);if (!string.IsNullOrEmpty(buildBillParam.TargetBillTypeId))pushArgs.TargetBillTypeId = buildBillParam.TargetBillTypeId;else{BillTypeMapPolicyElement billTypeMapPolicyElement = (from x in ruleElement.Policieswhere x is BillTypeMapPolicyElementselect x).FirstOrDefault<ConvertPolicyElement>() as BillTypeMapPolicyElement;BillTypeMapElement billTypeMapElement = billTypeMapPolicyElement.BillTypeMaps.FirstOrDefault((BillTypeMapElement x) => x.SourceBillTypeId == "(All)");if (!billTypeMapElement.IsNullOrEmpty()){pushArgs.TargetBillTypeId = billTypeMapElement.TargetBillTypeId;}}//下推ConvertOperationResult convertOperationResult = ServiceHelper.GetService<IConvertService>().Push(ctx, pushArgs, operateOption, true);result.MergeResult(convertOperationResult);if (!convertOperationResult.IsSuccess){goto end;}DynamicObject[] collection = (from p in convertOperationResult.TargetDataEntitiesselect p.DataEntity).ToArray<DynamicObject>();//修改目标单据modifyAction?.Invoke(collection);//保存目标单据buildBillParam.Option.SetIgnoreInteractionFlag(buildBillParam.Option.HasInteractionFlag("Kingdee.K3.SCM.App.Core.AppBusinessService.UpdateStockService,Kingdee.K3.SCM.App.Core"));IOperationResult saveResult = ServiceHelper.GetService<ISaveService>().Save(ctx, formMetadata.BusinessInfo, collection, OperateOption.Create());result.MergeResult(saveResult);if (!saveResult.IsSuccess){goto end;}//提交\审核if (autoSubmit){var ids = from bill in collection select bill["Id"];buildBillParam.Option.AddInteractionFlag("Kingdee.K3.SCM.App.Core.AppBusinessService.UpdateStockService,Kingdee.K3.SCM.App.Core");var submitResult = ServiceHelper.GetService<ISubmitService>().Submit(ctx, formMetadata.BusinessInfo, ids.ToArray(), "Submit", buildBillParam.Option);result.MergeResult(submitResult);//if (!submitResult.IsSuccess)// throw new KDException("提交异常", $"单据下推并保存成功,但是提交失败{ValidErrorUtil.FormatError(submitResult.ValidationErrors)}");if (submitResult.IsSuccess){buildBillParam.Option.AddInteractionFlag("Kingdee.K3.SCM.App.Core.AppBusinessService.UpdateStockService,Kingdee.K3.SCM.App.Core");var auditResult = ServiceHelper.GetService<IAuditService>().Audit(ctx, formMetadata.BusinessInfo, ids.ToArray(), buildBillParam.Option);result.MergeResult(auditResult);}}}else{throw new KDException("参数异常", "参数异常,未获取到目标单据元数据、转换规则、源单数据为空");}if (result.ValidationErrors != null && result.ValidationErrors.Count > 0){result.IsSuccess = false;}end:return result;}
Invoke方法最后一个参数为委托类型,可以对目标单据在保存前修改。调用方法如下
var ppbomRows = new ListSelectedRowCollection();ppbomRows.Add(new ListSelectedRow(row["F_PXJT_PPBOMFID"].ToString(), row["F_PXJT_PPBOMENTRYID"].ToString(), 0, "PRD_PPBOM"));var pickMtrlParam = new BuildBillParam(){SourceFormId =PrdFormIdEnum.PRD_PPBOM.ToString(), //"PRD_PPBOM",TargetFormId =PrdFormIdEnum.PRD_PickMtrl.ToString(), //"PRD_PickMtrl",ConvertRuleKey =_rulePickMtrlKey, //"PRD_PPBOM2PICKMTRL_NORMAL",BizSelectRows = ppbomRows.ToList(),Option = option,};IOperationResult pickMtrlResult = BuildTarget.Invoke(base.Context, pickMtrlParam, true, list =>{var entrys = (DynamicObjectCollection)list[0]["Entity"];list[0]["OwnerId_Id"] = _orgId;list[0]["StockOrgId_Id"] = _orgId;foreach (var dy in entrys){dy["Lot_Id"] = stockEntrys[0]["Lot_Id"]; //批号dy["Lot_Text"] = stockEntrys[0]["Lot_Text"];dy["OwnerTypeId"] = "BD_OwnerOrg"; //货主类型dy["OwnerId_Id"] = _orgId; //货主dy["OwnerId"] = _org;dy["StockId_Id"] = _whse["Id"]; //仓库dy["StockStatusId_Id"] = 10000;dy["APPQTY"] = _moveQty;dy["ACTUALQTY"] = _moveQty;dy["BASEAPPQTY"] = _moveQty * x;dy["BASEACTUALQTY"] = _moveQty * x;dy["STOCKAPPQTY"] = _moveQty;dy["STOCKACTUALQTY"] = _moveQty;}});
文章转载自不想做程序员,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




