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.

506 lines
24 KiB

3 weeks ago
  1. using Newtonsoft.Json.Linq;
  2. using Newtonsoft.Json;
  3. using NFine.Data.Extensions;
  4. using NFine.Domain._03_Entity.SRM;
  5. using NFine.Repository;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Data;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.IO;
  14. using System.Net;
  15. using NFine.Code;
  16. using System.Data.Common;
  17. using System.Data.SqlClient;
  18. namespace NFine.Application.WMS
  19. {
  20. /// <summary>
  21. /// 委外发料申请
  22. /// </summary>
  23. public class ICSOApplyApp : RepositoryFactory<ICSVendor>
  24. {
  25. public static DataTable Invmes = new DataTable();
  26. /// <summary>
  27. /// 委外发料申请删除
  28. /// </summary>
  29. /// <param name="keyValue"></param>
  30. /// <returns></returns>
  31. /// <exception cref="Exception"></exception>
  32. public string DeleteOApplyApplyNeg(string keyValue)
  33. {
  34. //站点信息
  35. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  36. string msg = "";
  37. keyValue = keyValue.Substring(1, keyValue.Length - 2);
  38. string sql = string.Empty;
  39. sql += string.Format(@"DELETE FROM dbo.ICSOApply WHERE ApplyCode IN ({0}) and WorkPoint ='{1}'", keyValue.TrimEnd(','), WorkPoint);
  40. //sql += string.Format(@"DELETE FROM dbo.ICSMOApplyNegDetail WHERE ApplyNegCode IN ({0}) and WorkPoint ='{1}'", keyValue.TrimEnd(','), WorkPoint);
  41. try
  42. {
  43. if (SqlHelper.CmdExecuteNonQueryLi(sql) > 0)
  44. {
  45. }
  46. }
  47. catch (Exception ex)
  48. {
  49. throw new Exception(ex.Message);
  50. }
  51. return msg;
  52. }
  53. /// <summary>
  54. /// 委外申请单号查询
  55. /// </summary>
  56. /// <param name="jqgridparam"></param>
  57. /// <returns></returns>
  58. public DataTable GetOApplyApplyNeg(ref Pagination jqgridparam)
  59. {
  60. DataTable dt = new DataTable();
  61. List<DbParameter> parameter = new List<DbParameter>();
  62. string sql = @"select a.ApplyCode, a.CreatePerson,CONVERT(NVARCHAR(20), a.CreateDateTime,20) as CreateDateTime from ICSOApply a
  63. group by a.ApplyCode,a.CreatePerson,a.CreateDateTime";
  64. sql = string.Format(sql);
  65. DataTable dttest = Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  66. return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  67. }
  68. /// <summary>
  69. /// 委外订单明细查询
  70. /// </summary>
  71. /// <param name="jqgridparam"></param>
  72. /// <returns></returns>
  73. public DataTable GetOApplyApplyNegDetail(string ApplyNegCode, ref Pagination jqgridparam)
  74. {
  75. DataTable dt = new DataTable();
  76. object Figure = GetDecimalDigits();
  77. List<DbParameter> parameter = new List<DbParameter>();
  78. string sql = @"SELECT a.ID,a.ApplyCode,a.Sequence,a.SourceCode,a.SourceSequence,a.InvCode,a.Quantity,a.Amount,a.IssueQuantity,a.WHCode,e.WarehouseName as WHName,a.Type,a.Status,a.CreatePerson,a.CreateDateTime
  79. ,a.ApplyID,a.ApplyDetailID,a.ExtensionID,a.MUSER,a.MUSERName,a.MTIME,a.WorkPoint,b.InvName
  80. ,f.Colspan ,f.ProjectCode ,f.BatchCode ,f.Version ,f.Brand ,f.cFree1 ,f.cFree2 ,f.cFree3 ,f.cFree4
  81. ,f.cFree5 ,f.cFree6 ,f.cFree7 ,f.cFree8 ,f.cFree9 ,f.cFree10 ,a.EATTRIBUTE as EATTRIBUTE1,a.EATTRIBUTE2,a.EATTRIBUTE3,a.EATTRIBUTE4,a.EATTRIBUTE5,a.EATTRIBUTE6,a.EATTRIBUTE7,a.EATTRIBUTE8,
  82. a.EATTRIBUTE9,a.EATTRIBUTE10
  83. from ICSOApply a
  84. left join ICSInventory b on a.InvCode = b.InvCode and a.WorkPoint = b.WorkPoint
  85. left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint
  86. left join ICSWarehouse e on a.WHCode = e.WarehouseCode and a.WorkPoint=e.WorkPoint
  87. where a.ApplyCode = '" + ApplyNegCode + "' ";
  88. sql = string.Format(sql, Figure);
  89. DataTable dttest = Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  90. return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  91. }
  92. public object GetDecimalDigits()
  93. {
  94. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location.TrimEnd(',');
  95. try
  96. {
  97. string sql = string.Empty;
  98. sql = @"select Figure from ICSConfiguration where Code='Figure001' and Enable='1' and WorkPoint='" + WorkPoint + "'";
  99. object Figure = SqlHelper.ExecuteScalar(sql);
  100. return Figure;
  101. }
  102. catch (Exception ex)
  103. {
  104. throw new Exception(ex.Message.ToString());
  105. }
  106. }
  107. public void ClearTemp()
  108. {
  109. Invmes.Rows.Clear();
  110. }
  111. /// <summary>
  112. /// 新增
  113. /// </summary>
  114. /// <param name="ICSASN"></param>
  115. /// <returns></returns>
  116. public string SaveICSOApplyApplyNeg(string ICSASN)
  117. {
  118. string msg = "";
  119. string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "OApply/Create";
  120. string result = HttpPost(APIURL, ICSASN);
  121. JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
  122. string MessAge = Obj["Message"].ToString();
  123. string Success = Obj["Success"].ToString();
  124. if (Success.ToUpper() == "FALSE")
  125. {
  126. msg = MessAge;
  127. }
  128. return msg;
  129. }
  130. /// <summary>
  131. /// 修改
  132. /// </summary>
  133. /// <param name="ICSASN"></param>
  134. /// <returns></returns>
  135. public string UpdateICSOApplyApplyNeg(string ICSASN)
  136. {
  137. string msg = "";
  138. string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "OApply/Update";
  139. string result = HttpPost(APIURL, ICSASN);
  140. JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
  141. string MessAge = Obj["Message"].ToString();
  142. string Success = Obj["Success"].ToString();
  143. if (Success.ToUpper() == "FALSE")
  144. {
  145. msg = MessAge;
  146. }
  147. return msg;
  148. }
  149. /// <summary>
  150. /// 获取单号
  151. /// </summary>
  152. /// <param name="WorkPoint"></param>
  153. /// <returns></returns>
  154. public string GetOOCode(string WorkPoint)
  155. {
  156. WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  157. string OOCode = string.Empty;
  158. if (!string.IsNullOrEmpty(WorkPoint))
  159. {
  160. OOCode = GetSerialCode(WorkPoint, "ICSOApply", "ApplyCode", "OA", 8);
  161. }
  162. if (!string.IsNullOrWhiteSpace(OOCode))
  163. {
  164. string sqlISHave = @"SELECT ApplyCode FROM ICSMOApply a
  165. WHERE a.ApplyCode = '{0}'";
  166. sqlISHave = string.Format(sqlISHave, OOCode);
  167. DataTable dtIsHave = SqlHelper.GetDataTableBySql(sqlISHave);
  168. if (dtIsHave.Rows.Count > 0)
  169. {
  170. throw new Exception("单号已存在!");
  171. }
  172. }
  173. return OOCode;
  174. }
  175. public string GetSerialCode(string workPointCode, string tbName, string colName, string Pre, int numLen)
  176. {
  177. string sql = "EXEC Addins_GetSerialCode '{0}','{1}','{2}','{3}',{4}";
  178. sql = string.Format(sql, new object[] { workPointCode, tbName, colName, Pre, numLen });
  179. return SqlHelper.ExecuteScalar(sql).ToString();
  180. }
  181. public void UpdateOutsourcingOrderApplyNegTemp(string json)
  182. {
  183. var data = json.ToJObject();
  184. string usercode = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  185. var info = Invmes.Select(string.Format("TLZID='{0}'", data["TLZID"]));
  186. if (info != null && info.Length > 0)
  187. {
  188. info[0]["ZJID"] = data["ZJID"];
  189. info[0]["InvName"] = data["InvName"];
  190. }
  191. else
  192. {
  193. DataRow newrow = Invmes.NewRow();
  194. newrow["ZJID"] = data["ZJID"];
  195. Invmes.Rows.Add(newrow);
  196. }
  197. }
  198. public DataTable GetICSOApplyReturnTemporary(string ApplyCode)
  199. {
  200. string sql = @"SELECT a.ID,a.ApplyCode,a.Sequence,a.SourceCode,a.SourceSequence,a.InvCode,a.Quantity,a.Amount,a.IssueQuantity
  201. ,a.WHCode,a.Type,a.Status,a.CreatePerson,a.CreateDateTime,b.InvName
  202. ,a.ApplyID,a.ApplyDetailID,a.ExtensionID,a.MUSER,a.MUSERName,a.MTIME,a.WorkPoint
  203. from ICSOApply a
  204. left join ICSInventory b on a.InvCode = b.InvCode and a.WorkPoint = b.WorkPoint
  205. where a.ApplyCode ='" + ApplyCode + "' ";
  206. DataTable table = Repository().FindDataSetBySql(sql).Tables[0];
  207. DataTable dtCloned = table.Clone();
  208. foreach (DataColumn col in dtCloned.Columns)
  209. {
  210. col.DataType = typeof(string);
  211. }
  212. foreach (DataRow row in table.Rows)
  213. {
  214. DataRow newrow = dtCloned.NewRow();
  215. foreach (DataColumn column in dtCloned.Columns)
  216. {
  217. newrow[column.ColumnName] = row[column.ColumnName].ToString();
  218. }
  219. dtCloned.Rows.Add(newrow);
  220. }
  221. if (Invmes.Rows.Count > 0)
  222. {
  223. dtCloned.Merge(Invmes, false);
  224. }
  225. return dtCloned;
  226. }
  227. /// <summary>
  228. /// 接口api解析
  229. /// </summary>
  230. /// <param name="url"></param>
  231. /// <param name="body"></param>
  232. /// <returns></returns>
  233. /// <exception cref="Exception"></exception>
  234. public static string HttpPost(string url, string body)
  235. {
  236. try
  237. {
  238. Encoding encoding = Encoding.UTF8;
  239. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  240. request.Method = "POST";
  241. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  242. request.ContentType = "application/json; charset=utf-8";
  243. byte[] buffer = encoding.GetBytes(body);
  244. request.ContentLength = buffer.Length;
  245. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  246. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  247. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  248. {
  249. return reader.ReadToEnd();
  250. }
  251. }
  252. catch (WebException ex)
  253. {
  254. throw new Exception(ex.Message);
  255. }
  256. }
  257. //委外领料单
  258. public string SetData_PR(String savePath)
  259. {
  260. //数据获取
  261. try
  262. {
  263. int index = 1;
  264. string msg = "";
  265. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  266. string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
  267. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  268. SqlConnection conn = SqlHelper.GetDataCenterConn();
  269. string sql = "";
  270. string sqlss = "";
  271. int count = 0;
  272. DataTable data = FileToExcel.ExcelToTable(savePath);
  273. var groupedData = from row in data.AsEnumerable()
  274. group row by row.Field<string>("委外申请单号") into g
  275. select new { Category = g.Key, Values = g.ToList() };
  276. // 输出结果
  277. foreach (var group in groupedData)
  278. {
  279. string CreateDateTime = DateTime.Now.ToString();
  280. Console.WriteLine($"Category: {group.Category}");
  281. foreach (DataRow row in group.Values)
  282. {
  283. string IDD = string.Empty;
  284. string ApplyCode = row["委外申请单号"].ToString();
  285. string Sequence = row["申请单行号"].ToString();
  286. string InvCode = row["助记码"].ToString();
  287. string Quantity = row["数量"].ToString();
  288. string WHCode = row["仓库代码"].ToString();
  289. string EATTRIBUTE3 = row["物料代码"].ToString();
  290. string EATTRIBUTE4 = row["组件"].ToString();
  291. if (ApplyCode == "" || ApplyCode == null)
  292. {
  293. throw new Exception("第 " + index + " 行委外申请单号不能为空!");
  294. }
  295. if (Sequence == "" || Sequence == null)
  296. {
  297. throw new Exception("第 " + index + " 行申请单行号不能为空!");
  298. }
  299. if (InvCode == "" || InvCode == null)
  300. {
  301. throw new Exception("第 " + index + " 行助记码不能为空!");
  302. }
  303. if (Quantity == "" || Quantity == null)
  304. {
  305. throw new Exception("第 " + index + " 行数量不能为空!");
  306. }
  307. if (WHCode == "" || WHCode == null)
  308. {
  309. throw new Exception("第 " + index + " 行仓库代码不能为空!");
  310. }
  311. if (EATTRIBUTE3 == "" || EATTRIBUTE3 == null)
  312. {
  313. throw new Exception("第 " + index + " 行物料代码不能为空!");
  314. }
  315. string sqls = @"select ApplyCode,Sequence from ICSOApply where ApplyCode='{0}' and Sequence='{2}' and WorkPoint='{1}'";
  316. sqls = string.Format(sqls, ApplyCode, WorkPoint, Sequence);
  317. DataTable dnum = SqlHelper.CmdExecuteDataTable(sqls);
  318. if (dnum != null && dnum.Rows.Count > 0)
  319. {
  320. throw new Exception("委外领料单: " + ApplyCode + ",行号: " + Sequence + " 已存在!");
  321. }
  322. else
  323. {
  324. //检验自由项
  325. string Colspan = row["批次"].ToString() + "~" + row["自由项1"].ToString()
  326. + "~" + row["自由项2"].ToString() + "~" + row["自由项3"].ToString() + "~" + row["自由项4"].ToString()
  327. + "~" + row["自由项5"].ToString() + "~" + row["自由项6"].ToString() + "~" + row["自由项7"].ToString()
  328. + "~" + row["自由项8"].ToString() + "~" + row["自由项9"].ToString() + "~" + row["自由项10"].ToString();
  329. sql = @"select ID,Colspan from ICSExtension a
  330. where Colspan='{0}' and WorkPoint='{1}'";
  331. sql = string.Format(sql, Colspan, WorkPoint);
  332. var dtt = SqlHelper.CmdExecuteDataTable(sql);
  333. if (dtt.Rows.Count == 0)
  334. {
  335. IDD = Guid.NewGuid().ToString();
  336. sql = @"Insert into ICSExtension(ID, Colspan, BatchCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
  337. select '{14}','{0}','{1}',
  338. '{2}','{3}','{4}','{5}',
  339. '{6}','{7}','{8}','{9}','{10}','{11}',GETDATE(),'{12}',f.F_RealName,'{13}'from Sys_SRM_User f where f.F_Account='{12}' and f.F_Location='{13}'";
  340. sql = string.Format(sql, Colspan, row["批次"].ToString(), row["自由项1"].ToString(), row["自由项2"].ToString(), row["自由项3"].ToString(), row["自由项4"].ToString(), row["自由项5"].ToString(), row["自由项6"].ToString(), row["自由项7"].ToString(), row["自由项8"].ToString(), row["自由项9"].ToString(), row["自由项10"].ToString(), MUSER, WorkPoint, IDD);
  341. if (SqlHelper.CmdExecuteNonQueryLi(sql) < 0)
  342. {
  343. throw new Exception("自由项添加失败");
  344. }
  345. }
  346. else
  347. {
  348. IDD = dtt.Rows[0]["ID"].ToString();
  349. }
  350. sqlss += @"Insert INto ICSOApply
  351. ( ID,ApplyCode,Sequence,SourceCode,SourceSequence,InvCode,Quantity,Amount, IssueQuantity,WHCode,Type,Status,CreatePerson,CreateDateTime,ApplyID,ApplyDetailID,ExtensionID,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE3,EATTRIBUTE4)
  352. Values (newid(),'{0}','{1}','{2}','{3}','{4}',{5},{6},
  353. {7},'{8}','{9}','{10}','{11}','{17}','{12}','{13}','{14}',
  354. '{11}','{15}',GETDATE(),'{16}','{17}','{18}' )";
  355. }
  356. sqlss = string.Format(sqlss, ApplyCode, Sequence, row["来源单据号"].ToString(), row["来源单据行号"].ToString(), InvCode, Quantity, row["辅计量数量"].ToString() == "" ? 0 : Convert.ToDecimal(row["辅计量数量"].ToString()), row["已发数量"].ToString() == "" ? 0 : Convert.ToDecimal(row["已发数量"].ToString()), WHCode, "1", "2", MUSER, "", "", IDD, MUSERNAME, WorkPoint, CreateDateTime, EATTRIBUTE3, EATTRIBUTE4);
  357. }
  358. }
  359. //foreach (DataRow dr in data.Rows)
  360. //{
  361. // index++;
  362. // var StackCode = "";
  363. // var StackName = "";
  364. // //string GUID = Guid.NewGuid().ToString();
  365. // string KuFang = dr["库房编码"].ToString().Trim().ToUpper();
  366. // string Qu = dr["区"].ToString().Trim().ToUpper();
  367. // string Pai = dr["排"].ToString().Trim().ToUpper();
  368. // string Jia = dr["货架"].ToString().Trim().ToUpper();
  369. // string Ceng = dr["层"].ToString().Trim().ToUpper();
  370. // string Ge = dr["格"].ToString().Trim().ToUpper();
  371. // if (KuFang == "" || KuFang == null)
  372. // {
  373. // throw new Exception("第 " + index + " 行库房编码不能为空!");
  374. // }
  375. // if (Qu == "" && Pai == "" && Jia == "" && Ceng == "" && Ge == "")
  376. // {
  377. // throw new Exception("第 " + index + " 区、排、货架、层、格至少填写一项!");
  378. // }
  379. // string sqlWH = @"select ID from ICSWarehouse where WarehouseCode='{0}' and WorkPoint='{1}'";
  380. // sqlWH = string.Format(sqlWH, KuFang, WorkPoint);
  381. // DataTable dd = SqlHelper.CmdExecuteDataTable(sqlWH);
  382. // if (dd == null || dd.Rows.Count <= 0)
  383. // {
  384. // throw new Exception("库房编码: " + KuFang + " 不存在!");
  385. // }
  386. // string WHID = dd.Rows[0]["ID"].ToString();
  387. // StackCode = KuFang;
  388. // StackName = KuFang + "仓库";
  389. // if (Qu != "")
  390. // {
  391. // StackCode += "-" + Qu;
  392. // StackName += Qu + "区";
  393. // }
  394. // if (Pai != "")
  395. // {
  396. // StackCode += "-" + Pai;
  397. // StackName += Pai + "排";
  398. // }
  399. // if (Jia != "")
  400. // {
  401. // StackCode += "-" + Jia;
  402. // StackName += Jia + "货架";
  403. // }
  404. // if (Ceng != "")
  405. // {
  406. // StackCode += "-" + Ceng;
  407. // StackName += Ceng + "层";
  408. // }
  409. // if (Ge != "")
  410. // {
  411. // StackCode += "-" + Ge;
  412. // StackName += Ge + "格";
  413. // }
  414. // string sqls = @"select LocationCode from ICSLocation where LocationCode='{0}' and WorkPoint='{1}'";
  415. // sqls = string.Format(sqls, StackCode, WorkPoint);
  416. // DataTable dnum = SqlHelper.CmdExecuteDataTable(sqls);
  417. // if (dnum != null && dnum.Rows.Count > 0)
  418. // {
  419. // throw new Exception("库位: " + StackCode + " 已存在!");
  420. // }
  421. // else
  422. // {
  423. // sql += @"Insert INto ICSLocation
  424. // ( ID, LocationCode, LocationName, WHID, MUSER, MUSERName, MTIME, WorkPoint, EATTRIBUTE1, Region, Row, GoodsShelf, Tier, Grid)
  425. // Values (newid(),'{0}','{1}','{2}','{3}','{4}',getdate(),'{5}','','{6}','{7}','{8}','{9}','{10}')";
  426. // }
  427. // sql = string.Format(sql, StackCode, StackName, WHID, MUSER, MUSERNAME, WorkPoint, Qu, Pai, Jia, Ceng, Ge);
  428. //}
  429. count = SqlHelper.CmdExecuteNonQueryLi(sqlss);
  430. if (count > 0)
  431. {
  432. msg = "导入成功";
  433. }
  434. else
  435. {
  436. return "无有效的导入数据。";
  437. }
  438. return msg;
  439. }
  440. catch (Exception ex)
  441. {
  442. return ex.Message;
  443. }
  444. }
  445. /// <summary>
  446. /// 全部导出
  447. /// <returns></returns>
  448. public DataTable GetASNListExport()
  449. {
  450. string sql = @"SELECT a.ApplyCode AS 委外申请单号
  451. ,a.Sequence AS
  452. ,a.SourceCode AS
  453. ,a.SourceSequence AS
  454. ,a.IssueQuantity AS
  455. ,a.Quantity AS
  456. ,a.WHCode AS
  457. ,e.WarehouseName as
  458. ,a.Type AS
  459. ,a.InvCode AS
  460. ,b.InvName AS
  461. ,a.Amount AS
  462. ,a.Status AS
  463. ,a.CreatePerson AS
  464. ,a.CreateDateTime AS
  465. ,a.MUSER AS
  466. ,a.MUSERName AS
  467. ,a.MTIME AS
  468. ,a.WorkPoint AS
  469. from ICSOApply a
  470. left join ICSInventory b on a.InvCode = b.InvCode and a.WorkPoint = b.WorkPoint
  471. left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint
  472. left join ICSWarehouse e on a.WHCode = e.WarehouseCode and a.WorkPoint=e.WorkPoint";
  473. DataTable dt = SqlHelper.GetDataTableBySql(sql);
  474. return dt;
  475. }
  476. }
  477. }