public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e) { base.AfterExecuteOperationTransaction(e); if (!e.DataEntitys.Any()) { return; } var metas = this.FormOperation.AppBusinessService.Where(item => item.IsEnabled) .Where(item => !item.IsForbidden) .Where(item => item is AutoPushBusinessServiceMeta) .Cast <AutoPushBusinessServiceMeta>() .Where(item => item.RealSourceFormId.EqualsIgnoreCase(this.BusinessInfo.GetForm().Id)) .Where(item => item.TargetFormId.EqualsIgnoreCase(PIWMSFormPrimaryKey.Instance.InNotice())) .ToArray(); if (!metas.Any()) { return; } var convertService = ServiceHelper.GetService <IConvertService>(); var doNothingService = ServiceHelper.GetService <IDoNothingService>(); foreach (var meta in metas) { var rule = convertService.GetConvertRules(this.Context, meta.RealSourceFormId, meta.TargetFormId) .Where(item => item.Status) .Where(item => meta.ConvertRuleKey.IsNullOrEmptyOrWhiteSpace() ? item.IsDefault : meta.ConvertRuleKey.EqualsIgnoreCase(item.Key)) .FirstOrDefault(); if (rule == null) { continue; } var entryKey = rule.GetDefaultConvertPolicyElement().SourceEntryKey; var selectedRows = e.DataEntitys.SelectMany(data => data.EntryProperty(this.BusinessInfo.GetEntity(entryKey)) .Where(entry => entry.FieldProperty <bool>(this.BusinessInfo.GetField(this.AutoPushFieldKey))) .Select(entry => new { EntryId = entry.PkId().ToChangeTypeOrDefault <string>(), BillId = data.PkId().ToChangeTypeOrDefault <string>() })) .Select(a => new ListSelectedRow(a.BillId, a.EntryId, 0, rule.SourceFormId).Adaptive(row => { row.EntryEntityKey = entryKey; return(row); })).ToArray(); if (!selectedRows.Any()) { continue; } PushArgs pushArgs = new PushArgs(rule, selectedRows); var pushResult = convertService.Push(this.Context, pushArgs); this.OperationResult.MergeResult(pushResult); if (pushResult.IsSuccess) { rule.TargetFormMetadata = FormMetaDataCache.GetCachedFormMetaData(this.Context, rule.TargetFormId); var dataEntities = pushResult.TargetDataEntities.Select(data => data.DataEntity).ToArray(); var uploadResult = doNothingService.DoNothingWithDataEntity(this.Context, rule.TargetFormMetadata.BusinessInfo, dataEntities, "Upload"); this.OperationResult.MergeResult(uploadResult); } //end if } //end foreach } //end method
/// <summary> /// 整单下推 /// </summary> /// <param name="ctx"></param> /// <param name="SourceFormId"></param> /// <param name="TargetFormId"></param> /// <param name="sourceBillIds"></param> /// <returns></returns> public ConvertOperationResult ConvertBills(Context ctx, string SourceFormId, string TargetFormId, List <long> sourceBillIds) { // 获取源单与目标单的转换规则 IConvertService convertService = ServiceHelper.GetService <IConvertService>(); var rules = convertService.GetConvertRules(ctx, SourceFormId, TargetFormId); if (rules == null || rules.Count == 0) { throw new KDBusinessException("", string.Format("未找到{0}到{1}之间,启用的转换规则,无法自动下推!", SourceFormId, TargetFormId)); } // 取勾选了默认选项的规则 var rule = rules.FirstOrDefault(t => t.IsDefault); // 如果无默认规则,则取第一个 if (rule == null) { rule = rules[0]; } // 开始构建下推参数: // 待下推的源单数据行 List <ListSelectedRow> srcSelectedRows = new List <ListSelectedRow>(); int rowkey = -1; foreach (long billId in sourceBillIds) { //把待下推的源单内码,逐个创建ListSelectedRow对象,添加到集合中 srcSelectedRows.Add(new ListSelectedRow(billId.ToString(), string.Empty, rowkey++, SourceFormId)); } // 指定目标单单据类型:情况比较复杂,直接留空,会下推到默认的单据类型 string targetBillTypeId = string.Empty; // 指定目标单据主业务组织:情况更加复杂, // 建议在转换规则中,配置好主业务组织字段的映射关系:运行时,由系统根据映射关系,自动从上游单据取主业务组织,避免由插件指定 long targetOrgId = 0; // 自定义参数字典:把一些自定义参数,传递到转换插件中;转换插件再根据这些参数,进行特定处理 Dictionary <string, object> custParams = new Dictionary <string, object>(); //custParams.Add("1", 1); //custParams.Add("2", 2); // 组装下推参数对象 PushArgs pushArgs = new PushArgs(rule, srcSelectedRows.ToArray()) { TargetBillTypeId = targetBillTypeId, TargetOrgId = targetOrgId, CustomParams = custParams }; // 调用下推服务,生成下游单据数据包 ConvertOperationResult operationResult = convertService.Push(ctx, pushArgs, OperateOption.Create()); return(operationResult); }
/// <summary> /// 关联发货明细批量新增发货明细。 /// </summary> /// <param name="ctx">上下文对象。</param> /// <param name="dataArray">发货明细关联发货通知数据实体数组。</param> /// <returns>返回新建保存事务结果。</returns> public IOperationResult CreateNewBillsFromInNoticeEntities(Context ctx, DynamicObjectCollection dataArray) { //取默认转换规则。 var rule = ConvertServiceHelper.GetConvertRules(ctx, "BAH_WMS_Pickup", "BAH_WMS_Outbound") .Where(element => element.IsDefault) .FirstOrDefault(); if (rule == null) { throw new KDBusinessException("RuleNotFound", "未找到拣货明细至发货明细之间,启用的转换规则,无法自动下推!"); } ListSelectedRowCollection listSelectedRowCollection = new ListSelectedRowCollection(); foreach (var data in dataArray) { var row = new ListSelectedRow(data["SourceBillId"].ToString(), data["SourceEntryId"].ToString(), 0, rule.SourceFormId) { EntryEntityKey = "FEntity" }; listSelectedRowCollection.Add(row); }//end foreach //将需要传入的数量作为参数传递进下推操作,并执行下推操作。 PushArgs args = new PushArgs(rule, listSelectedRowCollection.ToArray()); var inDetailDataObjects = ConvertServiceHelper.Push(ctx, args) .Adaptive(result => result.ThrowWhenUnSuccess(op => op.GetResultMessage())) .Adaptive(result => result.TargetDataEntities.Select(entity => entity.DataEntity).ToArray()); //修改明细行数据包。 var inDetailMetadata = FormMetaDataCache.GetCachedFormMetaData(ctx, rule.TargetFormId); var inDetailBillView = inDetailMetadata.CreateBillView(ctx); var inDetailDynamicFormView = inDetailBillView as IDynamicFormViewService; var inDetailBusinessInfo = inDetailMetadata.BusinessInfo; var inDetailEntryEntity = inDetailBusinessInfo.GetEntity("FEntity"); var inDetailEntryLink = inDetailBusinessInfo.GetForm().LinkSet.LinkEntitys.FirstOrDefault(); //调用上传操作,将暂存、保存、提交、审核操作放置在同一事务中执行。 return(inDetailDataObjects.DoNothing(ctx, inDetailBusinessInfo, "Upload")); }//end method
///// <summary> ///// 关联到货通知新增收货明细。 ///// </summary> ///// <param name="ctx">上下文对象。</param> ///// <param name="data">收货明细关联到货通知数据实体。</param> ///// <returns>返回新建保存事务结果。</returns>s //public IOperationResult CreateNewBillFromInNoticeEntry(Context ctx, PutDetailLinkInDetailDto.InDetailEntryLinkInNotice data) //{ // return this.CreateNewBillsFromInNoticeEntities(ctx, new PutDetailLinkInDetailDto.InDetailEntryLinkInNotice[] { data }); //}//end method /// <summary> /// 关联收货明细批量新增上架明细。 /// </summary> /// <param name="ctx">上下文对象。</param> /// <param name="dataArray">上架明细关联收货通知数据实体数组。</param> /// <returns>返回新建保存事务结果。</returns> public IOperationResult CreateNewBillsFromInNoticeEntities(Context ctx, IEnumerable <PutDetailLinkInDetailDto.PutDetailEntryLinkInNotice> dataArray) { //取默认转换规则。 var rule = ConvertServiceHelper.GetConvertRules(ctx, "BAH_WMS_Inbound", "BAH_WMS_Putaway") .Where(element => element.IsDefault) .FirstOrDefault(); if (rule == null) { throw new KDBusinessException("RuleNotFound", "未找到收货明细至上架明细之间,启用的转换规则,无法自动下推!"); } ListSelectedRowCollection listSelectedRowCollection = new ListSelectedRowCollection(); foreach (var data in dataArray) { var row = new ListSelectedRow(data.SourceBillId.ToString(), data.SourceEntryId.ToString(), 0, rule.SourceFormId) { EntryEntityKey = "FEntity" }; listSelectedRowCollection.Add(row); }//end foreach //将需要传入的数量作为参数传递进下推操作,并执行下推操作。 PushArgs args = new PushArgs(rule, listSelectedRowCollection.ToArray()); var inDetailDataObjects = ConvertServiceHelper.Push(ctx, args) .Adaptive(result => result.ThrowWhenUnSuccess(op => op.GetResultMessage())) .Adaptive(result => result.TargetDataEntities.Select(entity => entity.DataEntity).ToArray()); //修改明细行数据包。 var inDetailMetadata = FormMetaDataCache.GetCachedFormMetaData(ctx, rule.TargetFormId); var inDetailBillView = inDetailMetadata.CreateBillView(ctx); var inDetailDynamicFormView = inDetailBillView as IDynamicFormViewService; var inDetailBusinessInfo = inDetailMetadata.BusinessInfo; var inDetailEntryEntity = inDetailBusinessInfo.GetEntity("FEntity"); var inDetailEntryLink = inDetailBusinessInfo.GetForm().LinkSet.LinkEntitys.FirstOrDefault(); foreach (var data in inDetailDataObjects) { //新建并加载已有数据包。 inDetailBillView.AddNew(data); //逐行检索,并关联复制行。 var entryCollection = inDetailBillView.Model.GetEntityDataObject(inDetailEntryEntity); var entryMirrorArray = entryCollection.ToArray(); foreach (var entry in entryMirrorArray) { var sources = dataArray.Join(entry.Property <DynamicObjectCollection>(inDetailEntryLink.Key), left => new { SourceEntryId = left.SourceEntryId, SourceBillId = left.SourceBillId }, right => new { SourceEntryId = right.Property <long>("SId"), SourceBillId = right.Property <long>("SBillId") }, (left, right) => left).ToArray(); for (int i = 0; i < sources.Count(); i++) { var entryIndex = entryCollection.IndexOf(entry); var rowIndex = entryIndex + i; if (i > 0) { inDetailBillView.Model.CopyEntryRow("FEntity", entryIndex, rowIndex, true); }//end if var item = sources.ElementAt(i); inDetailDynamicFormView.UpdateValue("FFromTrackNo", rowIndex, item.FromTrackNo); inDetailDynamicFormView.UpdateValue("FToTrackNo", rowIndex, item.ToTrackNo); inDetailDynamicFormView.SetItemValueByID("FToLocId", item.ToLocId, rowIndex); if (item.ToAvgCty == 0) { inDetailDynamicFormView.UpdateValue("FFromCty", rowIndex, item.ToCty); inDetailDynamicFormView.SetItemValueByID("FFromUnitId", item.ToUnitId, rowIndex); inDetailDynamicFormView.UpdateValue("FFromQty", rowIndex, item.ToQty); } else { inDetailDynamicFormView.UpdateValue("FFromAvgCty", rowIndex, item.ToAvgCty); inDetailDynamicFormView.SetItemValueByID("FFromUnitId", item.ToUnitId, rowIndex); inDetailDynamicFormView.UpdateValue("FFromQty", rowIndex, item.ToQty); } //inDetailDynamicFormView.SetItemValueByID("FStockId", item.StockId, rowIndex); //inDetailDynamicFormView.SetItemValueByID("FStockPlaceId", item.StockPlaceId, rowIndex); //inDetailDynamicFormView.UpdateValue("FLotNo", rowIndex, item.BatchNo); //inDetailDynamicFormView.UpdateValue("FProduceDate", rowIndex, item.KFDate); //inDetailDynamicFormView.UpdateValue("FExpPeriod", rowIndex, item.ExpPeriod); //inDetailDynamicFormView.UpdateValue("FSerialNo", rowIndex, item.SerialNo); //inDetailDynamicFormView.UpdateValue("FTrayNo", rowIndex, item.TrayNo); //inDetailDynamicFormView.UpdateValue("FEntryRemark", rowIndex, item.Remark); } //end for } //end foreach } //end foreach //调用上传操作,将暂存、保存、提交、审核操作放置在同一事务中执行。 return(inDetailDataObjects.DoNothing(ctx, inDetailBusinessInfo, "Upload")); }//end method
/// <summary> /// 构造方法。 /// </summary> /// <param name="rule">单据转换规则。</param> /// <param name="op">操作结果。</param> /// <param name="rows">关联数据源。</param> public AfterCreatePushArgsEventArgs(ConvertRuleElement rule, PushArgs pushArgs) { this.Rule = rule; this.PushArgs = pushArgs; }
/// <summary> /// 自动下推并保存 /// </summary> /// <param name="sourceFormId">源单FormId</param> /// <param name="targetFormId">目标单FormId</param> /// <param name="sourceBillIds">源单内码</param> private IOperationResult DoPush(string sourceFormId, string targetFormId, long targetOrgId, List <long> sourceBillIds) { IOperationResult result = new OperationResult(); result.IsSuccess = false; // 获取源单与目标单的转换规则 IConvertService convertService = Kingdee.BOS.App.ServiceHelper.GetService <IConvertService>(); var rules = convertService.GetConvertRules(this.Context, sourceFormId, targetFormId); if (rules == null || rules.Count == 0) { throw new KDBusinessException("", string.Format("未找到{0}到{1}之间,启用的转换规则,无法自动下推!", sourceFormId, targetFormId)); } // 取勾选了默认选项的规则 var rule = rules.FirstOrDefault(t => t.IsDefault); //var rule = rules. // 如果无默认规则,则取第一个 if (rule == null) { rule = rules[0]; } // 开始构建下推参数: // 待下推的源单数据行 List <ListSelectedRow> srcSelectedRows = new List <ListSelectedRow>(); foreach (var billId in sourceBillIds) {// 把待下推的源单内码,逐个创建ListSelectedRow对象,添加到集合中 srcSelectedRows.Add(new ListSelectedRow(billId.ToString(), string.Empty, 0, sourceFormId)); // 特别说明:上述代码,是整单下推; // 如果需要指定待下推的单据体行,请参照下句代码,在ListSelectedRow中,指定EntryEntityKey以及EntryId //srcSelectedRows.Add(new ListSelectedRow(billId.ToString(), entityId, 0, sourceFormId) { EntryEntityKey = "FEntity" }); } // 指定目标单单据类型:情况比较复杂,没有合适的案例做参照,示例代码暂略,直接留空,会下推到默认的单据类型 string targetBillTypeId = string.Empty; // 指定目标单据主业务组织:情况更加复杂,需要涉及到业务委托关系,缺少合适案例,示例代码暂略 // 建议在转换规则中,配置好主业务组织字段的映射关系:运行时,由系统根据映射关系,自动从上游单据取主业务组织,避免由插件指定 //long targetOrgId = 0; // 自定义参数字典:把一些自定义参数,传递到转换插件中;转换插件再根据这些参数,进行特定处理 Dictionary <string, object> custParams = new Dictionary <string, object>(); // 组装下推参数对象 PushArgs pushArgs = new PushArgs(rule, srcSelectedRows.ToArray()) { TargetBillTypeId = targetBillTypeId, TargetOrgId = targetOrgId, CustomParams = custParams }; // 调用下推服务,生成下游单据数据包 ConvertOperationResult operationResult = convertService.Push(this.Context, pushArgs, OperateOption.Create()); // 开始处理下推结果: // 获取下推生成的下游单据数据包 DynamicObject[] targetBillObjs = (from p in operationResult.TargetDataEntities select p.DataEntity).ToArray(); if (targetBillObjs.Length == 0) { // 未下推成功目标单,抛出错误,中断审核 throw new KDBusinessException("", string.Format("由{0}自动下推{1},没有成功生成数据包,自动下推失败!", sourceFormId, targetFormId)); } // 对下游单据数据包,进行适当的修订,以避免关键字段为空,自动保存失败 // 示例代码略 // 读取目标单据元数据 IMetaDataService metaService = Kingdee.BOS.App.ServiceHelper.GetService <IMetaDataService>(); var targetBillMeta = metaService.Load(this.Context, targetFormId) as FormMetadata; // 构建保存操作参数:设置操作选项值,忽略交互提示 OperateOption saveOption = OperateOption.Create(); // 忽略全部需要交互性质的提示,直接保存; saveOption.SetIgnoreWarning(true); // 忽略交互提示 saveOption.SetInteractionFlag(this.Option.GetInteractionFlag()); // 如果有交互,传入用户选择的交互结果 // using Kingdee.BOS.Core.Interaction; saveOption.SetIgnoreInteractionFlag(this.Option.GetIgnoreInteractionFlag()); // 调用保存服务,自动保存 ISaveService saveService = Kingdee.BOS.App.ServiceHelper.GetService <ISaveService>(); var saveResult = saveService.Save(this.Context, targetBillMeta.BusinessInfo, targetBillObjs, saveOption, "Save"); // 判断自动保存结果:只有操作成功,才会继续 if (saveResult.SuccessDataEnity != null) { var submitRet = AppServiceContext.SubmitService.Submit(this.Context, targetBillMeta.BusinessInfo, saveResult.SuccessDataEnity.Select(o => o["Id"]).ToArray(), "Submit", saveOption); result.MergeResult(submitRet); if (submitRet.SuccessDataEnity != null) { var auditResult = AppServiceContext.SetStatusService.SetBillStatus(this.Context, targetBillMeta.BusinessInfo, submitRet.SuccessDataEnity.Select(o => new KeyValuePair <object, object>(o["Id"], 0)).ToList(), new List <object> { "1", "" }, "Audit", saveOption); result.MergeResult(auditResult); } } if (this.CheckOpResult(saveResult, saveOption)) { return(result); } return(result); }
/// <summary> /// 根据单据唯一标识下推单据 /// </summary> private void PushFormByFormId() { string status = this.View.BillModel.GetValue("FDocumentStatus").ToString(); if (status == "Z") { return; } string formId = this.View.BillView.GetFormId(); string targetFormId = "k0c6b452fa8154c4f8e8e5f55f96bcfac"; // 个人资金 var rules = ConvertServiceHelper.GetConvertRules(this.View.Context, formId, targetFormId); var rule = rules.FirstOrDefault(t => t.IsDefault); string fid = this.View.BillModel.GetPKValue().ToString(); ListSelectedRow[] selectedRows; if (formId == "k0c30c431418e4cf4a60d241a18cb241c") // 出差申请 { int count = this.View.BillModel.GetEntryRowCount("FEntity"); selectedRows = new ListSelectedRow[count]; for (int i = 0; i < count; i++) { string entryId = this.View.BillModel.GetEntryPKValue("FEntryID", i).ToString(); selectedRows[i] = new ListSelectedRow(fid, entryId, i, formId); } } else { ListSelectedRow row = new ListSelectedRow(fid, string.Empty, 0, formId); selectedRows = new ListSelectedRow[] { row }; } // 调用下推服务,生成下游单据数据包 ConvertOperationResult operationResult = null; PushArgs pushArgs = new PushArgs(rule, selectedRows) { TargetBillTypeId = "", TargetOrgId = 0, }; try { //执行下推操作,并获取下推结果 operationResult = ConvertServiceHelper.Push(this.View.Context, pushArgs, OperateOption.Create()); } catch (KDExceptionValidate ex) { this.View.ShowErrMessage(ex.Message, ex.ValidateString); return; } catch (Exception ex) { this.View.ShowErrMessage(ex.Message); return; } // 获取生成的目标单据数据包 DynamicObject[] objs = operationResult.TargetDataEntities.Select(p => p.DataEntity).ToArray(); // 读取目标单据元数据 var targetBillMeta = MetaDataServiceHelper.Load(this.View.Context, targetFormId) as FormMetadata; OperateOption option = OperateOption.Create(); // 忽略全部需要交互性质的提示 option.SetIgnoreWarning(true); // 暂存数据 var saveResult = BusinessDataServiceHelper.Draft(this.View.Context, targetBillMeta.BusinessInfo, objs, option); string targetId = saveResult.SuccessDataEnity.Select(item => item["Id"].ToString()).Distinct().FirstOrDefault(); // 打开目标单据 if (targetId != null) { MobileShowParameter param = new MobileShowParameter(); param.Caption = "个人资金申请"; param.FormId = "ora_GRZJJZ"; param.PKey = targetId; param.ParentPageId = this.View.PageId; param.Status = OperationStatus.EDIT; param.OpenStyle.ShowType = ShowType.Default; this.View.ShowForm(param); } }
/// <summary> /// 后台调用单据转换生成目标单 /// </summary> /// <param name="ctx"></param> /// <param name="option"></param> /// <returns></returns> public static IOperationResult ConvertBills(Context ctx, BillConvertOption option) { IOperationResult result = new OperationResult(); List <DynamicObject> list = new List <DynamicObject>(); ConvertRuleElement rule = AppServiceContext.ConvertService.GetConvertRules(ctx, option.sourceFormId, option.targetFormId) .FirstOrDefault(w => w.Id == option.ConvertRuleKey); FormMetadata metaData = (FormMetadata)AppServiceContext.MetadataService.Load(ctx, option.targetFormId, true); if ((rule != null) && option.BizSelectRows != null && option.BizSelectRows.Count() > 0) { PushArgs serviceArgs = new PushArgs(rule, option.BizSelectRows); serviceArgs.CustomParams.Add("CustomConvertOption", option); serviceArgs.CustomParams.Add("CustomerTransParams", option.customParams); OperateOption operateOption = OperateOption.Create(); operateOption.SetVariableValue("ValidatePermission", true); ConvertOperationResult convertOperationResult = AppServiceContext.ConvertService.Push(ctx, serviceArgs, operateOption); if (!convertOperationResult.IsSuccess) { result = convertOperationResult as IOperationResult; return(result); } DynamicObject[] collection = convertOperationResult.TargetDataEntities .Select(s => s.DataEntity).ToArray(); list.AddRange(collection); } if (list.Count > 0) { AppServiceContext.DBService.LoadReferenceObject(ctx, list.ToArray(), metaData.BusinessInfo.GetDynamicObjectType(), false); } if (option.IsDraft && list.Count > 0) { result = AppServiceContext.DraftService.Draft(ctx, metaData.BusinessInfo, list.ToArray()); } if (!result.IsSuccess) { return(result); } if (option.IsSave && !option.IsDraft && list.Count > 0) { OperateOption operateOption = OperateOption.Create(); operateOption.SetVariableValue("ValidatePermission", true); operateOption.SetIgnoreWarning(true); result = AppServiceContext.SaveService.Save(ctx, metaData.BusinessInfo, list.ToArray(), operateOption); //result = AppServiceContext.SaveService.Save(ctx, metaData.BusinessInfo, list.ToArray()); } if (!result.IsSuccess) { return(result); } if (option.IsSubmit && list.Count > 0) { result = AppServiceContext.SubmitService.Submit(ctx, metaData.BusinessInfo, list.Select(item => ((Object)(Convert.ToInt64(item["Id"])))).ToArray(), "Submit"); } if (!result.IsSuccess) { return(result); } if (option.IsAudit && list.Count > 0) { result = AppServiceContext.SubmitService.Submit(ctx, metaData.BusinessInfo, list.Select(item => ((Object)(Convert.ToInt64(item["Id"])))).ToArray(), "Submit"); if (!result.IsSuccess) { return(result); } List <KeyValuePair <object, object> > keyValuePairs = new List <KeyValuePair <object, object> >(); list.ForEach(item => { keyValuePairs.Add(new KeyValuePair <object, object>(item.GetPrimaryKeyValue(), item)); } ); List <object> auditObjs = new List <object>(); auditObjs.Add("1"); auditObjs.Add(""); //Kingdee.BOS.Util.OperateOptionUtils oou = null; OperateOption ooption = OperateOption.Create(); ooption.SetIgnoreWarning(false); ooption.SetIgnoreInteractionFlag(true); ooption.SetIsThrowValidationInfo(false); result = AppServiceContext.SetStatusService.SetBillStatus(ctx, metaData.BusinessInfo, keyValuePairs, auditObjs, "Audit", ooption); option.BillStatusOptionResult = ooption; if (!result.IsSuccess) { return(result); } } return(result); }
/// <summary> /// 审核结束自动下推(应付单下推付款申请单) /// </summary> /// <param name="e"></param> public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e) { base.AfterExecuteOperationTransaction(e); try { string sql = string.Empty; if (e.DataEntitys != null && e.DataEntitys.Count <DynamicObject>() > 0) { foreach (DynamicObject item in e.DataEntitys) { //收款单id string Fid = item["Id"].ToString(); string sql2 = ""; //收款单源单明细 DynamicObjectCollection RECEIVEBILLSRCENTRYList = item["RECEIVEBILLSRCENTRY"] as DynamicObjectCollection; foreach (var entry in RECEIVEBILLSRCENTRYList) { //销售订单明细内码 string FORDERENTRYID = entry["FSRCORDERENTRYID"].ToString(); //本次收款金额 decimal FREALRECAMOUNT = Convert.ToDecimal(entry["REALRECAMOUNT"].ToString()); if (!string.IsNullOrEmpty(FORDERENTRYID)) { //查询采购订单 sql = string.Format(@"select a.FBILLNO,b.FENTRYID as 采购订单明细内码 ,f.F_YBG_BUSINESSMODEL as 业务模式 from t_PUR_POOrder a inner join t_PUR_POOrderEntry b on a.fID=b.FID inner join T_PUR_POORDERENTRY_LK c on c.FENTRYID=b.FENTRYID left join T_SAL_ORDERENTRY d on d.FENTRYID=c.FSID left join T_SAL_ORDER f on f.FID=d.FID where FSID='{0}'", FORDERENTRYID); DataSet ds = DBServiceHelper.ExecuteDataSet(this.Context, sql); DataTable dt = ds.Tables[0]; if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { string F_YBG_BUSINESSMODEL = dt.Rows[i]["采购订单明细内码"].ToString(); if (F_YBG_BUSINESSMODEL == "01" || F_YBG_BUSINESSMODEL == "04") //挂靠的采用自动生成付款申请单 { string POFENTRYID = dt.Rows[i]["采购订单明细内码"].ToString(); if (string.IsNullOrEmpty(sql2)) { sql2 += " FPAYABLEENTRYID='" + POFENTRYID + "' "; } else { sql2 += " or FPAYABLEENTRYID='" + POFENTRYID + "' "; } } } } } } if (!string.IsNullOrEmpty(sql2)) { #region 应付单下推付款申请单 string srcFormId = "AP_Payable"; //应付单 string destFormId = "CN_PAYAPPLY"; //付款申请单 IMetaDataService mService = Kingdee.BOS.App.ServiceHelper.GetService <IMetaDataService>(); IViewService vService = Kingdee.BOS.App.ServiceHelper.GetService <IViewService>(); FormMetadata destmeta = mService.Load(this.Context, destFormId) as FormMetadata; //转换规则的唯一标识 string ruleKey = "AP_PayableToPayApply"; //var rules = ConvertServiceHelper.GetConvertRules(Context, srcFormId, destFormId); //var rule = rules.FirstOrDefault(t => t.IsDefault); ConvertRuleElement rule = GetDefaultConvertRule(Context, srcFormId, destFormId, ruleKey); List <ListSelectedRow> lstRows = new List <ListSelectedRow>(); string strsql = "select a.FID , a.FENTRYID from T_AP_PAYABLEPLAN a left join T_AP_PAYABLE b on a.FID=b.FID where b.FDOCUMENTSTATUS='C' and (" + sql2 + ") "; DataSet ds2 = DBServiceHelper.ExecuteDataSet(Context, strsql); if (ds2.Tables[0].Rows.Count > 0) { HashSet <string> hasset = new HashSet <string>(); for (int j = 0; j < ds2.Tables[0].Rows.Count; j++) { hasset.Add(ds2.Tables[0].Rows[j]["FID"].ToString()); long entryId = Convert.ToInt64(ds2.Tables[0].Rows[j]["FENTRYID"]); //源单单据标识 ListSelectedRow row = new ListSelectedRow(ds2.Tables[0].Rows[j]["FID"].ToString(), entryId.ToString(), 0, "AP_Payable"); //源单单据体标识 row.EntryEntityKey = "FEntityPlan"; lstRows.Add(row); } PushArgs pargs = new PushArgs(rule, lstRows.ToArray()); IConvertService cvtService = Kingdee.BOS.App.ServiceHelper.GetService <IConvertService>(); OperateOption option = OperateOption.Create(); option.SetIgnoreWarning(true); option.SetVariableValue("ignoreTransaction", false); option.SetIgnoreInteractionFlag(true); #region 提交审核 //OperateOption option2 = OperateOption.Create(); //option2.SetIgnoreWarning(true); //option2.SetVariableValue("ignoreTransaction", true); //foreach (var hid in hasset) //{ // //如果应付单没有提交先提交审核 // IMetaDataService BomService = Kingdee.BOS.App.ServiceHelper.GetService<IMetaDataService>(); // //应付单元素包 // FormMetadata APMeta = BomService.Load(Context, "AP_Payable") as FormMetadata; // IViewService APVService = Kingdee.BOS.App.ServiceHelper.GetService<IViewService>(); // //应付单数据包 // DynamicObject APmd = APVService.LoadSingle(Context, hid, APMeta.BusinessInfo.GetDynamicObjectType()); // DynamicObject[] dy = new DynamicObject[] { APmd }; // object[] items = dy.Select(p => p["Id"]).ToArray(); // ISubmitService submitService = Kingdee.BOS.App.ServiceHelper.GetService<ISubmitService>(); // IOperationResult submitresult = submitService.Submit(Context, APMeta.BusinessInfo, items, "Submit", option2); // IAuditService auditService = Kingdee.BOS.App.ServiceHelper.GetService<IAuditService>(); // IOperationResult auditresult = auditService.Audit(Context, APMeta.BusinessInfo, items, option2); //} #endregion ConvertOperationResult cvtResult = cvtService.Push(Context, pargs, option, false); if (cvtResult.IsSuccess) { DynamicObject[] dylist = (from p in cvtResult.TargetDataEntities select p.DataEntity).ToArray(); //修改应收单里面数据 for (int K = 0; K < dylist.Length; K++) { //付款原因 dylist[K]["F_YBG_Remarks"] = "供应商付款"; //明细信息 DynamicObjectCollection RECEIVEBILLENTRYList = dylist[K]["FPAYAPPLYENTRY"] as DynamicObjectCollection; foreach (var Entry in RECEIVEBILLENTRYList) { //结算方式 BaseDataField FSETTLETYPEID = destmeta.BusinessInfo.GetField("FSETTLETYPEID") as BaseDataField; Entry["FSETTLETYPEID_Id"] = 4; Entry["FSETTLETYPEID"] = vService.LoadSingle(Context, 4, FSETTLETYPEID.RefFormDynamicObjectType); } } //保存 ISaveService saveService = Kingdee.BOS.App.ServiceHelper.GetService <ISaveService>(); IOperationResult saveresult = saveService.Save(Context, destmeta.BusinessInfo, dylist, option); bool reult = CheckResult(saveresult, out string mssg); if (!reult) { throw new Exception("收款款单审核成功,生成付款申请单失败:"); } else { //纪录核销的纪录 OperateResultCollection operateResults = saveresult.OperateResult; string fnmber = operateResults[0].Number; string fid = operateResults[0].PKValue.ToString(); } } } else { throw new KDException("", "应付单不存在或者未审核,下推付款申请单失败"); } #endregion } } } } catch (Exception ex) { throw new KDException("", "应付单下推付款申请单失败:" + ex.ToString()); } }
public DynamicObject[] ConvertBillsWithEntry(Context ctx, List <ConvertOption> options, string SourceFormId, string TargetFormId, string targetBillTypeId, string SourceEntryEntityKey, OperateOption operateOption) { //List<DynamicObject[]> result = new List<DynamicObject[]>(); List <DynamicObject> before = new List <DynamicObject>(); //IEnumerable<DynamicObject> targetDatas = null; IConvertService convertService = ServiceHelper.GetService <IConvertService>(); var rules = convertService.GetConvertRules(ctx, SourceFormId, TargetFormId); string rulekey = operateOption.GetVariableValue("ruleKey", string.Empty);//单据转换规则 key 指定 ID var rule = rules.FirstOrDefault(t => t.IsDefault); if (rules == null || rules.Count == 0) { throw new KDBusinessException("", string.Format("未找到{0}到{1}之间,启用的转换规则,无法自动下推!", SourceFormId, TargetFormId)); } //如果指定 规则 if (rulekey.Equals(string.Empty) == false) { foreach (var ervryrule in rules) { if (ervryrule.Key.Equals(rulekey)) { rule = ervryrule; break; } } } if (rule == null) { rule = rules[0]; } foreach (ConvertOption option in options) { // 开始构建下推参数: // 待下推的源单数据行 List <ListSelectedRow> srcSelectedRows = new List <ListSelectedRow>(); // Dictionary<long, List<Tuple<string, int>>> dic = new Dictionary<long, List<Tuple<string, int>>>(); foreach (long billId in option.SourceBillIds) { srcSelectedRows = new List <ListSelectedRow>(); int rowKey = -1; for (int i = 0; i < option.SourceBillEntryIds.Count(); i++) { ListSelectedRow row = new ListSelectedRow(billId.ToString(), option.SourceBillEntryIds[i].ToString(), rowKey++, SourceFormId); row.EntryEntityKey = SourceEntryEntityKey; Dictionary <string, string> fieldValues = new Dictionary <string, string>(); fieldValues.Add(SourceEntryEntityKey, option.SourceBillEntryIds[i].ToString()); row.FieldValues = fieldValues; srcSelectedRows.Add(row); //dic.Add(option.SourceBillEntryIds[i], new List<Tuple<string, int>> { new Tuple<string, int>(" ", option.mount[i]) }); } } // 指定目标单单据类型:情况比较复杂,直接留空,会下推到默认的单据类型 if (targetBillTypeId == null) { targetBillTypeId = string.Empty; } // 指定目标单据主业务组织:情况更加复杂, // 建议在转换规则中,配置好主业务组织字段的映射关系:运行时,由系统根据映射关系,自动从上游单据取主业务组织,避免由插件指定 long targetOrgId = 0; // 自定义参数字典:把一些自定义参数,传递到转换插件中;转换插件再根据这些参数,进行特定处理 Dictionary <string, object> custParams = new Dictionary <string, object>(); //custParams.Add("1", 1); //custParams.Add("2", 2); // 组装下推参数对象 PushArgs pushArgs = new PushArgs(rule, srcSelectedRows.ToArray()) { TargetBillTypeId = targetBillTypeId, TargetOrgId = targetOrgId, CustomParams = custParams }; // 调用下推服务,生成下游单据数据包 //OperateOption option1 = OperateOption.Create(); //option1.SetVariableValue("OutStockAmount", option.mount); //option1.SetVariableValue("srcbillseq", option.srcbillseq); //option1.SetVariableValue("FDATE", Convert.ToDateTime(option.FDATE)); ConvertOperationResult operationResult = convertService.Push(ctx, pushArgs, operateOption); // 开始处理下推结果: // 获取下推生成的下游单据数据包 DynamicObject[] targetBillObjs = (from p in operationResult.TargetDataEntities select p.DataEntity).ToArray(); foreach (DynamicObject cc in targetBillObjs) { DynamicObjectCollection rpt = cc["FPAYAPPLYENTRY"] as DynamicObjectCollection; foreach (DynamicObject item in rpt) { // item["FDETABLEINID"] = option.dic[Convert.ToString(item["SoorDerno"]) + Convert.ToString(item["Fsrcbillseq"])]; } before.Add(cc); } if (targetBillObjs.Length == 0) { // 未下推成功目标单,抛出错误,中断审核 throw new KDBusinessException("", string.Format("由{0}自动下推{1},没有成功生成数据包,自动下推失败!", SourceFormId, TargetFormId)); } } DynamicObject[] aa = before.Select(p => p).ToArray() as DynamicObject[]; return(aa); }
/// <summary> /// 后台调用单据转换生成目标单 /// </summary> /// <param name="ctx"></param> /// <param name="option"></param> /// <returns></returns> public static IOperationResult ConvertBills(Context ctx, BillConvertOption option,string billtype) { //return AppServiceContext.ConvertBills(ctx, option); //判断单据类型资产接收单转化为资产应付;费用物料转为办公应付 string billtypeid = ""; if (billtype == "SLD04_SYS") { billtypeid = "56e224fd183867"; } if (billtype == "SLD05_SYS") { billtypeid = "777f5fd25084498a9e77e1ef2a72e645"; } IOperationResult result = new OperationResult(); List<DynamicObject> list = new List<DynamicObject>(); ConvertRuleElement rule = AppServiceContext.ConvertService.GetConvertRules(ctx, option.sourceFormId, option.targetFormId) .FirstOrDefault(w => w.Id == option.ConvertRuleKey); FormMetadata metaData = (FormMetadata)AppServiceContext.MetadataService.Load(ctx, option.targetFormId, true); if ((rule != null) && option.BizSelectRows != null && option.BizSelectRows.Count() > 0) { PushArgs serviceArgs = new PushArgs(rule, option.BizSelectRows) { TargetBillTypeId = billtypeid, }; serviceArgs.CustomParams.Add("CustomConvertOption", option); serviceArgs.CustomParams.Add("CustomerTransParams", option.customParams); OperateOption operateOption = OperateOption.Create(); operateOption.SetVariableValue("ValidatePermission", true); ConvertOperationResult convertOperationResult = AppServiceContext.ConvertService.Push(ctx, serviceArgs, operateOption); if (!convertOperationResult.IsSuccess) { result = convertOperationResult as IOperationResult; return result; } DynamicObject[] collection = convertOperationResult.TargetDataEntities .Select(s => s.DataEntity).ToArray(); list.AddRange(collection); } if (list.Count > 0) { AppServiceContext.DBService.LoadReferenceObject(ctx, list.ToArray(), metaData.BusinessInfo.GetDynamicObjectType(), false); } if (option.IsDraft && list.Count > 0) { result = AppServiceContext.DraftService.Draft(ctx, metaData.BusinessInfo, list.ToArray()); } if (!result.IsSuccess) { return result; } if (option.IsSave && !option.IsDraft && list.Count > 0) { OperateOption operateOption = OperateOption.Create(); operateOption.SetVariableValue("ValidatePermission", true); operateOption.SetIgnoreWarning(true); result = AppServiceContext.SaveService.Save(ctx, metaData.BusinessInfo, list.ToArray(), operateOption); //result = AppServiceContext.SaveService.Save(ctx, metaData.BusinessInfo, list.ToArray()); } if (!result.IsSuccess) { return result; } if (option.IsSubmit && list.Count > 0) { result = AppServiceContext.SubmitService.Submit(ctx, metaData.BusinessInfo, list.Select(item => ((Object)(Convert.ToInt64(item["Id"])))).ToArray(), "Submit"); } if (!result.IsSuccess) { return result; } bool systemParamter = Convert.ToBoolean(Kingdee.BOS.ServiceHelper.SystemParameterServiceHelper.GetParamter(ctx, OrgId, 0, "AP_SystemParameter", "F_VTRAutoReceiveBillPayable"));//获取系统参数生成应付单是否创建状态 if (option.IsAudit && list.Count > 0 && systemParamter==false) { result = AppServiceContext.SubmitService.Submit(ctx, metaData.BusinessInfo, list.Select(item => ((Object)(Convert.ToInt64(item["Id"])))).ToArray(), "Submit"); if (!result.IsSuccess) { return result; } List<KeyValuePair<object, object>> keyValuePairs = new List<KeyValuePair<object, object>>(); list.ForEach(item => { keyValuePairs.Add(new KeyValuePair<object, object>(item.GetPrimaryKeyValue(), item)); } ); List<object> auditObjs = new List<object>(); auditObjs.Add("1"); auditObjs.Add(""); //Kingdee.BOS.Util.OperateOptionUtils oou = null; OperateOption ooption = OperateOption.Create(); ooption.SetIgnoreWarning(false); ooption.SetIgnoreInteractionFlag(true); ooption.SetIsThrowValidationInfo(false); result = AppServiceContext.SetStatusService.SetBillStatus(ctx, metaData.BusinessInfo, keyValuePairs, auditObjs, "Audit", ooption); option.BillStatusOptionResult = ooption; if (!result.IsSuccess) { return result; } } return result; }
}//end method /// <summary> /// 执行下推生成目标单据。 /// </summary> /// <param name="ctx">上下文对象。</param> /// <param name="args">生成目标单据参数。</param> /// <returns>返回操作结果。</returns> public IOperationResult Push(Context ctx, GenTargetArgs[] genArgs) { var operationResult = new OperationResult(); if (!genArgs.Any()) { return(operationResult); } var convertService = ServiceHelper.GetService <IConvertService>(); var doNothingService = ServiceHelper.GetService <IDoNothingService>(); var uploadOption = default(OperateOption); var proxy = new BillBenchPlugInProxy(); foreach (var arg in genArgs) { //初始化单据工作台代理。 var metadata = FormMetaDataCache.GetCachedFormMetaData(ctx, arg.TargetFormId); var billView = metadata.CreateBillView(ctx); var executeContext = new BOSActionExecuteContext(billView); proxy.Initialize(ctx, billView); var rule = convertService.GetConvertRule(ctx, arg.ConvertRuleId, true).Adaptive(item => item == null ? default(ConvertRuleElement) : item.Rule); if (rule == null) { throw new KDBusinessException(string.Empty, "未配置有效的单据转换规则!"); } var sourceEntryKey = rule.Policies.Where(p => p is DefaultConvertPolicyElement) .Select(p => p.ToType <DefaultConvertPolicyElement>()) .FirstOrDefault().SourceEntryKey; var selectedRows = arg.DataRows.Select(row => new ListSelectedRow(row.SBillId.ToString(), row.SId.ToString(), 0, arg.SourceFormId) { EntryEntityKey = sourceEntryKey }).ToArray(); PushArgs pushArgs = new PushArgs(rule, selectedRows); proxy.FireAfterCreatePushArgs(new AfterCreatePushArgsEventArgs(rule, pushArgs)); var pushResult = convertService.Push(ctx, pushArgs); operationResult.MergeResult(pushResult); //整理下推生成的数据包。 var dataEntities = pushResult.TargetDataEntities.Select(data => data.DataEntity).ToArray(); //逐行赋值。 foreach (var data in dataEntities) { billView.Edit(data); billView.RuleContainer.Suspend(); billView.Model.SetValue("FDate", arg.Date); billView.Model.SetItemValueByID("FPHMXWMSFormId", arg.ObjectTypeId, -1); billView.Model.SetValue("FPHMXWMSBillId", arg.BillId.ToString()); billView.Model.SetValue("FPHMXWMSBillNo", arg.BillNo); proxy.FireAfterCreateTargetData(new AfterCreateTargetDataEventArgs(rule, operationResult, arg.DataRows)); billView.RuleContainer.Resume(executeContext); }//end foreach //调用上传操作完成暂存、保存、提交、审核一系列操作。 var uploadOperation = metadata.BusinessInfo.GetForm().FormOperations.Where(operation => operation.Operation.Contains("Upload")).FirstOrDefault(); if (uploadOperation == null) { throw new KDBusinessException(string.Empty, "未配置有效的上传操作!"); }//end if uploadOption = OperateOption.Create(); uploadOption.SetIgnoreWarning(true); uploadOption.SetIgnoreInteractionFlag(true); uploadOption.SetThrowWhenUnSuccess(false); proxy.FireBeforeUploadTargetData(new BeforeUploadTargetDataEventArgs(rule, dataEntities, arg, uploadOption)); try { var uploadResult = doNothingService.DoNothingWithDataEntity(ctx, metadata.BusinessInfo, dataEntities, uploadOperation.Operation, uploadOption); operationResult.MergeResult(uploadResult); } catch { var inner = uploadOption.GetVariableValue <IOperationResult>(BOSConst.CST_KEY_OperationResultKey); if (inner != null) { operationResult.MergeResult(inner); } else { throw; } } //end catch } //end foreach return(operationResult); }//end method