自动创建MTO库存释放单据将库存的计划跟踪号转变为空
【应用场景】
标准产品的MTO库存释放单据,一般是手工新增,录入一定过滤条件,找到空计划跟踪号的库存,然后保存提交审核,达到将库存的计划跟踪号转变为空的目的。但有些特殊应用场景,需要自动将非空计划跟踪号的库存,转换为空计划跟踪号的库存。本例以执行计划插件为示例,阐述如何实现自动创建MTO库存调整单将库存的计划跟踪号转变为空的目的。
【注意事项】
本方案示例是将系统中全部的非空计划跟踪号获取并转换为空计划跟踪号库存,实际应用中务必确认实际业务场景,添加必要地过滤条件来获取库存,比如起止计划跟踪号,物料,仓库等过滤条件。
【案例演示】
创建执行计划,设置执行周期参数,具体参数请根据业务要求设置,下图的关键是服务插件值若采用本示例代码,则须填写:MyScheduleTest_240408.CreateMTOInvReleaseBillTest,MyScheduleTest_240408,然后绑定本示例服务插件即可。
【实现步骤】
<1>编写执行计划服务插件,示例代码如下:
using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Interaction;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.K3.Core.MFG;
using Kingdee.K3.Core.MFG.PLN.ParamOption;
using Kingdee.K3.MFG.App;
using Kingdee.K3.Core.MFG.EntityHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.K3.MFG.Contracts.PLN;
using Kingdee.K3.Core.MFG.EnumConst.MTOINVAdjust;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Orm;
namespace MyScheduleTest_240408
{
/// <summary>
/// 创建MTO库存释放单,将系统中计划跟踪号不为空的库存转换为空
/// </summary>
public class CreateMTOInvReleaseBillTest : IScheduleService
{
public void Run(Context ctx, Schedule schedule)
{
FormMetadata metaData = AppServiceContext.MetadataService.Load(ctx, "PLN_MTOINVRelease") as FormMetadata;
//引用Kingdee.K3.MFG.App.dll组件可创建下一行代码的变量:dfv
IDynamicFormView dfv = MFGBaseDataHelperUtil.CreateBillAddNewView(ctx, metaData);
IBillView billView = dfv as IBillView;
//设置 单据类型、创建组织
billView.Model.SetValue("FBillTypeID", "6034ca77863f18", 0); // 单据类型
billView.Model.SetValue("FCreateOrgId", 1, 0); // 创建组织:1只是示例组织,即100组织,实际要根据业务调整
//设置计划跟踪号,可根据实际填写要释放的起止计划跟踪号
string beginMtoNo = string.Empty;
string endMtoNo = string.Empty;
billView.Model.SetValue("FBeginMtoNo", beginMtoNo, 0);
billView.Model.SetValue("FEndMtoNo", endMtoNo, 0);
// 设置仓库,可根据实际填写要释放的仓库
//List<long> stockIds = new List<long>() { 100407, 100488, 111011 };
//billView.Model.SetValue("FStockIds", stockIds, 0);
// 设置[MTO库存释放-库存明细]
var rowCount = billView.Model.GetEntryRowCount("FEntity");
billView.Model.CreateNewEntryRow("FEntity");
//获取数据包
billView.Model.DeleteEntryData("FEntity");
billView.Model.DeleteEntryData("FXTEntity");
//引用Kingdee.K3.Core组件,并using Kingdee.K3.Core.MFG.PLN.ParamOption
MTOINVOption mtoInvOption = BuildMTOINVOption(ctx, billView);
IOperationResult feelMtoInvInfos = AppServiceContext.GetService<IMTOINVAdjustService>().GetFeelMtoInvInfos(ctx, mtoInvOption);
IEnumerable<DynamicObject> mtoInvInfos = feelMtoInvInfos.FuncResult as IEnumerable<DynamicObject>;
if (mtoInvInfos.IsEmpty())
{
string errMsg = "无计划跟踪号为非空的库存数据,本次未释放。";
schedule.Notes += errMsg;
return;
}
AppendInvEntrys(ctx, mtoInvInfos, billView, mtoInvOption);
//保存
IOperationResult result = billView.Model.Save();
if (result.IsSuccess)
{
OperateOption tmpOption = OperateOption.Create();
tmpOption.SetForbidSaveFlexData(true);
tmpOption.SetIgnoreInteractionFlag(true); //须using Kingdee.BOS.Core.Interaction;
tmpOption.SetVariableValue("BulkCopy", true);
tmpOption.SetVariableValue("FromMrpMTOINVOption", mtoInvOption);
tmpOption.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);
var billNo = billView.Model.GetValue("FBillNo");// 获取单据编号
object pkId = billView.Model.GetPKValue();
//获取提交服务
ISubmitService submitService = Kingdee.BOS.App.ServiceHelper.GetService<ISubmitService>();
//执行提交服务
var submitResult = submitService.Submit(ctx, metaData.BusinessInfo, new object[] { pkId }, "Submit", tmpOption);
if (!submitResult.IsSuccess)
{
string errMsg = string.Format("单据编号为{0}的【MTO库存释放单】保存成功,但提交失败!\n {1}", billNo, string.Join(",", submitResult.ValidationErrors.Select(o => o.Message)));
schedule.Notes += errMsg;
return;
}
//获取单据状态转换服务
ISetStatusService setStatusService = Kingdee.BOS.App.ServiceHelper.GetService<ISetStatusService>();
//构建数据集合
List<KeyValuePair<object, object>> lstKeyValuePairs = new List<KeyValuePair<object, object>>();
KeyValuePair<object, object> keyValuePair = new KeyValuePair<object, object>(pkId, "");
lstKeyValuePairs.Add(keyValuePair);
List<object> paraAudit = new List<object>();
// 审核通过
paraAudit.Add("1");
// 审核意见
paraAudit.Add("");
//审核
IOperationResult auditResult = setStatusService.SetBillStatus(ctx, metaData.BusinessInfo, lstKeyValuePairs, paraAudit, "Audit", tmpOption);
if (auditResult.IsSuccess)
{
string errMsg = string.Format("单据编号为“{0}”的【MTO库存释放单】审核成功!", billNo);
schedule.Notes += errMsg;
}
else
{
string errMsg = string.Format("单据编号为{0}的【MTO库存释放单】提交成功,但审核失败!\n {1}", billNo, string.Join(",", auditResult.ValidationErrors.Select(o => o.Message)));
schedule.Notes += errMsg;
}
}
}
/// <summary>
/// 构建空计划跟踪号库存的查询参数选项
/// </summary>
/// <returns></returns>
private MTOINVOption BuildMTOINVOption(Context ctx, IBillView billView)
{
MTOINVOption mtoInvOption = new MTOINVOption();
mtoInvOption.MtoInvFormMetaData = AppServiceContext.MetadataService.Load(ctx, "PLN_MTOINVRelease") as FormMetadata;
DynamicObject billHead = billView.Model.DataObject;
mtoInvOption.MtoInvData = billHead;
mtoInvOption.BeginMtoNo = billHead.GetDynamicValue<string>(CONST_PLN_MTOINVRelease.CONST_FBillHead.ORM_BeginMtoNo);
mtoInvOption.EndMtoNo = billHead.GetDynamicValue<string>(CONST_PLN_MTOINVRelease.CONST_FBillHead.ORM_EndMtoNo);
//组织Ids
DynamicObjectCollection orgDatas = billHead.GetDynamicValue<DynamicObjectCollection>(CONST_PLN_MTOINVRelease.CONST_FBillHead.ORM_StockOrgIds);
if (!orgDatas.IsEmpty())
{
foreach (DynamicObject orgData in orgDatas)
{
DynamicObject orgObj = orgData.GetDynamicValue<DynamicObject>(CONST_PLN_MTOINVRelease.CONST_FBillHead.ORM_StockOrgIds);
if (orgObj == null) continue;
mtoInvOption.StockOrgIds.Add(orgObj.GetDynamicValue<long>("Id"));
}
}
//物料MasterIds
DynamicObjectCollection mtrlDatas = billHead.GetDynamicValue<DynamicObjectCollection>(CONST_PLN_MTOINVRelease.CONST_FBillHead.ORM_MaterialIds);
if (!mtrlDatas.IsEmpty())
{
foreach (DynamicObject mtrlData in mtrlDatas)
{
DynamicObject mtrlObj = mtrlData.GetDynamicValue<DynamicObject>(CONST_PLN_MTOINVRelease.CONST_FBillHead.ORM_MaterialIds);
if (mtrlObj == null) continue;
mtoInvOption.MtrlMasterIds.Add(mtrlObj.GetDynamicValue<long>("msterID"));
}
}
//仓库MasterIds
DynamicObjectCollection stockDatas = billHead.GetDynamicValue<DynamicObjectCollection>(CONST_PLN_MTOINVRelease.CONST_FBillHead.ORM_StockIds);
if (!stockDatas.IsEmpty())
{
foreach (DynamicObject stockData in stockDatas)
{
DynamicObject stockObj = stockData.GetDynamicValue<DynamicObject>(CONST_PLN_MTOINVRelease.CONST_FBillHead.ORM_StockIds);
if (stockObj == null) continue;
mtoInvOption.StockMasterIds.Add(stockObj.GetDynamicValue<long>("msterID"));
}
}
return mtoInvOption;
}
/// <summary>
/// 追加库存明细行
/// </summary>
/// <param name="ctx"></param>
/// <param name="mtoInvInfos"></param>
/// <param name="billView"></param>
/// <param name="mtoInvOption"></param>
private void AppendInvEntrys(Context ctx, IEnumerable<DynamicObject> mtoInvInfos, IBillView billView, MTOINVOption mtoInvOption)
{
if (mtoInvInfos.IsEmpty()) return;
Entity entity = billView.BusinessInfo.GetEntity(CONST_PLN_MTOINVRelease.CONST_FEntity.ENTITY_FEntity);
DynamicObjectCollection entrys = billView.Model.DataObject.GetDynamicValue<DynamicObjectCollection>(CONST_PLN_MTOINVRelease.CONST_FEntity.ENTITY_ORM_Entity);
List<DynamicObject> newEntrys = new List<DynamicObject>();
foreach (DynamicObject mtoInvInfo in mtoInvInfos)
{
DynamicObject entry = new DynamicObject(entity.DynamicObjectType);
entry.CopyPropertyValues(mtoInvInfo);
newEntrys.Add(entry);
}
if (mtoInvOption.IsNullOrEmpty()) mtoInvOption = BuildMTOINVOption(ctx, billView);
IEnumerable<DynamicObject> addEntrys = AppServiceContext.GetService<IMTOINVAdjustService>().AppendSerialNos(ctx, newEntrys, mtoInvOption);
if (!addEntrys.IsEmpty())
{
int seq = entrys.Count;
foreach (DynamicObject addEntry in addEntrys)
{
entrys.Add(addEntry);
addEntry["Seq"] = (++seq);
}
}
billView.UpdateView(CONST_PLN_MTOINVRelease.CONST_FEntity.ENTITY_FEntity);
}
}
}
<2>拷贝插件组件MyScheduleTest_240408.dll到应用站点的WebSite\Bin目录下,重启IIS。
【功能验证】
<1>登录业务站点,找到执行计划,点测试,待执行完成,到MTO库存释放单去找到相应的单据,然后检查相差的库存是否已经将计划跟踪号置为空。
自动创建MTO库存释放单据将库存的计划跟踪号转变为空
本文2024-09-23 03:14:58发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-157647.html