案例:
Cloud的采購訂單同步到第三方系統,第三方系統入庫后同步生成Cloud采購入庫單。
解決方案:調用采購訂單的下推API,先生成保存狀態的采購入庫單(采購入庫單中的倉庫是必填項,可以在采購訂單中默認一個倉庫通過單據轉換關系攜帶,或者通過單據轉換插件默認一個倉庫),然后根據第三方的系統傳過來的數據,調用這個采購入庫單的保存API(JSON中的FID和FEntryID需要給定值,這個定值就是這個采購入庫單的FID和FEntryID),將數量等信息按照第三方系統傳過來的數據進行修改。
示例代碼,僅供參考。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Contracts;
using Kingdee.BOS;
using Kingdee.BOS.Core;
using System.IO;
using System.Configuration;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.Core.Msg;
using Kingdee.BOS.Util;
using System.Dynamic;
using Kingdee.BOS.Orm.Drivers;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.DataEntity;
using Newtonsoft.Json;
namespace K3CloudPlugIn
{
public class GetInstockDataInfo : IScheduleService
{
K3CloudApiClient client = new K3CloudApiClient(Common.url);
/// <summary>
/// 實際運行的Run 方法
/// </summary>
/// <param name="ctx"></param>
/// <param name="schedule"></param>
public void Run(Context ctx, Schedule schedule)
{
//獲取入庫單中間表數據,並生成相應的采購入庫單,委外入庫單,生產入庫單
K3JSTService.XZGTCBS InstockService = new K3JSTService.XZGTCBS();
string FDate = DateTime.Now.ToShortDateString();
//調用WebService,將數據插入中間表
//FDate = "2019-12-11";
InstockService.GetCaiGouRuKuInfo(FDate);
string ReturnInfo = "";
string JsonInfo = "";
string BillSql = "";
string EntrySql = "";
string result = "";
int TotalRows = 0;
string io_id = "";//聚水潭入庫單ID
string po_id = "";//聚水潭入庫單關聯的采購單ID
DataSet ds_BillSql = new DataSet();
DataSet ds_EntrySql = new DataSet();
string OrderSql = "";
DataSet ds_OrderSql = new DataSet();
string FBillNo = "";
string FBillID = "";
JObject jsonRoot;
JArray Numbers;
JObject CustomParams;
var loginResult = "";
var resultType = 0;
string autrjson;
JObject joAudit;
JObject joAuditIsSucess;
JArray Fields;
string AlterSql = "";
DataSet ds_AlterSql = new DataSet();
JObject basedata;
string SaveResult = "";
string FOrgId = "";
BillSql = @"/*dialect*/select distinct a.* from MyOrderHouse a left join MyOrderHouseEntry b on a.io_id = b.io_id ";
BillSql = BillSql + "left join T_BD_MATERIAL c on c.F_RFGW_TEXT = b.sku_id where ISNULL(c.FMATERIALID,0) > 0 and ISNULL(a.WinBillID,0) = 0 ";
ds_BillSql = Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDataSet(ctx, BillSql);
TotalRows = ds_BillSql.Tables[0].Rows.Count;
if (TotalRows > 0)
{
for (int i = 0; i < TotalRows; i++)
{
io_id = ds_BillSql.Tables[0].Rows[i]["io_id"].ToString();
po_id = ds_BillSql.Tables[0].Rows[i]["po_id"].ToString();
#region 第一種情況:該入庫單對應的采購訂單是由Cloud的采購訂單同步過去的
//第一種情況:該入庫單對應的采購訂單是由Cloud的采購訂單同步過去的
OrderSql = @"/*dialect*/select * from t_PUR_POOrder where FJSTORDERNO = '" + po_id + "'";
ds_OrderSql = Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDataSet(ctx,OrderSql);
if (ds_OrderSql.Tables[0].Rows.Count > 0)
{
FOrgId = ds_OrderSql.Tables[0].Rows[0]["FPURCHASEORGID"].ToString();
//獲取入庫單的物料編碼和數量,以及對應采購訂單的分錄內碼
EntrySql = @"/*dialect*/select distinct e.FENTRYID as 'EntryID',b.qty as 'Qty',c.FNUMBER as 'MatNO' from MyOrderHouse a left join MyOrderHouseEntry b on a.io_id = b.io_id ";
EntrySql = EntrySql + "left join T_BD_MATERIAL c on b.sku_id = c.F_RFGW_TEXT left join t_PUR_POOrder d on a.po_id = d.FJSTORDERNO ";
EntrySql = EntrySql + "left join t_PUR_POOrderEntry e on d.FID = e.FID and c.FMATERIALID = e.FMATERIALID ";
EntrySql = EntrySql + "where a.io_id = '" + io_id + "' and ISNULL(e.FENTRYID,0) > 0 and ISNULL(c.FMATERIALID,0) > 0 and c.FUSEORGID = '" + FOrgId + "'";
ds_EntrySql = Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDataSet(ctx,EntrySql);
if (ds_EntrySql.Tables[0].Rows.Count > 0)
{
Common.STKEntryIds = new string[ds_EntrySql.Tables[0].Rows.Count];
Common.STKMats = new string[ds_EntrySql.Tables[0].Rows.Count];
Common.STKMatIds = new int[ds_EntrySql.Tables[0].Rows.Count];
Common.STKqty = new decimal[ds_EntrySql.Tables[0].Rows.Count];
Common.STKprice = new decimal[ds_EntrySql.Tables[0].Rows.Count];
for (int j = 0; j < ds_EntrySql.Tables[0].Rows.Count; j++)
{
Common.STKMats[j] = ds_EntrySql.Tables[0].Rows[j]["MatNO"].ToString();
Common.STKEntryIds[j] = ds_EntrySql.Tables[0].Rows[j]["EntryID"].ToString();
Common.STKqty[j] = Convert.ToDecimal(ds_EntrySql.Tables[0].Rows[j]["Qty"].ToString());
}
}
//調用采購訂單的下推API
// 開始構建Web API參數對象
// using Newtonsoft.Json.Linq; // (需引用Newtonsoft.Json.dll)
jsonRoot = new JObject();
jsonRoot.Add("Ids", "");//單據內碼集合,字符串類型,格式:"Id1,Id2,..."(使用內碼時必錄)
Numbers = new JArray();
jsonRoot.Add("Numbers", Numbers);//單據編碼集合,數組類型,格式:[No1,No2,...](使用編碼時必錄)
jsonRoot.Add("EntryIds", string.Join(",", Common.STKEntryIds));//分錄內碼集合,逗號分隔(分錄下推時必錄),注(按分錄下推時,單據內碼和編碼不需要填,否則按整單下推)
jsonRoot.Add("RuleId", "44789b6e-ef2e-44a3-bcb9-dd38b8f13eae");//轉換規則內碼,字符串類型(未啟用默認轉換規則時,則必錄)
jsonRoot.Add("TargetBillTypeId", "");//目標單據類型內碼,字符串類型(非必錄)
jsonRoot.Add("TargetOrgId", 0);//目標組織內碼,整型(非必錄)
jsonRoot.Add("TargetFormId", "STK_InStock");//目標單據FormId,字符串類型,(啟用默認轉換規則時,則必錄)
jsonRoot.Add("IsEnableDefaultRule", "true"); //是否啟用默認轉換規則,布爾類型,默認false(非必錄)
jsonRoot.Add("IsDraftWhenSaveFail", "false");
CustomParams = new JObject();//自定義參數,字典類型,格式:"{key1:value1,key2:value2,...}"(非必錄) ,注(傳到轉換插件的操作選項中,平台不會解析里面的值)
jsonRoot.Add("CustomParams", CustomParams);
JsonInfo = jsonRoot.ToString();
loginResult = client.ValidateLogin(Common.dbid, Common.username,Common.password, 2052);
resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
//登錄結果類型等於1,代表登錄成功
if (resultType == 1)
{
ReturnInfo = client.Push("PUR_PurchaseOrder", JsonInfo);
try
{
JObject jo = (JObject)JsonConvert.DeserializeObject(ReturnInfo);
JObject JoResult = (JObject)JsonConvert.DeserializeObject(jo["Result"]["ResponseStatus"].ToString());
JArray array = (JArray)JsonConvert.DeserializeObject(JoResult["SuccessEntitys"].ToString());
FBillNo = array[0]["Number"].ToString();
FBillID = array[0]["Id"].ToString();
if (FBillNo != "")
{
//調用采購入庫單的保存API,修改實收數量和倉庫
jsonRoot = new JObject();
jsonRoot.Add("Creator", "");
Fields = new JArray();
Fields.Add("FInStockEntry");
Fields.Add("FRealQty");
jsonRoot.Add("NeedUpDateFields", Fields);
jsonRoot.Add("NeedReturnFields", new JArray());
jsonRoot.Add("IsDeleteEntry", "false");
jsonRoot.Add("SubSystemId", "");
jsonRoot.Add("IsVerifyBaseDataField", "false");
jsonRoot.Add("IsEntryBatchFill", "True");
jsonRoot.Add("ValidateFlag", "True");
jsonRoot.Add("NumberSearch", "True");
jsonRoot.Add("InterationFlags", "STK_InvCheckResult");
// Model: 單據詳細數據參數
JObject model = new JObject();
jsonRoot.Add("Model", model);
// 單據主鍵:必須填寫,系統據此判斷是新增還是修改單據;新增單據,填0
model.Add("FID", FBillID.ToString());
JArray entryRows = new JArray();
// 把單據體行集合,添加到model中,以單據體Key為標識
string entityKey = "FInStockEntry";
model.Add(entityKey, entryRows);
AlterSql = @"/*dialect*/select b.FSEQ,b.FENTRYID,c.FNUMBER,b.FREALQTY from t_STK_InStock a left join T_STK_INSTOCKENTRY b on a.fid = b.fid ";
AlterSql = AlterSql + "left join T_BD_MATERIAL c on b.FMATERIALID = c.FMATERIALID where a.fbillno = '" + FBillNo + "'";
ds_AlterSql = Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDataSet(ctx, AlterSql);
for (int j = 0; j < ds_AlterSql.Tables[0].Rows.Count; j++)
{
for (int k = 0; k < Common.STKEntryIds.Length; k++)
{
//if (ds_AlterSql.Tables[0].Rows[j]["FSEQ"].ToString().Equals(Common.Seqs[k].ToString()) && ds_AlterSql.Tables[0].Rows[j]["FMATERIALID"].ToString().Equals(Common.TarMatIds[k].ToString()))
if (ds_AlterSql.Tables[0].Rows[j]["FNUMBER"].ToString().Equals(Common.STKMats[k].ToString()))
{
// 添加新行,把新行加入到單據體行集合
JObject entryRow = new JObject();
entryRows.Add(entryRow);
// 給新行,設置關鍵字段值
// 單據體主鍵:必須填寫,系統據此判斷是新增還是修改行
entryRow.Add("FEntryID", ds_AlterSql.Tables[0].Rows[j]["FENTRYID"].ToString());
entryRow.Add("FRealQty", Convert.ToDecimal(Common.STKqty[k].ToString()));
}
}
}
SaveResult = Save("STK_InStock", jsonRoot.ToString());
try
{
JObject josave = (JObject)JsonConvert.DeserializeObject(SaveResult);
string number = josave["Result"]["Number"].ToString();
string id = josave["Result"]["Id"].ToString();
if (number != "")
{
autrjson = "{\"CreateOrgId\":0,\"Numbers\":[\"" + FBillNo + "\"],\"InterationFlags\":\"STK_InvCheckResult\"}";
result = Audit("STK_InStock", autrjson);
joAudit = (JObject)JsonConvert.DeserializeObject(result);
joAuditIsSucess = (JObject)JsonConvert.DeserializeObject(joAudit["Result"]["ResponseStatus"].ToString());
string IsSucess = joAuditIsSucess["IsSuccess"].ToString();
if (IsSucess.Equals("True"))
{
Kingdee.BOS.ServiceHelper.DBServiceHelper.Execute(ctx, @"/*dialect*/update MyOrderHouse set WinBillID = '" + FBillID + "',WinBillNO = '" + FBillNo + "' where io_id = '" + io_id + "'");
}
}
}
catch (Exception)
{
throw;
}
}
}
catch (Exception)
{
}
}
}
#endregion
}
}
}
/// <summary>
/// 登錄
/// </summary>
/// <returns></returns>
public bool Login()
{
string dbid = Common.dbid;
string username = Common.username;
string password = Common.password;
var loginResult = client.Login(
dbid,
username,
password,
2052);
return loginResult;
}
/// <summary>
/// 保存
/// </summary>
/// <param name="formid"></param>
/// <param name="jsonstr"></param>
/// <returns></returns>
public string Save(string formid, string jsonstr)
{
string result = "登錄失敗,請檢查與站點地址、數據中心Id,用戶名及密碼!";
//string result = "";
if (Login())
{
result = client.Execute<string>(
"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save",
new object[] { formid, jsonstr });
}
return result;
}
/// <summary>
/// 審核
/// </summary>
/// <param name="formid"></param>
/// <param name="jsonstr"></param>
/// <returns></returns>
public string Audit(string formid, string jsonstr)
{
string result = "登錄失敗,請檢查與站點地址、數據中心Id,用戶名及密碼!";
result = client.Execute<string>("Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit", new object[] { formid, jsonstr });
result = client.Execute<string>("Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit", new object[] { formid, jsonstr });
return result;
}
}
}