【已解决】子单据体转换插件问题
金蝶云社区-挥着AK的悟空
挥着AK的悟空
3人赞赏了该文章 4,092次浏览 未经作者许可,禁止转载编辑于2017年10月24日 20:16:29

//


//主单据体的字段携带完毕,与源单的关联关系创建好之后,触发此事件
//

//
public override void OnAfterCreateLink(CreateLinkEventArgs e)
{
// 预先获取一些必要的元数据,后续代码要用到:
// 源单第二单据体
Entity srcSecondEntity = e.SourceBusinessInfo.GetEntity("FEntity2");

//目标单第一单据体
Entity mainEntity = e.TargetBusinessInfo.GetEntity("FPOOrderEntry");

SubEntryEntity secondEntity = e.TargetBusinessInfo.GetEntity("FSPOOrderEntry") as SubEntryEntity;

// 目标单关联子单据体
Entity linkEntity = null;
Form form = e.TargetBusinessInfo.GetForm();
if (form.LinkSet != null
&& form.LinkSet.LinkEntitys != null
&& form.LinkSet.LinkEntitys.Count != 0)
{
linkEntity = e.TargetBusinessInfo.GetEntity(
form.LinkSet.LinkEntitys[0].Key);
}

if (linkEntity == null)
{
return;
}

// 获取生成的全部下游单据
ExtendedDataEntity[] billDataEntitys = e.TargetExtendedDataEntities.FindByEntityKey("FBillHead");
//这里只能获取到主单据体,没有子单据体的任何数据,这里要怎样修改啊?

// 对下游单据,逐张单据进行处理
foreach (var item in billDataEntitys)
{
DynamicObject dataObject = item.DataEntity;

// 定义一个集合,用于收集本单对应的源单内码
HashSet srcBillIds = new HashSet();

// 开始到主单据体中,读取关联的源单内码
DynamicObjectCollection mainEntryRows =
mainEntity.DynamicProperty.GetValue(dataObject) as DynamicObjectCollection;
foreach (var mainEntityRow in mainEntryRows)
{
DynamicObjectCollection linkRows =
linkEntity.DynamicProperty.GetValue(mainEntityRow) as DynamicObjectCollection;
foreach (var linkRow in linkRows)
{
long srcBillId = Convert.ToInt64(linkRow["SBillId"]);
if (srcBillId != 0
&& srcBillIds.Contains(srcBillId) == false)
{
srcBillIds.Add(srcBillId);
}
}
}
if (srcBillIds.Count == 0)
{
continue;
}
//开始加载源单第二单据体上的字段

// 确定需要加载的源单字段(仅加载需要携带的字段)
List selector = new List();
selector.Add(new SelectorItemInfo("FBJBZ"));
// TODO: 继续添加其他需要携带的字段,示例代码略
// 设置过滤条件
string filter = string.Format(" {0} IN ({1}) ",
e.SourceBusinessInfo.GetForm().PkFieldName,
string.Join(",", srcBillIds));
OQLFilter filterObj = OQLFilter.CreateHeadEntityFilter(filter);

//读取源单
IViewService viewService = ServiceHelper.GetService();
var srcBillObjs = viewService.Load(this.Context,
e.SourceBusinessInfo.GetForm().Id,
selector,
filterObj);

//开始把源单单据体数据,填写到目标单上
if (secondEntity != null)
{

DynamicObjectCollection secondEntryRows =
secondEntity.DynamicProperty.GetValue(dataObject) as DynamicObjectCollection;
secondEntryRows.Clear(); // 删除空行

foreach (var srcBillObj in srcBillObjs)
{
DynamicObjectCollection srcEntryRows =
srcSecondEntity.DynamicProperty.GetValue(srcBillObj) as DynamicObjectCollection;

foreach (var srcEntryRow in srcEntryRows)
{
// 目标单添加新行,并接受源单字段值
DynamicObject newRow = new DynamicObject(secondEntity.DynamicObjectType);
secondEntryRows.Add(newRow);
// 填写字段值
newRow["FXMBZ"] = srcEntryRow["FBJBZ"];
// TODO: 逐个填写其他字段值,示例代码略
}
}
}
}
}
}
}