上游单据下推资产卡片使用分配信息二开示例原创
金蝶云社区-H4
H4
10人赞赏了该文章 324次浏览 未经作者许可,禁止转载编辑于2024年03月18日 09:34:54
1.继承AbstractConvertPlugIn,写一个单据转换插件,然后注册到单据转换规则就可以生效,示例代码如下


[Description("上游单据下推->卡片使用分配")]
    public class SourceToCard : AbstractConvertPlugIn
    {
        private DynamicObjectCollection SourceData;
        public override void OnGetDrawSourceData(GetDrawSourceDataEventArgs e)
        {
            base.OnGetDrawSourceData(e);
            SourceData = e.SourceData;
        }
        public override void OnGetSourceData(GetSourceDataEventArgs e)
        {
            base.OnGetSourceData(e);
            this.SourceData = e.SourceData;
        }
        /// <summary>
        /// 添加需要查询的字段
        /// </summary>
        /// <param name="e"></param>
        public override void OnQueryBuilderParemeter(QueryBuilderParemeterEventArgs e)
        {
            //按需加载上游使用部门,使用人,分配比例,费用项目,开始结束日期等
            e.SelectItems.AddRange(SelectorItemInfo.CreateItems(new[] {  "FSTARTDATE", "FENDDATE", "FUSEDEPTID", "FUSERID", "FRATIO", "FCOSTITEMID"}));
        }

        public override void AfterConvert(AfterConvertEventArgs e)
        {
            var dataEntitys = e.Result.FindByEntityKey("FBillHead");
            DynamicObject sourceDataObj;
            IMetaDataService metaService = ServiceHelper.GetService<IMetaDataService>();
            IViewService viewService = ServiceFactory.GetViewService(this.Context);
            for (int i = 0; i < dataEntitys.Length; i++)
            {
                DynamicObjectCollection cardDetails=dataEntitys[i].DataEntity["CardDetail"] as DynamicObjectCollection;
                if (cardDetails.Count==0)
                {
                    continue;
                }
                DynamicObjectCollection links = null;
                //数量大于零的行,才是有效行
                foreach (var item in cardDetails)
                {
                    decimal dclQty = Convert.ToDecimal(item["Quantity"]);
                    if (dclQty > 0)
                    {
                        links = item["FCardDetail_Link"] as DynamicObjectCollection;
                    }
                }
                if (links == null || links.Count == 0)
                {
                    continue;
                }
                long lngSourceId = Convert.ToInt64(links[0]["SID"]);
                var SourceDataTemp = SourceData.Where(p => Convert.ToInt64(p["FID"]) == lngSourceId).ToList();
                sourceDataObj = SourceDataTemp[0];
                var dataEntity = dataEntitys[i];
                //处理使用分配信息
                var allocColl = dataEntity["Allocation"] as DynamicObjectCollection;
                allocColl.Clear();
                Entity allocEntity = e.TargetBusinessInfo.GetEntity("FAllocation");
                FormMetadata formMetadataDept = (FormMetadata)metaService.Load(this.Context, "BD_Department", true);
                FormMetadata formMetadataUser = (FormMetadata)metaService.Load(this.Context, "BD_NEWSTAFF", true);
                FormMetadata formMetadataCostItem = (FormMetadata)metaService.Load(this.Context, "BD_Expense", true);
                int intSeq = 1;
                string strAssetno = SetAssetNoIfEmpty(dataEntity.DataEntity);
                foreach (var item in SourceDataTemp)
                {
                    long lngUseDept = Convert.ToInt64(item["FUSEDEPTID"]);
                    long lngUser = Convert.ToInt64(item["FUSERID"]);
                    decimal dclRatio = Convert.ToDecimal(item["FRATIO"]);
                    long lngCostItem = Convert.ToInt64(item["FCOSTITEMID"]);
                    DynamicObject alloc = (DynamicObject)allocEntity.DynamicObjectType.CreateInstance();
                    DynamicObject objUseDept = viewService.LoadSingle(this.Context, lngUseDept, formMetadataDept.BusinessInfo.GetDynamicObjectType());
                    DynamicObject objCostItem = viewService.LoadSingle(this.Context, lngCostItem, formMetadataCostItem.BusinessInfo.GetDynamicObjectType());
                    alloc["Seq"] = intSeq++;
                    alloc["AllocAssetNO"] = strAssetno;
                    alloc["AllocUseDeptID"] = objUseDept;
                    alloc["AllocUseDeptID_ID"] = lngUseDept;
                    if (lngUser > 0)
                    {
                        DynamicObject objUser = viewService.LoadSingle(this.Context, lngUser, formMetadataUser.BusinessInfo.GetDynamicObjectType());
                        alloc["AllocUserID"] = objUser;
                        alloc["AllocUserID_ID"] = lngUser;
                    }
                    alloc["AllocRatio"] = dclRatio;
                    alloc["AllocCostItemID"] = objCostItem;
                    alloc["AllocCostItemID_ID"] = lngCostItem;
                    alloc["AllocBeginDate"] = item["FSTARTDATE"];
                    alloc["AllocEndDate"] = item["FENDDATE"];
                    allocColl.Add(alloc);
                }
            }
        }
        /// <summary>
        /// 若资产编码为空,按规则设值
        /// </summary>
        private string SetAssetNoIfEmpty(DynamicObject headData)
        {
            //实物单据体数据
            var detailData = (DynamicObjectCollection)headData["CardDetail"];
            var numEmptyDatas = detailData.Where(p => string.IsNullOrWhiteSpace(Convert.ToString(p["AssetNo"]))).ToArray();
            if (numEmptyDatas.Length == 0) return "";

            var assetType = (DynamicObject)headData["AssetTypeId"];
            if (assetType == null) return "";

            var cardService = new CardService();
            string[] codes = cardService.GetAssetCodeByRule(this.Context, headData, 1, true, new List<string>());
            //数量大于零的行才是有效行
            foreach (var item in numEmptyDatas)
            {
                decimal dclQty = Convert.ToDecimal(item["Quantity"]);
                if (dclQty > 0)
                {
                    item["AssetNo"] = codes[0];
                    return codes[0];
                }
            }
            return codes[0];
        }
        
    }


赞 10