二开案例-【C#】如何获取物料清单成本查询结果
【应用场景】
需要根据指定bom获取物料清单成本查询结果,并将结果在其他场景使用。
【注意事项】
该实现方案实现已指定bom内码实现,相关参数都设置为默认,演示通过按钮触发,开发者可根据自身需求将代码封装自定义WebAPI接口。
星空版本不能低于PT-154002 [8.2.0.20240104]版本
【案例演示】
点击按钮,指定bom版本,获取物料清单成本查询结果。
【实现步骤】
需要引用组件
Kingdee.BOS
Kingdee.BOS.Core
Kingdee.BOS.Orm
Kingdee.K3.BD.ServiceHelper
Kingdee.K3.Core
Kingdee.K3.MFG.ServiceHelper
Kingdee.BOS.ServiceHelper
<1>编写表单插件,代码如下。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using Kingdee.BOS; using Kingdee.BOS.Core.DynamicForm; using Kingdee.BOS.Core.DynamicForm.PlugIn; using Kingdee.BOS.Core.DynamicForm.PlugIn.Args; using Kingdee.BOS.Core.Metadata; using Kingdee.BOS.Core.Metadata.EntityElement; using Kingdee.BOS.Core.SqlBuilder; using Kingdee.BOS.Orm; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.ServiceHelper; using Kingdee.BOS.Util; using Kingdee.K3.BD.ServiceHelper; using Kingdee.K3.Core.BD; using Kingdee.K3.Core.BD.ServiceArgs; using Kingdee.K3.Core.MFG.ENG.BomCost; using Kingdee.K3.Core.MFG.ENG.BomExpand; using Kingdee.K3.Core.MFG.ENG.ParamOption; using Kingdee.K3.MFG.ServiceHelper; using Kingdee.K3.MFG.ServiceHelper.ENG; namespace ITIBB.App.ServicePlugIn.Business.PlugIn { /// <summary> /// 自定义按钮 通过传递bom获取物料清单成本查询结果 /// </summary> [Description("物料清单成本查询按钮调用")] public class CustomExtBomQueryCost : AbstractDynamicFormPlugIn { public override void ButtonClick(ButtonClickEventArgs e) { base.ButtonClick(e); if (e.Key.EqualsIgnoreCase("FButton_qtr")) { //传递参数,指定bom内码 List<long> bomIds = new List<long>(); bomIds.Add(144259); bomIds.Add(144290); //获取bom展开结果 List<DynamicObject> bomExpandData = this.ExtFillBomChildData(this.View.Context, bomIds); //物料清单成本查询相关参数设置 BomCostArgs bomCostArgs = this.ExtGetQueryViewOption(this.View.Context); //调用物料清单成本查询取价接口 IOperationResult funcResult = BomQueryCostServiceHelper.GetQueryBomPrice(this.Context, bomCostArgs, bomExpandData); if (funcResult.IsSuccess) { //物料清单成本查询数据包 List<DynamicObject> resultBomDataLst = (List<DynamicObject>)funcResult.FuncResult; } } } /// <summary> /// 构建价格查询参数 /// </summary> /// <param name="ctx">上下文</param> private BomCostArgs ExtGetQueryViewOption(Context ctx) { BomCostArgs bomCostArgs = new BomCostArgs(); DynamicObject viewDatas = this.View.Model.DataObject; //需求数量,默认是1 bomCostArgs.NeetQty = 1; //成本计算方式,默认是1 bomCostArgs.CostType = "1"; //取价截止日期,默认当前时间 bomCostArgs.ExpirationDate = DateTime.Now; //排除供应商,默认为空 List<long> excSuppliers = new List<long>(); bomCostArgs.ExcSupplierMul = excSuppliers; //包含委外加工费,默认false bomCostArgs.IsSubProcessFee = false; //外购件BOM子项不参与卷算,默认false bomCostArgs.IsWgCost = false; //取折后价,默认false bomCostArgs.IsDiscountedPrice = false; //bom使用组织,默认当前登录组织 long bomUseOrgId = this.Context.CurrentOrganizationInfo.ID; bomCostArgs.BomUseOrgId = bomUseOrgId; //有采购业务优先取采购价格,默认false bomCostArgs.PurPriceCheck = false; //以下参数均来自工程数据参数-物料清单成本查询 //取价范围、 优先取使用组织下,使用组织下没有,则取管理中心级别 long takeDays = MFGServiceHelper.GetSystemProfile<long>(this.Context, bomUseOrgId, "MFG_EngParameter", "DateScope", 0); if (takeDays == 0) { //管理中心取不到,则默认180天 takeDays = MFGServiceHelper.GetSystemProfile<long>(this.Context, 0, "MFG_EngParameter", "DateScope", 180); } bomCostArgs.TakeDays = takeDays; //单据来源 string priceSourceBill = MFGServiceHelper.GetSystemProfile<string>(this.Context, bomUseOrgId, "MFG_EngParameter", "SourceBill", ""); if (priceSourceBill.IsNullOrEmptyOrWhiteSpace()) { //管理中心取不到,则默认采购订单返回 priceSourceBill = MFGServiceHelper.GetSystemProfile<string>(this.Context, 0, "MFG_EngParameter", "SourceBill", "ORDER"); } bomCostArgs.PriceSourceBill = priceSourceBill; //单价金额用量 string costQtyParam = MFGServiceHelper.GetSystemProfile<string>(this.Context, bomUseOrgId, "MFG_EngParameter", "CostQtyParam"); if (costQtyParam.IsNullOrEmptyOrWhiteSpace()) { //管理中心取不到,则默认实际用量返回 costQtyParam = MFGServiceHelper.GetSystemProfile<string>(this.Context, 0, "MFG_EngParameter", "CostQtyParam", "1"); } bomCostArgs.CostQtyParam = costQtyParam; //价目表及参考成本汇率来源 string sourceRate = MFGServiceHelper.GetSystemProfile<string>(this.Context, bomUseOrgId, "MFG_EngParameter", "SourceRate"); if (sourceRate.IsNullOrEmptyOrWhiteSpace()) { //管理中心取不到,则默认即期汇率返回 sourceRate = MFGServiceHelper.GetSystemProfile<string>(this.Context, 0, "MFG_EngParameter", "SourceRate", "SPOT"); } bomCostArgs.SourceRate = sourceRate; //取价范围、 优先取使用组织下,使用组织下没有,则取管理中心级别 string excSetAccountTpye = MFGServiceHelper.GetSystemProfile<string>(this.Context, bomUseOrgId, "MFG_EngParameter", "ExcSetAccountTpye'", "0"); bomCostArgs.ExcSetAccountTpye = excSetAccountTpye; //日期排序 string orderByDateField = MFGServiceHelper.GetSystemProfile<string>(this.Context, bomUseOrgId, "MFG_EngParameter", "OrderByDateField", "2"); bomCostArgs.OrderByField = orderByDateField; //其他扩展选项 bomCostArgs.Option = OperateOption.Create(); return bomCostArgs; } private List<DynamicObject> ExtFillBomChildData(Context ctx, List<long> bomIds) { List<DynamicObject> bomExpandData = new List<DynamicObject>(); //设置bom展开参数 MemBomExpandOption_ForPSV memBomExpandOption = this.ExtBomQueryExpandOption(); //设置展开数据源 List<DynamicObject> lstExpandSource = this.ExtBuildBomExpandSourceData(ctx, bomIds); //调用物料清单成本查询bom展开 bomExpandData = BomQueryServiceHelper.GetBomQueryForwardResult(this.Context, lstExpandSource, memBomExpandOption); //展开结果加载引用属性 FormMetadata formMetadata = (FormMetadata)Kingdee.BOS.ServiceHelper.MetaDataServiceHelper.Load(ctx, "ENG_BomQueryCost", true); EntryEntity childEntity = formMetadata.BusinessInfo.GetEntryEntity("FBottomEntity"); DBServiceHelper.LoadReferenceObject(this.View.Context, bomExpandData.ToArray(), childEntity.DynamicObjectType, false); return bomExpandData; } /// <summary> /// 构建bom展开参数,以下参数的设置尽量保持一致 /// </summary> /// <returns></returns> private MemBomExpandOption_ForPSV ExtBomQueryExpandOption() { //初始化BOM展开选项 MemBomExpandOption_ForPSV memBomExpandOption = new MemBomExpandOption_ForPSV(); //BOM展开层级数 默认30层 memBomExpandOption.ExpandLevelTo = 30; //是否要展开虚拟物料 memBomExpandOption.ExpandVirtualMaterial = false; //不要删除虚拟件,否则会造成BOM树表层级中断,个别子项不显示 memBomExpandOption.DeleteVirtualMaterial = false; //是否删除跳层行 memBomExpandOption.DeleteSkipRow = false; //是否展开跳层行 memBomExpandOption.ExpandSkipRow = false; //只显示采购件需指定为false,因为默认值是true memBomExpandOption.IsShowOutSource = false; //本次展开GUID memBomExpandOption.BomExpandId = SequentialGuid.NewGuid().ToString(); //BOM展开的第一层是否考虑成品率 默认为FALSE memBomExpandOption.ParentCsdYieldRate = true; //BOM展开的子项是否考虑成品率 默认为FALSE memBomExpandOption.ChildCsdYieldRate = true; //展开结果处理方式:采用内存方式不需要保存进数据库 memBomExpandOption.Mode = Kingdee.K3.Core.MFG.EnumConst.Enums.BomExpand.Enu_BomExpandMode.KdMemoryOnly; //BOM子项生效失效区间内需求日期 此处用当前电脑时间,可以自由指定,标准产品是需求日期 memBomExpandOption.ValidDate = DateTime.Now; //获取工程数据参数【kdBySystemProfile:取系统参数 kdByPrecisonCal:采用精确计 kdByTolerantCal:采用容错算法】 memBomExpandOption.BomExpandCalType = Kingdee.K3.Core.MFG.EnumConst.Enums.Enu_BomExpandCalType.kdBySystemProfile; //BOM展开是否数量单位换算 memBomExpandOption.IsConvertUnitQty = true; //是否是物料清单成本查询 如果设置为false则结果就是物料清单正查结果 memBomExpandOption.isBomCost = true; //是否查询当前系统所有bom信息 memBomExpandOption.IsSelectAllBomInfos = true; //对应标准成本计算方式参数,默认不展开判断是否展开替代料【1:按标准件计算 2:按替代件计算 3:按标准件和替代件较低价】 string costType = "1"; if (costType.EqualsIgnoreCase("1")) { //是否考虑替代方案:默认不考虑,是否考虑替代:默认不考虑 memBomExpandOption.CsdSubstitution = false; //是否展开替代料:默认不考虑 memBomExpandOption.IsExpandSubMtrl = false; } else { //是否考虑替代方案:默认不考虑,是否考虑替代:默认不考虑 memBomExpandOption.CsdSubstitution = true; //是否展开替代料:默认不考虑 memBomExpandOption.IsExpandSubMtrl = true; } //是否展开外购件bom 默认不展开[对应物料清单成本上外购件BOM子项不参与卷算参数] memBomExpandOption.IsHideOutSourceBOM = false; return memBomExpandOption; } /// <summary> /// 获得需要展开的BOM展开数据信息 /// </summary> /// <param name="iFocusRow"></param> /// <returns></returns> private List<DynamicObject> ExtBuildBomExpandSourceData(Context ctx, List<long> bomIds) { DateTime needDate = DateTime.Now; List<DynamicObject> lstExpandSource = new List<DynamicObject>(); QueryBuilderParemeter batchParam = new QueryBuilderParemeter { FormId = "ENG_BOM", SelectItems = SelectorItemInfo.CreateItems("FID", "FMaterialId.FMASTERID as MaterialMasterId", "FMATERIALID", "FUseOrgId", "FUNITID", "FBaseUnitId", "FParentAuxPropId"), }; batchParam.ExtJoinTables.Add(new ExtJoinTableDescription { FieldName = "FID", TableName = "table(fn_StrSplit(@ids,',',1))", TableNameAs = "ts", ScourceKey = "FID", }); List<SqlParam> sqlParam = new List<SqlParam> { new SqlParam("@ids",KDDbType.udt_inttable,bomIds.Distinct().ToArray()), }; DynamicObjectCollection bomHeadObjs = QueryServiceHelper.GetDynamicObjectCollection(ctx, batchParam, sqlParam); Dictionary<Int64, DynamicObject> dctBomHeadObjs = bomHeadObjs.ToDictionary(x => Convert.ToInt64(x["FID"])); List<GetUnitConvertRateArgs> converRateArgs = new List<GetUnitConvertRateArgs>(); foreach (DynamicObject bomHeadObj in bomHeadObjs) { long materialId = Convert.ToInt64(bomHeadObj["FMATERIALID"]); long masterId = Convert.ToInt64(bomHeadObj["MaterialMasterId"]); long unitId = Convert.ToInt64(bomHeadObj["FUNITID"]); long baseUnitId = Convert.ToInt64(bomHeadObj["FBaseUnitId"]); long supplyOrgId = Convert.ToInt64(bomHeadObj["FUseOrgId"]); //此处要对数量进行换算:不能直接传常用单位数量 converRateArgs.Add(new GetUnitConvertRateArgs() { PrimaryKey = bomHeadObjs.IndexOf(bomHeadObj), MasterId = masterId, SourceUnitId = unitId, DestUnitId = baseUnitId }); } Dictionary<long, UnitConvert> dctUnitConvert = UnitConvertServiceHelper.GetUnitConvertRateList(ctx, converRateArgs); foreach (DynamicObject bomHeadObj in bomHeadObjs) { long bomId = Convert.ToInt64(bomHeadObj["FID"]); long materialId = Convert.ToInt64(bomHeadObj["FMATERIALID"]); long unitId = Convert.ToInt64(bomHeadObj["FUNITID"]); long baseUnitId = Convert.ToInt64(bomHeadObj["FBaseUnitId"]); long supplyOrgId = Convert.ToInt64(bomHeadObj["FUseOrgId"]); long parentAuxProId = Convert.ToInt64(bomHeadObj["FParentAuxPropId"]); BomForwardSourceDynamicRow bomExpandSourceRowView = BomForwardSourceDynamicRow.CreateInstance(); UnitConvert rate = null; //默认需求数量为1 decimal dQty = 1; if (dctUnitConvert.TryGetValue(bomHeadObjs.IndexOf(bomHeadObj), out rate)) { dQty = rate.ConvertQty(dQty); } bomExpandSourceRowView.MaterialId_Id = materialId; bomExpandSourceRowView.BomId_Id = bomId; bomExpandSourceRowView.NeedQty = dQty; bomExpandSourceRowView.NeedDate = DateTime.Now; bomExpandSourceRowView.UnitId_Id = unitId; bomExpandSourceRowView.BaseUnitId_Id = baseUnitId; bomExpandSourceRowView.SupplyOrgId_Id = supplyOrgId; bomExpandSourceRowView.AuxPropId = parentAuxProId; //默认BOM展开过程使用的时间单位为天。 bomExpandSourceRowView.TimeUnit = ((int)Kingdee.K3.Core.MFG.EnumConst.Enums.Enu_TimeUnit.KdDay).ToString(); lstExpandSource.Add(bomExpandSourceRowView.DataEntity); } return lstExpandSource; } } }
<2>拷贝插件组件到应用站点的WebSite\Bin目录下,重启IIS。
<3>BOSIDE任意单据,添加自定义按钮,命名为FButton_qtr注册表单插件,保存元数据,开发完毕。
【功能验证】
<1>登录业务站点,点击自定义按钮
【参考资料】
自定义WebAPI接口调用BOM展开 (kingdee.com)
https://vip.kingdee.com/article/399149371909894144
【金蝶云星空BOS二次开发案例演示】
https://vip.kingdee.com/article/94751030918525696
二开案例-【C#】如何获取物料清单成本查询结果
【应用场景】需要根据指定bom获取物料清单成本查询结果,并将结果在其他场景使用。【注意事项】该实现方案实现已指定bom内码实现,相关参数...
点击下载文档
本文2024-09-23 03:08:30发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-156979.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章