如何通过代码调用物料供需状态服务获取物料供需数据
【应用场景】
有些业务场景是在系统的各业务对象要调用物料供需状态显示,这个办法详见:https://vip.kingdee.com/article/39174?productLineId=1。
但是,还有些业务场景是在业务对象上增加了一些自定义字段,需要获取物料供需状态的实时数据进行显示,本贴正是适用这个场景,介绍实现方法。
【注意事项】
系统已经将物料状态状态包装成服务,注意调用时尽量避免在循环中调用,毕竟该方法将实时读取各供需业务单据数据。
【案例演示】
调用本贴所述的物料供需状态服务,将得到一个IOperationResult类型的数据包,它的FuncResult 即是一个Tuple<List<DynamicObject>, Dictionary<string, List<DynamicObject>>>类型的业务数据包。
其中,Item1表示物料供需状态的时段表体数据,注意看Item1是个List,但每三行为一组表示一个物料(计划)维度的需求、供应和结存行,组别之间用后台字段FRowId来区分,同组的行FRowId相同。
另外,Item2表示物料供需状态的明细数据,注意到Item2本身是个字典,它的Key值对应的就是Item1各组的FRowId,它的Value值是个List,对应的就是Item1一组物料(计划)维度的供需状态信息。
【实现步骤】
本例示范在菜单点击来获取物料供需状态数据,步骤如下:
<1>编写表单插件,在BarItemClick事件中调用物料供需状态服务,示例代码如下。如下代码已经过编译验证功能正确,可直接拷贝做成一个表单插件插件验证和调试。有不明之处,可详加留意代码注释部分,或跟帖讨论。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.K3.Core.MFG.PLN.DynamicReport;
using Kingdee.K3.MFG.ServiceHelper.PLN;
using Kingdee.K3.Core.MFG.Utils;
using Kingdee.K3.Core.MFG.EnumConst;
using Kingdee.K3.Core.MFG.EntityHelper;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.K3.Core.MFG;
using Kingdee.BOS.Core.Metadata.FieldElement;
namespace Test20230926
{
public class Test20230926.TestCustDFPlugIn : AbstractDynamicFormPlugIn
{
/// <summary>
/// 菜单单击事件
/// </summary>
/// <param name="e"></param>
public override void BarItemClick(BarItemClickEventArgs e)
{
base.BarItemClick(e);
if (e.BarItemKey.Equals("tbGetMtroDSDataTest"))
{
GetMtroDSDataTest();
}
}
/// <summary>
/// 菜单单击事件
/// </summary>
private void GetMtroDSDataTest()
{
//首先,构建物料供需状态查询参数包
DemandSupplyStateQueryParam dssqPara = new DemandSupplyStateQueryParam();
dssqPara.BeginDate = new DateTime(1900, 1, 1);
dssqPara.EndDate = new DateTime(2100, 12, 31);
dssqPara.CurrDate = DateTime.Today.Date;
dssqPara.OrgIds = new List<long>() { 1 };
dssqPara.ProSchedule = string.Empty;
//指定需求参数单据类型,相当于过滤界面勾选需求单据:dssqPara.DemandBill.Add("PRD_MO", new List<string>() { "单据类型内码0", "单据类型内码1" });
dssqPara.DemandBill.Add("SAL_SaleOrder", new List<string>() { "eacb50844fc84a10b03d7b841f3a6278", "1e17ce9c878a483da7fe38e5a4e992a0", "88de45ad2499437f9b21096997b297e2", "7e206b700d1343b5b60e20b992c84469", "a300e2620037435492aed9842875b451", "abcdce9c878a483da7fe38e5a4e992a0", "abcd6b700d1343b5b60e20b992c84469", "78acc0ac34018b1e11e303f97ece4232", "54a350fbb6c248", "636372c501021e" });
dssqPara.DemandBill.Add("STK_OutStockApply", new List<string>() { "59dc3f6446d6c2", "59dc68755e39b2", "59dc68b45e3a46", "6397d59bd425e2", "642f86d750f6c7", "649114afd694a9" });
dssqPara.DemandBill.Add("STK_TRANSFEROUT", new List<string>() { "546abb491109ba", "de3bcacc98434ec68a358aa5abcd9183" });
dssqPara.DemandBill.Add("PLN_PLANORDER", new List<string>() { "4c1b07214adb4651aa2437ef48738af9", "77ddfb5c4daa44d288b4e9efbd768e94" });
dssqPara.DemandBill.Add("STK_MisDelivery", new List<string>() { "005056941128814b11e31b9d2979ed35", "0050569448a4966511e336d026c93cba", "136bf6db80a14da8b3c332383f796739", "54533291F9A44D38809F70000499BEE9", "551debb583e5ce", "6333bcf6dbb501", "6340cb2a34844b", "6397df6cd42a6c", "64910f29d678d5" });
dssqPara.DemandBill.Add("PRD_FeedMtrl", new List<string>() { "ca103ed148d0492dbe81d89f54d9ef85" });
dssqPara.DemandBill.Add("PRD_MO", new List<string>() { "123f39178eb2424c8449f992e1fff1ee", "0e74146732c24bec90178b6fe16a2d1c", "005056945fca85e511e42420f363da25", "6363a88801084d" });
dssqPara.DemandBill.Add("PRD_ReturnMtrl", new List<string>() { "577658dd483b7d", "c4e4cef46c844a2bb2a7faf0cf6dc2cb" });
dssqPara.DemandBill.Add("REM_IndepenReqPlan", new List<string>() { "005056a06707af9511e3e6efe1be1b00" });
dssqPara.DemandBill.Add("REM_MixedFlowPlan", new List<string>() { "005056a06707af9511e3e7ceb5cdc3fe" });
dssqPara.DemandBill.Add("REM_ProdSubDayPlan", new List<string>() { "547547f815fc40544e" });
dssqPara.DemandBill.Add("STK_TRANSFERAPPLY", new List<string>() { "59c46b1074cd1d", "59c46b3974cd6f" });
dssqPara.DemandBill.Add("SUB_FEEDMTRL", new List<string>() { "532c672029694473916a258fae962b1c" });
dssqPara.DemandBill.Add("SUB_EXCONSUME", new List<string>() { "584f4087a14649" });
dssqPara.DemandBill.Add("SUB_SUBREQORDER", new List<string>() { "87035fdb6a31407a8819750df751d233", "93904bf745d84ae0ad08da30949754e0" });
dssqPara.DemandBill.Add("SUB_RETURNMTRL", new List<string>() { "0c2c96bb7f534768a96ab08cdd6fc26e", "b943c27f285546f8a5c0d544034328de" });
dssqPara.DemandBill.Add("PLN_FORECAST", new List<string>() { "123ab4b2e10943ea8fe52dc771a727f9", "5b0f9aa7381170", "6334492cdc0977" });
dssqPara.DemandBill.Add("STK_TransferDirect", new List<string>() { "005056941128823c11e323525db18103", "0bcc8f3ce0a64171b1a901344d1ac239", "a7c7f20d5faa46ecbeb88ee4e207390e", "b15923ad5e39421aa188380c89849cb5", "ce8f49055c5c4782b65463a3f863bb4a" });
dssqPara.DemandBill.Add("PLN_REQUIREMENTORDER", new List<string>() { "f94e4845700c4dab93e652b61edcb819" });
//指定供应参数单据类型,相当于过滤界面勾选供应单据:dssqPara.SupplyBill.Add("PRD_MO", new List<string>() { "单据类型内码0", "单据类型内码1" });
dssqPara.SupplyBill.Add("PUR_PurchaseOrder", new List<string>() { "83d822ca3e374b4ab01e5dd46a0062bd", "6d01d059713d42a28bb976c90a121142", "ba3ad5fc48d44271a048da26b615b589", "0023240234df807511e308990e04cf6a", "5abd9deba59210", "6247e8be5b17b0" });
dssqPara.SupplyBill.Add("PUR_Requisition", new List<string>() { "03c6c047c65c4a17a792f85dcf3cabec", "60d2460b0e5742d58432f70a06f193b6", "93591469feb54ca2b08eb635f8b79de3", "ba0754db8aa04f8cb3d42d186858ed03" });
dssqPara.SupplyBill.Add("STK_TRANSFERIN", new List<string>() { "546ab95111081b", "e65a4f29743a44b7b67dc8145e1f9c92" });
dssqPara.SupplyBill.Add("PLN_PLANORDER", new List<string>() { "4c1b07214adb4651aa2437ef48738af9", "77ddfb5c4daa44d288b4e9efbd768e94" });
dssqPara.SupplyBill.Add("STK_MISCELLANEOUS", new List<string>() { "d772ead981e748d69dda1caac7583f8c" });
dssqPara.SupplyBill.Add("PRD_MO", new List<string>() { "123f39178eb2424c8449f992e1fff1ee", "0e74146732c24bec90178b6fe16a2d1c", "005056945fca85e511e42420f363da25", "6363a88801084d" });
dssqPara.SupplyBill.Add("REM_IndepenReqPlan", new List<string>() { "005056a06707af9511e3e6efe1be1b00" });
dssqPara.SupplyBill.Add("REM_MixedFlowPlan", new List<string>() { "005056a06707af9511e3e7ceb5cdc3fe" });
dssqPara.SupplyBill.Add("REM_ProdSubDayPlan", new List<string>() { "547547f815fc40544e" });
dssqPara.SupplyBill.Add("SUB_SUBREQORDER", new List<string>() { "87035fdb6a31407a8819750df751d233", "93904bf745d84ae0ad08da30949754e0" });
dssqPara.SupplyBill.Add("STK_TransferDirect", new List<string>() { "005056941128823c11e323525db18103", "0bcc8f3ce0a64171b1a901344d1ac239", "a7c7f20d5faa46ecbeb88ee4e207390e", "b15923ad5e39421aa188380c89849cb5", "ce8f49055c5c4782b65463a3f863bb4a" });
dssqPara.SupplyBill.Add("PLN_REQUIREMENTORDER", new List<string>() { "f94e4845700c4dab93e652b61edcb819" });
//指定要查询的物料编码,根据实际业务场景指定
dssqPara.MaterialNumbers.Add("000000005");
//指定物料属性,自制、委外、外购等
dssqPara.ClsIds.AddRange(new List<string>() { "1", "2", "3" });
//获取标准服务获取工作日历,其中new List<long>() { 1 }这个参数表示获取组织内码是1的工作日历,实际要按业务需求指定组织内码
IEnumerable<DynamicObject> workShopDays = SupplyDemandStateRptServiceHelper.GetWorkShopDays(base.Context, new List<long>() { 1 }, new DateTime(1900, 1, 1), new DateTime(2100, 12, 31));
if (!workShopDays.IsEmpty())
{
dssqPara.WorkShopDays = new HashSet<DateTime>(workShopDays.Select(s => s.GetDynamicValue<DateTime>("FDAY").Date));
}
//获取时段设置信息,详见本例下面的方法GetTimeZoneSetting(),该方法只是简单构建一个周为单位的时段
List<PLNTimeZoneSetting> timeZoneSetting = GetTimeZoneSetting();
List<PLNTimeZoneField> timeZoneField = SupplyDemandStateRptServiceHelper.GetTimeZoneField(base.Context, dssqPara, timeZoneSetting);
dssqPara.TimeZoneField = timeZoneField;
//以下尝试动态创建字段,但是因为存在一些只读属性无法通过这种方式实现,故注释
//FormMetadata sumMetaData = MetaDataServiceHelper.Load(this.Context, "PLN_SuppluDemandStateSummary") as FormMetadata;
////获取新的BusinessInfo
//Tuple<BusinessInfo, Kingdee.BOS.Core.Metadata.ControlElement.LayoutInfo> newSumBusinessInfos = SupplyDemandStateRptServiceHelper.GetNewBusinessInfo(this.Context, sumMetaData, "FSummary", timeZoneField, "FTemplateQty");
//Entity sEntity = newSumBusinessInfos.Item1.Entrys.Where(w => w.Key.EqualsIgnoreCase("FSummary")).FirstOrDefault();
//DynamicObject summaryData = new DynamicObject(sEntity.DynamicObjectType);
//this.RegisterSumEntiyDtProperty(summaryData.DynamicObjectType, sEntity);
//因此动态字段由下面这段SQL创建,特别注意,这条SQL包含了三个动态列PCUST00001,PCUST00002,PCUST00003,头和尾的动态列分别表示时段表体的期初数量和期末数量,中间的动态列对应的是过滤界面的时段设置列,本例因为GetTimeZoneSetting()只创建了一个周动态列,因此只要一个PCUST00002数据列即可。若GetTimeZoneSetting()创建了5列,则这条SQL须创建PCUST00002~PCUST00006共五列,然后再加一列PCUST00007作为结存列。
string sql = @" SELECT t0.FID AS RowId, NULL AS MaterialId, T0.FMATERIALMASTERID AS MaterialId_Id, N' ' AS MaterialName, N' ' AS Specification,
N' ' AS AuxPropId, N' ' AS MaterialType, N' ' AS BOM, N' ' AS MtoNo, NULL AS BaseUnitId, T0.FBASEUNITID BaseUnitId_Id,
' ' AS Inventory, ' ' AS SafetyStock, ' ' AS [Type], 0.0 AS SumQty, 0.0 AS BaseMaxStockQty,
' ' AS TemplateQty, ' ' AS PCUST00001, ' ' AS PCUST00002, ' ' AS PCUST00003
FROM T_PLN_MTRLDSSUMMARYDATA T0 WHERE 1=0 ";
DynamicObjectCollection tmpSumEntrys = DBServiceHelper.ExecuteDynamicObject(this.Context, sql);
FormMetadata detailMetaData = MetaDataServiceHelper.Load(this.Context, "PLN_SupplyDemandStateDetail") as FormMetadata;
Entity dEntity = detailMetaData.BusinessInfo.GetEntity("FDetail");
//真正调用物料供需状态服务,获取物料供需数据
IOperationResult dsResult = SupplyDemandStateRptServiceHelper.GetMtrlSupplyDemandState(this.Context, dssqPara, tmpSumEntrys.DynamicCollectionItemPropertyType, dEntity.DynamicObjectType, "");
Tuple<List<DynamicObject>, Dictionary<string, List<DynamicObject>>> dsDatas =
dsResult.FuncResult as Tuple<List<DynamicObject>, Dictionary<string, List<DynamicObject>>>;
if (!dsDatas.IsNullOrEmpty())
{
//以下示例是将时段表体的各行汇总数量SumQty累加,实际可根据业务需求取dsDatas.Item1或dsDatas.Item2里面的相关数据。
List<DynamicObject> sumDatas = dsDatas.Item1;
decimal sumDemandQty = 0;
foreach (DynamicObject sumData in sumDatas)
{
sumDemandQty += sumData.GetDynamicValue<decimal>("SumQty");
}
}
}
/// <summary>
/// 获取时段设置信息
/// </summary>
/// <param name="e"></param>
private List<PLNTimeZoneSetting> GetTimeZoneSetting()
{
List<PLNTimeZoneSetting> list = new List<PLNTimeZoneSetting>();
list.Add(new PLNTimeZoneSetting
{
IsKeepNatural = false,
Unit = "W",
Number = 1,
Length = 1,
Seq = 1
});
return list;
}
}
}
【功能验证】
获取到物料供需状态数据后,可取出有效信息,比如结存显示在自定义字段,也可以封装成API接口,供第三方调用等。
如何通过代码调用物料供需状态服务获取物料供需数据
本文2024-09-23 03:15:16发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-157681.html