爱思开
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.

364 lines
19 KiB

2 years ago
  1. using NFine.Data.Extensions;
  2. using System;
  3. using System.Data;
  4. using NFine.Code;
  5. using NFine.Repository;
  6. using NFine.Domain._03_Entity.SRM;
  7. using System.Data.SqlClient;
  8. namespace NFine.Application.WMS
  9. {
  10. public class HomeWorkApp : RepositoryFactory<ICSVendor>
  11. {
  12. public string SetData_PR(String savePath, string Year)
  13. {
  14. //数据获取
  15. try
  16. {
  17. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  18. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  19. string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
  20. SqlConnection conn = SqlHelper.GetDataCenterConn();
  21. DataTable data = FileToExcel.ExcelToTable(savePath);
  22. int index = 1;
  23. int countLot = 1;
  24. string msg = "";
  25. string StorageCode = "";
  26. DateTime time = DateTime.Now;
  27. string Tday = time.ToString("yyyyMMdd");
  28. string sql = string.Empty;
  29. sql = string.Format(@"select InvCode FROM dbo.ICSInventory where WorkPoint ='{0}' ", WorkPoint);
  30. sql += string.Format(@"select LocationCode ,WarehouseCode FROM dbo.ICSLocation a
  31. inner join ICSWarehouse b on a.WHID=b.ID and a.WorkPoint=b.WorkPoint where b.Enable='1' and a.WorkPoint ='{0}' ", WorkPoint);
  32. sql += string.Format(@" SELECT MAX(A.LotNO) AS LOTNO FROM ICSInventoryLot A WHERE A.LotNO LIKE '{0}%' and A.WorkPoint='{1}' AND LEN(a.LotNO) = 13", Tday, WorkPoint);
  33. sql += string.Format(@" SELECT top 0 ID,LotNo,InvCode,ProductDate,ExpirationDate,Quantity,Amount,ExtensionID,Type,PrintTimes,LastPrintUser,LastPrintTime,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1 FROM ICSInventoryLot ");
  34. sql += string.Format(@" SELECT top 0 ID,LotNo,WarehouseCode,LocationCode,InvCode,Quantity,InDate,LockQuantity,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1 FROM ICSWareHouseLotInfo ");
  35. 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, LogID, MergeID, MUSER, MUSERName, MTIME, WorkPoint, EATTRIBUTE1, EATTRIBUTE2, EATTRIBUTE3 FROM dbo.ICSWareHouseLotInfoLog ");
  36. 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 WorkPoint='{0}' ", WorkPoint);
  37. sql += string.Format(@" SELECT top 0 LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1 FROM ICSInventoryLotDetail");
  38. sql += string.Format(@" select InvCode,BatchEnable from ICSInventory");
  39. DataSet ds = SqlHelper.GetDataSetBySql(sql);
  40. DataSet dsSave = new DataSet();
  41. dsSave.Tables.Add(ds.Tables[3].Copy());
  42. dsSave.Tables.Add( ds.Tables[4].Copy());
  43. dsSave.Tables.Add( ds.Tables[5].Copy());
  44. dsSave.Tables.Add(ds.Tables[6].Clone());
  45. dsSave.Tables.Add(ds.Tables[7].Copy());
  46. //dsSave.Tables.Add(ds.Tables[8].Copy());
  47. dsSave.Tables[0].TableName="ICSInventoryLot";
  48. dsSave.Tables[1].TableName = "ICSWareHouseLotInfo";
  49. dsSave.Tables[2].TableName = "ICSWareHouseLotInfoLog";
  50. dsSave.Tables[3].TableName = "ICSExtension";
  51. dsSave.Tables[4].TableName = "ICSInventoryLotDetail";
  52. // dsSave.Tables[5].TableName = "ICSInventory";
  53. string LotNO = "";
  54. Int64 Lot_NO = 0;
  55. DataTable dts = ds.Tables[2];
  56. if (dts != null && dts.Rows.Count > 0)
  57. {
  58. string lot = dts.Rows[0]["LOTNO"].ToString();
  59. if(!string.IsNullOrWhiteSpace(lot))
  60. Lot_NO = Convert.ToInt64(lot.Substring(lot.Length - 5));
  61. }
  62. if (data != null && data.Rows.Count > 0)
  63. {
  64. foreach (DataRow dr in data.Rows)
  65. {
  66. index++;
  67. string ItemCode = dr["物料编码"].ToString();
  68. string StackCode = dr["库位"].ToString();
  69. string Date = dr["入库日期"].ToString();
  70. string VENCODE = dr["供应商"].ToString();
  71. decimal Amount = 0;
  72. if (data.Columns.Contains("辅计量数量"))
  73. {
  74. Amount = string.IsNullOrWhiteSpace(dr["辅计量数量"].ToString())?0:Convert.ToDecimal(dr["辅计量数量"].ToString());
  75. }
  76. string ProjectCode = string.Empty;
  77. string BatchCode = string.Empty;
  78. string Version = string.Empty;
  79. string Brand = string.Empty;
  80. string oldLotno = string.Empty;
  81. string cFree1 = string.Empty;
  82. string cFree2 = string.Empty;
  83. string cFree3 = string.Empty;
  84. string cFree4 = string.Empty;
  85. string cFree5 = string.Empty;
  86. string cFree6 = string.Empty;
  87. string cFree7 = string.Empty;
  88. string cFree8 = string.Empty;
  89. string cFree9 = string.Empty;
  90. string cFree10 = string.Empty;
  91. if (data.Columns.Contains("项目号"))
  92. {
  93. ProjectCode = dr["项目号"].ToString();
  94. }
  95. if (data.Columns.Contains("批次"))
  96. {
  97. BatchCode= dr["批次"].ToString();
  98. }
  99. if (data.Columns.Contains("版本"))
  100. {
  101. Version = dr["版本"].ToString();
  102. }
  103. if (data.Columns.Contains("厂牌"))
  104. {
  105. Brand = dr["厂牌"].ToString();
  106. }
  107. if (data.Columns.Contains("旧条码"))
  108. {
  109. oldLotno = dr["旧条码"].ToString();
  110. }
  111. if (data.Columns.Contains("自由项1"))
  112. {
  113. cFree1 = dr["自由项1"].ToString();
  114. }
  115. if (data.Columns.Contains("自由项2"))
  116. {
  117. cFree2 = dr["自由项2"].ToString();
  118. }
  119. if (data.Columns.Contains("自由项3"))
  120. {
  121. cFree3 = dr["自由项3"].ToString();
  122. }
  123. if (data.Columns.Contains("自由项4"))
  124. {
  125. cFree4 = dr["自由项4"].ToString();
  126. }
  127. if (data.Columns.Contains("自由项5"))
  128. {
  129. cFree5 = dr["自由项5"].ToString();
  130. }
  131. if (data.Columns.Contains("自由项6"))
  132. {
  133. cFree6 = dr["自由项6"].ToString();
  134. }
  135. if (data.Columns.Contains("自由项7"))
  136. {
  137. cFree7 = dr["自由项7"].ToString();
  138. }
  139. if (data.Columns.Contains("自由项8"))
  140. {
  141. cFree8 = dr["自由项8"].ToString();
  142. }
  143. if (data.Columns.Contains("自由项9"))
  144. {
  145. cFree9 = dr["自由项9"].ToString();
  146. }
  147. if (data.Columns.Contains("自由项10"))
  148. {
  149. cFree10 = dr["自由项10"].ToString();
  150. }
  151. string Colspan = ProjectCode + "~" + BatchCode + "~" + Version + "~" + Brand + "~" + oldLotno + "~" + cFree1 + "~" + cFree2 + "~" + cFree3 + "~" + cFree4 + "~" + cFree5 + "~" + cFree6 + "~" + cFree7 + "~" + cFree8 + "~" + cFree9 + "~" + cFree10;
  152. DateTime dataTime;
  153. if (Date == "")
  154. {
  155. dataTime = DateTime.Now;
  156. }
  157. else
  158. {
  159. dataTime = Convert.ToDateTime(Date);
  160. }
  161. decimal QTY = 0;
  162. bool result = decimal.TryParse(dr["库位数量或最小包装数量"].ToString(), out QTY);
  163. if (!result)
  164. {
  165. throw new Exception("第 " + index + " 行,库位数量或最小包装数量:" + dr["库位数量或最小包装数量"].ToString() + " 格式不正确!");
  166. }
  167. if (decimal.Parse(dr["库位数量或最小包装数量"].ToString()) < 0)
  168. {
  169. throw new Exception("第 " + index + " 行,库位数量或最小包装数量:" + dr["库位数量或最小包装数量"].ToString() + " 数量不正确,为负值!");
  170. }
  171. int count = 0;
  172. bool resultCount = int.TryParse(dr["整盘数"].ToString(), out count);
  173. if (!resultCount)
  174. {
  175. throw new Exception("第 " + index + " 行,整盘数:" + dr["整盘数"].ToString() + " 格式不正确!");
  176. }
  177. var itemdrs = ds.Tables[0].Select(string.Format("InvCode='{0}'", ItemCode));
  178. if (itemdrs == null || itemdrs.Length <= 0)
  179. {
  180. throw new Exception("第 " + index + " 行,物料编码:" + ItemCode + " 不存在!");
  181. }
  182. var itemOldLotno = ds.Tables[3].Select(string.Format("LotNo='{0}'", oldLotno));
  183. if (itemOldLotno != null && itemOldLotno.Length > 0)
  184. {
  185. throw new Exception("第 " + index + " 行,旧条码:" + oldLotno + " 已存在!");
  186. }
  187. var itemdrs2 = ds.Tables[1].Select(string.Format("LocationCode='{0}'", StackCode));
  188. if (itemdrs2 == null || itemdrs2.Length <= 0)
  189. {
  190. throw new Exception("第 " + index + " 行,库位:" + StackCode + " 不存在!");
  191. }
  192. else
  193. {
  194. StorageCode = itemdrs2[0]["WarehouseCode"].ToString();
  195. }
  196. var Enable = ds.Tables[8].Select(string.Format("InvCode='{0}'", ItemCode));
  197. if (Enable[0]["BatchEnable"].ToString()== "True")
  198. {
  199. if (BatchCode=="")
  200. {
  201. throw new Exception("第 " + index + " 行,料品:" + ItemCode + " 已启用批次管理,请输入批次!");
  202. }
  203. }
  204. var ID = "";
  205. var Extension = ds.Tables[6].Select(string.Format("Colspan='{0}'", Colspan));
  206. if (Extension == null || Extension.Length <= 0)
  207. {
  208. DataRow drExtension = dsSave.Tables[3].NewRow();
  209. ID = GetNewid();
  210. drExtension["ID"] = ID;
  211. drExtension["Colspan"] = Colspan;
  212. drExtension["ProjectCode"] = ProjectCode;
  213. drExtension["BatchCode"] = BatchCode;
  214. drExtension["Version"] = Version;
  215. drExtension["Brand"] = Brand;
  216. drExtension["cFree1"] = cFree1;
  217. drExtension["cFree2"] = cFree2;
  218. drExtension["cFree3"] = cFree3;
  219. drExtension["cFree4"] = cFree4;
  220. drExtension["cFree5"] = cFree5;
  221. drExtension["cFree6"] = cFree6;
  222. drExtension["cFree7"] = cFree7;
  223. drExtension["cFree8"] = cFree8;
  224. drExtension["cFree9"] = cFree9;
  225. drExtension["cFree10"] = cFree10;
  226. drExtension["MUSER"] = MUSER;
  227. drExtension["MUSERName"] = MUSERNAME;
  228. drExtension["MTIME"] = DateTime.Now;
  229. drExtension["WorkPoint"] = WorkPoint;
  230. dsSave.Tables[3].Rows.Add(drExtension);
  231. DataRow drNew = ds.Tables[6].NewRow();
  232. drNew.ItemArray = drExtension.ItemArray;
  233. ds.Tables[6].Rows.Add(drNew);
  234. }
  235. else
  236. {
  237. ID = Extension[0]["ID"].ToString();
  238. }
  239. var Quantity= Convert.ToDecimal(dr["库位数量或最小包装数量"].ToString());
  240. var logID = GetNewid();
  241. for (int i = count; i > 0; i--)
  242. {
  243. LotNO = Tday + (Lot_NO + countLot++).ToString().PadLeft(5, '0');
  244. #region 条码
  245. DataRow drInventory = dsSave.Tables[0].NewRow();
  246. drInventory["ID"] = GetNewid();
  247. drInventory["LotNo"] = LotNO;
  248. drInventory["InvCode"] = ItemCode;
  249. drInventory["ProductDate"] = dataTime;
  250. drInventory["ExpirationDate"] = Convert.ToDateTime("2999-12-31");
  251. drInventory["Quantity"] = Quantity;
  252. drInventory["Amount"] = Amount;
  253. drInventory["Type"] = "0";
  254. drInventory["ExtensionID"] = ID;
  255. drInventory["MUSER"] = MUSER;
  256. drInventory["MUSERName"] = MUSERNAME;
  257. drInventory["MTIME"] = DateTime.Now;
  258. drInventory["WorkPoint"] = WorkPoint;
  259. drInventory["EATTRIBUTE1"] = oldLotno;
  260. dsSave.Tables[0].Rows.Add(drInventory);
  261. #endregion
  262. #region 条码关联单据
  263. DataRow drInventoryLotDetail = dsSave.Tables[4].NewRow();
  264. drInventoryLotDetail["LotNo"] = LotNO;
  265. drInventoryLotDetail["TransCode"] = "";
  266. drInventoryLotDetail["TransSequence"] = "";
  267. drInventoryLotDetail["MUSER"] = MUSER;
  268. drInventoryLotDetail["MUSERName"] = MUSERNAME;
  269. drInventoryLotDetail["MTIME"] = DateTime.Now;
  270. drInventoryLotDetail["WorkPoint"] = WorkPoint;
  271. dsSave.Tables[4].Rows.Add(drInventoryLotDetail);
  272. #endregion
  273. #region 库存
  274. DataRow drHouseInfo = dsSave.Tables[1].NewRow();
  275. drHouseInfo["ID"] = GetNewid();
  276. drHouseInfo["LotNo"] = LotNO;
  277. drHouseInfo["WarehouseCode"] = StorageCode;
  278. drHouseInfo["LocationCode"] = StackCode;
  279. drHouseInfo["InvCode"] = ItemCode;
  280. drHouseInfo["Quantity"] = Quantity;
  281. drHouseInfo["InDate"] = dataTime;
  282. drHouseInfo["LockQuantity"] = 0;
  283. drHouseInfo["MUSER"] = MUSER;
  284. drHouseInfo["MUSERName"] = MUSERNAME;
  285. drHouseInfo["MTIME"] = DateTime.Now;
  286. drHouseInfo["WorkPoint"] = WorkPoint;
  287. dsSave.Tables[1].Rows.Add(drHouseInfo);
  288. #endregion
  289. #region 记录表
  290. DataRow drHouseLog = dsSave.Tables[2].NewRow();
  291. drHouseLog["ID"] = GetNewid();
  292. drHouseLog["Identification"] = logID;
  293. drHouseLog["LotNo"] = LotNO;
  294. drHouseLog["InvCode"] = ItemCode;
  295. drHouseLog["ToWarehouseCode"] = StorageCode;
  296. drHouseLog["ToLocationCode"] = StackCode;
  297. drHouseLog["Quantity"] = Quantity;
  298. drHouseLog["Lock"] = 0;
  299. drHouseLog["TransType"] = "1";
  300. drHouseLog["BusinessCode"] = "39";
  301. drHouseLog["ERPUpload"] = 0;
  302. drHouseLog["MUSER"] = MUSER;
  303. drHouseLog["MUSERName"] = MUSERNAME;
  304. drHouseLog["MTIME"] = DateTime.Now;
  305. drHouseLog["WorkPoint"] = WorkPoint;
  306. dsSave.Tables[2].Rows.Add(drHouseLog);
  307. #endregion
  308. }
  309. }
  310. SqlHelper.ExecuteDataSet(dsSave);
  311. }
  312. else
  313. {
  314. return "无有效的导入数据。";
  315. }
  316. return "true";
  317. }
  318. catch (Exception ex)
  319. {
  320. return ex.Message;
  321. }
  322. }
  323. public string GetNewid()
  324. {
  325. string sql = "select newid() AS ID";
  326. return Repository().FindTableBySql(sql, null).Rows[0]["ID"].ToString();
  327. }
  328. }
  329. }