金蝶云表单插件开发--物料清单BOM获取老系统的BOM信息【2023.12.27】
需求:
1、新系统中同一产品编码,可以通过快捷获取老系统中的同一产品编码的BOM信息;
2、数据信息查询:通过存储过程去查询,再转入子项明细中;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
namespace Lyh.K3cloud.Demo.Business.Plugin
{
[Description("获取物料用料清单BOM相关信息"),HotUpdate]
public class GetBomFromOldSysXX:AbstractBillPlugIn
{
public override void EntryBarItemClick(BarItemClickEventArgs e)
{
base.EntryBarItemClick(e);
//子明细菜单按钮
if(e.BarItemKey.Equals("QUYL_tbGetBomXX"))
{
//间接取物料清单的父物料编码的编码
DynamicObject fitmid = this.View.Model.GetValue("FMATERIALID") as DynamicObject;
//注意是取NUMBER而为是FNUMBER
string fnumber = fitmid["NUMBER"].ToString();
if(fnumber != null)
{
string sql = string.Concat(" EXEC PRO_LYH_GETBOMWLXX '", fnumber, "'");
DataSet dataSet = DBUtils.ExecuteDataSet(base.Context,sql );
DataTable dataTable = dataSet.Tables[0];
if(dataTable.Rows.Count >0 )
{
//考虑BOM明细中有多行记录,所以传递产生多行,定义Row
int row = 0;
foreach( object obj in dataTable.Rows)
{
DataRow dr = (DataRow)obj;
if(row >0)
{
//默认自动增加一行
this.View.Model.CreateNewEntryRow("FTreeEntity");
}
//子项明细中物料
base.View.Model.SetValue("FMATERIALIDCHILD", dr["FMATERIALID"].ToString(),row);
//分子
base.View.Model.SetValue("FNUMERATOR", dr["FNUMERATOR"].ToString(),row);
row = row + 1;
}
}
}
}
}
}
}
方法一:直接操作DynamicObject**
// 假设billObj是单据的数据包
DynamicObject billObj = this.Model.DataObject;
// 读取单据内码
long billId = Convert.ToInt64(billObj[0]);
// 普通文本字段(读取 + 设置)
string fldBillNoValue = Convert.ToString(billObj["BillNo"]);
billObj["BillNo"] = fldBillNoValue ;
// 日期字段(读取 + 设置)
DateTime fldDateValue = Convert.ToDateTime(billObj["F_JD_Date"]);
billObj["F_JD_Date"] = fldDateValue;
// 基础资料字段(读取 + 设置)
DynamicObject fldSupplierValue = billObj["F_JD_Supplier"] as DynamicObject;
billObj["F_JD_Supplier"] = fldSupplierValue ;
if (fldSupplierValue != null)
{ billObj["F_JD_Supplier_Id"] = Convert.ToInt64(fldSupplierValue[0]);
long supplierId = Convert.ToInt64(fldSupplierValue[0]);
string supplierNumber = fldSupplierValue["Number"].ToString();
string supplierName = fldSupplierValue["Name"].ToString();
}
// 单据体(单据体行集合属性本身只读,可以通过单据体集合提供的方法,添加行、删除行)
DynamicObjectCollection entityRows = billObj["FEntity"] as DynamicObjectCollection;
foreach (var entityRow in entity1Rows)
{
// 内码
long entityId = Convert.ToInt64(entityRow[0]);
// 数量 (读取 + 设置)
decimal fldQtyValue = Convert.ToDecimal(entityRow["F_JD_Qty"]);
entityRow["F_JD_Qty"] = fldQtyValue;
}
**方法二:通过字段的元数据,操作DynamicObject(推荐)**
// 假设billObj是单据的数据包
DynamicObject billObj = this.Model.DataObject;
// 首先获取各种元素的元数据
Field fldBillNo = this.View.BillBusinessInfo.GetField("FBillNo");
Field fldDate = this.View.BillBusinessInfo.GetField("F_JD_Date");
BaseDataField fldSupplier = this.View.BillBusinessInfo.GetField("F_JD_Supplier") as BaseDataField;
BaseDataField fldMaterial = this.View.BillBusinessInfo.GetField("F_JD_FMaterialId") as BaseDataField;
Field fldQty = this.View.BillBusinessInfo.GetField("F_JD_Qty");
Entity entity = this.ListView.BillBusinessInfo.GetEntity("FEntity");
// 读取单据内码
long billId = Convert.ToInt64(billObj[0]);
//单据编号
string billNo = Convert.ToDateTime(fldBillNo.DynamicProperty.GetValue(billObj));
fldBillNo.DynamicProperty.SetValue(billObj, billNo));
// 日期
DateTime fldDateValue = Convert.ToDateTime(fldDate.DynamicProperty.GetValue(billObj));
fldDate.DynamicProperty.SetValue(billObj, fldDateValue)
// 供应商:基础资料字段
DynamicObject fldSupplierValue = fldSupplier.DynamicProperty.GetValue(billObj) as DynamicObject;
// 设置供应商基础字段值
DynamicObject[] supplierObjs = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.LoadFromCache(
this.Context,
new object[] { fldSupplierValue[0] },
fldSupplier.RefFormDynamicObjectType);
fldSupplier.RefIDDynamicProperty.SetValue(billObj, supplierObjs[0][0]);
fldSupplier.DynamicProperty.SetValue(billObj, supplierObjs[0]);
// 基础资料属性值
if (fldSupplierValue != null)
{
long supplierId = Convert.ToInt64(fldSupplierValue[0]);
string supplierNumber = fldSupplier.GetRefPropertyValue(fldSupplierValue, "FNumber").ToString();
string supplierName = fldSupplier.GetRefPropertyValue(fldSupplierValue, "FName").ToString();
}
// 单据体的字段
DynamicObjectCollection entityRows = entity.DynamicProperty.GetValue(billObj) as DynamicObjectCollection;
foreach (var entityRow in entityRows)
{
// 内码
long entityId = Convert.ToInt64(entity1Row[0]);
// 物料:基础资料字段
DynamicObject fldMaterialValue = fldMaterial.DynamicProperty.GetValue(entityRow) as DynamicObject;
if (fldMaterialValue != null)
{
long materialId = Convert.ToInt64(fldMaterialValue[0]);
string materialNumber = fldMaterial.GetRefPropertyValue(fldMaterialValue, "FNumber").ToString();
string materialName = fldMaterial.GetRefPropertyValue(fldMaterialValue, "FName").ToString();
}
// 数量
decimal fldQtyValue = Convert.ToDecimal(fldQty.DynamicProperty.GetValue(entityRow));
fldQty.DynamicProperty.SetValue(entityRow, fldQtyValue);
}
// 给单据体添加新行
DynamicObject newRow = new DynamicObject(entity.DynamicObjectType);
entityRows.Add(newRow);