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.

436 lines
24 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  1. using ICSSoft.ERPWMS.Entity;
  2. using Microsoft.Data.SqlClient;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ICSSoft.ERPWMS.SQL
  11. {
  12. public class CreateOrDeleteInventoryLot
  13. {
  14. public static Result Create(List<CreateInventoryLotEntity> entityList)
  15. {
  16. Result res = new Result();
  17. try
  18. {
  19. string jsonstr = JsonConvert.SerializeObject(entityList);
  20. Log.WriteLogFile(jsonstr, "创建删除条码日志");
  21. StringBuilder sb = new StringBuilder();//接口返回Message
  22. foreach (CreateInventoryLotEntity entity in entityList)
  23. {
  24. SqlConnection conn = new SqlConnection(ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["WMS"].ToString());
  25. conn.Open();
  26. SqlTransaction sqlTran = conn.BeginTransaction();
  27. SqlCommand cmd = new SqlCommand();
  28. cmd.Transaction = sqlTran;
  29. cmd.Connection = conn;
  30. try
  31. {
  32. if (string.IsNullOrWhiteSpace(entity.LotNo))
  33. {
  34. throw new Exception("存在物料条码为空!");
  35. }
  36. if (string.IsNullOrWhiteSpace(entity.InvCode))
  37. {
  38. throw new Exception("物料编码为空!");
  39. }
  40. if (string.IsNullOrWhiteSpace(entity.TransNo))
  41. {
  42. throw new Exception("来源单据号为空!");
  43. }
  44. if (entity.TransLine == null)
  45. {
  46. throw new Exception("来源单据行号为空!");
  47. }
  48. if (entity.Quantity == null)
  49. {
  50. throw new Exception("数量为空!");
  51. }
  52. if (string.IsNullOrWhiteSpace(entity.Type))
  53. {
  54. throw new Exception("类型为空!");
  55. }
  56. if (string.IsNullOrWhiteSpace(entity.WorkPoint))
  57. {
  58. throw new Exception("站点为空!");
  59. }
  60. if (string.IsNullOrWhiteSpace(entity.SRMLotGroup))
  61. {
  62. throw new Exception("SRM条码Group为空为空!");
  63. }
  64. string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint);
  65. if (WorkPoint == "NotExit")
  66. {
  67. throw new Exception("站点编码不存在!");
  68. }
  69. string sqlLot = "Select LotNo,ExtensionID from ICSInventoryLot where workpoint='{0}' and LotNo='{1}'";
  70. sqlLot = string.Format(sqlLot, WorkPoint, entity.LotNo);
  71. DataTable dtLot = ICSHelper.SQlReturnData(sqlLot, cmd);
  72. if (dtLot != null && dtLot.Rows.Count > 0)
  73. {
  74. //检查条码是否绑定送货单,若绑定则不能删除
  75. string sqlASN = "Select LotNo from ICSASNDetail Where workpoint='{0}' and LotNo ='{1}'";
  76. sqlASN = string.Format(sqlASN, WorkPoint, entity.LotNo);
  77. DataTable dtASN = ICSHelper.SQlReturnData(sqlASN, cmd);
  78. if (dtASN != null && dtASN.Rows.Count > 0)
  79. {
  80. throw new Exception("条码已绑定送货单,不能删除!");
  81. }
  82. //删除条码表表头
  83. string sqlLotDelete = "Delete ICSInventoryLot where workpoint='{0}' and LotNo='{1}'";
  84. sqlLotDelete = string.Format(sqlLotDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString());
  85. Log.WriteLogFile(sqlLotDelete, "创建删除条码SQL日志");
  86. if (!ICSHelper.ExecuteNonQuery(sqlLotDelete, cmd))
  87. {
  88. throw new Exception("删除条码表表头失败!");
  89. }
  90. //删除条码表表体
  91. string sqlLotDetailDelete = "Delete ICSInventoryLotDetail where workpoint='{0}' and LotNo='{1}'";
  92. sqlLotDetailDelete = string.Format(sqlLotDetailDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString());
  93. Log.WriteLogFile(sqlLotDetailDelete, "创建删除条码SQL日志");
  94. if (!ICSHelper.ExecuteNonQuery(sqlLotDetailDelete, cmd))
  95. {
  96. throw new Exception("删除条码表表体失败!");
  97. }
  98. //删除自由项表
  99. //string sqlCheck = "Select * from ICSInventoryLot where ExtensionID ='" + dtLot.Rows[0]["ExtensionID"].ToString() + "' and";
  100. //DataTable dtCheck = ICSHelper.SQlReturnData(sqlCheck, cmd);
  101. //if (dtCheck.Rows.Count < 1)
  102. //{
  103. // string sqlFreeDelete = "Delete ICSExtension where workpoint='{0}' and ID ='{1}'";
  104. // sqlFreeDelete = string.Format(sqlFreeDelete, WorkPoint, dtLot.Rows[0]["ExtensionID"].ToString());
  105. // Log.WriteLogFile(sqlFreeDelete, "创建删除条码SQL日志");
  106. // if (!ICSHelper.ExecuteNonQuery(sqlFreeDelete, cmd))
  107. // {
  108. // throw new Exception("删除自由项表失败!");
  109. // }
  110. //}
  111. #region 新增条码
  112. string sqlInvCheck = "Select InvCode,BatchEnable from ICSInventory Where workpoint='{0}' and InvCode ='{1}'";
  113. sqlInvCheck = string.Format(sqlInvCheck, WorkPoint, entity.InvCode);
  114. DataTable dtInvCheck = ICSHelper.SQlReturnData(sqlInvCheck, cmd);
  115. if (dtInvCheck.Rows.Count < 1)
  116. {
  117. throw new Exception("物料代码不存在!");
  118. }
  119. string BatchEnable = dtInvCheck.Rows[0]["BatchEnable"].ToString().ToUpper();
  120. Log.WriteLogFile("是否启用批次:" + BatchEnable, "创建删除条码日志");
  121. String EffectiveEnable = "";
  122. String Colspan = "";
  123. String IDD = "";
  124. int EffectiveDays = 0;
  125. String Time = "";
  126. DateTime? dtt;
  127. DateTime now = DateTime.Now;
  128. string sql = @"select EffectiveEnable,EffectiveDays from ICSInventory
  129. where InvCode='{0}' and WorkPoint='{1}'";
  130. sql = string.Format(sql, entity.InvCode, WorkPoint);
  131. DataTable dt = ICSHelper.SQlReturnData(sql, cmd);
  132. if (dt.Rows.Count == 0)
  133. {
  134. throw new Exception("有效天数为空!");
  135. }
  136. else
  137. {
  138. EffectiveEnable = dt.Rows[0]["EffectiveEnable"].ToString();
  139. EffectiveDays = Convert.ToInt32(dt.Rows[0]["EffectiveDays"]);
  140. }
  141. //EffectiveDayss = Convert.ToInt32(EffectiveDays);
  142. if (!EffectiveEnable.Equals("False"))
  143. {
  144. Time = Convert.ToDateTime(entity.ProductDate).AddDays(EffectiveDays).ToString();
  145. }
  146. else
  147. {
  148. Time = "2999-12-31 00:00:00";
  149. }
  150. dtt = Convert.ToDateTime(Time);
  151. if (entity.dDisableDate != null)
  152. {
  153. dtt = entity.dDisableDate;
  154. }
  155. //检验自由项
  156. if (BatchEnable == "TRUE")
  157. {
  158. Colspan = "" + "~" + entity.BatchNo + "~" + ""
  159. + "~" + "" + "~" + entity.Extension1
  160. + "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
  161. + "~" + entity.Extension5 + "~" + "" + "~" + ""
  162. + "~" + "" + "~" + "" + "~" + "";
  163. }
  164. else
  165. {
  166. Colspan = "" + "~" + "" + "~" + ""
  167. + "~" + "" + "~" + entity.Extension1
  168. + "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
  169. + "~" + entity.Extension5 + "~" + "" + "~" + ""
  170. + "~" + "" + "~" + "" + "~" + "";
  171. }
  172. Log.WriteLogFile("生成的自由项:" + Colspan, "创建删除条码日志");
  173. sql = @"select ID,Colspan from ICSExtension a
  174. where Colspan='{0}' and WorkPoint='{1}'";
  175. sql = string.Format(sql, Colspan, WorkPoint);
  176. DataTable dttt = ICSHelper.SQlReturnData(sql, cmd);
  177. if (dttt.Rows.Count == 0)
  178. {
  179. IDD = Guid.NewGuid().ToString();
  180. if (BatchEnable == "TRUE")
  181. {
  182. sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
  183. select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
  184. sql = string.Format(sql, Colspan, "", entity.BatchNo, "", "", entity.Extension1, entity.Extension2, entity.Extension3,
  185. entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
  186. }
  187. else
  188. {
  189. sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
  190. select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
  191. sql = string.Format(sql, Colspan, "", "", "", "", entity.Extension1, entity.Extension2, entity.Extension3,
  192. entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
  193. }
  194. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  195. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  196. {
  197. throw new Exception("新增自由项失败!");
  198. }
  199. }
  200. else
  201. {
  202. IDD = dttt.Rows[0]["ID"].ToString();
  203. }
  204. //存入条码表
  205. sql = @"
  206. INSERT INTO ICSInventoryLot(ID,LotNo,InvCode,ProductDate,ExpirationDate,
  207. Quantity,Amount,ExtensionID,Type,PrintTimes,
  208. LastPrintUser,LastPrintTime,MUSER,MUSERName,MTIME,
  209. WorkPoint,EATTRIBUTE1)
  210. SELECT TOP 1 NEWID(),'{0}','{1}','{2}','{3}',
  211. '{4}','{5}','{6}','{7}','{8}',
  212. '{9}','{10}','{11}','{12}','{13}',
  213. '{14}','{15}'";
  214. sql = string.Format(sql, entity.LotNo, entity.InvCode, entity.ProductDate, dtt, entity.Quantity, entity.AmountRate, IDD,
  215. entity.Type, entity.PrintTimes, entity.LastPrintUser, entity.LastPrintTime, entity.MUSER,
  216. entity.MUSERName, entity.MTIME, WorkPoint, entity.SRMLotGroup);
  217. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  218. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  219. {
  220. throw new Exception("新增条码表表头失败!");
  221. }
  222. ///存入条码关联表
  223. sql = @"
  224. INSERT INTO ICSInventoryLotDetail(LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,
  225. WorkPoint,EATTRIBUTE1)
  226. SELECT '{0}','{1}','{2}','{3}','{4}','{5}','{6}',''
  227. ";
  228. sql = string.Format(sql, entity.LotNo, entity.TransNo, entity.TransLine, entity.MUSER, entity.MUSERName, entity.MTIME, WorkPoint);
  229. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  230. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  231. {
  232. throw new Exception("新增条码表表体失败!");
  233. }
  234. #endregion
  235. }
  236. else
  237. {
  238. string sqlInvCheck = "Select InvCode,BatchEnable from ICSInventory Where workpoint='{0}' and InvCode ='{1}'";
  239. sqlInvCheck = string.Format(sqlInvCheck, WorkPoint, entity.InvCode);
  240. DataTable dtInvCheck = ICSHelper.SQlReturnData(sqlInvCheck, cmd);
  241. if (dtInvCheck.Rows.Count < 1)
  242. {
  243. throw new Exception("物料代码不存在!");
  244. }
  245. string BatchEnable = dtInvCheck.Rows[0]["BatchEnable"].ToString().ToUpper();
  246. Log.WriteLogFile("是否启用批次:" + BatchEnable, "创建删除条码日志");
  247. String EffectiveEnable = "";
  248. String Colspan = "";
  249. String IDD = "";
  250. int EffectiveDays = 0;
  251. String Time = "";
  252. DateTime? dtt;
  253. DateTime now = DateTime.Now;
  254. string sql = @"select EffectiveEnable,EffectiveDays from ICSInventory
  255. where InvCode='{0}' and WorkPoint='{1}'";
  256. sql = string.Format(sql, entity.InvCode, WorkPoint);
  257. DataTable dt = ICSHelper.SQlReturnData(sql, cmd);
  258. if (dt.Rows.Count == 0)
  259. {
  260. throw new Exception("有效天数为空!");
  261. }
  262. else
  263. {
  264. EffectiveEnable = dt.Rows[0]["EffectiveEnable"].ToString();
  265. EffectiveDays = Convert.ToInt32(dt.Rows[0]["EffectiveDays"]);
  266. }
  267. //EffectiveDayss = Convert.ToInt32(EffectiveDays);
  268. if (!EffectiveEnable.Equals("False"))
  269. {
  270. Time = Convert.ToDateTime(entity.ProductDate).AddDays(EffectiveDays).ToString();
  271. }
  272. else
  273. {
  274. Time = "2999-12-31 00:00:00";
  275. }
  276. dtt = Convert.ToDateTime(Time);
  277. if (entity.dDisableDate != null)
  278. {
  279. dtt = entity.dDisableDate;
  280. }
  281. //检验自由项
  282. if (BatchEnable=="TRUE")
  283. {
  284. Colspan = "" + "~" + entity.BatchNo + "~" + ""
  285. + "~" + "" + "~" + entity.Extension1
  286. + "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
  287. + "~" + entity.Extension5 + "~" + "" + "~" + ""
  288. + "~" + "" + "~" + "" + "~" + "";
  289. }
  290. else
  291. {
  292. Colspan = "" + "~" + "" + "~" + ""
  293. + "~" + "" + "~" + entity.Extension1
  294. + "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
  295. + "~" + entity.Extension5 + "~" + "" + "~" + ""
  296. + "~" + "" + "~" + "" + "~" + "";
  297. }
  298. Log.WriteLogFile("生成的自由项:"+Colspan, "创建删除条码日志");
  299. sql = @"select ID,Colspan from ICSExtension a
  300. where Colspan='{0}' and WorkPoint='{1}'";
  301. sql = string.Format(sql, Colspan, WorkPoint);
  302. DataTable dttt = ICSHelper.SQlReturnData(sql, cmd);
  303. if (dttt.Rows.Count == 0)
  304. {
  305. IDD = Guid.NewGuid().ToString();
  306. if (BatchEnable == "TRUE")
  307. {
  308. sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
  309. select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
  310. sql = string.Format(sql, Colspan, "", entity.BatchNo, "", "", entity.Extension1, entity.Extension2, entity.Extension3,
  311. entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
  312. }
  313. else
  314. {
  315. sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
  316. select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
  317. sql = string.Format(sql, Colspan, "", "", "", "", entity.Extension1, entity.Extension2, entity.Extension3,
  318. entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
  319. }
  320. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  321. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  322. {
  323. throw new Exception("新增自由项失败!");
  324. }
  325. }
  326. else
  327. {
  328. IDD = dttt.Rows[0]["ID"].ToString();
  329. }
  330. //存入条码表
  331. sql = @"
  332. INSERT INTO ICSInventoryLot(ID,LotNo,InvCode,ProductDate,ExpirationDate,
  333. Quantity,Amount,ExtensionID,Type,PrintTimes,
  334. LastPrintUser,LastPrintTime,MUSER,MUSERName,MTIME,
  335. WorkPoint,EATTRIBUTE1)
  336. SELECT TOP 1 NEWID(),'{0}','{1}','{2}','{3}',
  337. '{4}','{5}','{6}','{7}','{8}',
  338. '{9}','{10}','{11}','{12}','{13}',
  339. '{14}','{15}'";
  340. sql = string.Format(sql, entity.LotNo, entity.InvCode, entity.ProductDate, dtt, entity.Quantity, entity.AmountRate, IDD,
  341. entity.Type, entity.PrintTimes, entity.LastPrintUser, entity.LastPrintTime, entity.MUSER,
  342. entity.MUSERName, entity.MTIME, WorkPoint, entity.SRMLotGroup);
  343. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  344. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  345. {
  346. throw new Exception("新增条码表表头失败!");
  347. }
  348. ///存入条码关联表
  349. sql = @"
  350. INSERT INTO ICSInventoryLotDetail(LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,
  351. WorkPoint,EATTRIBUTE1)
  352. SELECT '{0}','{1}','{2}','{3}','{4}','{5}','{6}',''
  353. ";
  354. sql = string.Format(sql, entity.LotNo, entity.TransNo, entity.TransLine, entity.MUSER, entity.MUSERName, entity.MTIME, WorkPoint);
  355. Log.WriteLogFile(sql, "创建删除条码SQL日志");
  356. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  357. {
  358. throw new Exception("新增条码表表体失败!");
  359. }
  360. }
  361. cmd.Transaction.Commit();
  362. }
  363. catch (Exception ex)
  364. {
  365. cmd.Transaction.Rollback();
  366. sb.Append("执行报错,条码为:" + entity.LotNo + ",报错信息:" + ex.Message + "!!!");
  367. continue;
  368. }
  369. finally
  370. {
  371. if (conn.State == ConnectionState.Open)
  372. {
  373. conn.Close();
  374. }
  375. conn.Dispose();
  376. }
  377. }
  378. if (sb.Length > 0)
  379. {
  380. res.IsSuccess = false;
  381. res.Message = sb.ToString();
  382. }
  383. else
  384. {
  385. res.IsSuccess = true;
  386. res.Message = "执行成功!";
  387. }
  388. return res;
  389. }
  390. catch (Exception ex)
  391. {
  392. res.IsSuccess = false;
  393. res.Message = ex.Message;
  394. return res;
  395. }
  396. }
  397. }
  398. }