如何經過插件攜帶第二個單據體到下游單據

背景說明:
K/3 Cloud 不支持在下推時,同時攜帶兩個單據體的數據;
所以,須要在轉換規則上,配置好攜帶主單據體;
而輔助單據體的數據,須要在插件中自行攜帶;

案例說明:
本案例,設計了兩張單據A和B,都有兩個單據體,主單據體爲FEntity,第二單據體爲FEntity2;
主單據體的字段,已經配置的單據轉換規則 - 字段映射關係;
而第二單據體,須要經過插件攜帶;
第二單據體僅有一個文本字段F_JD_Text;

特別說明:
本案例不適用於同一張源單同時下推爲多張目標單的狀況:
這樣狀況是分單了,而第二單據體,沒法對應拆分;

示例代碼:css

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.App;

namespace JDSample.ServicePlugIn.BillConvert
{
    /// <summary>
    /// 攜帶其餘單據體
    /// </summary>
    [Description("攜帶其餘單據體")]
    public class S151105TakeOtherEntryConvPlug : AbstractConvertPlugIn
    {
        /// <summary>
        /// 主單據體的字段攜帶完畢,與源單的關聯關係建立好以後,觸發此事件
        /// </summary>
        /// <param name="e"></param>
        public override void OnAfterCreateLink(CreateLinkEventArgs e)
        {
            // 預先獲取一些必要的元數據,後續代碼要用到:
            // 源單第二單據體
            Entity srcSecondEntity = e.SourceBusinessInfo.GetEntity("FEntity2");

            // 目標單第一單據體
            Entity mainEntity = e.TargetBusinessInfo.GetEntity("FEntity");

            // 目標單第二單據體
            Entity secondEntity = e.TargetBusinessInfo.GetEntity("FEntity2");

            // 目標單關聯子單據體
            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<long> srcBillIds = new HashSet<long>();
               
                // 開始到主單據體中,讀取關聯的源單內碼
                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<SelectorItemInfo> selector = new List<SelectorItemInfo>();
                selector.Add(new SelectorItemInfo("F_JD_Text"));
                // TODO: 繼續添加其餘須要攜帶的字段,示例代碼略
                // 設置過濾條件
                string filter = string.Format(" {0} IN ({1}) ",
                    e.SourceBusinessInfo.GetForm().PkFieldName,
                    string.Join(",", srcBillIds));
                OQLFilter filterObj = OQLFilter.CreateHeadEntityFilter(filter);
               
                // 讀取源單
                IViewService viewService = ServiceHelper.GetService<IViewService>();
                var srcBillObjs = viewService.Load(this.Context,
                    e.SourceBusinessInfo.GetForm().Id,
                    selector,
                    filterObj);

                // 開始把源單單據體數據,填寫到目標單上
                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["F_JD_Text"] = srcEntryRow["F_JD_Text"];
                        // TODO: 逐個填寫其餘字段值,示例代碼略
                    }
                }
            }
        }
    }
}
相關文章
相關標籤/搜索