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.

365 lines
15 KiB

  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 Newtonsoft.Json.Linq;
  18. namespace NFine.Application.WMS
  19. {
  20. public class YLICSMoPickApp : RepositoryFactory<ICSVendor>
  21. {
  22. public static DataTable Invmes = new DataTable();
  23. //工单备料(工单子件)
  24. public DataTable GetGridJson(string queryJson, ref Pagination jqgridparam)
  25. {
  26. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  27. DataTable dt = new DataTable();
  28. var queryParam = queryJson.ToJObject();
  29. List<DbParameter> parameter = new List<DbParameter>();
  30. string sql = "";
  31. #region [SQL]
  32. sql = @"select a.ID,b.MOCode as Code,a.Sequence as ZSequence,b.Sequence as MSequence,b.Sequence + '-' + a.Sequence AS Sequence,a.EATTRIBUTE2,
  33. a.MTIME,a.MUSERName,a.MUSER,a.WHCode,c.WarehouseName,a.InvCode,d.InvName,d.InvDesc,d.InvStd,d.InvUnit,
  34. case when e.SourceID is null then '' else '' end as IsNew,a.Quantity
  35. ,f.Colspan ,f.ProjectCode ,f.BatchCode ,f.Version ,f.Brand ,f.cFree1 ,f.cFree2 ,f.cFree3 ,f.cFree4
  36. ,f.cFree5 ,f.cFree6 ,f.cFree7 ,f.cFree8 ,f.cFree9 ,f.cFree10,a.IssueQuantity as IssueQuantity
  37. from ICSMOPick a
  38. left
  39. join ICSMO b on a.MODetailID = b.MODetailID and a.WorkPoint = b.WorkPoint and b.MOStatus<>'3'
  40. left join Sys_SRM_Items g on g.F_EnCode='Reduction'
  41. left join ICSWarehouse c on a.WHCode = c.WarehouseCode and a.WorkPoint = c.WorkPoint
  42. inner join ICSInventory d on a.InvCode = d.InvCode and a.WorkPoint = d.WorkPoint
  43. left join ICSMOPickMerge e on a.ID = e.SourceID and a.WorkPoint = e.WorkPoint
  44. left join ICSExtension f on a.ExtensionID = f.ID and a.WorkPoint = f.WorkPoint
  45. where a.EATTRIBUTE1 <> '1'
  46. and SUBSTRING(b.InvCode, 1, CONVERT(int,g.F_Description)) NOT IN (select b.F_ItemCode from Sys_SRM_Items a left join Sys_SRM_ItemsDetail b on a.F_Id=b.F_ItemId
  47. where a.F_EnCode='Reduction')";
  48. sql = string.Format(sql, WorkPoint);
  49. sql = string.Format(sql, DbHelper.GetErpIp(), DbHelper.GetErpName());
  50. #endregion
  51. if (!string.IsNullOrWhiteSpace(queryJson))
  52. {
  53. if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
  54. {
  55. sql += " and b.MOCode like '%" + queryParam["POCode"].ToString() + "%' ";
  56. }
  57. if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
  58. {
  59. sql += " and a.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
  60. }
  61. if (!string.IsNullOrWhiteSpace(queryParam["Mechanism"].ToString()))
  62. {
  63. sql += " and a.EATTRIBUTE2 like '%" + queryParam["Mechanism"].ToString() + "%' ";
  64. }
  65. if (!string.IsNullOrWhiteSpace(queryParam["TimeFrom"].ToString()))
  66. {
  67. sql += " and convert(nvarchar(20),b.CreateDateTime,23) >= '" + queryParam["TimeFrom"].ToString() + "' ";
  68. }
  69. if (!string.IsNullOrWhiteSpace(queryParam["TimeArrive"].ToString()))
  70. {
  71. sql += " and convert(nvarchar(20),b.CreateDateTime,23) <= '" + queryParam["TimeArrive"].ToString() + "' ";
  72. }
  73. }
  74. return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  75. }
  76. public DataTable GetICSMOPickMergeTemp(string ID, string Type)
  77. {
  78. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  79. string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
  80. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  81. ID = string.IsNullOrWhiteSpace(ID) ? "''" : ID.TrimEnd(',');
  82. string sqls = "";
  83. string wheresql = "";
  84. #region 各单据sql
  85. if (Type == "1")
  86. {
  87. #region 工单备料sql
  88. sqls = @" SELECT
  89. c.MOCode as Code,
  90. c.Sequence+'-'+a.Sequence as Sequence,
  91. a.InvCode,
  92. b.InvName,
  93. b.InvStd,
  94. SUM(ISNULL(a.Quantity, 0)) AS iQuantity,
  95. a.WhCode,
  96. b.InvUnit,
  97. a.ExtensionID,
  98. a.EATTRIBUTE2,
  99. f.ProjectCode ,f.BatchCode ,f.Version ,f.Brand ,f.cFree1 ,f.cFree2 ,f.cFree3 ,f.cFree4
  100. ,f.cFree5 ,f.cFree6 ,f.cFree7 ,f.cFree8 ,f.cFree9 ,f.cFree10
  101. FROM
  102. ICSMOPick a
  103. inner join ICSMO c on a.MODetailID=c.MODetailID and a.WorkPoint=c.WorkPoint and c.MOStatus<>'3'
  104. LEFT JOIN ICSINVENTORY b ON a.InvCode = b.InvCode AND a.WorkPoint=b.WorkPoint
  105. LEFT JOIN ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint
  106. WHERE c.MOCode+c.Sequence+'-'+a.Sequence in ({0}) AND a.WorkPoint = '{1}' AND ISNULL(a.Quantity, 0)>ISNULL(a.IssueQuantity, 0) and a.EATTRIBUTE1<>'1'
  107. GROUP BY c.MOCode,a.InvCode,b.InvName,b.InvStd,b.InvUnit,a.WhCode,a.ExtensionID,a.EATTRIBUTE2,c.Sequence+'-'+a.Sequence,f.ProjectCode ,f.BatchCode ,f.Version ,f.Brand ,f.cFree1 ,f.cFree2 ,f.cFree3 ,f.cFree4
  108. ,f.cFree5 ,f.cFree6 ,f.cFree7 ,f.cFree8 ,f.cFree9 ,f.cFree10";
  109. #endregion
  110. }
  111. #endregion
  112. sqls = string.Format(sqls, ID, WorkPoint);
  113. string sql = $@"SELECT row_number() over ( order by c.EATTRIBUTE2,c.InvCode,d.LocationCode) AS rowNo,
  114. c.Code,
  115. c.Sequence,
  116. c.InvCode,
  117. c.InvName,
  118. c.InvStd,
  119. c.InvUnit,
  120. c.iQuantity AS iQuantity,
  121. c.WHCode AS WarehouseCode,
  122. d.LotNO,
  123. ISNULL(d.Quantity, 0) AS QTY,
  124. ISNULL(d.Quantity, 0) AS QTYLeft,
  125. CONVERT(decimal(18,6),0) AS SendQTY,
  126. d.LocationCode AS LocationCode,
  127. CONVERT(varchar(100),d.MTIME, 23) MTIME,
  128. f.QTYTotal QTYTotal,c.ExtensionID,
  129. c.EATTRIBUTE2,
  130. getdate() as NowMTIME
  131. FROM
  132. ({sqls}) c
  133. left join (select d.WarehouseCode,d.Quantity-d.LockQuantity as Quantity,d.INVCode, m.LotNo, d.MTIME,d.LocationCode,m.ExtensionID ,
  134. f.ProjectCode ,f.BatchCode ,f.Version ,f.Brand ,f.cFree1 ,f.cFree2 ,f.cFree3 ,f.cFree4
  135. ,f.cFree5 ,f.cFree6 ,f.cFree7 ,f.cFree8 ,f.cFree9 ,f.cFree10
  136. from ICSWareHouseLotInfo d ,ICSInventoryLot m
  137. LEFT JOIN ICSExtension f on m.ExtensionID=f.ID and m.WorkPoint=f.WorkPoint
  138. where d.LotNo=m.LotNo and d.WorkPoint=m.WorkPoint
  139. and d.WorkPoint='{WorkPoint}' AND d.Quantity-d.LockQuantity>0 AND d.Quantity>0
  140. ) d
  141. on c.InvCode=d.INVCode
  142. AND (LEN(ISNULL(c.WHCode,''))<=0 OR (LEN(ISNULL(c.WHCode,''))>0 AND c.WHCode=d.WarehouseCode))
  143. AND (LEN(ISNULL(c.ProjectCode,''))<=0 OR (LEN(ISNULL(c.ProjectCode,''))>0 AND c.ProjectCode=d.ProjectCode))
  144. AND (LEN(ISNULL(c.BatchCode,''))<=0 OR (LEN(ISNULL(c.BatchCode,''))>0 AND c.BatchCode=d.BatchCode))
  145. AND (LEN(ISNULL(c.Version,''))<=0 OR (LEN(ISNULL(c.Version,''))>0 AND c.Version=d.Version))
  146. AND (LEN(ISNULL(c.Brand,''))<=0 OR (LEN(ISNULL(c.Brand,''))>0 AND c.Brand=d.Brand))
  147. AND (LEN(ISNULL(c.cFree1,''))<=0 OR (LEN(ISNULL(c.cFree1,''))>0 AND c.cFree1=d.cFree1))
  148. AND (LEN(ISNULL(c.cFree2,''))<=0 OR (LEN(ISNULL(c.cFree2,''))>0 AND c.cFree2=d.cFree2))
  149. AND (LEN(ISNULL(c.cFree3,''))<=0 OR (LEN(ISNULL(c.cFree3,''))>0 AND c.cFree3=d.cFree3))
  150. AND (LEN(ISNULL(c.cFree4,''))<=0 OR (LEN(ISNULL(c.cFree4,''))>0 AND c.cFree4=d.cFree4))
  151. AND (LEN(ISNULL(c.cFree5,''))<=0 OR (LEN(ISNULL(c.cFree5,''))>0 AND c.cFree5=d.cFree5))
  152. AND (LEN(ISNULL(c.cFree6,''))<=0 OR (LEN(ISNULL(c.cFree6,''))>0 AND c.cFree6=d.cFree6))
  153. AND (LEN(ISNULL(c.cFree7,''))<=0 OR (LEN(ISNULL(c.cFree7,''))>0 AND c.cFree7=d.cFree7))
  154. AND (LEN(ISNULL(c.cFree8,''))<=0 OR (LEN(ISNULL(c.cFree8,''))>0 AND c.cFree8=d.cFree8))
  155. AND (LEN(ISNULL(c.cFree9,''))<=0 OR (LEN(ISNULL(c.cFree9,''))>0 AND c.cFree9=d.cFree9))
  156. AND (LEN(ISNULL(c.cFree10,''))<=0 OR (LEN(ISNULL(c.cFree10,''))>0 AND c.cFree10=d.cFree10))
  157. LEFT JOIN (SELECT INVCode,WarehouseCode,SUM(Quantity-LockQuantity) AS QTYTotal FROM ICSWareHouseLotInfo WHERE WorkPoint='{WorkPoint}' GROUP BY INVCode,WarehouseCode) f ON c.InvCode=f.INVCode AND c.WhCode=f.WarehouseCode
  158. order by cast( row_number() over ( order by c.EATTRIBUTE2,c.InvCode,d.LocationCode) as int), c.EATTRIBUTE2,c.InvCode,d.LocationCode ";
  159. // sql = string.Format(sql, sqls)1;
  160. var dataset = Repository().FindDataSetBySql(sql);
  161. if (dataset.Tables[0].Rows.Count == 0)
  162. return null;
  163. DataTable table = dataset.Tables[0];
  164. decimal qtyCount = 0;
  165. bool remove = false;
  166. List<int> removeList = new List<int>();
  167. for (int i = 0; i < table.Rows.Count; i++)
  168. {
  169. if (i != 0
  170. && (!table.Rows[i]["InvCode"].ToString().Equals(table.Rows[i - 1]["InvCode"].ToString())
  171. || !table.Rows[i]["Sequence"].ToString().Equals(table.Rows[i - 1]["Sequence"].ToString())
  172. || !table.Rows[i]["Code"].ToString().Equals(table.Rows[i - 1]["Code"].ToString())
  173. || !table.Rows[i]["ExtensionID"].ToString().Equals(table.Rows[i - 1]["ExtensionID"].ToString()))
  174. )
  175. {
  176. qtyCount = 0;
  177. remove = false;
  178. }
  179. if (table.Rows[i]["QTYLeft"].ToString().ToDecimal() == 0 && table.Rows[i]["LotNO"].ToString() != "")
  180. {
  181. table.Rows[i]["LotNO"] = "";
  182. string a = "0.000000";
  183. table.Rows[i]["QTY"] = Convert.ToDecimal(a);
  184. //removeList.Add(i);
  185. continue;
  186. }
  187. if (remove && table.Rows[i]["LotNO"].ToString() != "")
  188. {
  189. removeList.Add(i);
  190. }
  191. else
  192. {
  193. var lotQty = table.Rows[i]["QTYLeft"].ToString().ToDecimal();
  194. var orderQty = table.Rows[i]["iQuantity"].ToString().ToDecimal();
  195. qtyCount += lotQty;
  196. foreach (DataRow dr in table.Rows)
  197. {
  198. if (dr["LotNO"].ToString() == table.Rows[i]["LotNO"].ToString())
  199. {
  200. if (qtyCount > orderQty)
  201. {
  202. dr["QTYLeft"] = Convert.ToDecimal(qtyCount) - Convert.ToDecimal(orderQty);
  203. }
  204. else
  205. {
  206. dr["QTYLeft"] = 0;
  207. }
  208. }
  209. }
  210. if (qtyCount >= orderQty)
  211. {
  212. table.Rows[i]["SendQTY"] = Convert.ToDecimal(lotQty) - (Convert.ToDecimal(qtyCount) - Convert.ToDecimal(orderQty));
  213. remove = true;
  214. }
  215. else
  216. {
  217. table.Rows[i]["SendQTY"] = lotQty;
  218. }
  219. }
  220. }
  221. if (removeList.Count > 0)
  222. {
  223. removeList.Reverse();
  224. foreach (var item in removeList)
  225. {
  226. table.Rows.RemoveAt(item);
  227. }
  228. }
  229. try
  230. {
  231. DataRow [] dss= table.Select("LotNO=''");
  232. foreach (var item in dss)
  233. {
  234. DataRow[] dsss = table.Select("Code='"+ item["Code"].ToString()+ "' and Sequence='"+ item["Sequence"].ToString() + "' and LotNO <> '' ");
  235. if (dsss!=null && dsss.Length>0)
  236. {
  237. table.Rows.Remove(item);
  238. }
  239. }
  240. for (int i = 0; i < table.Rows.Count; i++)
  241. {
  242. table.Rows[i]["rowNo"] = i+1;
  243. }
  244. var result = ConvertCellToString(table);
  245. if (Invmes.Rows.Count > 0)
  246. {
  247. result.Merge(Invmes, false);
  248. }
  249. return result;
  250. }
  251. catch (Exception ex)
  252. {
  253. throw new Exception(ex.Message);
  254. }
  255. }
  256. public DataTable ConvertCellToString(DataTable data)
  257. {
  258. DataTable dtCloned = data.Clone();
  259. foreach (DataColumn col in dtCloned.Columns)
  260. {
  261. col.DataType = typeof(string);
  262. }
  263. foreach (DataRow row in data.Rows)
  264. {
  265. DataRow newrow = dtCloned.NewRow();
  266. foreach (DataColumn column in dtCloned.Columns)
  267. {
  268. newrow[column.ColumnName] = row[column.ColumnName].ToString();
  269. }
  270. dtCloned.Rows.Add(newrow);
  271. }
  272. return dtCloned;
  273. }
  274. public string CheckQty(string objArr)
  275. {
  276. objArr = objArr.TrimEnd(',');
  277. string sql = @"select * from ICSMOPickMerge a
  278. left join ICSMOPick b on a.SourceID=b.ID and a.WorkPoint=b.WorkPoint
  279. left join ICSMO c on b.MODetailID=c.MODetailID and b.WorkPoint=c.WorkPoint and c.MOStatus<>'3'
  280. where MOCode in ({0})";
  281. sql = string.Format(sql, objArr);
  282. DataTable dt = SqlHelper.GetDataTableBySql(sql);
  283. if (dt != null && dt.Rows.Count > 0)
  284. {
  285. return "1";
  286. }
  287. return "0";
  288. }
  289. public object SeachPickingListType(string Type)
  290. {
  291. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  292. string sql = @" select b.F_EnabledMark as flag from Sys_SRM_Items a left join Sys_SRM_ItemsDetail b on a.F_Id=b.F_ItemId
  293. where a.F_EnCode='PL00001' and b.F_ItemCode='{0}' order by cast(b.F_SortCode as int) asc";
  294. sql = string.Format(sql, Type);
  295. object flag = SqlHelper.ExecuteScalar(sql);
  296. return flag;
  297. }
  298. }
  299. }