1012 lines
58 KiB
Plaintext
Executable File
1012 lines
58 KiB
Plaintext
Executable File
using GXWebApi.Common;
|
||
using Kingdee.BOS.WebApi.Client;
|
||
using log4net;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.Web.Http;
|
||
using System.Xml.Linq;
|
||
|
||
namespace GXWebApi.Controllers
|
||
{
|
||
public class DifferentialController : ApiController
|
||
{
|
||
private static readonly ILog Logger = LogManager.GetLogger(typeof(DifferentialController));
|
||
|
||
// TODO: 在此添加金蝶云星空相关的 Action 方法
|
||
// 示例:
|
||
[HttpGet]
|
||
public IHttpActionResult Test()
|
||
{
|
||
return Ok("GXWebApi is running.");
|
||
}
|
||
|
||
#region 单独更新创建CRM产品
|
||
[HttpPost]
|
||
public IHttpActionResult CrUpCrmProduct([FromBody] JObject obj)
|
||
{
|
||
#region 定义参数
|
||
string FMaNumber = obj["FMaNumber"].ToString();
|
||
K3CloudApiClient client = null;
|
||
string acctID = "20191111204222978"; //登录帐套ID
|
||
string userID = "sysmes"; //用户账号
|
||
string password = "Admin@123"; //用户密码
|
||
string erpURL = "https://gaoxian.ik3cloud.com/k3cloud/"; //K3URL
|
||
int erpLaType = 2052; //语言种类4
|
||
|
||
#endregion
|
||
|
||
try
|
||
{
|
||
//第一步,登录金蝶系统
|
||
client = new K3CloudApiClient(erpURL);
|
||
var loginResult = client.ValidateLogin(acctID, userID, password, erpLaType);
|
||
var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
|
||
if (resultType == 1)
|
||
{
|
||
//ERP接口Json
|
||
JObject QueryData = new JObject()
|
||
{
|
||
{ "FormId", "BD_MATERIAL" },
|
||
{ "FieldKeys", "" } ,
|
||
{ "OrderString", "" },
|
||
{ "FilterString", "" } ,
|
||
{ "TopRowCount", 0 },
|
||
{ "StartRow", 0 } ,
|
||
{ "Limit", 5000 }
|
||
};
|
||
QueryData["FieldKeys"] = "FMATERIALID,FNumber,FName,FUSEORGID.FNUMBER,FTaxRateId.FNumber,FSpecification,FForbidStatus,FIsEnable";
|
||
QueryData["OrderString"] = "FMATERIALID";
|
||
QueryData["FilterString"] = $" FDOCUMENTSTATUS = 'C' and FUSEORGID = '1' and FInvPtyId=10003 and FNumber = '{FMaNumber}' ";
|
||
var QueryList = client.ExecuteBillQuery(QueryData.ToString());
|
||
if (QueryList != null && QueryList.Count > 0)
|
||
{
|
||
|
||
string firstCellValue = QueryList[0][0].ToStringEx();
|
||
try
|
||
{
|
||
JToken returnToken = JToken.Parse(firstCellValue);
|
||
if (returnToken is JObject returnInfo && returnInfo.TryGetValue("Result", out JToken VALUE))
|
||
{
|
||
//LogHelper.WriteLog(returnInfo["Result"]["ResponseStatus"]["Errors"][0]["Message"].ToStringEx());//写入错误日志中(2021/10/12,经测试此代码暂时无法写入日志)
|
||
throw new Exception(returnInfo["Result"]["ResponseStatus"]["Errors"][0]["Message"].ToStringEx());//提示错误信息,并中止此次调用
|
||
}
|
||
}
|
||
catch (JsonReaderException)
|
||
{
|
||
// firstCellValue 不是 JSON 格式,说明是正常的查询结果数据,跳过错误检查
|
||
}
|
||
|
||
foreach (var sub in QueryList)
|
||
{
|
||
#region 获取金蝶字段信息
|
||
CrmApi crmApi = new CrmApi();
|
||
string MaterialID = sub[0].ToStringEx(); //物料内码
|
||
string MaterialNumber = sub[1].ToStringEx(); //物料内码
|
||
string MaterialName = sub[2].ToStringEx().Replace("\0", "").Replace(@"\u0000", ""); //物料名称
|
||
string OrgNumber = sub[3].ToStringEx(); //使用组织编码
|
||
string FTAXRATEID = sub[4].ToStringEx(); //税率
|
||
string FITEMMODEL = sub[5].ToStringEx(); //规格型号
|
||
string FForbidStatus = sub[6].ToStringEx().Equals("A") ? "1" : "2"; //禁用状态
|
||
bool FIsEnable = Convert.ToBoolean(sub[7]); //是否启用BOM版本
|
||
if (FITEMMODEL.Length > 200)
|
||
{
|
||
FITEMMODEL = FITEMMODEL.Substring(0, 199);
|
||
}
|
||
#endregion
|
||
|
||
#region 获取Token
|
||
string SelSQL = "select id from productCategory where code__c ='" + MaterialNumber + "' ";
|
||
string Token = crmApi.GetToken();
|
||
if (string.IsNullOrEmpty(Token))
|
||
{
|
||
throw new Exception("获取CRM Token失败!");
|
||
}
|
||
#endregion
|
||
|
||
#region 产品(物料)
|
||
|
||
string SelData = string.Empty;
|
||
|
||
#region 获取税率
|
||
SelSQL = "select id from taxRate__c where code__c = '" + FTAXRATEID + "'";
|
||
SelData = crmApi.SelectSQLApi(SelSQL, Token);
|
||
SelSQLData TAXRATEData = JsonConvert.DeserializeObject<SelSQLData>(SelData);
|
||
int TAXRATECount = TAXRATEData.result.count;
|
||
string TAXRATEID = string.Empty;
|
||
if (TAXRATECount > 0)
|
||
{
|
||
List<Dictionary<string, object>> keys = TAXRATEData.result.records;
|
||
TAXRATEID = keys[0]["id"].ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region 判断是否已有
|
||
string ProductID = string.Empty;
|
||
SelSQL = "select id from product where productName ='" + MaterialNumber + "' ";
|
||
SelData = crmApi.SelectSQLApi(SelSQL, Token);
|
||
SelSQLData selSQLData2 = JsonConvert.DeserializeObject<SelSQLData>(SelData);
|
||
int count2 = selSQLData2.result.count;
|
||
|
||
ProductRoot productRoot = new ProductRoot();
|
||
ProductData productData = new ProductData();
|
||
productData.entityType = "11010000400001";
|
||
productData.customItem168__c = MaterialID;
|
||
productData.productName = MaterialNumber;
|
||
productData.customItem161__c = MaterialName;
|
||
productData.fscProductModel = FITEMMODEL;
|
||
productData.enableStatus = FForbidStatus;//禁用状态
|
||
productData.defaultTaxRate__c = TAXRATEID;
|
||
productData.baseUnitId = "4084356036024971"; //PCS
|
||
productData.parentId = "4084144016153157";//产品目录
|
||
productData.isActiveBom__c = FIsEnable;//是否启用BOM版本 1:是;0:否
|
||
productRoot.data = productData;
|
||
|
||
if (count2 <= 0)
|
||
{
|
||
#region 创建
|
||
string CproductReturnData = crmApi.CreateApi("product", Token, JsonConvert.SerializeObject(productRoot));
|
||
CreateReturnData createReturnData = JsonConvert.DeserializeObject<CreateReturnData>(CproductReturnData);
|
||
if (!createReturnData.code.Equals("200"))
|
||
{
|
||
throw new Exception("返回值:" + createReturnData.code + ",创建CRM产品失败:" + createReturnData.msg + "接口传入JSON:" + JsonConvert.SerializeObject(productRoot));
|
||
}
|
||
#endregion
|
||
}
|
||
else
|
||
{
|
||
#region 更新
|
||
string F_PATC_Text_qtr = selSQLData2.result.records[0]["id"].ToString();
|
||
string ReturnData = crmApi.UpdateApi("product", F_PATC_Text_qtr, Token, JsonConvert.SerializeObject(productRoot));
|
||
CreateReturnData createReturnData = JsonConvert.DeserializeObject<CreateReturnData>(ReturnData);
|
||
if (!createReturnData.code.Equals("200"))
|
||
{
|
||
throw new Exception("返回值:" + createReturnData.code + ",更新CRM产品失败:" + createReturnData.msg + "接口传入JSON:" + JsonConvert.SerializeObject(productRoot));
|
||
}
|
||
#endregion
|
||
}
|
||
#endregion
|
||
|
||
#endregion
|
||
}
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("未找到当前物料信息!");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("登录云星空失败!");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Ok(new
|
||
{
|
||
success = "false",
|
||
message = "同步失败:" + ex.Message
|
||
});
|
||
}
|
||
return Ok(new
|
||
{
|
||
success = "true",
|
||
message = "同步成功"
|
||
});
|
||
}
|
||
#endregion
|
||
|
||
#region 单独更新金蝶销售订单新变更单到CRM系统
|
||
[HttpPost]
|
||
public IHttpActionResult KingdeeSynCRMXSDDXBGD([FromBody] JObject obj)
|
||
{
|
||
#region 定义参数
|
||
string FBillNO = obj["FBillNO"].ToString();
|
||
K3CloudApiClient client = null;
|
||
string acctID = "20191111204222978"; //登录帐套ID
|
||
string userID = "sysmes"; //用户账号
|
||
string password = "Admin@123"; //用户密码
|
||
string erpURL = "https://gaoxian.ik3cloud.com/k3cloud/"; //K3URL
|
||
int erpLaType = 2052; //语言种类4
|
||
#endregion
|
||
|
||
|
||
try
|
||
{
|
||
//第一步,登录金蝶系统
|
||
client = new K3CloudApiClient(erpURL);
|
||
var loginResult = client.ValidateLogin(acctID, userID, password, erpLaType);
|
||
var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
|
||
if (resultType == 1)
|
||
{
|
||
//ERP接口Json
|
||
JObject QueryData = new JObject() {
|
||
{ "FormId", "SAL_XORDER" },
|
||
{ "FieldKeys", "" } ,
|
||
{ "OrderString", "" },
|
||
{ "FilterString", "" } ,
|
||
{ "TopRowCount", 0 },
|
||
{ "StartRow", 0 } ,
|
||
{ "Limit", 5000 }
|
||
};
|
||
string strFieldKeys = "FBILLNO,F_PATC_Text_qtr1,F_PATC_Text_qtr,FChangeType,FMATERIALID.FNUMBER,FMaterialName,FBOMID.FNUMBER,FBOMID.FNAME,F_PATC_Text_h1g,FUnitID.FNUMBER,FQty,FPrice,FTaxPrice,FEntryTaxRate,FEntryTaxAmount," +
|
||
"FAmount,FAllAmount,FDiscountRate,FDiscount,FDeliveryDate,FPriceUnitQty,FStockOrgId.FNUMBER,FSettleOrgIds.FNUMBER,F_PATC_Text_fg2,FSaleOrderEntry_FENTRYID,FCloseStatus,FMrpTerminateStatus,FMrpCloseStatus";
|
||
QueryData["FieldKeys"] = strFieldKeys;
|
||
QueryData["OrderString"] = "FID";
|
||
QueryData["FilterString"] = $"FBILLNO = '{FBillNO}' ";
|
||
|
||
var QueryList = client.ExecuteBillQuery(QueryData.ToString());
|
||
if (QueryList != null && QueryList.Count > 0)
|
||
{
|
||
string firstCellValue = QueryList[0][0].ToStringEx();
|
||
|
||
#region 同步异常抓取
|
||
try
|
||
{
|
||
JToken returnToken = JToken.Parse(firstCellValue);
|
||
if (returnToken is JObject returnInfo && returnInfo.TryGetValue("Result", out JToken VALUE))
|
||
{
|
||
//LogHelper.WriteLog(returnInfo["Result"]["ResponseStatus"]["Errors"][0]["Message"].ToStringEx());//写入错误日志中(2021/10/12,经测试此代码暂时无法写入日志)
|
||
throw new Exception(returnInfo["Result"]["ResponseStatus"]["Errors"][0]["Message"].ToStringEx());//提示错误信息,并中止此次调用
|
||
}
|
||
}
|
||
catch (JsonReaderException)
|
||
{
|
||
// firstCellValue 不是 JSON 格式,说明是正常的查询结果数据,跳过错误检查
|
||
}
|
||
#endregion
|
||
|
||
#region ======同步销售订单新变更单表头数据==============
|
||
string OrderID = string.Empty, Token = string.Empty, FBILLNO = string.Empty, F_PATC_Text_qtr1 = string.Empty;
|
||
CrmApi crmApi = new CrmApi();
|
||
string entityType = string.Empty;
|
||
foreach (var sub in QueryList)
|
||
{
|
||
#region 获取金蝶信息
|
||
F_PATC_Text_qtr1 = sub[1].ToStringEx();//CRM订单内码
|
||
FBILLNO = sub[0].ToStringEx();//订单编号
|
||
string FCloseStatus = sub[25].ToStringEx().Equals("A") ? "1" : "2";//关闭状态 A=正常;B=关闭
|
||
if (F_PATC_Text_qtr1.IsNullOrEmpty())
|
||
{
|
||
break;
|
||
}
|
||
#endregion
|
||
|
||
#region 获取Token
|
||
Token = crmApi.GetToken();
|
||
if (string.IsNullOrEmpty(Token))
|
||
{
|
||
throw new Exception("获取CRMToken失败!");
|
||
}
|
||
#endregion
|
||
|
||
#region 获取订单头信息 - 使用JObject避免动态类型问题
|
||
string OrderInfoJson = crmApi.GetOrderInfoApi(F_PATC_Text_qtr1, Token);
|
||
|
||
// 方法1:使用JObject而不是dynamic
|
||
JObject OrderInfo = null;
|
||
OrderInfo = JObject.Parse(OrderInfoJson);
|
||
|
||
// 检查data是否存在
|
||
if (OrderInfo["data"] == null || !OrderInfo["data"].HasValues)
|
||
{
|
||
throw new Exception("获取订单头信息失败;接口订单ID值:" + OrderInfoJson);
|
||
}
|
||
|
||
JToken data = OrderInfo["data"];
|
||
#endregion
|
||
|
||
#region 创建订单头
|
||
string OrderentityType = data["entityType"].ToStringEx();
|
||
if (OrderentityType.Equals("11010003500001") || OrderentityType.Equals("4092834627246728"))
|
||
{
|
||
entityType = "11010001900001";
|
||
}
|
||
else
|
||
{
|
||
entityType = "11010001900004";
|
||
}
|
||
string[] propertiesToCopy = new string[]
|
||
{
|
||
"orderCategory__c",
|
||
"contractPayMethod__c",
|
||
"payMethod__c",
|
||
"contractAdvancePayRatio__c",
|
||
"advancePaymentRatio__c",
|
||
"contractPayRatio__c",
|
||
"paymentRatio__c",
|
||
"contractBalanceRatio__c",
|
||
"balanceRatio__c",
|
||
"paymentTerms__c",
|
||
"salesOrg__c",
|
||
"accountId",
|
||
"domeAndInternational__c",
|
||
"accountEntityType__c",
|
||
"customerCategory__c",
|
||
"tradeTerms__c",
|
||
"settlementCurrency__c",
|
||
"salesContractNo__c",
|
||
"salesCategory__c",
|
||
"deliveryConditions__c",
|
||
"comment",
|
||
"salesOwnerId__c",
|
||
"salesOwner__c",
|
||
"country__c",
|
||
"isTax__c",
|
||
"salesRegion__c",
|
||
"usageScenarios__c",
|
||
"attachment__c",
|
||
"isSignAgreement__c",
|
||
"contractId",
|
||
"opportunityId",
|
||
"collaboratorId__c",
|
||
"collaborator__c",
|
||
"demoMachineSN__c",
|
||
"demoLocomotiveNum__c",
|
||
"totalAmount__c",
|
||
"amountCapitalLetters__c",
|
||
"amount",
|
||
"wholeOrderdiscountamount__c",
|
||
"totalDiscount__c",
|
||
"isLessThanOff__c",
|
||
"orderDetailMinDiscount__c",
|
||
"isProductLessThanOff__c",
|
||
"totalDiscountAmount",
|
||
"invoiceTaxRatio__c",
|
||
"freightBearer__c",
|
||
"consumablesProvider__c",
|
||
"receiverName",
|
||
"contactAddress",
|
||
"receiverTel",
|
||
"printingMethod__c",
|
||
"sealOrg__c",
|
||
"sealName__c",
|
||
"businessType__c",
|
||
"trackingBumber__c",
|
||
"deliveryDate",
|
||
"isPresalesConfirm__c",
|
||
"shippingMethod__c",
|
||
"forwarderMethod__c",
|
||
"dangerousGoodsCerti__c",
|
||
"parentAccount__c",
|
||
"exchangeRate__c",
|
||
"isPlanProd__c",
|
||
"planProdDate__c",
|
||
"payType__c",
|
||
"syncStatus__c",
|
||
"sendERPTime__c",
|
||
"syncMessage__c",
|
||
"jd_FNumber__c",
|
||
"jd_FId__c",
|
||
"changeReason__c",
|
||
"ownerId",
|
||
"dimDepart"
|
||
};
|
||
|
||
JObject orderData = new JObject();
|
||
orderData["originalOrderId"] = F_PATC_Text_qtr1;
|
||
orderData["po"] = FBILLNO;
|
||
orderData["co"] = FBILLNO;
|
||
orderData["entityType"] = "11010003500003";
|
||
orderData["erpCloseStatus__c"] = FCloseStatus;
|
||
foreach (var prop in propertiesToCopy)
|
||
{
|
||
var sourceValue = data[prop];
|
||
if (sourceValue != null)
|
||
{
|
||
orderData[prop] = sourceValue;
|
||
}
|
||
else
|
||
{
|
||
orderData[prop] = null;
|
||
}
|
||
}
|
||
JObject OrderJson = new JObject();
|
||
OrderJson["data"] = orderData;
|
||
string OrderJsonString = OrderJson.ToString(Formatting.Indented);
|
||
string ReturnData = crmApi.CreateOrderInfoApi(OrderJsonString, Token);
|
||
CreateReturnData ReturnDataD = JsonConvert.DeserializeObject<CreateReturnData>(ReturnData);
|
||
if (!ReturnDataD.code.ToStringEx().Equals("200"))
|
||
{
|
||
throw new Exception("返回值:" + ReturnDataD.code.ToStringEx() + ",创建销售订单变更单订单头失败:" + ReturnDataD.msg.ToStringEx() + "接口传入JSON:" + OrderJsonString);
|
||
}
|
||
OrderID = ReturnDataD.data.id.ToStringEx();
|
||
break;
|
||
#endregion
|
||
}
|
||
#endregion
|
||
|
||
#region ======同步销售订单新变更单表体数据==============
|
||
List<Dictionary<string, string>> ChangeFEntryID_CList = new List<Dictionary<string, string>>();
|
||
string FEntryID = string.Empty;
|
||
foreach (var sub in QueryList)
|
||
{
|
||
#region 获取金蝶信息
|
||
Dictionary<string, string> ChangeFEntryID_C = new Dictionary<string, string>();
|
||
string F_PATC_Text_qtr = sub[2].ToStringEx();//CRM订单明细内码
|
||
if (F_PATC_Text_qtr1.IsNullOrEmpty())
|
||
{
|
||
break;
|
||
}
|
||
string FChangeType1 = sub[3].ToStringEx();//变更类型--新增:A;修改:B;删除:D;终止:T;关闭:C
|
||
if (F_PATC_Text_qtr.Equals(" ")) //特殊处理,存在变更类型是修改的,但是实际是新增的情况
|
||
{
|
||
FChangeType1 = "A";
|
||
}
|
||
|
||
string FMaterialNumber = sub[4].ToStringEx();//物料编号
|
||
string FMaterialName = sub[5].ToStringEx();//物料名称
|
||
string FBomId = sub[6].ToStringEx();//BOM版本
|
||
string FBomShortId = sub[7].ToStringEx();//BOM简称
|
||
string F_PATC_Text_h1g = sub[8].ToStringEx();//工艺SOP标准
|
||
string FUnitID = sub[9].ToStringEx();//销售单位
|
||
string FQty = sub[10].ToStringEx();//销售数量
|
||
string FPrice = sub[11].ToStringEx();//单价
|
||
string FTaxPrice = sub[12].ToStringEx();//含税单价
|
||
string FEntryTaxRate = (sub[13].ToInt32() / 100.0).ToStringEx();//税率--
|
||
string FEntryTaxAmount = sub[14].ToStringEx();//税额
|
||
string FAmount = sub[15].ToStringEx();//金额
|
||
string FAllAmount = sub[16].ToStringEx();//价税合计
|
||
string FDiscountRate = (sub[17].ToInt32() / 100.0).ToStringEx();//折扣率--
|
||
string FDiscount = sub[18].ToStringEx();//折扣额
|
||
long FDeliveryDate = crmApi.ToUnixTimeMilliseconds(Convert.ToDateTime(sub[19]));
|
||
//long FDeliveryDate = crmApi.ToUnixTimeMilliseconds(Convert.ToDateTime("2026/6/1 17:42:35"));
|
||
string FPriceUnitQty = sub[20].ToStringEx();//计价数量
|
||
string FStockOrgId = sub[21].ToStringEx();//库存组织--
|
||
string FSettleOrgIds = sub[22].ToStringEx();//结算组织--
|
||
string F_PATC_Text_fg2 = sub[23].ToStringEx();//软件版本类型--
|
||
FEntryID = sub[24].ToStringEx();//行内码
|
||
string FMrpTerminateStatus = sub[26].ToStringEx().Equals("A") ? "1" : "2";//业务终止 A=正常;B=终止
|
||
string FMrpCloseStatus = sub[27].ToStringEx().Equals("A") ? "1" : "2";//业务关闭 A=未关闭;B=业务关闭
|
||
string FChangeType = string.Empty;
|
||
string erpChangeType__c = string.Empty;
|
||
switch (FChangeType1)
|
||
{
|
||
case "A":
|
||
FChangeType = "1";
|
||
erpChangeType__c = "1";
|
||
break;
|
||
case "B":
|
||
FChangeType = "3";
|
||
erpChangeType__c = "2";
|
||
break;
|
||
case "T":
|
||
FChangeType = "3";
|
||
erpChangeType__c = "4";
|
||
break;
|
||
case "C":
|
||
FChangeType = "3";
|
||
erpChangeType__c = "5";
|
||
break;
|
||
case "D":
|
||
FChangeType = "2";
|
||
erpChangeType__c = "3";
|
||
break;
|
||
}
|
||
|
||
switch (F_PATC_Text_fg2)
|
||
{
|
||
case "当前出货隐私版":
|
||
F_PATC_Text_fg2 = "1";
|
||
break;
|
||
case "当前出货非隐私版":
|
||
F_PATC_Text_fg2 = "2";
|
||
break;
|
||
case "客户定制版":
|
||
F_PATC_Text_fg2 = "3";
|
||
break;
|
||
case "75(亚马逊定制-隐私版)":
|
||
F_PATC_Text_fg2 = "4";
|
||
break;
|
||
case "75(港铁定制-非隐私版)":
|
||
F_PATC_Text_fg2 = "5";
|
||
break;
|
||
default:
|
||
F_PATC_Text_fg2 = "6";
|
||
break;
|
||
}
|
||
#endregion
|
||
|
||
#region 获取销售单位
|
||
string SelSQL = "select id from unit where unitCode = '" + FUnitID + "'";
|
||
string SelData = crmApi.SelectSQLApi(SelSQL, Token);
|
||
SelSQLData UnitData = JsonConvert.DeserializeObject<SelSQLData>(SelData);
|
||
int UnitCount = UnitData.result.count;
|
||
if (UnitCount > 0)
|
||
{
|
||
List<Dictionary<string, object>> keys = UnitData.result.records;
|
||
FUnitID = keys[0]["id"].ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region 获取物料内码
|
||
string FMaterialID = string.Empty;
|
||
SelSQL = "select id from product where productName ='" + FMaterialNumber + "' ";
|
||
SelData = crmApi.SelectSQLApi(SelSQL, Token);
|
||
SelSQLData MaterialData = JsonConvert.DeserializeObject<SelSQLData>(SelData);
|
||
int MaterialCount = MaterialData.result.count;
|
||
if (MaterialCount > 0)
|
||
{
|
||
List<Dictionary<string, object>> keys = MaterialData.result.records;
|
||
FMaterialID = keys[0]["id"].ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region 获取BOM内码
|
||
string BOMID = string.Empty;
|
||
SelSQL = "select id from bomVersion__c where parentProductCode__c= '" + FMaterialID + "' and name='" + FBomId + "'";
|
||
SelData = crmApi.SelectSQLApi(SelSQL, Token);
|
||
SelSQLData BOMData = JsonConvert.DeserializeObject<SelSQLData>(SelData);
|
||
int BOMCount = BOMData.result.count;
|
||
if (BOMCount > 0)
|
||
{
|
||
List<Dictionary<string, object>> keys = BOMData.result.records;
|
||
BOMID = keys[0]["id"].ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region 获取库存组织和结算组织
|
||
FStockOrgId = crmApi.GetCRMOrgID(FStockOrgId, Token);
|
||
FSettleOrgIds = crmApi.GetCRMOrgID(FSettleOrgIds, Token);
|
||
#endregion
|
||
|
||
#region 获取订单明细信息 - 使用JObject避免动态类型问题
|
||
JToken data = null;
|
||
if (!F_PATC_Text_qtr.IsNullOrEmpty())
|
||
{
|
||
string OrderInfoJson = crmApi.GetOrderEntryInfoApi(F_PATC_Text_qtr, Token);
|
||
|
||
// 方法1:使用JObject而不是dynamic
|
||
JObject OrderInfo = null;
|
||
OrderInfo = JObject.Parse(OrderInfoJson);
|
||
|
||
// 检查data是否存在
|
||
if (OrderInfo["data"] == null || !OrderInfo["data"].HasValues)
|
||
{
|
||
throw new Exception("获取订单明细信息失败;接口订单ID值:" + OrderInfoJson);
|
||
}
|
||
|
||
data = OrderInfo["data"];
|
||
entityType = data["entityType"].ToStringEx();
|
||
}
|
||
#endregion
|
||
|
||
#region 创建订单明细
|
||
JObject OrderEntryJson = new JObject();
|
||
DateTime dt = DateTime.Now;
|
||
long ticks = crmApi.ToUnixTimeMilliseconds(dt);
|
||
if (F_PATC_Text_qtr.IsNullOrEmpty())
|
||
{
|
||
ChangeFEntryID_C.Add("FChangeEntryID", FEntryID);
|
||
ChangeFEntryID_C.Add("FMaterialNumber", FMaterialNumber);
|
||
ChangeFEntryID_C.Add("FBomId", FBomId);
|
||
ChangeFEntryID_C.Add("FQty", FQty);
|
||
ChangeFEntryID_CList.Add(ChangeFEntryID_C);
|
||
OrderEntryJson = new JObject
|
||
{
|
||
["data"] = new JObject
|
||
{
|
||
//["orderProductId"] = null,
|
||
["originalOrderProductAPIId"] = null,
|
||
["orderId"] = OrderID,
|
||
["originalOrderId"] = F_PATC_Text_qtr1,
|
||
["entityType"] = entityType,
|
||
["changeType"] = FChangeType,
|
||
["erpChangeType__c"] = erpChangeType__c,
|
||
["productId"] = FMaterialID,
|
||
["productCode__c"] = FMaterialNumber,
|
||
["productName__c"] = FMaterialName,
|
||
["bomVersionId__c"] = BOMID.IsNullOrEmpty() ? null : BOMID,
|
||
["bomVersion__c"] = FBomId,
|
||
["bomShortName__c"] = FBomShortId,
|
||
["processSopStandard__c"] = F_PATC_Text_h1g,
|
||
["salesUnit__c"] = FUnitID,
|
||
["quantity"] = FQty,
|
||
["taxUnitPrice__c"] = FPrice,
|
||
["unitPrice"] = FTaxPrice,
|
||
["taxRate__c"] = FEntryTaxRate,
|
||
["taxAmount__c"] = FEntryTaxAmount,
|
||
["totalAmountIncludingTax__c"] = FAmount,
|
||
["priceTotal"] = FAllAmount,
|
||
["discount"] = FDiscountRate,
|
||
["totalDiscountAmount"] = FDiscount,
|
||
["deliveryDate__c"] = FDeliveryDate,
|
||
["quantity"] = FPriceUnitQty,
|
||
["inventoryOrg__c"] = FStockOrgId,
|
||
["settlementOrg__c"] = FSettleOrgIds,
|
||
["softwareVersionType__c"] = F_PATC_Text_fg2,
|
||
["dimDepart"] = "3942016152148652",
|
||
["transactionDate"] = ticks,
|
||
["jdChangeFEntryId__c"] = FEntryID,
|
||
["erpBusinessTerminate__c"] = FMrpTerminateStatus,
|
||
["erpBusinssClose__c"] = FMrpCloseStatus,
|
||
["productModel__c"] = "1"
|
||
}
|
||
};
|
||
}
|
||
else
|
||
{
|
||
var entryData = new JObject
|
||
{
|
||
//["orderProductId"] = F_PATC_Text_qtr,
|
||
["originalOrderProductAPIId"] = F_PATC_Text_qtr,
|
||
["orderId"] = OrderID,
|
||
["originalOrderId"] = F_PATC_Text_qtr1,
|
||
["changeType"] = FChangeType,
|
||
["erpChangeType__c"] = erpChangeType__c,
|
||
["productId"] = FMaterialID,
|
||
["productCode__c"] = FMaterialNumber,
|
||
["productName__c"] = FMaterialName,
|
||
["bomVersionId__c"] = BOMID.IsNullOrEmpty() ? null : BOMID,
|
||
["bomVersion__c"] = FBomId,
|
||
["bomShortName__c"] = FBomShortId,
|
||
["processSopStandard__c"] = F_PATC_Text_h1g,
|
||
["salesUnit__c"] = FUnitID,
|
||
["quantity"] = FQty,
|
||
["taxUnitPrice__c"] = FPrice,
|
||
["unitPrice"] = FTaxPrice,
|
||
["taxRate__c"] = FEntryTaxRate,
|
||
["taxAmount__c"] = FEntryTaxAmount,
|
||
["totalAmountIncludingTax__c"] = FAmount,
|
||
["priceTotal"] = FAllAmount,
|
||
["discount"] = FDiscountRate,
|
||
["totalDiscountAmount"] = FDiscount,
|
||
["deliveryDate__c"] = FDeliveryDate,
|
||
["priceUnitQty"] = FPriceUnitQty,
|
||
["inventoryOrg__c"] = FStockOrgId,
|
||
["settlementOrg__c"] = FSettleOrgIds,
|
||
["softwareVersionType__c"] = F_PATC_Text_fg2,
|
||
["dimDepart"] = "3942016152148652",
|
||
["transactionDate"] = ticks,
|
||
["jdChangeFEntryId__c"] = FEntryID,
|
||
["erpBusinessTerminate__c"] = FMrpTerminateStatus,
|
||
["erpBusinssClose__c"] = FMrpCloseStatus
|
||
};
|
||
string[] dataFieldsToCopy = new string[]
|
||
{
|
||
"entityType",
|
||
"isJapanSoftBank__c",
|
||
"certReq__c",
|
||
"isCustomized__c",
|
||
"vesionName__c",
|
||
"vesionNo__c",
|
||
"specModel__c",
|
||
"standardPrice",
|
||
"amountRMB__c",
|
||
"advancePayRatioRMB__c",
|
||
"paymentRatioRMB__c",
|
||
"balanceRatioRMB__c",
|
||
"discountAmountRMB__c",
|
||
"productModel__c",
|
||
"planStorageDate__c",
|
||
"actualStorageDate__c"
|
||
};
|
||
foreach (var field in dataFieldsToCopy)
|
||
{
|
||
if (data != null && data[field] != null)
|
||
{
|
||
entryData[field] = data[field];
|
||
}
|
||
else
|
||
{
|
||
entryData[field] = null;
|
||
}
|
||
}
|
||
OrderEntryJson = new JObject
|
||
{
|
||
["data"] = entryData
|
||
};
|
||
}
|
||
string OrderEntryJsonString = OrderEntryJson.ToString(Formatting.Indented);
|
||
string ReturnData = crmApi.CreateOrderEntryInfoApi(OrderEntryJsonString, Token);
|
||
CreateReturnData ReturnDataD = JsonConvert.DeserializeObject<CreateReturnData>(ReturnData);
|
||
if (!ReturnDataD.code.ToStringEx().Equals("200"))
|
||
{
|
||
throw new Exception("返回值:" + ReturnDataD.code.ToStringEx() + ",创建销售订单变更单订单明细失败:" + ReturnDataD.msg.ToStringEx() + "接口传入JSON:" + OrderEntryJsonString);
|
||
}
|
||
#endregion
|
||
}
|
||
#endregion
|
||
|
||
#region ======订单变更单生效=====================
|
||
if (F_PATC_Text_qtr1.IsNullOrEmpty())
|
||
{
|
||
throw new Exception("订单变更生效失败:获取CRM订单内码失败!");
|
||
}
|
||
string ActivationData = crmApi.OrderActivation(OrderID, Token);
|
||
JObject ActivationObj = JObject.Parse(ActivationData);
|
||
if (!ActivationObj["code"].ToStringEx().Equals("200"))
|
||
{
|
||
throw new Exception("返回值:" + ActivationObj["code"].ToStringEx() + ",订单生效失败:" + ActivationObj["msg"].ToStringEx() + "接口传入变更单内码:" + OrderID);
|
||
}
|
||
#endregion
|
||
|
||
#region =====维护销售订单新增行CRM明细内码========
|
||
foreach (var ChangeData in ChangeFEntryID_CList)
|
||
{
|
||
#region 获取金蝶销售订单明细内码
|
||
JObject QueryData1 = new JObject() {
|
||
{ "FormId", "SAL_SaleOrder" },
|
||
{ "FieldKeys", "" } ,
|
||
{ "OrderString", "" },
|
||
{ "FilterString", "" } ,
|
||
{ "TopRowCount", 0 },
|
||
{ "StartRow", 0 } ,
|
||
{ "Limit", 5000 }
|
||
};
|
||
|
||
string FilterString = string.Empty;
|
||
if (ChangeData["FBomId"].ToStringEx().IsNullOrEmpty())
|
||
{
|
||
FilterString = $"FBILLNO = '{FBILLNO.Split('_')[0]}' and FMaterialId.FNUMBER = '{ChangeData["FMaterialNumber"].ToStringEx()}' and FBOMID.FNUMBER is null and FQTY = '{ChangeData["FQty"].ToStringEx()}'";
|
||
}
|
||
else
|
||
{
|
||
FilterString = $"FBILLNO = '{FBILLNO.Split('_')[0]}' and FMaterialId.FNUMBER = '{ChangeData["FMaterialNumber"].ToStringEx()}' and FBOMID.FNUMBER = '{ChangeData["FBomId"].ToStringEx()}' and FQTY = '{ChangeData["FQty"].ToStringEx()}'";
|
||
}
|
||
|
||
QueryData1["FieldKeys"] = "FID,FSaleOrderEntry_FEntryID";
|
||
QueryData1["OrderString"] = "FID";
|
||
QueryData1["FilterString"] = FilterString;
|
||
|
||
|
||
var QueryList1 = client.ExecuteBillQuery(QueryData1.ToString());
|
||
#endregion
|
||
// F_PATC_Text_qtr1 FEntryID
|
||
|
||
#region 获取订单明细新增内码
|
||
string OrderEntryID = string.Empty;
|
||
string SelSQL = $"select id, jd_FEntryId__c, jdChangeFEntryId__c from orderProduct where orderId = '{F_PATC_Text_qtr1}'and jdChangeFEntryId__c = '{ChangeData["FChangeEntryID"].ToStringEx()}'";
|
||
string SelData = crmApi.SelectSQLApi(SelSQL, Token);
|
||
SelSQLData OrderEntryIDData = JsonConvert.DeserializeObject<SelSQLData>(SelData);
|
||
int OrderEntryIDCount = OrderEntryIDData.result.count;
|
||
if (OrderEntryIDCount > 0)
|
||
{
|
||
List<Dictionary<string, object>> keys = OrderEntryIDData.result.records;
|
||
OrderEntryID = keys[0]["id"].ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region 修改销售订单指定行内码
|
||
if (QueryList1 != null && QueryList1.Count > 0)
|
||
{
|
||
foreach (var sub in QueryList1)
|
||
{
|
||
int FSaleOrderID = sub[0].ToInt32();//金蝶原销售订单内码
|
||
int FSaleOrderEntryID = sub[1].ToInt32();//金蝶原销售订单明细内码
|
||
|
||
JObject JsonData = new JObject();
|
||
JsonData.Add("IsDeleteEntry", "false");
|
||
JObject Model = new JObject();
|
||
Model.Add("FID", FSaleOrderID);
|
||
|
||
JArray FSaleOrderEntry = new JArray();
|
||
JObject entry = new JObject();
|
||
entry.Add("FEntryID", FSaleOrderEntryID);
|
||
entry.Add("F_PATC_Text_qtr", OrderEntryID);
|
||
FSaleOrderEntry.Add(entry);
|
||
Model.Add("FSaleOrderEntry", FSaleOrderEntry);
|
||
JsonData.Add("Model", Model);
|
||
var data = JsonData.ToStringEx();
|
||
var ret = client.Save("SAL_SaleOrder", data);
|
||
K3Response myK3Response = WmsCommonFunction.GetBodyData<K3Response>(ret);
|
||
if (!myK3Response.Result.ResponseStatus.IsSuccess)
|
||
{
|
||
foreach (K3Error sub1 in myK3Response.Result.ResponseStatus.Errors)
|
||
{
|
||
throw new Exception($"{sub1.FieldName}:{sub1.Message},修改销售订单指定行内码Json:{data}");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
#endregion }
|
||
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("未找到当前销售订单新变更单信息!");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("登录云星空失败!");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Ok(new
|
||
{
|
||
success = "false",
|
||
message = "同步失败:" + ex.Message
|
||
});
|
||
}
|
||
return Ok(new
|
||
{
|
||
success = "true",
|
||
message = "同步成功"
|
||
});
|
||
}
|
||
#endregion
|
||
|
||
#region 根据OA费用报销单更新金蝶付款申请单部门字段
|
||
[HttpPost]
|
||
public IHttpActionResult UpKingdeeDept([FromBody] JObject obj)
|
||
{
|
||
#region 变量
|
||
string RequestID = string.Empty;
|
||
MySqlUtils mySqlUtils = new MySqlUtils();
|
||
string SQL = string.Empty;
|
||
DataTable dt = new DataTable(), dt1 = new DataTable();
|
||
K3CloudApiClient client = null;
|
||
string FID = string.Empty;
|
||
#endregion
|
||
|
||
try
|
||
{
|
||
#region 检验参数
|
||
if (obj == null || obj.Property("RequestID") == null)
|
||
{
|
||
throw new Exception("请检查是否传参 RequestID!");
|
||
}
|
||
RequestID = obj["RequestID"].ToString();
|
||
if (RequestID.Equals(""))
|
||
{
|
||
throw new Exception("请检查传参是否正确!");
|
||
}
|
||
#endregion
|
||
|
||
#region 定义参数
|
||
string acctID = "20191111204222978"; //登录帐套ID
|
||
string userID = "sysmes"; //用户账号
|
||
string password = "Admin@123"; //用户密码
|
||
string erpURL = "https://gaoxian.ik3cloud.com/k3cloud/"; //K3URL
|
||
//string erpDB = PBCursor["K3DB"].ToString(); //K3 账套 -- 公有云,不再支持金蝶帐套
|
||
int erpLaType = 2052; //语言种类4
|
||
#endregion
|
||
|
||
#region 连接OA数据库
|
||
string OAConnection = ConfigurationManager.ConnectionStrings["OAConnection"].ConnectionString;
|
||
mySqlUtils.ConnectionString = OAConnection;
|
||
mySqlUtils.GetConnection();
|
||
#endregion
|
||
|
||
#region 获取费用报销数据
|
||
SQL = $@"select
|
||
S1.DEPARTMENTNAME
|
||
,S2.sqzz
|
||
,S.djbh
|
||
,parentDept.departmentname as parent_dept_name
|
||
from formtable_main_24 S
|
||
LEFT JOIN hrmdepartment S1 ON S.szbm = S1.id
|
||
LEFT JOIN hrmdepartment parentDept ON S1.supdepid = parentDept.id AND parentDept.canceled IS NULL
|
||
left join uf_cgsqzz S2 on S.sqzz = S2.id
|
||
where S.requestId ='{RequestID}'";
|
||
dt = mySqlUtils.ExecQuery(SQL);
|
||
if (dt == null || dt.Rows.Count == 0)
|
||
{
|
||
throw new Exception("【" + RequestID + "】查询费用报销数据为空!");
|
||
}
|
||
SQL = $@"select jdbmbh ,jdbmmc from uf_oa_kingdee_dept
|
||
where zzmc ='{dt.Rows[0]["sqzz"].ToString()}' and oabm = '{dt.Rows[0]["DEPARTMENTNAME"].ToString()}' and sjbm = '{dt.Rows[0]["parent_dept_name"].ToString()}'";
|
||
dt1 = mySqlUtils.ExecQuery(SQL);
|
||
if (dt1 == null || dt1.Rows.Count == 0)
|
||
{
|
||
throw new Exception("【"+ RequestID + "】,【" + dt.Rows[0]["sqzz"].ToString() + "】,【" + dt.Rows[0]["DEPARTMENTNAME"].ToString() + "】,【" + dt.Rows[0]["parent_dept_name"].ToString() + "】查询部门映射关系为空!");
|
||
}
|
||
#endregion
|
||
|
||
#region 获取付款申请单FID
|
||
//第一步,登录金蝶系统
|
||
client = new K3CloudApiClient(erpURL);
|
||
var loginResult = client.ValidateLogin(acctID, userID, password, erpLaType);
|
||
var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
|
||
|
||
//登录结果类型等于1,代表登录成功
|
||
if (resultType == 1) //登录成功
|
||
{
|
||
#region 查询条件
|
||
JObject QueryData = new JObject();
|
||
QueryData["FormId"] = "CN_PAYAPPLY";
|
||
QueryData["FieldKeys"] = "FID";
|
||
QueryData["OrderString"] = "FID";
|
||
QueryData["FilterString"] = "FBILLNO = '" + dt.Rows[0]["djbh"].ToString() + "'";
|
||
QueryData["StartRow"] = 0;
|
||
#endregion
|
||
|
||
#region 返回结果
|
||
var QueryList = client.ExecuteBillQuery(QueryData.ToString());
|
||
if (QueryList != null && QueryList.Count > 0)
|
||
{
|
||
FID = QueryList[0][0].ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region 更新付款申请单部门字段
|
||
JArray NeedUpDateFields = new JArray();
|
||
NeedUpDateFields.Add("FDEPARTMENT");
|
||
JObject JsonData = new JObject();
|
||
JsonData.Add("NeedUpDateFields", NeedUpDateFields);
|
||
JsonData.Add("IsDeleteEntry", "false");
|
||
JObject Model = new JObject();
|
||
Model.Add("FID", FID);
|
||
Model.Add("FDEPARTMENT", new JObject() { { "FNUMBER", dt1.Rows[0]["jdbmbh"].ToString() } }); // 部门
|
||
JsonData.Add("Model", Model);
|
||
var data = JsonData.ToStringEx();
|
||
var ret = client.Save("CN_PAYAPPLY", data);
|
||
K3Response myK3Response = WmsCommonFunction.GetBodyData<K3Response>(ret);
|
||
if (!myK3Response.Result.ResponseStatus.IsSuccess)
|
||
{
|
||
foreach (K3Error sub1 in myK3Response.Result.ResponseStatus.Errors)
|
||
{
|
||
throw new Exception($"{sub1.FieldName}:{sub1.Message},修改付款申请单部门Json:{data}");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("登录新云星空失败,初始数据失败!");
|
||
}
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog("更新金蝶付款申请单部门字段异常:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
#region 关闭连接
|
||
mySqlUtils.Dispose();
|
||
#endregion
|
||
}
|
||
LogHelper.WriteLog("【" + RequestID + "】更新金蝶付款申请单部门字段成功");
|
||
return Ok();
|
||
}
|
||
#endregion
|
||
|
||
#region 金蝶生产订单删除按钮删除WMS生产任务单
|
||
[HttpPost]
|
||
public IHttpActionResult RestoreMOInfo([FromBody] JObject obj)
|
||
{
|
||
#region 变量
|
||
string SQL = string.Empty,FMOID = string.Empty;
|
||
string FMOBillNO = obj["FMOBillNO"].ToString();
|
||
DBHelpe db = new DBHelpe();
|
||
#endregion
|
||
|
||
try
|
||
{
|
||
SQL = $@"SELECT FINTERID from T_PRD_MO
|
||
where FBILLNO = '{FMOBillNO}';";
|
||
DataTable dt = db.ExecuteTable(SQL);
|
||
if (dt != null && dt.Rows.Count > 0)
|
||
{
|
||
FMOID = dt.Rows[0]["FINTERID"].ToString();
|
||
SQL = $@"DELETE FROM T_PRD_MO WHERE FINTERID = '{FMOID}';
|
||
DELETE FROM T_PRD_MOENTRY WHERE FINTERID = '{FMOID}';
|
||
DELETE FROM T_PRD_MOENTRY1 WHERE FINTERID = '{FMOID}';";
|
||
db.ExecuteNonQuery(SQL);
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog("WMS删除生产任务单异常:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
LogHelper.WriteLog("金蝶生产任务单传入值:" + FMOBillNO);
|
||
}
|
||
return Ok(obj);
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
}
|