注意事项:
修改目标单据对应字段的值,而不是源单,因为源单在转换过程中没有结构数据包,无法触发实体服务等。
注意调用字段对应的实体服务,因为字段往往是关联存在的
需要在运行转换实体服务之前修改,一般在OnAfterCreateLink事件中处理
示例代码:
销售订单下推销售出库单,把销售订单的每行可出数量(销售基本)的携带值改成10,则通过转换规则找到对应的销售出库单的销售基本数量字段是需要修改的字段。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
namespace Kingdee.BOS.TestPlugIn.ConvertPlugIn
{
[HotUpdate]
[Description("修改携带值")]
public class ModifyTakeValueConvertPlugIn : AbstractConvertPlugIn
{
// 关联关系创建之后事件
public override void OnAfterCreateLink(CreateLinkEventArgs e)
{
//创建目标单据视图
var tView = this.CreateView(e.TargetBusinessInfo.GetForm().Id);
var exBillDatas = e.TargetExtendedDataEntities.FindByEntityKey("FBillHead"); //目标单据扩展数据包集合
var field = e.TargetBusinessInfo.GetField("FSALBASEQTY"); //销售出库单中的销售基本数量
int modifyValue = 10; //修改值
var linkEntity = e.TargetBusinessInfo.GetForm().LinkSet.LinkEntitys[0]; //关联实体
foreach (var exData in exBillDatas)
{
tView.Model.DataObject = exData.DataEntity;
//字段所在的分录数据包,如果字段在单据头,则直接使用exData.DataEntity即可
var entryDatas = field.Entity.DynamicProperty.GetValue(exData.DataEntity) as DynamicObjectCollection;
int rowIndex = 0;
foreach (var entryData in entryDatas)
{
tView.Model.SetValue(field, entryData, modifyValue); //这字段值并且会触发值更新事件
tView.InvokeFieldUpdateService(field.Key, rowIndex); //调用实体服务规则
this.UpdateLinkInfo(entryData, linkEntity, field, modifyValue); //更新关联信息
rowIndex++;
}
}
}
// 更新关联数据包
public void UpdateLinkInfo(DynamicObject entityData, LinkEntity linkEntity, Field ctlField, Decimal updateValue)
{
if (linkEntity.WriteBackFieldKeys.Contains(ctlField.Key))
{
var linkDatas = entityData[linkEntity.Key] as DynamicObjectCollection;
linkDatas[0][ctlField.PropertyName + "Old"] = updateValue;
linkDatas[0][ctlField.PropertyName] = updateValue;
}
}
// 创建表单视图
public IDynamicFormView CreateView(string formId)
{
FormMetadata metadata = FormMetaDataCache.GetCachedFormMetaData(this.Context, formId);
var OpenParameter = CreateOpenParameter(this.Context, metadata);
var Provider = metadata.BusinessInfo.GetForm().GetFormServiceProvider(true);
string importViewClass = "Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web";
Type type = Type.GetType(importViewClass);
IDynamicFormView view = (IDynamicFormView)Activator.CreateInstance(type);
((IDynamicFormViewService)view).Initialize(OpenParameter, Provider);
return view;
}
// 构建视图所需参数
private BillOpenParameter CreateOpenParameter(Context ctx, FormMetadata metaData)
{
Form form = metaData.BusinessInfo.GetForm();
BillOpenParameter openPara = new BillOpenParameter(form.Id, metaData.GetLayoutInfo().Id);
openPara = new BillOpenParameter(form.Id, string.Empty);
openPara.Context = ctx;
openPara.ServiceName = form.FormServiceName;
openPara.PageId = Guid.NewGuid().ToString();
openPara.FormMetaData = metaData;
openPara.LayoutId = metaData.GetLayoutInfo().Id;
openPara.Status = OperationStatus.ADDNEW;
openPara.PkValue = null;
openPara.CreateFrom = CreateFrom.Default;
openPara.ParentId = 0;
openPara.GroupId = "";
openPara.DefaultBillTypeId = null;
openPara.DefaultBusinessFlowId = null;
openPara.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);
List<AbstractDynamicFormPlugIn> plugins = form.CreateFormPlugIns();
openPara.SetCustomParameter(FormConst.PlugIns, plugins);
return openPara;
}
}
}
推荐阅读