插件生成单据原创
4人赞赏了该文章
238次浏览
编辑于2023年09月26日 09:18:37
public void CreatePur(BusinessInfo bizInfo){ //存储中间数据 Dictionary<string, Dictionary<string, string[]>> DATA = new Dictionary<string, Dictionary<string, string[]>>(); List<long> ok = new List<long>();//成功创建 List<long> off = new List<long>();//创建失败 List<string> offstring = new List<string>();//创建 string kczz = ""; string wlbm = ""; string jycgsl = ""; string materialunit = ""; foreach (Field fld in bizInfo.GetEntity(entityKey).Fields.Skip(1)) { if (fld.Name.ToString() == "库存组织") { kczz = fld.Key; } else if (fld.Name.ToString() == "物料编码") { wlbm = fld.Key; } else if (fld.Name.ToString() == "建议采购数量") { jycgsl = fld.Key; } else if (fld.Name.ToString() == "单位") { materialunit = fld.Key; } } DynamicObjectCollection entityObjs = this.Model.GetEntityDataObject(this.View.BillBusinessInfo.GetEntity("F_Entity")); //选择已勾选的 foreach (var item in entityObjs.Where(x => Convert.ToBoolean(x[1]))) { var id = entityObjs.IndexOf(item); if (entityObjs[id][kczz] is null || entityObjs[id][wlbm] is null || Convert.ToDouble(entityObjs[id][jycgsl]) == 0) { off.Add(id); continue; } else { DATA.Add(Convert.ToString(entityObjs[id][kczz]), new Dictionary<string, string[]> { {Convert.ToString(entityObjs[id][wlbm]), new string[2] { Convert.ToString(entityObjs[id][materialunit]),Convert.ToString(entityObjs[id][jycgsl]) } } }); ok.Add(id); } } if (ok.Count > 0) { DynamicObject[] EntryRows = new DynamicObject[ok.Count]; var BILLNOS = BusinessDataServiceHelper.GetListBillNO(this.Context, CGSQD, ok.Count); int i = 0; foreach (var Item in DATA) { var Metadata = FormMetaDataCache.GetCachedFormMetaData(this.Context, CGSQD); DynamicObject dynObj = new DynamicObject(Metadata.BusinessInfo.GetDynamicObjectType()); Form form = Metadata.BusinessInfo.GetForm(); if (form.FormIdDynamicProperty != null) { form.FormIdDynamicProperty.SetValue(dynObj, form.Id); } dynObj["BillNo"] = BILLNOS[i];//单据编号 dynObj["DocumentStatus"] = "A"; dynObj["CancelStatus"] = "A"; dynObj["CloseStatus"] = "A"; SetBasedataValue(Metadata.BusinessInfo, "FApplicationOrgId", dynObj, Org[Item.Key]);//申请组织 SetBasedataValue(Metadata.BusinessInfo, "FCreatorId", dynObj, USERID);//创建人 SetBasedataValue(Metadata.BusinessInfo, "FModifierId", dynObj, USERID);//修改人 SetBasedataValue(Metadata.BusinessInfo, "FApplicantId", dynObj, RGID);//申请人 SetBasedataValue(Metadata.BusinessInfo, "FCurrencyId", dynObj, "1");//币别 dynObj["FACCTYPE"] = "Q";//验收方式 SetBasedataValue(Metadata.BusinessInfo, "FBillTypeID", dynObj, CGSQD);//单据类型 dynObj["RequestType"] = "Material";// 申请类型 var now = DateTime.Now; dynObj["CreateDate"] = now;// 创建时间 dynObj["ApplicationDate"] = now;// 修改时间 dynObj["ModifyDate"] = now;// 最后修改时间 dynObj["Note"] = "";// 备注 var entityInvPty = Metadata.BusinessInfo.GetEntity("F_Entity"); var entityInvPtyData = dynObj[entityInvPty.EntryName] as DynamicObjectCollection; int j = 1; foreach (var entry in Item.Value) { //增加单据体一行数据 var entityInvPtyRow = new DynamicObject(entityInvPty.DynamicObjectType); entityInvPtyRow["Seq"] = j++;//序号 entityInvPtyRow["ReqQty"] = Convert.ToDouble(entry.Value[1]);//申请数量 SetBasedataValue(Metadata.BusinessInfo, "FPurchaseOrgId", entityInvPtyRow, Org[Item.Key]);//采购组织 SetBasedataValue(Metadata.BusinessInfo, "FReceiveOrgId", entityInvPtyRow, Org[Item.Key]);//收货组织 SetBasedataValue(Metadata.BusinessInfo, "FRequireOrgId", entityInvPtyRow, Org[Item.Key]);//需求组织 SetBasedataFieldByNumber(Metadata.BusinessInfo, entityInvPtyRow, "FMaterialId", entry.Key);//物料 SetBasedataValue(Metadata.BusinessInfo, "FBaseUnitId", entityInvPtyRow, Unit[entry.Value[0]]);//基础单位 SetBasedataValue(Metadata.BusinessInfo, "FUnitId", entityInvPtyRow, Unit[entry.Value[0]]);//单位 SetBasedataValue(Metadata.BusinessInfo, "FPriceUnitId", entityInvPtyRow, Unit[entry.Value[0]]);//价格单位 entityInvPtyData.Add(entityInvPtyRow); } EntryRows[i] = dynObj;//组合数组 i++; } BusinessDataServiceHelper.Save(this.Context, EntryRows);//批量执行(也可改为单条执行 只传一个DynamicObject数据包) if (off.Count > 0) { this.View.ShowMessage($@"本次成功生成{ok.Count}项物料,合计{DATA.Count}条采购申请,生成失败{off.Count}条!", MessageBoxOptions.YesNo, new Action<MessageBoxResult>((result) => //通过result接收值 { string offid = ""; off.ForEach(x => offid += (x + 1) + " "); this.View.ShowErrMessage($"生成失败的序号如下:{offid} Tips;可能由于当前库存充足,无需生成采购。"); })); } else { this.View.ShowMessage($@"本次成功生成{ok.Count}项物料,合计{DATA.Count}条采购申请!"); } } else { this.View.ShowErrMessage("生成失败,请检查是否需要补充库存! 若当前选中的物料尚未配置,请进入配置页面填写相关信息!"); }}/// <summary>/// 设置基础资料字段/// </summary>/// <param name="baseDataFieldKey">基础资料字段Key:FBaseUnitId</param>/// <param name="baseDataFieldValue">基础资料字段主键值:10087</param>private void SetBasedataValue(BusinessInfo bizInfo, string baseDataFieldKey, DynamicObject dynObj, object baseDataFieldValue){ BaseDataField bdField = bizInfo.GetField(baseDataFieldKey) as BaseDataField; DynamicObject[] bdObjs = BusinessDataServiceHelper.LoadFromCache(this.Context, new object[] { baseDataFieldValue }, bdField.RefFormDynamicObjectType); if (bdField.Entity is SubHeadEntity) { var subData = dynObj[bdField.Entity.EntryName] as DynamicObjectCollection; if (subData.Count == 0) { subData.Add(new DynamicObject(bdField.Entity.DynamicObjectType)); } bdField.RefIDDynamicProperty.SetValue(subData[0], baseDataFieldValue); bdField.DynamicProperty.SetValue(subData[0], bdObjs[0]); } else { bdField.RefIDDynamicProperty.SetValue(dynObj, baseDataFieldValue); bdField.DynamicProperty.SetValue(dynObj, bdObjs[0]); }}/// <summary>/// 用编码设置基础资料字段/// </summary>/// <param name="bizInfo">物料元数据</param>/// <param name="dynObj">物料数据包</param>/// <param name="fieldKey">基础资料字段Key</param>/// <param name="number">基础资料编码</param>private void SetBasedataFieldByNumber(BusinessInfo bizInfo, DynamicObject dynObj, string fieldKey, string number){ //基础资料字段 BaseDataField bdField = bizInfo.GetField(fieldKey) as BaseDataField; if (bdField == null || bdField.LookUpObject == null) return; QueryBuilderParemeter query = new QueryBuilderParemeter() { FormId = bdField.LookUpObject.FormId, FilterClauseWihtKey = string.Format("FNUMBER='{0}'", number) }; DynamicObject[] bdObjs = ServiceHelper.GetService<Kingdee.BOS.Contracts.IViewService>().LoadFromCache(this.Context, bdField.RefFormDynamicObjectType, query); if (bdObjs.IsEmpty()) return;//查不到基础资料,记录日志或报异常 if (bdField.Entity is Kingdee.BOS.Core.Metadata.EntityElement.SubHeadEntity) { var subData = dynObj[bdField.Entity.EntryName] as DynamicObjectCollection; if (subData.Count == 0) { subData.Add(new DynamicObject(bdField.Entity.DynamicObjectType)); } bdField.RefIDDynamicProperty.SetValue(subData[0], bdObjs[0][0]); bdField.DynamicProperty.SetValue(subData[0], bdObjs[0]); } else { bdField.RefIDDynamicProperty.SetValue(dynObj, bdObjs[0][0]); bdField.DynamicProperty.SetValue(dynObj, bdObjs[0]); }}
推荐阅读