有很多客户需求在后台自动生成某张单据,金蝶云星空提供了WebApi,包含了保存,提交,审核,删除单据的接口,下面以生产订单的保存,提交,审核为例,说明一下应用WebApi后台自动生成生产订单的功能,下面是代码示例,其他单据可以根据示例代码做相应的修改:
using Kingdee.BOS.Core.Bill.PlugIn;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ControlElement;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Resource;
using Kingdee.BOS.Util;
//using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.Msg;
using Kingdee.BOS.Orm.Drivers;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.DataEntity;
using System.ComponentModel;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using System.Data;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json;
public class DisProOrder : AbstractBillPlugIn
{
K3CloudApiClient client = new K3CloudApiClient("http://X.X.X.X:XXX/K3Cloud/");//参数是K3/Cloud的URL
//传入的参数根据需求来自行修改
public void SaveBill(DataSet ds, string OrderQty, string TouLiaoQty,int billno)
{
DataSet dstemp = new DataSet();
DataTable dtTemp = new DataTable();
string result = "";
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count == 0)
{
}
else
{
#region 保存生产订单信息
try
{
// 开始构建Web API参数对象
// 参数根对象:包含Creator、NeedUpDateFields、Model这三个子参数
// using Newtonsoft.Json.Linq; // (需引用Newtonsoft.Json.dll)
JObject jsonRoot = new JObject();
// Creator: 创建用户
jsonRoot.Add("CREATOR", "administrator");
// NeedUpDateFields: 哪些字段需要更新?为空则表示参数中全部字段,均需要更新
jsonRoot.Add("NEEDUPDATEFIELDS", new JArray(""));
jsonRoot.Add("NEEDRETURNFIELDS", new JArray(""));
jsonRoot.Add("ISDELETEENTRY", "TRUE");
jsonRoot.Add("SUBSYSTEMID", "");
// Model: 单据详细数据参数
JObject model = new JObject();
jsonRoot.Add("Model", model);
// 开始设置单据字段值
// 必须设置的字段:主键、单据类型、主业务组织,各必录且没有设置默认值的字段
// 特别注意:字段Key大小写是敏感的,建议从BOS设计器中,直接复制字段的标识属性过来
// 单据主键:必须填写,系统据此判断是新增还是修改单据;新增单据,填0
model.Add("FID", "0");
JObject basedata = new JObject();
//单据编号
model.Add("FBillNo", this.View.Model.GetValue("FBillNo").ToString()+billno.ToString("000"));
//单据类型
if (this.View.Model.GetValue("FBillType").ToString() != "")
{
basedata = new JObject();
DynamicObject BillType = this.View.Model.GetValue("FBillType") as DynamicObject;
string BillTypeNO = BillType["number"].ToString();
basedata.Add("FNumber", BillTypeNO);
model.Add("FBillType", basedata);
}
//单据日期
model.Add("FDate", this.View.Model.GetValue("FDate").ToString());
//生产组织
if (this.View.Model.GetValue("FPrdOrgId").ToString() != "")
{
basedata = new JObject();
DynamicObject FPrdOrg = this.View.Model.GetValue("FPrdOrgId") as DynamicObject;
string FPrdOrgNO = FPrdOrg["number"].ToString();
basedata.Add("FNumber", FPrdOrgNO);
model.Add("FPrdOrgId", basedata);
}
//计划组:FWorkGroupId
if (ds.Tables[0].Rows[0]["FWORKGROUPID"].ToString() != "0")
{
basedata = new JObject();
DynamicObject FWorkGroup = this.View.Model.GetValue("FWorkGroupId") as DynamicObject;
string FWorkGroupNO = FWorkGroup["number"].ToString();
basedata.Add("FNumber", FWorkGroupNO);
model.Add("FWorkGroupId", basedata);
}
//计划员:FPlannerID
if (ds.Tables[0].Rows[0]["FPLANNERID"].ToString() !="0")
{
basedata = new JObject();
DynamicObject FPlanner = this.View.Model.GetValue("FPlannerID") as DynamicObject;
string FPlannerNO = FPlanner["number"].ToString();
basedata.Add("FNumber", FPlannerNO);
model.Add("FPlannerID", basedata);
}
//备注
model.Add("FDescription", "");
//拆单数:F_PAEZ_CHAIDAN
model.Add("F_PAEZ_CHAIDAN", this.View.Model.GetValue("F_PAEZ_CHAIDAN").ToString());
//本单数:F_PAEZ_BENDAN
model.Add("F_PAEZ_BENDAN", OrderQty);
//原单数:F_PAEZ_YUANDAN
model.Add("F_PAEZ_YUANDAN", this.View.Model.GetValue("F_PAEZ_YUANDAN").ToString());
//投料数:F_PAEZ_TOULIAO
model.Add("F_PAEZ_TOULIAO", TouLiaoQty);
//是否已拆单:F_PAEZ_Text
model.Add("F_PAEZ_Text", "是");
// 单据类型:必须填写,是基础资料字段
// 基础资料类型字段填值,必须再构建一个JObject对象,设置基础资料编码
// 单据编号:可以忽略,由系统根据编码规则自动生成
//model.Add("FBILLNO", "201602160001");
// 开始构建单据体参数:集合参数JArray
JArray entryRows = new JArray();
// 把单据体行集合,添加到model中,以单据体Key为标识
string entityKey = "FTreeEntity";//单据体的标识
model.Add(entityKey, entryRows);
int rowscount;
rowscount = Convert.ToInt32(this.Model.GetEntryRowCount("FTreeEntity"));
// 通过循环创建单据体行:
for (int i = 0; i <= rowscount - 1; i++)
{
// 添加新行,把新行加入到单据体行集合
JObject entryRow = new JObject();
entryRows.Add(entryRow);
// 给新行,设置关键字段值
// 单据体主键:必须填写,系统据此判断是新增还是修改行
entryRow.Add("FEntryID", "0");
//产品类型:FProductType
entryRow.Add("FProductType", "");
//物料:基础资料,填写编码
DynamicObject Mat = this.View.Model.GetValue("FMaterialId", i) as DynamicObject;
string MatNO = Mat["number"].ToString();
//dtTemp = GetMaterialInfo(MatId);
basedata = new JObject();
basedata.Add("FNumber", MatNO);
entryRow.Add("FMaterialId", basedata);
//生产车间:FWorkShopID
if (ds.Tables[0].Rows[0]["FWORKSHOPID"].ToString() != "0")
{
basedata = new JObject();
DynamicObject FWorkShop = this.View.Model.GetValue("FWorkShopID", i) as DynamicObject;
string FWorkShopNO = FWorkShop["number"].ToString();
basedata.Add("FNumber", FWorkShopNO);
entryRow.Add("FWorkShopID", basedata);
}
单位,基础资料,填写编码
if (ds.Tables[0].Rows[0]["FUnitId"].ToString() != "0")
{
basedata = new JObject();
DynamicObject FUnit = this.View.Model.GetValue("FUnitId", i) as DynamicObject;
string FUnitNO = FUnit["number"].ToString();
basedata.Add("FNumber", FUnitNO);
entryRow.Add("FUnitId", basedata);
}
//数量:FQty
entryRow.Add("FQty", OrderQty);
//计划开工时间:FPlanStartDate (必填项)
entryRow.Add("FPlanStartDate", this.View.Model.GetValue("FPlanStartDate", i).ToString());
//计划完工时间:FPlanFinishDate (必填项)
entryRow.Add("FPlanFinishDate", this.View.Model.GetValue("FPlanFinishDate", i).ToString());
//BOM版本:FBomId
if (ds.Tables[0].Rows[0]["FBomId"].ToString() != "0")
{
basedata = new JObject();
DynamicObject FBom = this.View.Model.GetValue("FBomId", i) as DynamicObject;
string FBomNO = FBom["number"].ToString();
basedata.Add("FNumber", FBomNO);
entryRow.Add("FBomId", basedata);
}
//批号:FLot
//basedata = new JObject();
//basedata.Add("FNumber", "");
//entryRow.Add("FLot", basedata);
//备注:FMemoItem
entryRow.Add("FMemoItem", "");
//工艺路线:FRoutingId
if (ds.Tables[0].Rows[0]["FRoutingId"].ToString() != "0")
{
basedata = new JObject();
DynamicObject FRouting = this.View.Model.GetValue("FRoutingId") as DynamicObject;
string FRoutingNO = FRouting["number"].ToString();
basedata.Add("FNumber", FRoutingNO);
entryRow.Add("FRoutingId", basedata);
}
//仓库:FStockId
if (ds.Tables[0].Rows[0]["FStockId"].ToString() != "0")
{
basedata = new JObject();
DynamicObject FStock = this.View.Model.GetValue("FStockId") as DynamicObject;
string FStockNO = FStock["number"].ToString();
basedata.Add("FNumber", FStockNO);
entryRow.Add("FStockId", basedata);
}
创建Link行集合
//JArray linkRows = new JArray();
添加到单据体行中:Link子单据体标识 = 关联主单据体标识(POOrderEntry) + _Link
//string linkEntityKey = string.Format("{0}_Link", entityKey);
//entryRow.Add(linkEntityKey, linkRows);
创建Link行:
如有多条源单行,则分别创建Link行记录各条源单行信息
//JObject linkRow = new JObject();
//linkRows.Add(linkRow);
填写Link行上的字段值
特别说明:Link子单据体上字段的标识,必须在前面增加子单据体标识
FFlowId : 业务流程图,可选
//string fldFlowIdKey = string.Format("{0}_FFlowId", linkEntityKey);
//linkRow.Add(fldFlowIdKey, "");
FFlowLineId :业务流程图路线,可选
//string fldFlowLineIdKey = string.Format("{0}_FFlowLineId", linkEntityKey);
//linkRow.Add(fldFlowLineIdKey, "");
FRuleId :两单之间的转换规则内码,必填
可以通过如下SQL语句到数据库获取
select FID, *
from T_META_CONVERTRULE
where FSOURCEFORMID = 'PUR_Requisition'
and FTARGETFORMID = 'PUR_PurchaseOrder'
and FDEVTYPE = 0;
//string fldRuleIdKey = string.Format("{0}_FRuleId", linkEntityKey);
//linkRow.Add(fldRuleIdKey, "605a20be-79f9-4e86-b87c-08fa9dfa98f4");
FSTableName :必填,源单单据体表格编码,通过如下语句获取:
SELECT FTableNumber
FROM t_bf_tabledefine
WHERE fformid = 'PUR_Requisition'
AND fentitykey = 'FEntity'
如果如上语句未返回结果,请到K/3 Cloud中,手工选单一次,后台会自动产生表格编码
//string fldSTableNameKey = string.Format("{0}_FSTableName", linkEntityKey);
//linkRow.Add(fldSTableNameKey, "t_Dev_PUR_ReceiveEntry");
通过收料单号获取收料单内码和单据体分录内码
//string BillNo = ds.Tables["InStockMatInfo"].Rows[i]["F_DEV_SOURCEBILLNO"].ToString();
//string MatId = ds.Tables["InStockMatInfo"].Rows[i]["F_DEV_MATID"].ToString();
//string StockId = ds.Tables["InStockMatInfo"].Rows[i]["F_Dev_StockID"].ToString();
//string Position = ds.Tables["InStockMatInfo"].Rows[i]["F_Dev_Position"].ToString();
//dtTemp = GetData.GetReceiveInfo(BillNo, MatId, StockId, Position);
FSBillId :必填,源单单据内码
//string fldSBillIdKey = string.Format("{0}_FSBillId", linkEntityKey);
//linkRow.Add(fldSBillIdKey, dtTemp.Rows[0]["FID"].ToString());
FSId : 必填,源单单据体行内码。如果源单主关联实体是单据头,则此属性也填写源单单据内码
//string fldSIdKey = string.Format("{0}_FSId", linkEntityKey);
//linkRow.Add(fldSIdKey, dtTemp.Rows[0]["FEntryID"].ToString());
// FEntity_Link_FBaseQty :数量实际携带值,下推后,用户可以手工修改数量值;此字段存储最终的数量值
// 可选字段:
// 在保存时,系统会自动把单据体上数量值,更新到此字段;因此,这个字段可以不用填写(即使填写了,也会被覆盖)
string fldBaseQtyKey = string.Format("{0}_FBaseUnitQty", linkEntityKey);
linkRow.Add(fldBaseQtyKey, 10);
}
//return jsonRoot.ToString();
// 调用Web API接口服务,保存采购订单
result = Save("PRD_MO", jsonRoot.ToString());
//result = client.Execute(
// "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save",
// new object[] { "Dev_STK_InStock", jsonRoot.ToString() });
try
{
JObject jo = (JObject)JsonConvert.DeserializeObject(result);
string number = jo["Result"]["Number"].ToString();
//if (number != "")
//{
// string autrjson = "{\"CreateOrgId\":0,\"Numbers\":[\"" + number + "\"]}";
// result = Common.Audit("Dev_STK_InStock", autrjson);
//}
}
catch (Exception exp)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("程序运行遇到了未知的错误:");
sb.Append("错误提示:").AppendLine(exp.Message);
sb.Append("错误堆栈:").AppendLine(exp.StackTrace);
throw new Exception(sb.ToString() + result.ToString());
//throw new Exception(result);
}
}
catch (Exception exp)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("程序运行遇到了未知的错误:");
sb.Append("错误提示:").AppendLine(exp.Message);
sb.Append("错误堆栈:").AppendLine(exp.StackTrace);
throw new Exception(sb.ToString() + result.ToString());
}
#endregion
}
}
}
///
/// 登录
///
///
public bool Login()
{
string dbid = "5b59aae46cb7b1";//DBID,select FDATACENTERID from T_BAS_DATACENTER 在管理中心数据库中执行,可查到账套的DBID
string username = "administrator";//账套用户名
string password = "888888";//账套用户名密码
var loginResult = client.Login(
dbid,
username,
password,
2052);
return loginResult;
//return true;
}
//保存接口
public string Save(string formid, string jsonstr)
{
string result = "登录失败,请检查与站点地址、数据中心Id,用户名及密码!";
//string result = "";
if (Login())
{
//result = client.Execute(
// "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save",
// new object[] { formid, jsonstr });
result = client.Save(formid, jsonstr);
}
return result;
}
///
/// 审核
///
///
///
///
public static string Audit(string formid, string jsonstr)
{
string result = "登录失败,请检查与站点地址、数据中心Id,用户名及密码!";
result = client.Execute("Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit", new object[] { formid, jsonstr });
result = client.Execute("Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit", new object[] { formid, jsonstr });
return result;
}
}