纽威
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.

259 lines
13 KiB

3 years ago
  1. using NFine.Data.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using NFine.Code;
  9. using NFine.Repository;
  10. using System.Data.Common;
  11. using NFine.Domain._03_Entity.SRM;
  12. using ICS.Application.Entity;
  13. using Newtonsoft.Json;
  14. using System.Configuration;
  15. using System.Data.SqlClient;
  16. using ICS.Data;
  17. using NFine.Domain._03_Entity.WMS;
  18. using System.IO;
  19. namespace NFine.Application.WMS
  20. {
  21. public class HomeWorkApp : RepositoryFactory<ICSVendor>
  22. {
  23. public string SetData_PR(String savePath, string Year)
  24. {
  25. //数据获取
  26. try
  27. {
  28. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  29. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  30. string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
  31. SqlConnection conn = SqlHelper.GetDataCenterConn();
  32. DataTable data = FileToExcel.ExcelToTable(savePath);
  33. int index = 1;
  34. int countLot = 1;
  35. string msg = "";
  36. string StorageCode = "";
  37. DateTime time = DateTime.Now;
  38. string Tday = time.ToString("yyyyMMdd");
  39. string sql = string.Empty;
  40. sql = string.Format(@"select InvCode FROM dbo.ICSInventory where WorkPoint ='{0}' ", WorkPoint);
  41. sql += string.Format(@"select LocationCode ,WarehouseCode FROM dbo.ICSLocation a
  42. inner join ICSWarehouse b on a.WHID=b.ID and a.WorkPoint=b.WorkPoint where b.Enable='1' and a.WorkPoint ='{0}' ", WorkPoint);
  43. sql += string.Format(@"SELECT MAX(A.LotNO) AS LOTNO FROM ICSInventoryLot A WHERE A.LotNO LIKE '{0}%' AND LEN(a.LotNO) = 13", Tday);
  44. sql += string.Format(@"SELECT top 0 ID,LotNo,InvCode,ProductDate,ExpirationDate,Quantity,AmountRate,ExtensionID,Type,PrintTimes,LastPrintUser,LastPrintTime,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1 FROM ICSInventoryLot ");
  45. sql += string.Format(@"SELECT top 0 ID,LotNo,WarehouseCode,LocationCode,InvCode,Quantity,InDate,LockQuantity,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1 FROM ICSWareHouseLotInfo ");
  46. sql += string.Format(@"SELECT top 0 ID,Identification,TransCode,TransSequence,LotNo,InvCode,FromWarehouseCode,FromLocationCode,ToWarehouseCode,ToLocationCode,Quantity,Memo,Lock,TransType,BusinessCode,ERPUpload,ERPID,ERPDetailID,ERPCode,ERPSequence,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1 FROM ICSWareHouseLotInfoLog ");
  47. sql += string.Format(@"SELECT ID,Colspan,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,MTIME,MUSER,MUSERName,WorkPoint,EATTRIBUTE1,EATTRIBUTE2,EATTRIBUTE3,EATTRIBUTE4,EATTRIBUTE5 FROM ICSExtension where Colspan='' ");
  48. sql += string.Format(@"SELECT top 0 LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1 FROM ICSInventoryLotDetail");
  49. DataSet ds = SqlHelper.GetDataSetBySql(sql);
  50. DataSet dsSave = new DataSet();
  51. dsSave.Tables.Add(ds.Tables[3].Copy());
  52. dsSave.Tables.Add( ds.Tables[4].Copy());
  53. dsSave.Tables.Add( ds.Tables[5].Copy());
  54. dsSave.Tables.Add(ds.Tables[6].Clone());
  55. dsSave.Tables.Add(ds.Tables[7].Copy());
  56. dsSave.Tables[0].TableName="ICSInventoryLot";
  57. dsSave.Tables[1].TableName = "ICSWareHouseLotInfo";
  58. dsSave.Tables[2].TableName = "ICSWareHouseLotInfoLog";
  59. dsSave.Tables[3].TableName = "ICSExtension";
  60. dsSave.Tables[4].TableName = "ICSInventoryLotDetail";
  61. string LotNO = "";
  62. Int64 Lot_NO = 0;
  63. DataTable dts = ds.Tables[2];
  64. if (dts != null && dts.Rows.Count > 0)
  65. {
  66. string lot = dts.Rows[0]["LOTNO"].ToString();
  67. if(!string.IsNullOrWhiteSpace(lot))
  68. Lot_NO = Convert.ToInt64(lot.Substring(lot.Length - 5));
  69. }
  70. if (data != null && data.Rows.Count > 0)
  71. {
  72. foreach (DataRow dr in data.Rows)
  73. {
  74. index++;
  75. string ItemCode = dr["物料编码"].ToString();
  76. string StackCode = dr["库位"].ToString();
  77. string Date = dr["入库日期"].ToString();
  78. string VENCODE = dr["供应商"].ToString();
  79. DateTime dataTime;
  80. if (Date == "")
  81. {
  82. dataTime = DateTime.Now;
  83. }
  84. else
  85. {
  86. dataTime = Convert.ToDateTime(Date);
  87. }
  88. decimal QTY = 0;
  89. bool result = decimal.TryParse(dr["库位数量或最小包装数量"].ToString(), out QTY);
  90. if (!result)
  91. {
  92. throw new Exception("第 " + index + " 行,库位数量或最小包装数量:" + dr["库位数量或最小包装数量"].ToString() + " 格式不正确!");
  93. }
  94. if (decimal.Parse(dr["库位数量或最小包装数量"].ToString()) < 0)
  95. {
  96. throw new Exception("第 " + index + " 行,库位数量或最小包装数量:" + dr["库位数量或最小包装数量"].ToString() + " 数量不正确,为负值!");
  97. }
  98. int count = 0;
  99. bool resultCount = int.TryParse(dr["整盘数"].ToString(), out count);
  100. if (!resultCount)
  101. {
  102. throw new Exception("第 " + index + " 行,整盘数:" + dr["整盘数"].ToString() + " 格式不正确!");
  103. }
  104. var itemdrs = ds.Tables[0].Select(string.Format("InvCode='{0}'", ItemCode));
  105. if (itemdrs == null || itemdrs.Length <= 0)
  106. {
  107. throw new Exception("第 " + index + " 行,物料编码:" + ItemCode + " 不存在!");
  108. }
  109. var itemdrs2 = ds.Tables[1].Select(string.Format("LocationCode='{0}'", StackCode));
  110. if (itemdrs2 == null || itemdrs2.Length <= 0)
  111. {
  112. throw new Exception("第 " + index + " 行,库位:" + StackCode + " 不存在!");
  113. }
  114. else
  115. {
  116. StorageCode = itemdrs2[0]["WarehouseCode"].ToString();
  117. }
  118. var ID = "";
  119. var Extension = ds.Tables[6].Select(string.Format("Colspan='{0}'", ""));
  120. if (Extension == null || Extension.Length <= 0)
  121. {
  122. DataRow drExtension = dsSave.Tables[3].NewRow();
  123. ID = GetNewid();
  124. drExtension["ID"] = ID;
  125. drExtension["Colspan"] = "";
  126. drExtension["MUSER"] = MUSER;
  127. drExtension["MUSERName"] = MUSERNAME;
  128. drExtension["MTIME"] = DateTime.Now;
  129. drExtension["WorkPoint"] = WorkPoint;
  130. dsSave.Tables[3].Rows.Add(drExtension);
  131. }
  132. else
  133. {
  134. ID = Extension[0]["ID"].ToString();
  135. }
  136. var Quantity= Convert.ToDecimal(dr["库位数量或最小包装数量"].ToString());
  137. var logID = GetNewid();
  138. for (int i = count; i > 0; i--)
  139. {
  140. LotNO = Tday + (Lot_NO + countLot++).ToString().PadLeft(5, '0');
  141. #region 条码
  142. DataRow drInventory = dsSave.Tables[0].NewRow();
  143. drInventory["ID"] = GetNewid();
  144. drInventory["LotNo"] = LotNO;
  145. drInventory["InvCode"] = ItemCode;
  146. drInventory["ProductDate"] = dataTime;
  147. drInventory["ExpirationDate"] = Convert.ToDateTime("2999-12-31");
  148. drInventory["Quantity"] = Quantity;
  149. drInventory["AmountRate"] = "1";
  150. drInventory["Type"] = "11";
  151. drInventory["ExtensionID"] = ID;
  152. drInventory["MUSER"] = MUSER;
  153. drInventory["MUSERName"] = MUSERNAME;
  154. drInventory["MTIME"] = DateTime.Now;
  155. drInventory["WorkPoint"] = WorkPoint;
  156. dsSave.Tables[0].Rows.Add(drInventory);
  157. #endregion
  158. #region 条码关联单据
  159. DataRow drInventoryLotDetail = dsSave.Tables[4].NewRow();
  160. drInventoryLotDetail["LotNo"] = LotNO;
  161. drInventoryLotDetail["TransCode"] = "";
  162. drInventoryLotDetail["TransSequence"] = "";
  163. drInventoryLotDetail["MUSER"] = MUSER;
  164. drInventoryLotDetail["MUSERName"] = MUSERNAME;
  165. drInventoryLotDetail["MTIME"] = DateTime.Now;
  166. drInventoryLotDetail["WorkPoint"] = WorkPoint;
  167. dsSave.Tables[4].Rows.Add(drInventoryLotDetail);
  168. #endregion
  169. #region 库存
  170. DataRow drHouseInfo = dsSave.Tables[1].NewRow();
  171. drHouseInfo["ID"] = GetNewid();
  172. drHouseInfo["LotNo"] = LotNO;
  173. drHouseInfo["WarehouseCode"] = StorageCode;
  174. drHouseInfo["LocationCode"] = StackCode;
  175. drHouseInfo["InvCode"] = ItemCode;
  176. drHouseInfo["Quantity"] = Quantity;
  177. drHouseInfo["InDate"] = dataTime;
  178. drHouseInfo["LockQuantity"] = 0;
  179. drHouseInfo["MUSER"] = MUSER;
  180. drHouseInfo["MUSERName"] = MUSERNAME;
  181. drHouseInfo["MTIME"] = DateTime.Now;
  182. drHouseInfo["WorkPoint"] = WorkPoint;
  183. dsSave.Tables[1].Rows.Add(drHouseInfo);
  184. #endregion
  185. #region 记录表
  186. DataRow drHouseLog = dsSave.Tables[2].NewRow();
  187. drHouseLog["ID"] = GetNewid();
  188. drHouseLog["Identification"] = logID;
  189. drHouseLog["LotNo"] = LotNO;
  190. drHouseLog["InvCode"] = ItemCode;
  191. drHouseLog["ToWarehouseCode"] = StorageCode;
  192. drHouseLog["ToLocationCode"] = StackCode;
  193. drHouseLog["Quantity"] = Quantity;
  194. drHouseLog["Lock"] = 0;
  195. drHouseLog["TransType"] = "1";
  196. drHouseLog["BusinessCode"] = "39";
  197. drHouseLog["ERPUpload"] = 0;
  198. drHouseLog["MUSER"] = MUSER;
  199. drHouseLog["MUSERName"] = MUSERNAME;
  200. drHouseLog["MTIME"] = DateTime.Now;
  201. drHouseLog["WorkPoint"] = WorkPoint;
  202. dsSave.Tables[2].Rows.Add(drHouseLog);
  203. #endregion
  204. }
  205. }
  206. SqlHelper.ExecuteDataSet(dsSave);
  207. }
  208. else
  209. {
  210. return "无有效的导入数据。";
  211. }
  212. return "true";
  213. }
  214. catch (Exception ex)
  215. {
  216. return ex.Message;
  217. }
  218. }
  219. public string GetNewid()
  220. {
  221. string sql = "select newid() AS ID";
  222. return Repository().FindTableBySql(sql, null).Rows[0]["ID"].ToString();
  223. }
  224. }
  225. }