[K/3Cloud] 创建一个单据转换插件

时间:2021-08-05 07:06:57

概念:

创建一个业务单据转换插件,在单据转换的各个时点干预单据转换的相关逻辑控制。

示例:

新建一个类,继承自单据转换插件基类Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.AbstractConvertPlugIn。

using Kingdee.BOS;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using System;
namespace Kingdee.K3.Example.App.ServicePlugIn
{
/// <summary>
/// 销售出库单到外购入库单的单据转换插件
/// </summary>
public class OutStockToInStockConvertPlugIn : Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.AbstractConvertPlugIn
{
// Summary:
// 最后触发:单据转换后事件
//
// Parameters:
// e:
public override void AfterConvert(AfterConvertEventArgs e)
{
}
//
// Summary:
// 创建关联关系后事件
//
// Parameters:
// e:
// 事件参数包
public override void OnAfterCreateLink(CreateLinkEventArgs e)
{
}
//
// Summary:
// 下推/选单,自动填充目标字段值完毕
//
// Parameters:
// e:
public override void OnAfterFieldMapping(AfterFieldMappingEventArgs e)
{
}
//
// Summary:
// 下推/选单时,根据字段映射,向目标字段填充值之前
//
// Parameters:
// e:
public override void OnBeforeFieldMapping(BeforeFieldMappingEventArgs e)
{
}
//
// Summary:
// 取源单数据前事件。
//
// Parameters:
// e:
public override void OnBeforeGetSourceData(BeforeGetSourceDataEventArgs e)
{
}
//
// Summary:
// 源数据分组:把全部分录合并到一个目标单据
//
// Parameters:
// e:
// 事件参数包
public override void OnBeforeGroupBy(BeforeGroupByEventArgs e)
{
}
//
// Summary:
// 选单时,初始化创建目标单据数据包(输出一个数据对象)
//
// Parameters:
// e:
// 事件参数包
public override void OnCreateDrawTarget(CreateDrawTargetEventArgs e)
{
}
//
// Summary:
// 创建关联关系
//
// Parameters:
// e:
// 事件参数包
public override void OnCreateLink(CreateLinkEventArgs e)
{
}
//
// Summary:
// 下推时,根据源数据分组结果,初始化创建目标单据数据包
//
// Parameters:
// e:
// 事件参数包
public override void OnCreateTarget(CreateTargetEventArgs e)
{
}
//
// Summary:
// 下推/选单时,根据字段映射,向目标字段填充值
//
// Parameters:
// e:
public override void OnFieldMapping(FieldMappingEventArgs e)
{
}
//
// Summary:
// 获取单据转换中自定义表单服务
//
// Parameters:
// e:
public override void OnGetConvertBusinessService(ConvertBusinessServiceEventArgs e)
{
}
//
// Summary:
// 选单时,执行SQL,读取源单数据
//
// Parameters:
// e:
// 事件参数包
public override void OnGetDrawSourceData(GetDrawSourceDataEventArgs e)
{
}
//
// Summary:
// 下推时,执行SQL,读取源单数据
//
// Parameters:
// e:
// 事件参数包
public override void OnGetSourceData(GetSourceDataEventArgs e)
{
}
//
// Summary:
// 初始化变量,这个事件可以得到转换规则,源单和目标单的BussinessInfo
//
// Parameters:
// e:
// 事件参数包
public override void OnInitVariable(InitVariableEventArgs e)
{
}
//
// Summary:
// 根据选择行生成取数SQL的条件子句 ID in ()语句。
//
// Parameters:
// e:
// 事件参数包
public override void OnInSelectedRow(InSelectedRowEventArgs e)
{
}
//
// Summary:
// 选单前事件:解析过滤策略中配置的条件;
//
// Parameters:
// e:
// 事件参数包
public override void OnParseFilter(ParseFilterEventArgs e)
{
}
//
// Summary:
// 选单前事件;解析字段映射关系中配置的过滤选项;
//
// Parameters:
// e:
// 事件参数包
public override void OnParseFilterOptions(ParseFilterOptionsEventArgs e)
{
}
//
// Summary:
// 解析字段映射关系,并构建查询参数。 这里可以加入你想要的字段
//
// Parameters:
// e:
// 事件参数包
public override void OnQueryBuilderParemeter(QueryBuilderParemeterEventArgs e)
{
}
//
// Summary:
// 设置钩稽数量
//
// Parameters:
// e:
// 事件参数包
public override void OnSetLinkAmount(SetLinkAmountEventArgs e)
{
}
}
}

K3Cloud案例

采购申请单到采购订单的单据转换插件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Orm.Metadata;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.BusinessEntity.Organizations;
using Kingdee.BOS.Resource;
using Kingdee.K3.SCM.Contracts;
using Kingdee.K3.SCM.App.Utils;
namespace Kingdee.K3.SCM.App.Pur.ServicePlugIn
{
/// <summary>
/// 采购申请单到采购订单的单据转换插件
/// </summary>
public class PurReqToOrderService : AbstractConvertPlugIn
{ public override void OnAfterFieldMapping(BOS.Core.Metadata.ConvertElement.PlugIn.Args.AfterFieldMappingEventArgs e)
{
long assignSupplierId = 0;
ExtendedDataEntity[] heads = e.TargetExtendDataEntitySet.FindByEntityKey("FBillHead");
if (heads == null)
{
return;
}
foreach (ExtendedDataEntity head in heads)
{
//映射过来表头指定供应商有值的更改供应商字段值为指定供应商的值
assignSupplierId = Convert.ToInt64(head.DataEntity["AssignSupplierId_Id"]);
if (assignSupplierId != 0)
{
SetSupplierId(e, assignSupplierId, head);
}
DynamicObject supplierObj = head.DataEntity["SupplierId"] as DynamicObject;
if (supplierObj != null)
{
DynamicObjectCollection porderFinance = head.DataEntity["POOrderFinance"] as DynamicObjectCollection;
DynamicObjectCollection supplierBusiness = supplierObj["SupplierFinance"] as DynamicObjectCollection;
if (supplierBusiness != null && supplierBusiness.Count > 0)
{
long payConditionId = Convert.ToInt64(supplierBusiness[0]["PayCondition_Id"]);
BaseDataField payConditionIdOjb = e.TargetBusinessInfo.GetField("FPayConditionId") as BaseDataField;
FieldUtils.SetBaseDataFieldValue(this.Context, payConditionIdOjb, porderFinance[0], payConditionId);
}
}
}
} public override void OnFieldMapping(BOS.Core.Metadata.ConvertElement.PlugIn.Args.FieldMappingEventArgs e)
{
if (e.TargetField.Key.ToUpperInvariant() == "FSUPPLIERID")
{
if (this.OperationNumber.EqualsIgnoreCase("Draw"))
{
if (Convert.ToInt64(e.ExtendedDataEntity.DataEntity["SupplierId_Id"]) > 0)
{
e.Cancel = true;
}
}
else
{
List<DynamicObject> sourceDatas = e.ExtendedDataEntity["ConvertSource"] as List<DynamicObject>;
long suggestSupplierId = sourceDatas.Where(p => p != null && Convert.ToInt64(p["FSuggestSupplierId"]) > 0).Select(p => Convert.ToInt64(p["FSuggestSupplierId"])).FirstOrDefault();
e.MapValue = suggestSupplierId;
}
} base.OnFieldMapping(e);
} private void SetSupplierId(BOS.Core.Metadata.ConvertElement.PlugIn.Args.AfterFieldMappingEventArgs e, long supplierId, ExtendedDataEntity head)
{
head.DataEntity["SupplierId_Id"] = supplierId;
BaseDataField supplier = e.TargetBusinessInfo.GetField("FSupplierId") as BaseDataField;
FieldUtils.SetBaseDataFieldValue(this.Context, supplier, head.DataEntity, supplierId);
}
}
}