You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
24 KiB

3 weeks ago
  1. using NFine.Data.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Text;
  6. using NFine.Repository;
  7. using System.Data.Common;
  8. using NFine.Domain._03_Entity.SRM;
  9. using Newtonsoft.Json;
  10. using System.Configuration;
  11. using Newtonsoft.Json.Linq;
  12. using System.Net;
  13. using System.IO;
  14. using NFine.Domain._03_Entity.WMS;
  15. using System.Data.SqlClient;
  16. namespace NFine.Application.JHWMS
  17. {
  18. public class DeciliterApp : RepositoryFactory<ICSVendor>
  19. {
  20. /// <summary>
  21. /// 点击委外退料生成条码
  22. /// </summary>
  23. public string GetNewLotNo(string LotNO)
  24. {
  25. DataTable dt = new DataTable();
  26. List<DbParameter> parameter = new List<DbParameter>();
  27. //string sql = @"select max(LotNo) as NewLotNo from ICSInventoryLot where EATTRIBUTE1='{0}' ";
  28. string sql = @"SELECT TOP 1 LotNO as NewLotNo FROM ICSInventoryLot WHERE EATTRIBUTE1='{0}'
  29. ORDER BY CAST(SUBSTRING(LotNO, (LEN(LotNO)-CHARINDEX('-',REVERSE(LotNO))+1)+1,CHARINDEX('-',REVERSE(LotNO))-1) AS INT) DESC";
  30. sql = string.Format(sql, LotNO);
  31. dt = Repository().FindTableBySql(sql.ToString());
  32. if (dt == null || dt.Rows.Count == 0 || dt.Rows[0]["NewLotNo"].ToString() == "")
  33. {
  34. return LotNO + "-1";
  35. }
  36. else
  37. {
  38. string newLotNO = dt.Rows[0]["NewLotNo"].ToString();
  39. int COUNT = Convert.ToInt32(newLotNO.Substring(newLotNO.LastIndexOf('-') + 1)) + 1;
  40. return LotNO + "-" + COUNT.ToString();
  41. }
  42. }
  43. public string GetNewLotNoByPacking(string LotNO)
  44. {
  45. DataTable dt = new DataTable();
  46. List<DbParameter> parameter = new List<DbParameter>();
  47. //string sql = @"select max(LotNo) as NewLotNo from ICSInventoryLot where EATTRIBUTE1='{0}' ";
  48. string sql = @"SELECT TOP 1 LotNO as NewLotNo FROM ICSInventoryLot WHERE EATTRIBUTE1='{0}'
  49. ORDER BY CAST(SUBSTRING(LotNO, (LEN(LotNO)-CHARINDEX('-',REVERSE(LotNO))+1)+1,CHARINDEX('-',REVERSE(LotNO))-1) AS INT) DESC";
  50. sql = string.Format(sql, LotNO);
  51. dt = Repository().FindTableBySql(sql.ToString());
  52. if (dt == null || dt.Rows.Count == 0 || dt.Rows[0]["NewLotNo"].ToString() == "")
  53. {
  54. // return LotNO + "-1";
  55. return "1";
  56. }
  57. else
  58. {
  59. string newLotNO = dt.Rows[0]["NewLotNo"].ToString();
  60. int COUNT = Convert.ToInt32(newLotNO.Substring(newLotNO.LastIndexOf('-') + 1)) + 1;
  61. //return LotNO + "-" + COUNT.ToString();
  62. return COUNT.ToString();
  63. }
  64. }
  65. public string Split(string Parameter)
  66. {
  67. string msg = "";
  68. string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "LOTSplit/Create";
  69. string result = HttpPost(APIURL, Parameter);
  70. JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
  71. string MessAge = Obj["Message"].ToString();
  72. string Success = Obj["Success"].ToString();
  73. if (Success.ToUpper() == "FALSE")
  74. {
  75. msg = MessAge;
  76. }
  77. return msg;
  78. }
  79. //接口api解析
  80. public static string HttpPost(string url, string body)
  81. {
  82. try
  83. {
  84. Encoding encoding = Encoding.UTF8;
  85. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  86. request.Method = "POST";
  87. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  88. request.ContentType = "application/json; charset=utf-8";
  89. byte[] buffer = encoding.GetBytes(body);
  90. request.ContentLength = buffer.Length;
  91. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  92. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  93. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  94. {
  95. return reader.ReadToEnd();
  96. }
  97. }
  98. catch (WebException ex)
  99. {
  100. throw new Exception(ex.Message);
  101. }
  102. }
  103. /// <summary>
  104. /// 根据ID获取条码
  105. /// </summary>
  106. /// <returns></returns>
  107. public DataTable GetLotNoByID(string ID)
  108. {
  109. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  110. string sql = @"select '' as ID,'' as LotNo union all
  111. SELECT ID,LotNo FROM ICSWareHouseLotInfo WITH (NOLOCK) WHERE ID in ({0}) ";
  112. sql = string.Format(sql, ID.TrimEnd(','));
  113. //string role = NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode;
  114. //if (role != "admin")
  115. //{
  116. // sql += " and b.WorkPoint='" + WorkPoint + "'";
  117. //}
  118. DataTable dt = SqlHelper.GetDataTableBySql(sql);
  119. return dt;
  120. }
  121. //合批
  122. public string Combine(string LotNo, string ID)
  123. {
  124. try
  125. {
  126. string connString = SqlHelper.DataCenterConnString;
  127. string msg = "";
  128. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  129. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  130. string sql = @" select distinct LotNo,Quantity from ICSWareHouseLotInfo WHERE ID IN (" + ID.TrimEnd(',') + ") AND LotNo!='" + LotNo + "'";
  131. DataTable dt = Repository().FindTableBySql(sql.ToString());
  132. if (dt.Rows.Count == 0)
  133. {
  134. throw new Exception("未查询到相关条码信息,无法分批!");
  135. }
  136. #region 验证条码批号是否相同,获取批号不同的条码在最后调用形态转换接口
  137. string smtjson = "";
  138. string smtjsondetail = "";
  139. string smtsql = @" select a.LotNo AS FinalLotNo,e.BatchCode AS FinalBatchCode
  140. ,c.LotNo AS SourceLotNo,f.BatchCode AS SourceBatchCode,c.Quantity
  141. ,a.InvCode,a.WarehouseCode
  142. from ICSWareHouseLotInfo a
  143. inner join ICSInventoryLot b on b.LotNo=a.LotNo and b.WorkPoint=a.WorkPoint
  144. left join ICSWareHouseLotInfo c on c.ID in (" + ID.TrimEnd(',') + @")
  145. and c.LotNo !='" + LotNo + @"' and c.WorkPoint=a.WorkPoint
  146. inner join ICSInventoryLot d on d.LotNo=c.LotNo and d.WorkPoint=c.WorkPoint
  147. left join ICSExtension e on e.ID=b.ExtensionID
  148. left join ICSExtension f on f.ID=d.ExtensionID
  149. where a.LotNo='" + LotNo + "' and a.WorkPoint='" + WorkPoint + "'";
  150. DataTable smtdt = Repository().FindTableBySql(smtsql.ToString());
  151. if (smtdt.Rows.Count > 0)
  152. {
  153. int SequenceCount = 0;
  154. int GroupCount = 0;
  155. smtjson += "[ {";
  156. smtjson += "\"User\": \"" + MUSER + "\",";
  157. smtjson += "\"MTime\": \"" + DateTime.Now + "\",";
  158. smtjson += "\"WorkPoint\": \"" + WorkPoint + "\",";
  159. smtjson += "\"UpdateStock\": \"true\",";
  160. smtjson += "\"cMemo\": \"\",";
  161. smtjson += "\"details\": [ ";
  162. smtjson += "<0>";
  163. smtjson += " ] ";
  164. smtjson += "} ]";
  165. foreach (DataRow smtdr in smtdt.Rows)
  166. {
  167. SequenceCount++;
  168. GroupCount++;
  169. if (smtdr["FinalBatchCode"].ToString() != smtdr["SourceBatchCode"].ToString())
  170. {
  171. smtjsondetail += "{ ";
  172. smtjsondetail += "\"Sequence\": \"" + SequenceCount + "\",";
  173. smtjsondetail += "\"GroupNO\": \"" + GroupCount + "\",";
  174. smtjsondetail += "\"Type\": \"转换前\",";
  175. smtjsondetail += "\"InvCode\": \"" + smtdr["InvCode"].ToString() + "\",";
  176. smtjsondetail += "\"WHCode\": \"" + smtdr["WarehouseCode"].ToString() + "\",";
  177. smtjsondetail += "\"BatchCode\": \"" + smtdr["SourceBatchCode"].ToString() + "\",";
  178. smtjsondetail += "\"ProjectCode\": \"\",";
  179. smtjsondetail += "\"Quantity\": " + Convert.ToDecimal(smtdr["Quantity"].ToString()) + ",";
  180. smtjsondetail += "\"Amount\": 0,";
  181. smtjsondetail += "\"Brand\": \"\",";
  182. smtjsondetail += "\"cFree1\": \"\",";
  183. smtjsondetail += "\"cFree2\": \"\",";
  184. smtjsondetail += "\"cFree3\": \"\",";
  185. smtjsondetail += "\"cFree4\": \"\",";
  186. smtjsondetail += "\"cFree5\": \"\",";
  187. smtjsondetail += "\"cFree6\": \"\",";
  188. smtjsondetail += "\"cFree7\": \"\",";
  189. smtjsondetail += "\"cFree8\": \"\",";
  190. smtjsondetail += "\"cFree9\": \"\",";
  191. smtjsondetail += "\"cFree10\": \"\"";
  192. smtjsondetail += " },";
  193. SequenceCount++;
  194. smtjsondetail += "{ ";
  195. smtjsondetail += "\"Sequence\": \"" + SequenceCount + "\",";
  196. smtjsondetail += "\"GroupNO\": \"" + GroupCount + "\",";
  197. smtjsondetail += "\"Type\": \"转换后\",";
  198. smtjsondetail += "\"InvCode\": \"" + smtdr["InvCode"].ToString() + "\",";
  199. smtjsondetail += "\"WHCode\": \"" + smtdr["WarehouseCode"].ToString() + "\",";
  200. smtjsondetail += "\"BatchCode\": \"" + smtdr["FinalBatchCode"].ToString() + "\",";
  201. smtjsondetail += "\"ProjectCode\": \"\",";
  202. smtjsondetail += "\"Quantity\": " + Convert.ToDecimal(smtdr["Quantity"].ToString()) + ",";
  203. smtjsondetail += "\"Amount\": 0,";
  204. smtjsondetail += "\"Brand\": \"\",";
  205. smtjsondetail += "\"cFree1\": \"\",";
  206. smtjsondetail += "\"cFree2\": \"\",";
  207. smtjsondetail += "\"cFree3\": \"\",";
  208. smtjsondetail += "\"cFree4\": \"\",";
  209. smtjsondetail += "\"cFree5\": \"\",";
  210. smtjsondetail += "\"cFree6\": \"\",";
  211. smtjsondetail += "\"cFree7\": \"\",";
  212. smtjsondetail += "\"cFree8\": \"\",";
  213. smtjsondetail += "\"cFree9\": \"\",";
  214. smtjsondetail += "\"cFree10\": \"\"";
  215. smtjsondetail += " },";
  216. }
  217. }
  218. smtjsondetail = smtjsondetail.TrimEnd(',');
  219. smtjson = smtjson.Replace("<0>", smtjsondetail);
  220. }
  221. #endregion
  222. using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))
  223. {
  224. conn.Open();
  225. SqlTransaction sqlTran = conn.BeginTransaction();
  226. SqlCommand cmd = new SqlCommand();
  227. cmd.Transaction = sqlTran;
  228. cmd.Connection = conn;
  229. cmd.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimeOut"]);
  230. try
  231. {
  232. string Identification = Guid.NewGuid().ToString();
  233. #region 合并前目标条码日志
  234. sql = @" INSERT INTO ICSWareHouseLotInfoLog(ID,Identification,TransCode,TransSequence,LotNo,InvCode,
  235. FromWarehouseCode,FromLocationCode,ToWarehouseCode,ToLocationCode,Quantity,
  236. Memo,Lock,TransType,BusinessCode,ERPUpload,ERPID,
  237. ERPDetailID,ERPCode,ERPSequence,MUSER,MUSERName,
  238. MTIME,WorkPoint,EATTRIBUTE1)
  239. SELECT NEWID(),'{3}',b.TransCode,b.TransSequence,a.LotNo ,a.InvCode ,
  240. '','',c.WarehouseCode,c.LocationCode,c.Quantity,
  241. '','0','{4}','{5}','0','',
  242. '','','',f.F_Account ,f.F_RealName ,
  243. SYSDATETIME() ,a.WorkPoint ,''
  244. FROM ICSInventoryLot a
  245. INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
  246. INNER JOIN ICSWareHouseLotInfo c ON a.LotNo=c.LotNo AND a.WorkPoint=c.WorkPoint
  247. INNER JOIN Sys_SRM_User f ON f.F_Account='{2}' AND a.WorkPoint=f.F_Location
  248. WHERE a.LotNo='{0}' AND a.WorkPoint='{1}'";
  249. sql = string.Format(sql, LotNo, WorkPoint, MUSER, Identification, "5", "33");
  250. cmd.CommandText = sql;
  251. int result = cmd.ExecuteNonQuery();
  252. if (result == 0)
  253. {
  254. throw new Exception("合批前目标条码日志记录写入失败!");
  255. }
  256. #endregion
  257. foreach (DataRow dr in dt.Rows)
  258. {
  259. #region 合并前来源条码日志
  260. sql = @" INSERT INTO ICSWareHouseLotInfoLog(ID,Identification,TransCode,TransSequence,LotNo,InvCode,
  261. FromWarehouseCode,FromLocationCode,ToWarehouseCode,ToLocationCode,Quantity,
  262. Memo,Lock,TransType,BusinessCode,ERPUpload,ERPID,
  263. ERPDetailID,ERPCode,ERPSequence,MUSER,MUSERName,
  264. MTIME,WorkPoint,EATTRIBUTE1)
  265. SELECT NEWID(),'{3}',b.TransCode,b.TransSequence,a.LotNo ,a.InvCode ,
  266. '','',c.WarehouseCode,c.LocationCode,c.Quantity,
  267. '','0','{4}','{5}','0','',
  268. '','','',f.F_Account ,f.F_RealName ,
  269. SYSDATETIME() ,a.WorkPoint ,''
  270. FROM ICSInventoryLot a
  271. INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
  272. INNER JOIN ICSWareHouseLotInfo c ON a.LotNo=c.LotNo AND a.WorkPoint=c.WorkPoint
  273. INNER JOIN Sys_SRM_User f ON f.F_Account='{2}' AND a.WorkPoint=f.F_Location
  274. WHERE a.LotNo='{0}' AND a.WorkPoint='{1}'";
  275. sql = string.Format(sql, dr["LotNo"], WorkPoint, MUSER, Identification, "5", "33");
  276. cmd.CommandText = sql;
  277. result = cmd.ExecuteNonQuery();
  278. if (result == 0)
  279. {
  280. throw new Exception("合批前来源条码日志记录写入失败!");
  281. }
  282. #endregion
  283. #region 更新目标条码库存
  284. sql = @" IF NOT EXISTS(SELECT a.LotNo FROM ICSInventoryLot a
  285. INNER JOIN ICSInventoryLot b ON b.LotNo='{3}' AND a.InvCode=b.InvCode AND a.WorkPoint=b.WorkPoint
  286. WHERE a.LotNo='{0}' AND a.WorkPoint='{1}')
  287. BEGIN
  288. RAISERROR('',16,1);
  289. RETURN
  290. END
  291. IF NOT EXISTS(SELECT a.LotNo FROM ICSWareHouseLotInfo a
  292. INNER JOIN ICSWareHouseLotInfo b ON b.LotNo='{3}' AND a.WarehouseCode=b.WarehouseCode AND a.WorkPoint=b.WorkPoint
  293. INNER JOIN ICSInventoryLot c on a.LotNo=c.LotNo and a.WorkPoint=c.WorkPoint
  294. INNER JOIN ICSInventoryLot d on b.LotNo=d.LotNo and b.WorkPoint=d.WorkPoint
  295. WHERE a.LotNo='{0}' AND a.WorkPoint='{1}')
  296. BEGIN
  297. RAISERROR('',16,1);
  298. RETURN
  299. END
  300. UPDATE ICSWareHouseLotInfo SET Quantity=ISNULL(Quantity,0)+{2} WHERE LotNo='{0}' AND WorkPoint='{1}'
  301. UPDATE ICSInventoryLot SET Amount=(ISNULL(Amount,0)+(select Amount from
  302. ICSInventoryLot where LotNo='{3}' AND WorkPoint='{1}')) WHERE LotNo='{0}' AND WorkPoint='{1}'";
  303. sql = string.Format(sql, LotNo, WorkPoint, Convert.ToDecimal(dr["Quantity"]), dr["LotNo"].ToString());
  304. cmd.CommandText = sql;
  305. result = cmd.ExecuteNonQuery();
  306. if (result == 0)
  307. {
  308. throw new Exception("目标条码库存增加失败!");
  309. }
  310. #endregion
  311. #region 更新来源条码库存
  312. sql = @" UPDATE ICSWareHouseLotInfo SET Quantity=ISNULL(Quantity,0)-'{2}' WHERE LotNo='{0}' AND WorkPoint='{1}'
  313. UPDATE ICSInventoryLot SET Amount=(ISNULL(Amount,0)-(select Amount from
  314. ICSInventoryLot where LotNo='{0}' AND WorkPoint='{1}')) WHERE LotNo='{0}' AND WorkPoint='{1}'
  315. IF EXISTS(SELECT a.LotNo FROM ICSWareHouseLotInfo a WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND Quantity<0)
  316. BEGIN
  317. RAISERROR('!',16,1);
  318. RETURN
  319. END";
  320. sql = string.Format(sql, dr["LotNo"].ToString(), WorkPoint, Convert.ToDecimal(dr["Quantity"]));
  321. cmd.CommandText = sql;
  322. result = cmd.ExecuteNonQuery();
  323. if (result == 0)
  324. {
  325. throw new Exception("来源条码库存扣减失败!");
  326. }
  327. #endregion
  328. #region 来源条码合并后日志写入
  329. sql = @" INSERT INTO ICSWareHouseLotInfoLog(ID,Identification,TransCode,TransSequence,LotNo,InvCode,
  330. FromWarehouseCode,FromLocationCode,ToWarehouseCode,ToLocationCode,Quantity,
  331. Memo,Lock,TransType,BusinessCode,ERPUpload,ERPID,
  332. ERPDetailID,ERPCode,ERPSequence,MUSER,MUSERName,
  333. MTIME,WorkPoint,EATTRIBUTE1)
  334. SELECT NEWID(),'{3}',b.TransCode,b.TransSequence,a.LotNo ,a.InvCode ,
  335. '','',c.WarehouseCode,c.LocationCode,c.Quantity,
  336. '','0','{4}','{5}','0','',
  337. '','','',f.F_Account ,f.F_RealName ,
  338. SYSDATETIME() ,a.WorkPoint ,''
  339. FROM ICSInventoryLot a
  340. INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
  341. INNER JOIN ICSWareHouseLotInfo c ON a.LotNo=c.LotNo AND a.WorkPoint=c.WorkPoint
  342. INNER JOIN Sys_SRM_User f ON f.F_Account='{2}' AND a.WorkPoint=f.F_Location
  343. WHERE a.LotNo='{0}' AND a.WorkPoint='{1}'
  344. ";
  345. sql = string.Format(sql, dr["LotNo"].ToString(), WorkPoint, MUSER, Identification, "5", "34");
  346. cmd.CommandText = sql;
  347. result = cmd.ExecuteNonQuery();
  348. if (result == 0)
  349. {
  350. throw new Exception("来源条码合并后日志写入失败!");
  351. }
  352. #endregion
  353. }
  354. #region 合并后目标条码日志写入
  355. sql = @"
  356. INSERT INTO ICSWareHouseLotInfoLog(ID,Identification,TransCode,TransSequence,LotNo,InvCode,
  357. FromWarehouseCode,FromLocationCode,ToWarehouseCode,ToLocationCode,Quantity,
  358. Memo,Lock,TransType,BusinessCode,ERPUpload,ERPID,
  359. ERPDetailID,ERPCode,ERPSequence,MUSER,MUSERName,
  360. MTIME,WorkPoint,EATTRIBUTE1)
  361. SELECT NEWID(),'{3}',b.TransCode,b.TransSequence,a.LotNo ,a.InvCode ,
  362. '','',c.WarehouseCode,c.LocationCode,c.Quantity,
  363. '','0','{4}','{5}','0','',
  364. '','','',f.F_Account ,f.F_RealName ,
  365. SYSDATETIME() ,a.WorkPoint ,''
  366. FROM ICSInventoryLot a
  367. INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
  368. INNER JOIN ICSWareHouseLotInfo c ON a.LotNo=c.LotNo AND a.WorkPoint=c.WorkPoint
  369. INNER JOIN Sys_SRM_User f ON f.F_Account='{2}' AND a.WorkPoint=f.F_Location
  370. WHERE a.LotNo='{0}' AND a.WorkPoint='{1}'
  371. ";
  372. sql = string.Format(sql, LotNo, WorkPoint, MUSER, Identification, "5", "34");
  373. cmd.CommandText = sql;
  374. result = cmd.ExecuteNonQuery();
  375. if (result == 0)
  376. {
  377. throw new Exception("合并后目标条码日志写入失败!");
  378. }
  379. #endregion
  380. if (smtjsondetail != "")
  381. {
  382. #region 开始调用形态转换接口
  383. string APIURL = ConfigurationManager.ConnectionStrings["ERPAPIURL"].ConnectionString + "MorphologicalTransformationDoc/Create";
  384. string apiresult = HttpPost(APIURL, smtjson);
  385. JObject Obj = (JObject)JsonConvert.DeserializeObject(apiresult);//或者JObject jo = JObject.Parse(jsonText);
  386. string MessAge = Obj["Message"].ToString();
  387. string Success = Obj["Success"].ToString();
  388. string ERPSql = "";
  389. if (Success.ToUpper() == "FALSE")
  390. {
  391. throw new Exception("ERP接口调用失败:" + MessAge);
  392. }
  393. #endregion
  394. }
  395. cmd.Transaction.Commit();
  396. return "";
  397. }
  398. catch (Exception ex)
  399. {
  400. if (cmd.Transaction != null)
  401. cmd.Transaction.Rollback();
  402. throw new Exception(ex.Message);
  403. }
  404. finally
  405. {
  406. if (conn.State == ConnectionState.Open)
  407. {
  408. conn.Close();
  409. }
  410. conn.Dispose();
  411. }
  412. }
  413. }
  414. catch (Exception ex)
  415. {
  416. return ex.Message;
  417. }
  418. }
  419. }
  420. }