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.

1695 lines
95 KiB

3 weeks ago
  1. using Newtonsoft.Json.Linq;
  2. using Newtonsoft.Json;
  3. using NFine.Code;
  4. using NFine.Data.Extensions;
  5. using NFine.Domain._03_Entity.SRM;
  6. using NFine.Repository;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Configuration;
  10. using System.Data.Common;
  11. using System.Data;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using NFine.Application.Entity;
  18. using NFine.Application.Models;
  19. using NFine.Application.WMS;
  20. using System.Web.UI.WebControls;
  21. using System.Net.NetworkInformation;
  22. using System.Collections;
  23. using System.Xml;
  24. using System.Data.SqlClient;
  25. using System.Reflection.Emit;
  26. namespace NFine.Application.MFWMS
  27. {
  28. public class ICSMOIssueSendBackApp : RepositoryFactory<ICSVendor>
  29. {
  30. public static DataTable Invmes = new DataTable();
  31. /// <summary>
  32. /// 产成品退库 汇总查询
  33. /// </summary>
  34. /// <param name="queryJson"></param>
  35. /// <param name="jqgridparam"></param>
  36. /// <returns></returns>
  37. public DataTable GetMOIssueSendBackApplyNeg(string queryJson, ref Pagination jqgridparam)
  38. {
  39. //站点信息
  40. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  41. DataTable dt = new DataTable();
  42. List<DbParameter> parameter = new List<DbParameter>();
  43. var queryParam = queryJson.ToJObject();
  44. object Figure = GetDecimalDigits();
  45. #region [SQL]
  46. // 同步过来的红字(退库) 产成品入库
  47. string sql = @" select * from (select a.RCVCode, a.MUSERName,a.MTIME,a.Status,a.Type ,b.WarehouseCode as WHCode,b.WarehouseName as WHName
  48. from ICSManufactureReceive a
  49. join ICSWarehouse b on a.WHCode = b.WarehouseCode and a.WorkPoint = b.WorkPoint
  50. join ICSInventory c on a.InvCode = c.InvCode and a.WorkPoint = b.WorkPoint
  51. where a.Type = '2' and ISNULL(a.SourceCode,'') != '' ";
  52. sql = string.Format(sql, Figure);
  53. sql = string.Format(sql, DbHelper.GetErpIp(), DbHelper.GetErpName());
  54. #endregion
  55. if (!string.IsNullOrWhiteSpace(queryJson))
  56. {
  57. if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
  58. {
  59. sql += " and a.RCVCode like '%" + queryParam["POCode"].ToString() + "%' ";
  60. }
  61. if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
  62. {
  63. sql += " and a.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
  64. }
  65. if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
  66. {
  67. sql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
  68. }
  69. if (!string.IsNullOrWhiteSpace(queryParam["WHCode"].ToString()))
  70. {
  71. sql += " and b.WarehouseCode like '%" + queryParam["WHCode"].ToString() + "%' ";
  72. }
  73. if (!string.IsNullOrWhiteSpace(queryParam["WHName"].ToString()))
  74. {
  75. sql += " and b.WarehouseName like '%" + queryParam["WHName"].ToString() + "%' ";
  76. }
  77. if (!string.IsNullOrWhiteSpace(queryParam["FromTime"].ToString()))
  78. {
  79. sql += " and a.CreateDateTime >= '" + queryParam["FromTime"].ToString() + "' ";
  80. }
  81. if (!string.IsNullOrWhiteSpace(queryParam["ToTime"].ToString()))
  82. {
  83. sql += " and a.CreateDateTime <= '" + queryParam["ToTime"].ToString() + "' ";
  84. }
  85. if (!string.IsNullOrWhiteSpace(queryParam["F_Type"].ToString()))
  86. {
  87. var status = queryParam["F_Type"].ToString();
  88. if (status == "1")
  89. {
  90. sql += " and a.Status in ('2','3') ";
  91. }
  92. else if (status == "2")
  93. {
  94. sql += " and a.Status = '1' ";
  95. }
  96. }
  97. }
  98. if (NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode != "admin")
  99. {
  100. sql += " and a.WorkPoint='" + NFine.Code.OperatorProvider.Provider.GetCurrent().Location + "'";
  101. }
  102. sql += @" GROUP BY a.RCVCode, a.MUSERName,a.MTIME,a.Status,a.Type,b.WarehouseCode,b.WarehouseName ) t
  103. where 1=1 ";
  104. return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  105. }
  106. public object GetDecimalDigits()
  107. {
  108. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location.TrimEnd(',');
  109. try
  110. {
  111. string sql = string.Empty;
  112. sql = @"select Figure from ICSConfiguration where Code='Figure001' and Enable='1' and WorkPoint='" + WorkPoint + "'";
  113. object Figure = SqlHelper.ExecuteScalar(sql);
  114. return Figure;
  115. }
  116. catch (Exception ex)
  117. {
  118. throw new Exception(ex.Message.ToString());
  119. }
  120. }
  121. /// <summary>
  122. /// 订单备料明细查询
  123. /// </summary>
  124. /// <param name="jqgridparam"></param>
  125. /// <returns></returns>
  126. public DataTable GetMOIssueApplyNegDetail(string RCVCode, ref Pagination jqgridparam)
  127. {
  128. DataTable dt = new DataTable();
  129. //object Figure = GetDecimalDigits();
  130. List<DbParameter> parameter = new List<DbParameter>();
  131. string sql = @"SELECT a.ID,a.RCVCode,a.Sequence,a.SourceSequence,a.SourceCode,a.InvCode,a.Quantity,a.Amount,a.RCVQuantity,a.WHCode,e.WarehouseName as WHName,a.Type
  132. ,a.ExtensionID,a.MUSER,a.MUSERName,a.MTIME,a.WorkPoint,b.InvName,a.CreateDateTime,a.CreatePerson
  133. ,f.Colspan ,f.ProjectCode ,f.BatchCode ,f.Version ,f.Brand ,f.cFree1 ,f.cFree2 ,f.cFree3 ,f.cFree4
  134. ,f.cFree5 ,f.cFree6 ,f.cFree7 ,f.cFree8 ,f.cFree9 ,f.cFree10
  135. ,a.EATTRIBUTE1,a.EATTRIBUTE2,a.EATTRIBUTE3,a.EATTRIBUTE4,a.EATTRIBUTE5,a.EATTRIBUTE6,a.EATTRIBUTE7,a.EATTRIBUTE8,a.EATTRIBUTE9,a.EATTRIBUTE10
  136. from ICSManufactureReceive a
  137. left join ICSInventory b on a.InvCode = b.InvCode and a.WorkPoint = b.WorkPoint
  138. left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint
  139. left join ICSWarehouse e on a.WHCode = e.WarehouseCode and a.WorkPoint=e.WorkPoint
  140. where a.RCVCode = '" + RCVCode + "' ";
  141. //sql = string.Format(sql, Figure);
  142. DataTable dttest = Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  143. return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  144. }
  145. public void ClearTemp()
  146. {
  147. Invmes.Rows.Clear();
  148. }
  149. /// <summary>
  150. /// 弃审
  151. /// </summary>
  152. /// <param name="keyValue"></param>
  153. /// <returns></returns>
  154. public string ICSMOIssueSendBackAuditRollback(string keyValue)
  155. {
  156. string MUSER = OperatorProvider.Provider.GetCurrent().UserCode;
  157. string MUSERNAME = OperatorProvider.Provider.GetCurrent().UserName;
  158. string WorkPoint = OperatorProvider.Provider.GetCurrent().Location;
  159. var Dates = DateTime.Now;
  160. var sql = string.Empty;
  161. var Batchids = Guid.NewGuid().ToString();
  162. var sqlBuilder = new StringBuilder();
  163. try
  164. {
  165. // 优先根据Batchids去找修改记录 完成弃审反向修改库存 旧数据ICSManufactureReceive没有EATTRIBUTE1记录批次id,所以保留旧逻辑
  166. string bidInfosql = @"select a.TransCode,a.TransSequence,a.LotNo,a.InvCode,a.Quantity,a.FromWarehouseCode,a.FromLocationCode from ICSWareHouseLotInfoLog a
  167. join (select EATTRIBUTE1 from ICSManufactureReceive where RCVCode in ({0}) group by EATTRIBUTE1) b on a.Identification = b.EATTRIBUTE1
  168. where a.WorkPoint='{1}'";
  169. bidInfosql = string.Format(bidInfosql, keyValue, WorkPoint);
  170. DataTable bidInfosqldt = SqlHelper.GetDataTableBySql(bidInfosql);
  171. if (bidInfosqldt.Rows.Count > 0)
  172. {
  173. foreach (DataRow grMFitem in bidInfosqldt.Rows)
  174. {
  175. var qty = grMFitem["Quantity"].ToDecimal();
  176. // 1. 库存更新
  177. sqlBuilder.AppendFormat(
  178. @"UPDATE ICSWareHouseLotInfo
  179. SET Quantity = Quantity + {0},
  180. MUSER = '{1}',
  181. MUSERName = '{2}',
  182. MTIME = '{3}'
  183. WHERE LotNo = '{4}'; ",
  184. -(qty),
  185. MUSER.Replace("'", "''"),
  186. MUSERNAME.Replace("'", "''"),
  187. Dates.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  188. grMFitem["LotNo"].ToString().Replace("'", "''")
  189. );
  190. // 2. 库存日志
  191. sqlBuilder.AppendFormat(
  192. @"INSERT INTO ICSWareHouseLotInfoLog (ID, Identification, TransCode, TransSequence,
  193. FromWarehouseCode, FromLocationCode, LotNo, [Lock], TransType, BusinessCode,
  194. Quantity, InvCode, MUSER, MUSERName, MTIME, WorkPoint)
  195. VALUES (NEWID(), '{11}', '{0}', '{1}', '{2}', '{3}', '{4}', 0, '17', '75',
  196. {5}, '{6}', '{7}', '{8}', '{9}', '{10}'); ",
  197. grMFitem["TransCode"].ToString().Replace("'", "''"), // 0: TransCode
  198. grMFitem["TransSequence"].ToString().Replace("'", "''"), // 1: TransSequence
  199. grMFitem["FromWarehouseCode"].ToString().Replace("'", "''"), // 2: FromWarehouseCode
  200. grMFitem["FromLocationCode"].ToString().Replace("'", "''"), // 3: FromLocationCode
  201. grMFitem["LotNo"].ToString().Replace("'", "''"), // 4: LotNo
  202. -(qty), // 5: 数量
  203. grMFitem["InvCode"].ToString().Replace("'", "''"), // 6: InvCode
  204. MUSER.Replace("'", "''"), // 7: MUSER
  205. MUSERNAME.Replace("'", "''"), // 8: MUSERName
  206. Dates.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"), // 9: MTIME
  207. WorkPoint.Replace("'", "''"), // 10: WorkPoint
  208. Batchids // 11: Identification
  209. );
  210. }
  211. }
  212. else
  213. {
  214. //增加库存逆向记录
  215. #region 入库表修改库存
  216. //选中单据根据物料分组
  217. string groupManufactureReceivesql = @"select a.RCVCode,a.Sequence,a.InvCode,Sum(a.Quantity) as InvQty,status,WHCode,e.BatchCode
  218. from ICSManufactureReceive a
  219. inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint --and c.InvIQC='1'
  220. inner JOIN ICSExtension e on a.ExtensionID = e.ID and a.WorkPoint = e.WorkPoint
  221. where a.RCVCode in ({0}) and a.WorkPoint='{1}'
  222. group by a.RCVCode,a.Sequence,a.InvCode,status,e.BatchCode,WHCode";
  223. groupManufactureReceivesql = string.Format(groupManufactureReceivesql, keyValue, WorkPoint);
  224. DataTable groupManufactureReceivesqldt = SqlHelper.GetDataTableBySql(groupManufactureReceivesql);
  225. //关联库存表 单个或多个库存 库存数量先进先出扣除
  226. sql = @"
  227. select a.RCVCode,a.Sequence,a.InvCode,d.LotNo,g.Quantity,g.InDate,g.ID,g.WarehouseCode,g.LocationCode,a.Quantity as RCVQuantity,e.BatchCode
  228. from ICSManufactureReceive a
  229. inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint --and c.InvIQC='1'
  230. inner JOIN ICSInventoryLot d ON a.ExtensionID=d.ExtensionID and a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint and d.type<>'101' --and d.Type = '13'
  231. inner JOIN ICSExtension e on a.ExtensionID = e.ID and ISNULL(e.BatchCode,'') != '' and a.WorkPoint = e.WorkPoint
  232. inner JOIN dbo.ICSWareHouseLotInfo g ON g.LotNo=d.LotNo and a.WHCode = g.WarehouseCode and g.WorkPoint=a.WorkPoint
  233. where a.RCVCode in ({0}) and a.WorkPoint='{1}' order by e.BatchCode asc";
  234. sql = string.Format(sql, keyValue, WorkPoint);
  235. DataTable MfRetables = SqlHelper.GetDataTableBySql(sql);
  236. if (MfRetables.Rows.Count == 0)
  237. {
  238. throw new Exception("产成品单据库存不足,请先检查库存");
  239. }
  240. foreach (DataRow grMFitem in groupManufactureReceivesqldt.Rows)
  241. {
  242. //检查状态
  243. if (grMFitem["status"].ToString() != "2")
  244. {
  245. throw new Exception("选中行数据非审核状态,弃审失败。");
  246. }
  247. //var First = MfRetables.Select("RCVCode = '" + grMFitem["RCVCode"].ToString() + "'and Sequence='" + grMFitem["Sequence"].ToString() + "' and LotNO <> '' and BatchCode = '" + grMFitem["BatchCode"].ToString() + "'").FirstOrDefault();
  248. //if (First == null)
  249. //{
  250. // throw new Exception($"未找到对应的库存记录(RCVCode: {grMFitem["RCVCode"]}, Sequence: {grMFitem["Sequence"]}, BatchCode: {grMFitem["BatchCode"]})");
  251. //}
  252. // 获取库存记录
  253. var filter = $"RCVCode = '{grMFitem["RCVCode"]}' and Sequence='{grMFitem["Sequence"]}'and LotNO <> '' and WarehouseCode = '{grMFitem["WHCode"]}' and BatchCode = '{grMFitem["BatchCode"]}'";
  254. DataRow First = MfRetables.Select(filter).FirstOrDefault();
  255. if (First == null)
  256. {
  257. throw new Exception($"未找到库存记录(RCVCode: {grMFitem["RCVCode"]}, " +
  258. $"Sequence: {grMFitem["Sequence"]},仓库编码: {grMFitem["WHCode"]})");
  259. }
  260. #region 库存表修改 注释
  261. //库存表修改 根据退库表的数量 按先进先出 扣除库存
  262. //var LotEntity = MsSqlData.Get<ICSWareHouseLotInfo>(First["ID"].ToString());
  263. //if (LotEntity != null)
  264. //{
  265. // //此处改为扣除数量后的剩余数量
  266. // LotEntity.Quantity = LotEntity.Quantity + grMFitem["InvQty"].ToDecimal();
  267. // LotEntity.MUSER = MUSER;
  268. // LotEntity.MUSERName = MUSERNAME;
  269. // LotEntity.MTIME = dateNow.ToString("yyyy-MM-dd HH:mm:ss");
  270. // var result2 = MsSqlData.Update<ICSWareHouseLotInfo>(LotEntity);
  271. // if (!result2)
  272. // {
  273. // returnValue = "产成品库存表修改失败";
  274. // }
  275. //}
  276. #endregion
  277. // 1. 库存更新
  278. sqlBuilder.AppendFormat(
  279. @"UPDATE ICSWareHouseLotInfo
  280. SET Quantity = Quantity + {0},
  281. MUSER = '{1}',
  282. MUSERName = '{2}',
  283. MTIME = '{3}'
  284. WHERE ID = '{4}'; ",
  285. grMFitem["InvQty"].ToDecimal(),
  286. MUSER.Replace("'", "''"),
  287. MUSERNAME.Replace("'", "''"),
  288. Dates.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  289. First["ID"].ToString().Replace("'", "''")
  290. );
  291. // 2. 库存日志
  292. sqlBuilder.AppendFormat(
  293. @"INSERT INTO ICSWareHouseLotInfoLog (ID, Identification, TransCode, TransSequence,
  294. FromWarehouseCode, FromLocationCode, LotNo, [Lock], TransType, BusinessCode,
  295. Quantity, InvCode, MUSER, MUSERName, MTIME, WorkPoint)
  296. VALUES (NEWID(), '{11}', '{0}', '{1}', '{2}', '{3}', '{4}', 0, '17', '75',
  297. {5}, '{6}', '{7}', '{8}', '{9}', '{10}'); ",
  298. First["RCVCode"].ToString().Replace("'", "''"), // 0: TransCode
  299. First["Sequence"].ToString().Replace("'", "''"), // 1: TransSequence
  300. First["WarehouseCode"].ToString().Replace("'", "''"), // 2: FromWarehouseCode
  301. First["LocationCode"].ToString().Replace("'", "''"), // 3: FromLocationCode
  302. First["LotNo"].ToString().Replace("'", "''"), // 4: LotNo
  303. -(grMFitem["InvQty"].ToDecimal()), // 5: 数量
  304. First["InvCode"].ToString().Replace("'", "''"), // 6: InvCode
  305. MUSER.Replace("'", "''"), // 7: MUSER
  306. MUSERNAME.Replace("'", "''"), // 8: MUSERName
  307. Dates.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"), // 9: MTIME
  308. WorkPoint.Replace("'", "''"), // 10: WorkPoint
  309. Batchids // 11: Identification
  310. );
  311. #region 库存日志记录 注释
  312. //库存日志记录 产成品退库
  313. //var entity = new ICSWareHouseLotInfoLog();
  314. ////ConvertExt.Mapping<ICSWareHouseLotInfoLogEdit, ICSWareHouseLotInfoLog>(model, entity);
  315. //entity.ID = Guid.NewGuid().ToString();
  316. //entity.Identification = Guid.NewGuid().ToString();
  317. //entity.TransCode = MfRetables.Rows[0]["RCVCode"].ToString();
  318. //entity.TransSequence = MfRetables.Rows[0]["Sequence"].ToString();
  319. //entity.FromWarehouseCode = MfRetables.Rows[0]["WarehouseCode"].ToString();
  320. //entity.FromLocationCode = MfRetables.Rows[0]["LocationCode"].ToString();
  321. //entity.LotNo = MfRetables.Rows[0]["LotNo"].ToString();
  322. //entity.Lock = false;
  323. //entity.TransType = "17";//铭锋产成品退库
  324. //entity.BusinessCode = "75";//铭锋产成品退库
  325. //entity.Quantity = -grMFitem["InvQty"].ToDecimal();
  326. //entity.InvCode = MfRetables.Rows[0]["InvCode"].ToString();
  327. //entity.MUSER = MUSER;
  328. //entity.MUSERName = MUSERNAME;
  329. //entity.MTIME = Dates;
  330. //entity.WorkPoint = WorkPoint;
  331. //var result = MsSqlData.Insert<ICSWareHouseLotInfoLog>(entity);
  332. //if (!result)
  333. //{
  334. // returnValue = "新增库存日志记录失败";
  335. //}
  336. #endregion
  337. }
  338. #endregion
  339. #region 材料出库表修改库存
  340. // 红字生产入库单ICSManufactureReceive 会生成对应的红字生成领料单 也就是ICSMOIssue材料出库表
  341. //选中的物料 计算扣除数量
  342. string groupmoIssuesql = @"select a.InvCode,Sum(a.Quantity) as InvQty,a.WHCode,e.BatchCode
  343. from ICSMOIssue a
  344. inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
  345. inner JOIN ICSExtension e on a.ExtensionID = e.ID and a.WorkPoint = e.WorkPoint
  346. where a.EATTRIBUTE1 in ({0}) and a.WorkPoint='{1}'
  347. group by a.InvCode,a.WHCode,e.BatchCode";
  348. groupmoIssuesql = string.Format(groupmoIssuesql, keyValue, WorkPoint);
  349. DataTable groupmoIssuesqldt = SqlHelper.GetDataTableBySql(groupmoIssuesql);
  350. //铭锋 因为客户在ERP操作了入库 但在WMS操作退库 没有找到条码就生成一个条码
  351. if (groupmoIssuesqldt.Rows.Count <= 0)
  352. {
  353. //returnValue = "未找到对应的材料出库单。";
  354. throw new Exception("未找到对应的材料出库单。");
  355. }
  356. //关联库存表 单个或多个库存 库存数量不够时先进先出扣除
  357. sql = @"
  358. select a.IssueCode,a.Sequence,a.InvCode,d.LotNo,g.Quantity,g.InDate,g.ID,g.WarehouseCode,g.LocationCode,e.BatchCode
  359. from ICSMOIssue a
  360. inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint --and c.InvIQC='1'
  361. inner JOIN ICSInventoryLot d ON a.ExtensionID=d.ExtensionID and a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint -- and d.type<>'101' and d.Type = '13'
  362. inner JOIN ICSExtension e on a.ExtensionID = e.ID and ISNULL(e.BatchCode,'') != '' and a.WorkPoint = e.WorkPoint
  363. inner JOIN dbo.ICSWareHouseLotInfo g ON g.LotNo=d.LotNo and a.WHCode = g.WarehouseCode and g.WorkPoint=a.WorkPoint
  364. where a.EATTRIBUTE1 in ({0}) and a.WorkPoint='{1}' order by e.BatchCode asc";
  365. sql = string.Format(sql, keyValue, WorkPoint);
  366. DataTable tables = SqlHelper.GetDataTableBySql(sql);
  367. if (tables.Rows.Count <= 0)
  368. {
  369. //returnValue = "物料未找到条码库存,请联系管理员补充资料。";
  370. throw new Exception("物料未找到条码库存,请联系管理员补充资料。");
  371. }
  372. //材料出库数据 物料循环 物料总数量扣除
  373. foreach (DataRow groupdr in groupmoIssuesqldt.Rows)
  374. {
  375. string InvCode = groupdr["InvCode"].ToString().Replace("'", "''");
  376. string WhCode = groupdr["WHCode"].ToString().Replace("'", "''");
  377. //同一产品需增加的数量 退库为负数
  378. var InvQty = decimal.Parse(groupdr["InvQty"].ToString());
  379. DataRow[] tableRows = tables.Select("InvCode = '" + groupdr["InvCode"].ToString() + "' and WarehouseCode = '" + groupdr["WHCode"].ToString() + "' and BatchCode = '" + groupdr["BatchCode"].ToString() + "'");
  380. if (tableRows.Length <= 0)
  381. {
  382. throw new Exception($"物料编码:{InvCode},仓库编码:{WhCode},批次:{groupdr["BatchCode"].ToString()},未找到条码库存。");
  383. }
  384. //先进先出第一个条码 增加数量
  385. DataRow MoIssueLot = tableRows[0];
  386. string lotId = MoIssueLot["ID"].ToString().Replace("'", "''");
  387. // 条码当前数量
  388. var Quantity = decimal.Parse(MoIssueLot["Quantity"].ToString());
  389. // 本次修改后的数量
  390. var LogQty = Quantity + InvQty;
  391. // 1. 库存更新
  392. sqlBuilder.AppendFormat(
  393. @"UPDATE ICSWareHouseLotInfo
  394. SET Quantity = {0},
  395. MUSER = '{1}',
  396. MUSERName = '{2}',
  397. MTIME = '{3}'
  398. WHERE ID = '{4}'; ",
  399. LogQty,
  400. MUSER.Replace("'", "''"),
  401. MUSERNAME.Replace("'", "''"),
  402. Dates.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  403. lotId
  404. );
  405. // 2. 库存日志
  406. sqlBuilder.AppendFormat(
  407. @"INSERT INTO ICSWareHouseLotInfoLog (ID, Identification, TransCode, TransSequence,
  408. FromWarehouseCode, FromLocationCode, LotNo, [Lock], TransType, BusinessCode,
  409. Quantity, InvCode, MUSER, MUSERName, MTIME, WorkPoint)
  410. VALUES (NEWID(), '{11}', '{0}', '{1}', '{2}', '{3}', '{4}', 0, '17', '75',
  411. {5}, '{6}', '{7}', '{8}', '{9}', '{10}'); ",
  412. MoIssueLot["IssueCode"].ToString().Replace("'", "''"),
  413. MoIssueLot["Sequence"].ToString().Replace("'", "''"),
  414. MoIssueLot["WarehouseCode"].ToString().Replace("'", "''"),
  415. MoIssueLot["LocationCode"].ToString().Replace("'", "''"),
  416. MoIssueLot["LotNo"].ToString().Replace("'", "''"),
  417. -InvQty,
  418. InvCode,
  419. MUSER.Replace("'", "''"),
  420. MUSERNAME.Replace("'", "''"),
  421. Dates.ToString("yyyy-MM-dd HH:mm:ss.fff").Replace("'", "''"),
  422. WorkPoint.Replace("'", "''"),
  423. Batchids
  424. );
  425. #region 库存日志记录 库存表修改 注释
  426. //库存表修改 根据退库表的数量 按先进先出 扣除库存
  427. //var LotEntity = MsSqlData.Get<ICSWareHouseLotInfo>(MoIssueLot["ID"].ToString());
  428. //if (LotEntity != null)
  429. //{
  430. // LotEntity.Quantity = LogQty;
  431. // LotEntity.MUSER = MUSER;
  432. // LotEntity.MUSERName = MUSERNAME;
  433. // LotEntity.MTIME = dateNow.ToString("yyyy-MM-dd HH:mm:ss");
  434. // var result2 = MsSqlData.Update<ICSWareHouseLotInfo>(LotEntity);
  435. // if (!result2)
  436. // {
  437. // returnValue = "库存表修改失败";
  438. // }
  439. //}
  440. //库存日志记录
  441. //var entity = new ICSWareHouseLotInfoLog();
  442. ////ConvertExt.Mapping<ICSWareHouseLotInfoLogEdit, ICSWareHouseLotInfoLog>(model, entity);
  443. //entity.ID = Guid.NewGuid().ToString();
  444. //entity.Identification = Guid.NewGuid().ToString();
  445. //entity.TransCode = MoIssueLot["IssueCode"].ToString();
  446. //entity.TransSequence = MoIssueLot["Sequence"].ToString();
  447. //entity.FromWarehouseCode = MoIssueLot["WarehouseCode"].ToString();
  448. //entity.FromLocationCode = MoIssueLot["LocationCode"].ToString();
  449. //entity.LotNo = MoIssueLot["LotNo"].ToString();
  450. //entity.Lock = false;
  451. //entity.TransType = "17";//铭锋产成品退库
  452. //entity.BusinessCode = "75";//铭锋产成品退库
  453. //entity.Quantity = -InvQty;
  454. //entity.InvCode = groupdr["InvCode"].ToString();
  455. //entity.MUSER = MUSER;
  456. //entity.MUSERName = MUSERNAME;
  457. //entity.MTIME = Dates;
  458. //entity.WorkPoint = WorkPoint;
  459. //var result = MsSqlData.Insert<ICSWareHouseLotInfoLog>(entity);
  460. //if (!result)
  461. //{
  462. // returnValue = "新增库存日志记录失败";
  463. //}
  464. #endregion
  465. }
  466. #endregion
  467. }
  468. //修改审核状态
  469. // 1. 材料出库表状态
  470. sqlBuilder.AppendFormat(
  471. @"UPDATE ICSMOIssue SET Status = '1'
  472. WHERE EATTRIBUTE1 IN ({0}) AND WorkPoint = '{1}' AND Status = '2';
  473. IF @@ROWCOUNT = 0 RAISERROR('', 16, 1); ",
  474. keyValue, WorkPoint
  475. );
  476. // 2. 产成品入库表状态
  477. sqlBuilder.AppendFormat(
  478. @"UPDATE ICSManufactureReceive SET Status = '1',EATTRIBUTE1 = '{2}'
  479. WHERE RCVCode IN ({0}) AND WorkPoint = '{1}' AND Status = '2';
  480. IF @@ROWCOUNT = 0 RAISERROR('', 16, 1); ",
  481. keyValue, WorkPoint,Batchids
  482. );
  483. // 3. 修改工单入库数量
  484. //工单表:ICSMO表RCVQuantity字段 ----减去产红字产成品入库单数量
  485. //工单子件表:ICSMOPICK表IssueQuantity字段 ----- 加上红字材料出库单的数量
  486. sqlBuilder.AppendFormat(
  487. @"UPDATE ICSMO
  488. SET RCVQuantity = RCVQuantity + b.SumQuantity
  489. FROM ICSMO a
  490. JOIN (
  491. SELECT SourceCode, SourceSequence, SUM(Quantity) AS SumQuantity
  492. FROM ICSManufactureReceive
  493. WHERE RCVCode IN ({0}) AND WorkPoint = '{1}'
  494. GROUP BY SourceCode, SourceSequence
  495. ) b ON b.SourceCode = a.MOCode AND b.SourceSequence = a.Sequence;
  496. IF @@ROWCOUNT = 0 RAISERROR('', 16, 1); ",
  497. keyValue, WorkPoint
  498. );
  499. // 4. 领料单数量
  500. sqlBuilder.AppendFormat(
  501. @"UPDATE ICSMOPick
  502. SET IssueQuantity = IssueQuantity + b.SumQuantity
  503. FROM ICSMOPick a
  504. JOIN (
  505. SELECT PickID, SUM(Quantity) AS SumQuantity
  506. FROM ICSMOIssue
  507. WHERE EATTRIBUTE1 IN ({0}) AND WorkPoint = '{1}'
  508. GROUP BY PickID
  509. ) b ON a.PickID = b.PickID;
  510. IF @@ROWCOUNT = 0 RAISERROR('', 16, 1); ",
  511. keyValue, WorkPoint
  512. );
  513. var count = SqlHelper.CmdExecuteNonQueryLi(sqlBuilder.ToString());
  514. #region 废弃注释
  515. // string IChecksql = @"select EATTRIBUTE1
  516. // from ICSMOIssue a
  517. // where a.EATTRIBUTE1 in ({0}) and a.WorkPoint='{1}' and Status = '2'";
  518. // IChecksql = string.Format(IChecksql, keyValue, WorkPoint);
  519. // DataTable IChecksqldt = SqlHelper.GetDataTableBySql(IChecksql);
  520. // //审核完成 修改材料出库表状态
  521. // if (IChecksqldt.Rows.Count > 0)
  522. // {
  523. // string sql1 = @"
  524. //update ICSMOIssue set Status = '1' where EATTRIBUTE1 in (" + keyValue + ") and WorkPoint ='" + WorkPoint + "' and Status = '2'";
  525. // sql1 = string.Format(sql1, keyValue, WorkPoint);
  526. // int Figure1 = SqlHelper.ExecuteNonQuery(sql1);
  527. // if (Figure1 <= 0)
  528. // {
  529. // //returnValue = "修改材料出库表状态失败!" + sql1;
  530. // throw new Exception("修改材料出库表状态失败!" + sql1);
  531. // }
  532. // }
  533. // string IChecksql2 = @"select RCVCode from ICSManufactureReceive a
  534. // where a.RCVCode in ({0}) and a.WorkPoint='{1}' and Status = '2'";
  535. // IChecksql2 = string.Format(IChecksql2, keyValue, WorkPoint);
  536. // DataTable IChecksqldt2 = SqlHelper.GetDataTableBySql(IChecksql2);
  537. // //修改产成品入库表ICSManufactureReceive状态
  538. // if (IChecksqldt2.Rows.Count > 0)
  539. // {
  540. // string sql2 = @"
  541. //update ICSManufactureReceive set Status = '1' where RCVCode in (" + keyValue + ") and WorkPoint ='" + WorkPoint + "' and Status = '2'";
  542. // sql2 = string.Format(sql2, keyValue, WorkPoint);
  543. // int Figure1 = SqlHelper.ExecuteNonQuery(sql2);
  544. // if (Figure1 <= 0)
  545. // {
  546. // //returnValue = "修改产成品入库表状态失败!" + sql2;
  547. // throw new Exception("修改产成品入库表状态失败!" + sql2);
  548. // }
  549. // }
  550. //工单表:ICSMO表RCVQuantity字段 ----减去产红字产成品入库单数量
  551. //工单子件表:ICSMOPICK表IssueQuantity字段 ----- 加上红字材料出库单的数量
  552. //string IMosql = @"select a.Quantity,b.RCVQuantity from ICSManufactureReceive a
  553. // join ICSMO b on a.SourceCode = b.MOCode and a.SourceSequence = b.Sequence
  554. // where a.RCVCode in ({0}) and a.WorkPoint='{1}' ";
  555. //IMosql = string.Format(IMosql, keyValue, WorkPoint);
  556. //DataTable IMosqldt = SqlHelper.GetDataTableBySql(IMosql);
  557. ////审核完成 修改工单入库数量 弃审数量加
  558. //if (IMosqldt.Rows.Count > 0)
  559. //{
  560. // string sql1 = @"
  561. // update ICSMO
  562. // set RCVQuantity = RCVQuantity + SumQuantity
  563. // from ICSMO a join (
  564. // select SUM(a.Quantity) as SumQuantity,a.SourceCode,a.SourceSequence from ICSManufactureReceive a
  565. // where a.RCVCode in ({0}) and a.WorkPoint='{1}'
  566. // group by a.SourceCode,a.SourceSequence,a.InvCode
  567. // ) b on b.SourceCode = a.MOCode and b.SourceSequence = a.Sequence";
  568. // sql1 = string.Format(sql1, keyValue, WorkPoint);
  569. // int Figure1 = SqlHelper.ExecuteNonQuery(sql1);
  570. // if (Figure1 <= 0)
  571. // {
  572. // //returnValue = "修改材料出库表状态失败!" + sql1;
  573. // throw new Exception("修改工单数量失败!" + sql1);
  574. // }
  575. //}
  576. // string IMopciksql = @"select a.IssueQuantity,b.Quantity from ICSMOPick a
  577. // join ICSMOIssue b on a.PickID = b.PickID
  578. // where b.EATTRIBUTE1 in ({0}) and b.WorkPoint='{1}'";
  579. // IMopciksql = string.Format(IMopciksql, keyValue, WorkPoint);
  580. // DataTable IMopicksqldt = SqlHelper.GetDataTableBySql(IMopciksql);
  581. // //审核完成 修改领料单数量 弃审 数量减
  582. // if (IMopicksqldt.Rows.Count > 0)
  583. // {
  584. // string sql1 = @"
  585. // update ICSMOPick set IssueQuantity = IssueQuantity + SumQuantity
  586. // from ICSMOPick a
  587. // join ( select b.PickID,b.InvCode,SUM(b.Quantity) as SumQuantity from
  588. //ICSMOIssue b
  589. // where b.EATTRIBUTE1 in ({0}) and b.WorkPoint='{1}'
  590. // group by b.PickID,b.InvCode
  591. // ) b on a.PickID = b.PickID";
  592. // sql1 = string.Format(sql1, keyValue, WorkPoint);
  593. // int Figure1 = SqlHelper.ExecuteNonQuery(sql1);
  594. // if (Figure1 <= 0)
  595. // {
  596. // //returnValue = "修改材料出库表状态失败!" + sql1;
  597. // throw new Exception("修改领料单数量失败!" + sql1);
  598. // }
  599. // }
  600. #endregion
  601. return "";
  602. }
  603. catch (Exception ex)
  604. {
  605. return ex.Message;
  606. }
  607. }
  608. /// <summary>
  609. /// 多个产成品退库单审核 减库存 调用其他系统同步
  610. /// </summary>
  611. /// <param name="keyValue"></param>
  612. /// <returns></returns>
  613. public string ICSMOIssueSendBackAndReduceStocks(string keyValue)
  614. {
  615. var sql = string.Empty;
  616. var sqlBuilder = new StringBuilder();
  617. var batchId = Guid.NewGuid().ToString(); // 批次ID用于日志记录
  618. string MUSER = OperatorProvider.Provider.GetCurrent().UserCode;
  619. string MUSERNAME = OperatorProvider.Provider.GetCurrent().UserName;
  620. string WorkPoint = OperatorProvider.Provider.GetCurrent().Location;
  621. var dateNow = DateTime.Now;
  622. try
  623. {
  624. // 1. 产成品入库和材料出库 根据物料过滤分组 判断产品分组库存数量是否足够扣除
  625. // 2. 关联库存表
  626. // 3. 计算修改后的库存
  627. // 4. 修改库存表 记录 请求erp
  628. // 5. 错误回调
  629. #region 入库表修改库存
  630. //选中单据根据物料分组
  631. string groupManufactureReceivesql = @"select * from ICSManufactureReceive a
  632. inner join (select a.InvCode,b.ExtensionID,a.WorkPoint,a.WarehouseCode,sum(a.Quantity) Quantity from ICSWareHouseLotInfo a
  633. inner join ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
  634. group by a.InvCode,b.ExtensionID,a.WorkPoint,a.WarehouseCode) b on a.WorkPoint=b.WorkPoint and a.ExtensionID=b.ExtensionID and a.InvCode=b.InvCode and a.WHCode=b.WarehouseCode
  635. where a.RCVCode in ({0}) and a.WorkPoint='{1}' and a.Quantity>b.Quantity
  636. ";
  637. groupManufactureReceivesql = string.Format(groupManufactureReceivesql, keyValue, WorkPoint);
  638. DataTable groupManufactureReceivesqldt = SqlHelper.GetDataTableBySql(groupManufactureReceivesql);
  639. if (groupManufactureReceivesqldt.Rows.Count > 0)
  640. {
  641. throw new Exception("产成品单据库存不足,请先检查库存");
  642. }
  643. //关联库存表 单个或多个库存 库存数量先进先出扣除
  644. sql = @"
  645. --
  646. SELECT ROW_NUMBER() OVER (ORDER BY a.RCVCode,a.Sequence) AS rowNo,
  647. a.RCVCode AS Code,
  648. a.Sequence,
  649. a.WhCode AS WarehouseCode,
  650. ISNULL(a.Quantity, 0) - ISNULL(a.RCVQuantity, 0) AS iQuantity,
  651. a.InvCode,
  652. b.InvName,
  653. b.InvStd,
  654. b.InvUnit,
  655. a.ExtensionID,
  656. ISNULL(c.ProjectCode, '') AS ProjectCode,
  657. ISNULL(c.BatchCode, '') AS BatchCode,
  658. ISNULL(c.Version, '') AS Version,
  659. ISNULL(c.Brand, '') AS Brand,
  660. ISNULL(c.cFree1, '') AS cFree1,
  661. ISNULL(c.cFree2, '') AS cFree2,
  662. ISNULL(c.cFree3, '') AS cFree3,
  663. ISNULL(c.cFree4, '') AS cFree4,
  664. ISNULL(c.cFree5, '') AS cFree5,
  665. ISNULL(c.cFree6, '') AS cFree6,
  666. ISNULL(c.cFree7, '') AS cFree7,
  667. ISNULL(c.cFree8, '') AS cFree8,
  668. ISNULL(c.cFree9, '') AS cFree9,
  669. ISNULL(c.cFree10, '') AS cFree10,
  670. CAST(NULL AS nvarchar(100)) AS LocationCode,
  671. CAST(NULL AS nvarchar(100)) AS LotNO,
  672. CAST(NULL AS DECIMAL(18,6)) AS QTY,
  673. CAST(NULL AS DECIMAL(18,6)) AS QTYTotal,
  674. CAST(NULL AS DECIMAL(18,6)) AS ReserveQuantity,
  675. CAST(NULL AS nvarchar(100)) AS MTIME
  676. INTO #TempVouchs
  677. FROM ICSManufactureReceive a
  678. LEFT JOIN ICSINVENTORY b ON a.InvCode = b.InvCode AND a.WorkPoint = b.WorkPoint
  679. LEFT JOIN ICSExtension c ON a.ExtensionID = c.ID AND a.WorkPoint = c.WorkPoint
  680. WHERE a.WorkPoint = '{1}' AND a.RCVCode in ({0}) AND ISNULL(a.Quantity, 0) > ISNULL(a.RCVQuantity, 0)
  681. DECLARE @VouchsTotal INT=@@rowcount, --
  682. @VouchsRowCurrent INT=1 --
  683. --
  684. SELECT
  685. a.WarehouseCode,
  686. a.Quantity - a.LockQuantity AS LotQuantity,
  687. a.INVCode,
  688. b.LotNo,
  689. ISNULL(c.BatchCode, '1') AS MTIME,
  690. a.inDate,
  691. a.LocationCode,
  692. b.ExtensionID,
  693. b.ProductDate,
  694. b.ExpirationDate,
  695. ISNULL(c.ProjectCode, '') AS ProjectCode,
  696. ISNULL(c.BatchCode, '') AS BatchCode,
  697. ISNULL(c.Version, '') AS Version,
  698. ISNULL(c.Brand, '') AS Brand,
  699. ISNULL(c.cFree1, '') AS cFree1,
  700. ISNULL(c.cFree2, '') AS cFree2,
  701. ISNULL(c.cFree3, '') AS cFree3,
  702. ISNULL(c.cFree4, '') AS cFree4,
  703. ISNULL(c.cFree5, '') AS cFree5,
  704. ISNULL(c.cFree6, '') AS cFree6,
  705. ISNULL(c.cFree7, '') AS cFree7,
  706. ISNULL(c.cFree8, '') AS cFree8,
  707. ISNULL(c.cFree9, '') AS cFree9,
  708. ISNULL(c.cFree10, '') AS cFree10
  709. INTO #TempWH
  710. FROM ICSWareHouseLotInfo a
  711. INNER JOIN ICSInventoryLot b ON a.LotNo = b.LotNo AND a.WorkPoint = b.WorkPoint
  712. INNER JOIN ICSExtension c ON b.ExtensionID = c.ID AND b.WorkPoint = c.WorkPoint
  713. WHERE a.WorkPoint = 'UFDATA_008_2022'
  714. AND a.Inspect='0' --
  715. AND a.Freeze='0' --
  716. AND a.Quantity > a.LockQuantity --
  717. AND a.InvCode IN (SELECT InvCode FROM #TempVouchs)
  718. --
  719. SELECT a.InvCode,a.Quantity AS QTYTotal,b.Quantity AS ReserveQuantity,a.Quantity-ISNULL(b.Quantity, 0) AS CanOutQuantity
  720. INTO #TempWHTotal
  721. FROM (SELECT InvCode,Sum(LotQuantity) AS Quantity FROM #TempWH GROUP BY InvCode) a
  722. LEFT JOIN ICSReserve b ON a.InvCode=b.InvCode AND b.Enable='1' AND b.WorkPoint='UFDATA_008_2022' AND (GETDATE() BETWEEN b.BeginTime AND b.EndTime)
  723. -- SELECT * FROM #TempWH
  724. -- SELECT * FROM #TempWHTotal
  725. --
  726. SELECT TOP 0 Code,Sequence,InvCode,InvName,InvStd,InvUnit,iQuantity,WarehouseCode,ExtensionID,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,LocationCode,LotNO,QTY,QTYTotal,ReserveQuantity,MTIME
  727. INTO #TempResult FROM #TempVouchs
  728. --
  729. ALTER TABLE #TempResult ADD [rowNo] INT IDENTITY(1,1);
  730. --
  731. WHILE @VouchsRowCurrent<=@VouchsTotal
  732. BEGIN
  733. SELECT ROW_NUMBER() OVER (ORDER BY a.rowNo,b.MTIME,b.LotNO) AS rowNo,a.Code,a.Sequence,a.InvCode,a.InvName,a.InvStd,a.InvUnit,a.iQuantity
  734. ,ISNULL(b.WarehouseCode,'') AS WarehouseCode,ISNULL(b.ExtensionID,'') AS ExtensionID
  735. ,ISNULL(b.ProjectCode,'') AS ProjectCode,ISNULL(b.BatchCode,'') AS BatchCode
  736. ,ISNULL(b.Version,'') AS Version,ISNULL(b.Brand,'') AS Brand,
  737. ISNULL(b.cFree1, '') AS cFree1,
  738. ISNULL(b.cFree2, '') AS cFree2,
  739. ISNULL(b.cFree3, '') AS cFree3,
  740. ISNULL(b.cFree4, '') AS cFree4,
  741. ISNULL(b.cFree5, '') AS cFree5,
  742. ISNULL(b.cFree6, '') AS cFree6,
  743. ISNULL(b.cFree7, '') AS cFree7,
  744. ISNULL(b.cFree8, '') AS cFree8,
  745. ISNULL(b.cFree9, '') AS cFree9,
  746. ISNULL(b.cFree10, '') AS cFree10,
  747. b.LocationCode,b.LotNO,b.LotQuantity,c.QTYTotal,c.ReserveQuantity,b.MTIME,a.iQuantity AS NeedQuantity,c.CanOutQuantity
  748. INTO #TempWHVouchs
  749. FROM #TempVouchs a
  750. LEFT JOIN #TempWH b ON a.InvCode=b.INVCode
  751. AND ((LEN(a.WarehouseCode)>0 AND a.WarehouseCode=b.WarehouseCode))
  752. AND (LEN(a.ProjectCode)<=0 OR (LEN(a.ProjectCode)>0 AND a.ProjectCode=b.ProjectCode))
  753. AND (LEN(a.BatchCode)<=0 OR (LEN(a.BatchCode)>0 AND a.BatchCode=b.BatchCode))
  754. AND (LEN(a.Version)<=0 OR (LEN(a.Version)>0 AND a.Version=b.Version))
  755. AND (LEN(a.Brand)<=0 OR (LEN(a.Brand)>0 AND a.Brand=b.Brand))
  756. AND (LEN(a.cFree1)<=0 OR (LEN(a.cFree1)>0 AND a.cFree1=b.cFree1))
  757. AND (LEN(a.cFree2)<=0 OR (LEN(a.cFree2)>0 AND a.cFree2=b.cFree2))
  758. AND (LEN(a.cFree3)<=0 OR (LEN(a.cFree3)>0 AND a.cFree3=b.cFree3))
  759. AND (LEN(a.cFree4)<=0 OR (LEN(a.cFree4)>0 AND a.cFree4=b.cFree4))
  760. AND (LEN(a.cFree5)<=0 OR (LEN(a.cFree5)>0 AND a.cFree5=b.cFree5))
  761. AND (LEN(a.cFree6)<=0 OR (LEN(a.cFree6)>0 AND a.cFree6=b.cFree6))
  762. AND (LEN(a.cFree7)<=0 OR (LEN(a.cFree7)>0 AND a.cFree7=b.cFree7))
  763. AND (LEN(a.cFree8)<=0 OR (LEN(a.cFree8)>0 AND a.cFree8=b.cFree8))
  764. AND (LEN(a.cFree9)<=0 OR (LEN(a.cFree9)>0 AND a.cFree9=b.cFree9))
  765. AND (LEN(a.cFree10)<=0 OR (LEN(a.cFree10)>0 AND a.cFree10=b.cFree10))
  766. LEFT JOIN #TempWHTotal c ON a.InvCode=c.INVCode
  767. WHERE rowNo=@VouchsRowCurrent
  768. DECLARE @WHTotal INT=@@rowcount, --
  769. @WHRowCurrent INT=1 --
  770. --
  771. IF EXISTS(SELECT rowNo FROM #TempWHTotal a LEFT JOIN #TempVouchs b ON a.InvCode=b.InvCode WHERE rowNo=@VouchsRowCurrent AND a.CanOutQuantity<=ISNULL(b.iQuantity, 0))
  772. BEGIN
  773. --
  774. UPDATE #TempWHVouchs SET NeedQuantity=CanOutQuantity
  775. --
  776. UPDATE a SET CanOutQuantity=0 FROM #TempWHTotal a INNER JOIN #TempVouchs b ON a.InvCode=b.InvCode WHERE rowNo=@VouchsRowCurrent
  777. --0
  778. DELETE FROM #TempWH WHERE InvCode=(SELECT InvCode FROM #TempVouchs WHERE rowNo=@VouchsRowCurrent)
  779. END
  780. --
  781. ELSE
  782. BEGIN
  783. --
  784. UPDATE a SET CanOutQuantity=a.CanOutQuantity-b.iQuantity FROM #TempWHTotal a INNER JOIN #TempVouchs b ON a.InvCode=b.InvCode WHERE rowNo=@VouchsRowCurrent
  785. END
  786. WHILE @WHRowCurrent<=@WHTotal
  787. BEGIN
  788. --,
  789. IF EXISTS(SELECT rowNo FROM #TempWHVouchs WHERE rowNo=@WHRowCurrent AND NeedQuantity<=LotQuantity)
  790. BEGIN
  791. INSERT INTO #TempResult
  792. SELECT a.Code,a.Sequence,a.InvCode,a.InvName,a.InvStd,a.InvUnit,a.iQuantity,a.WarehouseCode,a.ExtensionID,a.ProjectCode,a.BatchCode,a.Version,a.Brand,a.cFree1,a.cFree2,a.cFree3,a.cFree4,a.cFree5,a.cFree6,a.cFree7,a.cFree8,a.cFree9,a.cFree10,
  793. a.LocationCode,a.LotNO,a.NeedQuantity,a.QTYTotal,a.ReserveQuantity,a.MTIME
  794. FROM #TempWHVouchs a
  795. WHERE rowNo=@WHRowCurrent
  796. --
  797. IF EXISTS(SELECT rowNo FROM #TempWHVouchs WHERE rowNo=@WHRowCurrent AND NeedQuantity=LotQuantity)
  798. BEGIN
  799. --使
  800. DELETE FROM #TempWH WHERE LotNO=(SELECT LotNO FROM #TempWHVouchs WHERE rowNo=@WHRowCurrent)
  801. END
  802. ELSE
  803. BEGIN
  804. --使
  805. UPDATE a SET LotQuantity=b.LotQuantity-b.NeedQuantity FROM #TempWH a INNER JOIN #TempWHVouchs b ON a.LotNO=b.LotNO WHERE rowNo=@WHRowCurrent
  806. END
  807. --
  808. BREAK
  809. END
  810. --
  811. ELSE
  812. BEGIN
  813. INSERT INTO #TempResult
  814. SELECT a.Code,a.Sequence,a.InvCode,a.InvName,a.InvStd,a.InvUnit,a.iQuantity,a.WarehouseCode,a.ExtensionID,a.ProjectCode,a.BatchCode,a.Version,a.Brand,a.cFree1,a.cFree2,a.cFree3,a.cFree4,a.cFree5,a.cFree6,a.cFree7,a.cFree8,a.cFree9,a.cFree10,
  815. a.LocationCode,a.LotNO,a.LotQuantity,a.QTYTotal,a.ReserveQuantity,a.MTIME
  816. FROM #TempWHVouchs a
  817. WHERE rowNo=@WHRowCurrent
  818. --
  819. UPDATE #TempWHVouchs SET NeedQuantity=NeedQuantity-(SELECT LotQuantity FROM #TempWHVouchs WHERE rowNo=@WHRowCurrent)
  820. --使
  821. DELETE FROM #TempWH WHERE LotNO=(SELECT LotNO FROM #TempWHVouchs WHERE rowNo=@WHRowCurrent)
  822. END
  823. --1
  824. SET @WHRowCurrent=@WHRowCurrent+1
  825. END
  826. --1
  827. SET @VouchsRowCurrent=@VouchsRowCurrent+1
  828. DROP TABLE #TempWHVouchs
  829. END
  830. -- SELECT * FROM #TempWH
  831. -- SELECT * FROM #TempWHTotal
  832. -- SELECT * FROM #TempVouchs
  833. SELECT * FROM #TempResult ORDER BY cast(Sequence as int), rowNo
  834. --
  835. DROP TABLE #TempVouchs
  836. DROP TABLE #TempWH
  837. DROP TABLE #TempWHTotal
  838. DROP TABLE #TempResult ";
  839. sql = string.Format(sql, keyValue, WorkPoint);
  840. DataTable MfRetables = SqlHelper.GetDataTableBySql(sql);
  841. if (MfRetables.Rows.Count == 0)
  842. {
  843. throw new Exception("产成品单据库存不足,请先检查库存");
  844. }
  845. foreach (DataRow MfReitem in MfRetables.Rows)
  846. {
  847. // 本次扣除数量
  848. var Quantity = decimal.Parse(MfReitem["QTY"].ToString());
  849. if (Quantity <= 0) continue;
  850. string LotNO = MfReitem["LotNO"].ToString().Replace("'", "''");
  851. string InvCode = MfReitem["InvCode"].ToString().Replace("'", "''");
  852. // 1. 库存更新SQL
  853. sqlBuilder.AppendFormat(
  854. @"UPDATE ICSWareHouseLotInfo
  855. SET Quantity = Quantity + {0},
  856. MUSER = '{1}',
  857. MUSERName = '{2}',
  858. MTIME = '{3}'
  859. WHERE LotNO = '{4}'; ",
  860. -Quantity,
  861. MUSER.Replace("'", "''"),
  862. MUSERNAME.Replace("'", "''"),
  863. dateNow.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  864. LotNO
  865. );
  866. // 2. 库存日志插入SQL
  867. sqlBuilder.AppendFormat(
  868. @"INSERT INTO ICSWareHouseLotInfoLog (ID, Identification, TransCode, TransSequence,
  869. FromWarehouseCode, FromLocationCode, LotNo, [Lock], TransType, BusinessCode,
  870. Quantity, InvCode, MUSER, MUSERName, MTIME, WorkPoint)
  871. VALUES (NEWID(), '{11}', '{0}', '{1}', '{2}', '{3}', '{4}', 0, '17', '75',
  872. {5}, '{6}', '{7}', '{8}', '{9}', '{10}'); ",
  873. MfReitem["Code"].ToString().Replace("'", "''"),
  874. MfReitem["Sequence"].ToString().Replace("'", "''"),
  875. MfReitem["WarehouseCode"].ToString().Replace("'", "''"),
  876. MfReitem["LocationCode"].ToString().Replace("'", "''"),
  877. LotNO,
  878. -Quantity,
  879. InvCode,
  880. MUSER.Replace("'", "''"),
  881. MUSERNAME.Replace("'", "''"),
  882. dateNow.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  883. WorkPoint.Replace("'", "''"),
  884. batchId
  885. );
  886. }
  887. #region 废弃逻辑 使用捡料
  888. //foreach (DataRow grMFitem in groupManufactureReceivesqldt.Rows)
  889. //{
  890. // if (grMFitem["status"].ToString() != "1")
  891. // {
  892. // throw new Exception("选中行数据已审核。");
  893. // }
  894. // string invCode = grMFitem["InvCode"].ToString().Replace("'", "''");
  895. // string whCode = grMFitem["WHCode"].ToString().Replace("'", "''");
  896. // string batchCode = grMFitem["BatchCode"].ToString().Replace("'", "''");
  897. // //红字产成品入库表 同一产品需扣除数量
  898. // var InvQty = decimal.Parse(grMFitem["InvQty"].ToString());
  899. // DataRow[] tableRows = MfRetables.Select($"InvCode = '{invCode}' and WarehouseCode = '{whCode}' and BatchCode = '{batchCode}'");
  900. // //DataRow[]求和 LotQtySum当前物料的库存数量
  901. // // 计算库存总量
  902. // var LotQtySum = decimal.Parse(tableRows.Sum(x => x.Field<decimal>("Quantity")).ToString());
  903. // if (LotQtySum < InvQty)
  904. // {
  905. // throw new Exception($"产品编码:{invCode},仓库编码:{whCode},批次:{batchCode},库存数量不足!当前库存:{LotQtySum},需要扣除:{InvQty}");
  906. // }
  907. // //循环 修改产成品的库存 新增库存日志
  908. // foreach (DataRow MfReitem in tableRows)
  909. // {
  910. // #region 回滚 废弃注释
  911. // //是否回滚
  912. // //if (!string.IsNullOrEmpty(returnValue))
  913. // //{
  914. // // //回滚
  915. // // if (ReList.Any())
  916. // // {
  917. // // foreach (var item in ReList)
  918. // // {
  919. // // ReSql += "update ICSWareHouseLotInfo set Quantity = " + item.Value + " where LotNo = '" + item.Key + "' and WorkPoint ='" + WorkPoint + "' ";
  920. // // }
  921. // // var ReFlag = SqlHelper.ExecuteNonQuery(ReSql);
  922. // // if (ReFlag > 0)
  923. // // {
  924. // // if (string.IsNullOrEmpty(ReCode))
  925. // // {
  926. // // // 将库存记录删除
  927. // // ReSql = "delete from ICSWareHouseLotInfoLog where TransCode in (" + ReCode + ") and WorkPoint ='" + WorkPoint + "'";
  928. // // var ReLogFlag = SqlHelper.ExecuteNonQuery(ReSql);
  929. // // }
  930. // // throw new Exception(returnValue + " 已修改的数据已回滚。");
  931. // // }
  932. // // else
  933. // // {
  934. // // throw new Exception(returnValue + " 数据回滚失败。");
  935. // // }
  936. // // }
  937. // // else
  938. // // {
  939. // // throw new Exception(returnValue);
  940. // // }
  941. // //}
  942. // #endregion
  943. // //扣除数量未清零则继续操作库存
  944. // if (InvQty <= 0) continue;
  945. // var Quantity = decimal.Parse(MfReitem["Quantity"].ToString());
  946. // if (Quantity <= 0) continue;
  947. // // 本次扣除数量
  948. // var LogQty = Quantity <= InvQty ? Quantity : InvQty;
  949. // InvQty = Quantity <= InvQty ? InvQty - Quantity : 0;
  950. // string lotId = MfReitem["ID"].ToString().Replace("'", "''");
  951. // // 1. 库存更新SQL
  952. // sqlBuilder.AppendFormat(
  953. // @"UPDATE ICSWareHouseLotInfo
  954. // SET Quantity = Quantity + {0},
  955. // MUSER = '{1}',
  956. // MUSERName = '{2}',
  957. // MTIME = '{3}'
  958. // WHERE ID = '{4}'; ",
  959. // -LogQty,
  960. // MUSER.Replace("'", "''"),
  961. // MUSERNAME.Replace("'", "''"),
  962. // dateNow.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  963. // lotId
  964. // );
  965. // // 2. 库存日志插入SQL
  966. // sqlBuilder.AppendFormat(
  967. // @"INSERT INTO ICSWareHouseLotInfoLog (ID, Identification, TransCode, TransSequence,
  968. // FromWarehouseCode, FromLocationCode, LotNo, [Lock], TransType, BusinessCode,
  969. // Quantity, InvCode, MUSER, MUSERName, MTIME, WorkPoint)
  970. // VALUES (NEWID(), '{11}', '{0}', '{1}', '{2}', '{3}', '{4}', 0, '17', '75',
  971. // {5}, '{6}', '{7}', '{8}', '{9}', '{10}'); ",
  972. // MfReitem["RCVCode"].ToString().Replace("'", "''"),
  973. // MfReitem["Sequence"].ToString().Replace("'", "''"),
  974. // MfReitem["WarehouseCode"].ToString().Replace("'", "''"),
  975. // MfReitem["LocationCode"].ToString().Replace("'", "''"),
  976. // MfReitem["LotNo"].ToString().Replace("'", "''"),
  977. // LogQty,
  978. // invCode,
  979. // MUSER.Replace("'", "''"),
  980. // MUSERNAME.Replace("'", "''"),
  981. // dateNow.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  982. // WorkPoint.Replace("'", "''"),
  983. // batchId
  984. // );
  985. // #region 废弃逻辑
  986. // // if (InvQty > 0)
  987. // // {
  988. // // var Quantity = decimal.Parse(MfReitem["Quantity"].ToString());
  989. // // if (Quantity > 0)
  990. // // {
  991. // // // 本次扣除数量
  992. // // var LogQty = Quantity <= InvQty ? Quantity : InvQty;
  993. // // // 扣除数量
  994. // // InvQty = Quantity <= InvQty ? InvQty - Quantity : 0;
  995. // // string lotId = MfReitem["ID"].ToString().Replace("'", "''");
  996. // // //库存表修改 根据退库表的数量 按先进先出 扣除库存
  997. // // var LotEntity = MsSqlData.Get<ICSWareHouseLotInfo>(MfReitem["ID"].ToString());
  998. // // if (LotEntity != null)
  999. // // {
  1000. // // //回调时使用
  1001. // // ReList.Add(LotEntity.LotNo, LotEntity.Quantity);
  1002. // // //此处改为扣除数量后的剩余数量
  1003. // // LotEntity.Quantity = LotEntity.Quantity - LogQty;
  1004. // // LotEntity.MUSER = MUSER;
  1005. // // LotEntity.MUSERName = MUSERNAME;
  1006. // // LotEntity.MTIME = dateNow.ToString("yyyy-MM-dd HH:mm:ss");
  1007. // // var result2 = MsSqlData.Update<ICSWareHouseLotInfo>(LotEntity);
  1008. // // if (!result2)
  1009. // // {
  1010. // // returnValue = "库存表修改失败";
  1011. // // }
  1012. // // }
  1013. // // //库存日志记录 回滚时删除数据
  1014. // // ReCode = string.IsNullOrEmpty(ReCode) ? "'" + MfReitem["RCVCode"].ToString() + "'" : ReCode + ",'" + MfReitem["RCVCode"].ToString() + "'";
  1015. // // //库存日志记录 产成品退库
  1016. // // var entity = new ICSWareHouseLotInfoLog();
  1017. // // //ConvertExt.Mapping<ICSWareHouseLotInfoLogEdit, ICSWareHouseLotInfoLog>(model, entity);
  1018. // // entity.ID = Guid.NewGuid().ToString();
  1019. // // entity.Identification = Guid.NewGuid().ToString();
  1020. // // entity.TransCode = MfReitem["RCVCode"].ToString();
  1021. // // entity.TransSequence = MfReitem["Sequence"].ToString();
  1022. // // entity.FromWarehouseCode = MfReitem["WarehouseCode"].ToString();
  1023. // // entity.FromLocationCode = MfReitem["LocationCode"].ToString();
  1024. // // entity.LotNo = MfReitem["LotNo"].ToString();
  1025. // // entity.Lock = false;
  1026. // // entity.TransType = "17";//铭锋产成品退库
  1027. // // entity.BusinessCode = "75";//铭锋产成品退库
  1028. // // entity.Quantity = LogQty;
  1029. // // entity.InvCode = MfReitem["InvCode"].ToString();
  1030. // // entity.MUSER = MUSER;
  1031. // // entity.MUSERName = MUSERNAME;
  1032. // // entity.MTIME = Dates;
  1033. // // entity.WorkPoint = WorkPoint;
  1034. // // var result = MsSqlData.Insert<ICSWareHouseLotInfoLog>(entity);
  1035. // // if (!result)
  1036. // // {
  1037. // // returnValue = "新增库存日志记录失败";
  1038. // // }
  1039. // // // 2. 库存日志插入SQL
  1040. // // sqlBuilder.AppendFormat(
  1041. // // @"INSERT INTO ICSWareHouseLotInfoLog (ID, Identification, TransCode, TransSequence,
  1042. // //FromWarehouseCode, FromLocationCode, LotNo, [Lock], TransType, BusinessCode,
  1043. // //Quantity, InvCode, MUSER, MUSERName, MTIME, WorkPoint)
  1044. // //VALUES (NEWID(), '{11}', '{0}', '{1}', '{2}', '{3}', '{4}', 0, '17', '75',
  1045. // //{5}, '{6}', '{7}', '{8}', '{9}', '{10}'); ",
  1046. // // MfReitem["RCVCode"].ToString().Replace("'", "''"),
  1047. // // MfReitem["Sequence"].ToString().Replace("'", "''"),
  1048. // // MfReitem["WarehouseCode"].ToString().Replace("'", "''"),
  1049. // // MfReitem["LocationCode"].ToString().Replace("'", "''"),
  1050. // // MfReitem["LotNo"].ToString().Replace("'", "''"),
  1051. // // LogQty,
  1052. // // invCode,
  1053. // // MUSER.Replace("'", "''"),
  1054. // // MUSERNAME.Replace("'", "''"),
  1055. // // dateNow.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  1056. // // WorkPoint.Replace("'", "''"),
  1057. // // batchId
  1058. // // );
  1059. // // }
  1060. // // }
  1061. // // else
  1062. // // {
  1063. // // continue;
  1064. // // }
  1065. // #endregion
  1066. // }
  1067. //}
  1068. #endregion
  1069. #endregion
  1070. #region 材料出库表修改库存
  1071. // 红字生产入库单ICSManufactureReceive 会生成对应的红字生成领料单 也就是ICSMOIssue材料出库表
  1072. //选中的物料 计算扣除数量
  1073. string groupmoIssuesql = @"select a.InvCode,Sum(a.Quantity) as InvQty,a.WHCode,e.BatchCode
  1074. from ICSMOIssue a
  1075. inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
  1076. inner JOIN ICSExtension e on a.ExtensionID = e.ID and a.WorkPoint = e.WorkPoint
  1077. where a.EATTRIBUTE1 in ({0}) and a.WorkPoint='{1}'
  1078. group by a.InvCode,a.WHCode,e.BatchCode";
  1079. groupmoIssuesql = string.Format(groupmoIssuesql, keyValue, WorkPoint);
  1080. DataTable groupmoIssuesqldt = SqlHelper.GetDataTableBySql(groupmoIssuesql);
  1081. //铭锋 因为客户在ERP操作了入库 但在WMS操作退库 没有找到条码就生成一个条码
  1082. if (groupmoIssuesqldt.Rows.Count <= 0)
  1083. {
  1084. throw new Exception("未找到对应的材料出库单。");
  1085. }
  1086. #region 回滚 废弃
  1087. //if (!string.IsNullOrEmpty(returnValue))
  1088. //{
  1089. // //回滚
  1090. // if (ReList.Any())
  1091. // {
  1092. // foreach (var item in ReList)
  1093. // {
  1094. // ReSql += "update ICSWareHouseLotInfo set Quantity = " + item.Value + " where LotNo = '" + item.Key + "' and WorkPoint ='" + WorkPoint + "' ";
  1095. // }
  1096. // var ReFlag = SqlHelper.ExecuteNonQuery(ReSql);
  1097. // if (ReFlag > 0)
  1098. // {
  1099. // if (string.IsNullOrEmpty(ReCode))
  1100. // {
  1101. // // 将库存记录删除
  1102. // ReSql = "delete from ICSWareHouseLotInfoLog where TransCode in (" + ReCode + ") and WorkPoint ='" + WorkPoint + "'";
  1103. // var ReLogFlag = SqlHelper.ExecuteNonQuery(ReSql);
  1104. // }
  1105. // throw new Exception(returnValue + " 已修改的数据已回滚。");
  1106. // }
  1107. // else
  1108. // {
  1109. // throw new Exception(returnValue + " 数据回滚失败。");
  1110. // }
  1111. // }
  1112. // else
  1113. // {
  1114. // throw new Exception(returnValue);
  1115. // }
  1116. //}
  1117. #endregion
  1118. string moIssuesql = @"select a.InvCode,a.IssueCode,a.Sequence,a.WHCode,e.BatchCode
  1119. from ICSMOIssue a
  1120. join ICSExtension e on a.ExtensionID = e.ID and ISNULL(e.BatchCode,'') != '' and a.WorkPoint = e.WorkPoint
  1121. where a.EATTRIBUTE1 in ({0}) and a.WorkPoint='{1}'";
  1122. moIssuesql = string.Format(moIssuesql, keyValue, WorkPoint);
  1123. DataTable moIssue = SqlHelper.GetDataTableBySql(moIssuesql);
  1124. if (moIssue.Rows.Count <= 0)
  1125. {
  1126. throw new Exception("材料出库单未找到关联批次。");
  1127. }
  1128. else
  1129. {
  1130. foreach (DataRow item in moIssue.Rows)
  1131. {
  1132. sql = @"
  1133. select a.IssueCode,a.Sequence,a.InvCode,d.LotNo,g.Quantity,g.InDate,g.ID,g.WarehouseCode,g.LocationCode
  1134. from ICSMOIssue a
  1135. inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
  1136. inner JOIN ICSInventoryLot d ON a.ExtensionID=d.ExtensionID and a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
  1137. inner JOIN ICSExtension e on a.ExtensionID = e.ID and ISNULL(e.BatchCode,'') != '' and a.WorkPoint = e.WorkPoint
  1138. inner JOIN (select a.*,b.ExtensionID from ICSWareHouseLotInfo a
  1139. left join ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint ) g
  1140. ON g.LotNo=d.LotNo and a.WHCode = g.WarehouseCode and g.WorkPoint=a.WorkPoint and a.ExtensionID=g.ExtensionID
  1141. where a.IssueCode = '{0}' and a.Sequence = {1} and a.WorkPoint='{2}' order by e.BatchCode asc";
  1142. sql = string.Format(sql, item["IssueCode"].ToString(), item["Sequence"].ToString(), WorkPoint);
  1143. DataTable tablesDemo = SqlHelper.GetDataTableBySql(sql);
  1144. if (tablesDemo.Rows.Count <= 0)
  1145. {
  1146. throw new Exception("物料未找到条码库存,请联系管理员补充资料。物料:" + item["InvCode"].ToString() + ";仓库:" + item["WHCode"].ToString() + ";批次:" + item["BatchCode"].ToString() + ";");
  1147. }
  1148. }
  1149. }
  1150. // 获取库存数据
  1151. sql = @"select a.IssueCode,a.Sequence,a.InvCode,d.LotNo,g.Quantity,g.InDate,g.ID,g.WarehouseCode,g.LocationCode,e.BatchCode
  1152. from ICSMOIssue a
  1153. inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
  1154. inner JOIN ICSInventoryLot d ON a.ExtensionID=d.ExtensionID and a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
  1155. inner JOIN ICSExtension e on a.ExtensionID = e.ID and a.WorkPoint = e.WorkPoint
  1156. inner JOIN (select a.*,b.ExtensionID from ICSWareHouseLotInfo a
  1157. left join ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint ) g
  1158. ON g.LotNo=d.LotNo and a.WHCode = g.WarehouseCode and g.WorkPoint=a.WorkPoint and a.ExtensionID=g.ExtensionID
  1159. where a.EATTRIBUTE1 in ({0}) and a.WorkPoint='{1}' order by e.BatchCode asc;";
  1160. sql = string.Format(sql, keyValue, WorkPoint);
  1161. DataTable tables = SqlHelper.GetDataTableBySql(sql);
  1162. if (tables.Rows.Count <= 0)
  1163. {
  1164. throw new Exception("物料未找到条码库存,请联系管理员补充资料。");
  1165. }
  1166. #region 废弃
  1167. //关联库存表 单个或多个库存 库存数量不够时先进先出扣除
  1168. // sql = @"
  1169. // select a.IssueCode,a.Sequence,a.InvCode,d.LotNo,g.Quantity,g.InDate,g.ID,g.WarehouseCode,g.LocationCode,e.BatchCode
  1170. //from ICSMOIssue a
  1171. //inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint --and c.InvIQC='1'
  1172. //inner JOIN ICSInventoryLot d ON a.ExtensionID=d.ExtensionID and a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint --and d.Type = '13'
  1173. //inner JOIN ICSExtension e on a.ExtensionID = e.ID and a.WorkPoint = e.WorkPoint
  1174. //inner JOIN (select a.*,b.ExtensionID from ICSWareHouseLotInfo a
  1175. //left join ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint ) g ON g.LotNo=d.LotNo and a.WHCode = g.WarehouseCode and g.WorkPoint=a.WorkPoint and a.ExtensionID=g.ExtensionID
  1176. // where a.EATTRIBUTE1 in ({0}) and a.WorkPoint='{1}' order by e.BatchCode asc";
  1177. // sql = string.Format(sql, keyValue, WorkPoint);
  1178. // DataTable tables = SqlHelper.GetDataTableBySql(sql);
  1179. // if (tables.Rows.Count <= 0)
  1180. // {
  1181. // returnValue = "物料未找到条码库存,请联系管理员补充资料。";
  1182. // }
  1183. #endregion
  1184. //材料出库数据 物料循环 物料总数量扣除
  1185. foreach (DataRow groupdr in groupmoIssuesqldt.Rows)
  1186. {
  1187. //同一产品需增加的数量 退库为负数
  1188. var InvQty = decimal.Parse(groupdr["InvQty"].ToString());
  1189. string invCode = groupdr["InvCode"].ToString().Replace("'", "''");
  1190. string wHCode = groupdr["WHCode"].ToString().Replace("'", "''");
  1191. string batchCode = groupdr["BatchCode"].ToString().Replace("'", "''");
  1192. // 筛选库存记录
  1193. DataRow[] tableRows = tables.Select($"InvCode = '{invCode}' and WarehouseCode = '{wHCode}' and BatchCode = '{batchCode}'");
  1194. if (tableRows.Length <= 0)
  1195. {
  1196. throw new Exception($"物料编码:{invCode},未找到条码库存。");
  1197. }
  1198. //先进先出第一个条码增加数量
  1199. DataRow MoIssueLot = tableRows[0];
  1200. // 条码当前数量
  1201. var Quantity = decimal.Parse(MoIssueLot["Quantity"].ToString());
  1202. // 先进先出处理
  1203. string lotId = MoIssueLot["ID"].ToString().Replace("'", "''");
  1204. // 本次修改后的数量
  1205. var LogQty = Quantity - InvQty;
  1206. // 1. 库存更新SQL
  1207. sqlBuilder.AppendFormat(
  1208. @"UPDATE ICSWareHouseLotInfo
  1209. SET Quantity = {0},
  1210. MUSER = '{1}',
  1211. MUSERName = '{2}',
  1212. MTIME = '{3}'
  1213. WHERE ID = '{4}'; ",
  1214. LogQty,
  1215. MUSER.Replace("'", "''"),
  1216. MUSERNAME.Replace("'", "''"),
  1217. dateNow.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  1218. lotId
  1219. );
  1220. // 2. 库存日志插入SQL
  1221. sqlBuilder.AppendFormat(
  1222. @"INSERT INTO ICSWareHouseLotInfoLog (ID, Identification, TransCode, TransSequence,
  1223. FromWarehouseCode, FromLocationCode, LotNo, [Lock], TransType, BusinessCode,
  1224. Quantity, InvCode, MUSER, MUSERName, MTIME, WorkPoint)
  1225. VALUES (NEWID(), '{11}', '{0}', '{1}', '{2}', '{3}', '{4}', 0, '17', '75',
  1226. {5}, '{6}', '{7}', '{8}', '{9}', '{10}'); ",
  1227. MoIssueLot["IssueCode"].ToString().Replace("'", "''"),
  1228. MoIssueLot["Sequence"].ToString().Replace("'", "''"),
  1229. MoIssueLot["WarehouseCode"].ToString().Replace("'", "''"),
  1230. MoIssueLot["LocationCode"].ToString().Replace("'", "''"),
  1231. MoIssueLot["LotNo"].ToString().Replace("'", "''"),
  1232. -(InvQty),
  1233. invCode,
  1234. MUSER.Replace("'", "''"),
  1235. MUSERNAME.Replace("'", "''"),
  1236. dateNow.ToString("yyyy-MM-dd HH:mm:ss").Replace("'", "''"),
  1237. WorkPoint.Replace("'", "''"),
  1238. batchId
  1239. );
  1240. }
  1241. #region 库存表修改 废弃
  1242. //var LotEntity = MsSqlData.Get<ICSWareHouseLotInfo>(MoIssueLot["ID"].ToString());
  1243. //if (LotEntity != null)
  1244. //{
  1245. // //ReList回滚时使用
  1246. // ReList.Add(LotEntity.LotNo, LotEntity.Quantity);
  1247. // LotEntity.Quantity = LogQty;
  1248. // LotEntity.MUSER = MUSER;
  1249. // LotEntity.MUSERName = MUSERNAME;
  1250. // LotEntity.MTIME = dateNow.ToString("yyyy-MM-dd HH:mm:ss");
  1251. // var result2 = MsSqlData.Update<ICSWareHouseLotInfo>(LotEntity);
  1252. // if (!result2)
  1253. // {
  1254. // returnValue = "库存表修改失败";
  1255. // }
  1256. //}
  1257. //库存日志记录 回滚时删除数据
  1258. //ReCode = string.IsNullOrEmpty(ReCode) ? "'" + MoIssueLot["IssueCode"].ToString() + "'" : ReCode + ",'" + MoIssueLot["IssueCode"].ToString() + "'";
  1259. ////库存日志记录
  1260. //var entity = new ICSWareHouseLotInfoLog();
  1261. ////ConvertExt.Mapping<ICSWareHouseLotInfoLogEdit, ICSWareHouseLotInfoLog>(model, entity);
  1262. //entity.ID = Guid.NewGuid().ToString();
  1263. //entity.Identification = Guid.NewGuid().ToString();
  1264. //entity.TransCode = MoIssueLot["IssueCode"].ToString();
  1265. //entity.TransSequence = MoIssueLot["Sequence"].ToString();
  1266. //entity.FromWarehouseCode = MoIssueLot["WarehouseCode"].ToString();
  1267. //entity.FromLocationCode = MoIssueLot["LocationCode"].ToString();
  1268. //entity.LotNo = MoIssueLot["LotNo"].ToString();
  1269. //entity.Lock = false;
  1270. //entity.TransType = "17";//铭锋产成品退库
  1271. //entity.BusinessCode = "75";//铭锋产成品退库
  1272. //entity.Quantity = InvQty;
  1273. //entity.InvCode = groupdr["InvCode"].ToString();
  1274. //entity.MUSER = MUSER;
  1275. //entity.MUSERName = MUSERNAME;
  1276. //entity.MTIME = Dates;
  1277. //entity.WorkPoint = WorkPoint;
  1278. //var result = MsSqlData.Insert<ICSWareHouseLotInfoLog>(entity);
  1279. //if (!result)
  1280. //{
  1281. // returnValue = "新增库存日志记录失败";
  1282. //}
  1283. //}
  1284. #endregion
  1285. #endregion
  1286. #region 修改状态
  1287. // 3. 工单入库数量
  1288. sqlBuilder.AppendFormat(
  1289. @"UPDATE ICSMO
  1290. SET RCVQuantity = RCVQuantity - b.SumQuantity,MTIME = GETDATE()
  1291. FROM ICSMO a
  1292. JOIN (
  1293. SELECT SourceCode, SourceSequence, SUM(Quantity) AS SumQuantity
  1294. FROM ICSManufactureReceive
  1295. WHERE RCVCode IN ({0}) AND WorkPoint = '{1}'
  1296. GROUP BY SourceCode, SourceSequence
  1297. ) b ON b.SourceCode = a.MOCode AND b.SourceSequence = a.Sequence; ",
  1298. keyValue, WorkPoint
  1299. );
  1300. // 4. 领料单数量
  1301. sqlBuilder.AppendFormat(
  1302. @"UPDATE ICSMOPick
  1303. SET IssueQuantity = IssueQuantity - b.SumQuantity,MTIME = GETDATE()
  1304. FROM ICSMOPick a
  1305. JOIN (
  1306. SELECT PickID, SUM(Quantity) AS SumQuantity
  1307. FROM ICSMOIssue
  1308. WHERE EATTRIBUTE1 IN ({0}) AND WorkPoint = '{1}'
  1309. GROUP BY PickID
  1310. ) b ON a.PickID = b.PickID; ",
  1311. keyValue, WorkPoint
  1312. );
  1313. // 1. 材料出库表状态
  1314. sqlBuilder.AppendFormat(
  1315. @"UPDATE ICSMOIssue SET Status = '2' ,MTIME = GETDATE()
  1316. WHERE EATTRIBUTE1 IN ({0}) AND WorkPoint = '{1}' AND Status = '1'; ",
  1317. keyValue, WorkPoint
  1318. );
  1319. // 2. 产成品入库表状态
  1320. sqlBuilder.AppendFormat(
  1321. @"UPDATE ICSManufactureReceive SET Status = '2',EATTRIBUTE1 = '{2}',MTIME = GETDATE()
  1322. WHERE RCVCode IN ({0}) AND WorkPoint = '{1}' AND Status = '1'; ",
  1323. keyValue, WorkPoint, batchId
  1324. );
  1325. #endregion
  1326. // 执行所有数据库操作
  1327. //SqlHelper.CmdExecuteNonQueryLi(sqlBuilder.ToString());
  1328. // 查找 ICSManufactureReceive 入库单表
  1329. string mofactureReceivesql = @"select a.RCVCode,a.InvCode,a.RCVID
  1330. from ICSManufactureReceive a
  1331. where a.RCVCode in ({0}) and a.WorkPoint='{1}' group by a.RCVCode,a.InvCode,a.RCVID";
  1332. mofactureReceivesql = string.Format(mofactureReceivesql, keyValue, WorkPoint);
  1333. DataTable moReceiveqldt = SqlHelper.GetDataTableBySql(mofactureReceivesql);
  1334. // 查找 ICSMOIssue 入库单表
  1335. string MOIssuesql = @"select a.IssueID from ICSMOIssue a where a.EATTRIBUTE1 in ({0}) and a.WorkPoint='{1}' group by a.IssueID";
  1336. MOIssuesql = string.Format(MOIssuesql, keyValue, WorkPoint);
  1337. DataTable MOIssuesqldt = SqlHelper.GetDataTableBySql(MOIssuesql);
  1338. using (SqlConnection conn = SqlHelper.GetDataCenterConn())
  1339. {
  1340. conn.Open();
  1341. SqlTransaction sqlTran = conn.BeginTransaction();
  1342. SqlCommand cmd = new SqlCommand();
  1343. cmd.Transaction = sqlTran;
  1344. cmd.Connection = conn;
  1345. try
  1346. {
  1347. cmd.CommandText = sqlBuilder.ToString();
  1348. int RES = cmd.ExecuteNonQuery();
  1349. #region 调用ERP接口
  1350. var erplist = new List<ApproveInput>();
  1351. foreach (DataRow item in moReceiveqldt.Rows)
  1352. {
  1353. if (!erplist.Any(q => q.ID == item["RCVID"].ToString() && q.Type == "入库"))
  1354. {
  1355. erplist.Add(new ApproveInput
  1356. {
  1357. ID = item["RCVID"].ToString(),
  1358. User = MUSER,
  1359. WorkPoint = WorkPoint,
  1360. Type = "入库",
  1361. UpdateStock = true,
  1362. MTime = dateNow,
  1363. UpdateTodoQuantity = true,
  1364. });
  1365. }
  1366. }
  1367. foreach (DataRow item in MOIssuesqldt.Rows)
  1368. {
  1369. if (!erplist.Any(q => q.ID == item["IssueID"].ToString() && q.Type == "倒冲"))
  1370. {
  1371. erplist.Add(new ApproveInput
  1372. {
  1373. ID = item["IssueID"].ToString(),
  1374. User = MUSER,
  1375. WorkPoint = WorkPoint,
  1376. Type = "倒冲",
  1377. UpdateStock = true,
  1378. MTime = dateNow,
  1379. UpdateTodoQuantity = true,
  1380. });
  1381. }
  1382. }
  1383. string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "FinishedProductReToWHErp/Audit";
  1384. var erpinput = JsonConvert.SerializeObject(erplist.Distinct());
  1385. string result1 = HttpPost(APIURL, erpinput);
  1386. JObject Obj = (JObject)JsonConvert.DeserializeObject(result1);//或者JObject jo = JObject.Parse(jsonText);
  1387. string MessAge = Obj["Message"].ToString();
  1388. string Success = Obj["Success"].ToString();
  1389. if (Success.ToUpper() == "FALSE")
  1390. {
  1391. throw new Exception("调用ERP请求失败,返回:" + MessAge);
  1392. }
  1393. #endregion
  1394. cmd.Transaction.Commit();
  1395. }
  1396. catch (Exception ex)
  1397. {
  1398. cmd.Transaction.Rollback();
  1399. throw new Exception(ex.Message);
  1400. }
  1401. finally
  1402. {
  1403. if (conn.State != ConnectionState.Closed)
  1404. {
  1405. conn.Close();
  1406. conn.Dispose();
  1407. }
  1408. }
  1409. }
  1410. #region 废弃
  1411. // string IChecksql = @"select EATTRIBUTE1
  1412. // from ICSMOIssue a
  1413. // where a.EATTRIBUTE1 in ({0}) and a.WorkPoint='{1}' and Status = '1'";
  1414. // IChecksql = string.Format(IChecksql, keyValue, WorkPoint);
  1415. // DataTable IChecksqldt = SqlHelper.GetDataTableBySql(IChecksql);
  1416. // //审核完成 修改材料出库表状态
  1417. // if (IChecksqldt.Rows.Count > 0)
  1418. // {
  1419. // string sql1 = @"
  1420. //update ICSMOIssue set Status = '2' where EATTRIBUTE1 in (" + keyValue + ") and WorkPoint ='" + WorkPoint + "' and Status = '1'";
  1421. // sql1 = string.Format(sql1, keyValue, WorkPoint);
  1422. // int Figure1 = SqlHelper.ExecuteNonQuery(sql1);
  1423. // if (Figure1 <= 0)
  1424. // {
  1425. // //returnValue = "修改材料出库表状态失败!" + sql1;
  1426. // throw new Exception("修改材料出库表状态失败!" + sql1);
  1427. // }
  1428. // }
  1429. // string IChecksql2 = @"select RCVCode from ICSManufactureReceive a
  1430. // where a.RCVCode in ({0}) and a.WorkPoint='{1}' and Status = '1'";
  1431. // IChecksql2 = string.Format(IChecksql2, keyValue, WorkPoint);
  1432. // DataTable IChecksqldt2 = SqlHelper.GetDataTableBySql(IChecksql2);
  1433. // //修改产成品入库表ICSManufactureReceive状态
  1434. // if (IChecksqldt2.Rows.Count > 0)
  1435. // {
  1436. // string sql2 = @"
  1437. //update ICSManufactureReceive set Status = '2' where RCVCode in (" + keyValue + ") and WorkPoint ='" + WorkPoint + "' and Status = '1'";
  1438. // sql2 = string.Format(sql2, keyValue, WorkPoint);
  1439. // int Figure1 = SqlHelper.ExecuteNonQuery(sql2);
  1440. // if (Figure1 <= 0)
  1441. // {
  1442. // //returnValue = "修改产成品入库表状态失败!" + sql2;
  1443. // throw new Exception("修改产成品入库表状态失败!" + sql2);
  1444. // }
  1445. // }
  1446. // //工单表:ICSMO表RCVQuantity字段 ----减去产红字产成品入库单数量
  1447. // //工单子件表:ICSMOPICK表IssueQuantity字段 ----- 加上红字材料出库单的数量
  1448. // string IMosql = @"select a.Quantity,b.RCVQuantity from ICSManufactureReceive a
  1449. // join ICSMO b on a.SourceCode = b.MOCode and a.SourceSequence = b.Sequence
  1450. // where a.RCVCode in ({0}) and a.WorkPoint='{1}' ";
  1451. // IMosql = string.Format(IMosql, keyValue, WorkPoint);
  1452. // DataTable IMosqldt = SqlHelper.GetDataTableBySql(IMosql);
  1453. // //审核完成 修改工单入库数量 审核 数量减
  1454. // if (IMosqldt.Rows.Count > 0)
  1455. // {
  1456. // string sql1 = @"
  1457. // update ICSMO
  1458. // set RCVQuantity = RCVQuantity - SumQuantity
  1459. // from ICSMO a join (
  1460. // select SUM(a.Quantity) as SumQuantity,a.SourceCode,a.SourceSequence from ICSManufactureReceive a
  1461. // where a.RCVCode in ({0}) and a.WorkPoint='{1}'
  1462. // group by a.SourceCode,a.SourceSequence,a.InvCode
  1463. // ) b on b.SourceCode = a.MOCode and b.SourceSequence = a.Sequence";
  1464. // sql1 = string.Format(sql1, keyValue, WorkPoint);
  1465. // int Figure1 = SqlHelper.ExecuteNonQuery(sql1);
  1466. // if (Figure1 <= 0)
  1467. // {
  1468. // //returnValue = "修改材料出库表状态失败!" + sql1;
  1469. // throw new Exception("修改工单数量失败!" + sql1);
  1470. // }
  1471. // }
  1472. // string IMopciksql = @"select a.IssueQuantity,b.Quantity from ICSMOPick a
  1473. // join ICSMOIssue b on a.PickID = b.PickID
  1474. // where b.EATTRIBUTE1 in ({0}) and b.WorkPoint='{1}'";
  1475. // IMopciksql = string.Format(IMopciksql, keyValue, WorkPoint);
  1476. // DataTable IMopicksqldt = SqlHelper.GetDataTableBySql(IMopciksql);
  1477. // //审核完成 修改领料单数量 审核 数量加
  1478. // if (IMopicksqldt.Rows.Count > 0)
  1479. // {
  1480. // string sql1 = @"
  1481. // update ICSMOPick set IssueQuantity = IssueQuantity - SumQuantity
  1482. // from ICSMOPick a
  1483. // join ( select b.PickID,b.InvCode,SUM(b.Quantity) as SumQuantity from
  1484. //ICSMOIssue b
  1485. // where b.EATTRIBUTE1 in ({0}) and b.WorkPoint='{1}'
  1486. // group by b.PickID,b.InvCode
  1487. // ) b on a.PickID = b.PickID and a.InvCode = b.InvCode";
  1488. // sql1 = string.Format(sql1, keyValue, WorkPoint);
  1489. // int Figure1 = SqlHelper.ExecuteNonQuery(sql1);
  1490. // if (Figure1 <= 0)
  1491. // {
  1492. // //returnValue = "修改材料出库表状态失败!" + sql1;
  1493. // throw new Exception("修改领料单数量失败!" + sql1);
  1494. // }
  1495. // }
  1496. #endregion
  1497. return "";
  1498. }
  1499. catch (Exception ex)
  1500. {
  1501. return ex.Message;
  1502. }
  1503. }
  1504. //拣料
  1505. public DataTable GetICSMOPickMergeTemp(string ID, string Type)
  1506. {
  1507. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  1508. string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
  1509. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  1510. ID = ID.Replace("'", "''");
  1511. ID = string.IsNullOrWhiteSpace(ID) ? "''" : ID.TrimEnd(',');
  1512. string Code = SqlHelper.GetItemsDetailEnabledMark("MtimeControl");
  1513. string sql = @"EXEC ICSPicking '{0}','{1}','0','{2}'";
  1514. sql = string.Format(sql, ID, Type, WorkPoint);
  1515. var dataset = Repository().FindDataSetBySql(sql);
  1516. if (dataset.Tables[0].Rows.Count == 0)
  1517. return null;
  1518. DataTable table = dataset.Tables[0];
  1519. try
  1520. {
  1521. DataRow[] dss = table.Select("LotNO=''");
  1522. foreach (var item in dss)
  1523. {
  1524. DataRow[] dsss = table.Select("Code='" + item["Code"].ToString() + "' and Sequence='" + item["Sequence"].ToString() + "' and LotNO <> '' ");
  1525. if (dsss != null && dsss.Length > 0)
  1526. {
  1527. table.Rows.Remove(item);
  1528. }
  1529. }
  1530. var result = ConvertCellToString(table);
  1531. if (Invmes.Rows.Count > 0)
  1532. {
  1533. result.Merge(Invmes, false);
  1534. }
  1535. var groupedData = from row in table.AsEnumerable()
  1536. group row by new
  1537. {
  1538. Code = row.Field<string>("Code"),
  1539. Sequence = row.Field<string>("Sequence")
  1540. } into g
  1541. select new
  1542. {
  1543. Code = g.Key.Code,
  1544. Sequence = g.Key.Sequence,
  1545. TotalQTY = g.Sum(x =>
  1546. {
  1547. return !x.IsNull("QTY") ? x.Field<decimal>("QTY") : 0;
  1548. }),
  1549. TotaliQuantity = g.First().IsNull("iQuantity") ? 0 : g.First().Field<decimal>("iQuantity"),
  1550. };
  1551. foreach (var item in groupedData)
  1552. {
  1553. if (item.TotalQTY != item.TotaliQuantity)
  1554. {
  1555. string msg = "单据号:" + item.Code + "行号:" + item.Sequence + "库存不足,请先检查库存";
  1556. throw new Exception(msg);
  1557. }
  1558. }
  1559. return result;
  1560. }
  1561. catch (Exception ex)
  1562. {
  1563. throw new Exception(ex.Message);
  1564. }
  1565. }
  1566. public DataTable ConvertCellToString(DataTable data)
  1567. {
  1568. DataTable dtCloned = data.Clone();
  1569. foreach (DataColumn col in dtCloned.Columns)
  1570. {
  1571. col.DataType = typeof(string);
  1572. }
  1573. foreach (DataRow row in data.Rows)
  1574. {
  1575. DataRow newrow = dtCloned.NewRow();
  1576. foreach (DataColumn column in dtCloned.Columns)
  1577. {
  1578. newrow[column.ColumnName] = row[column.ColumnName].ToString();
  1579. }
  1580. dtCloned.Rows.Add(newrow);
  1581. }
  1582. return dtCloned;
  1583. }
  1584. //接口api解析
  1585. public static string HttpPost(string url, string body)
  1586. {
  1587. try
  1588. {
  1589. Encoding encoding = Encoding.UTF8;
  1590. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  1591. request.Method = "POST";
  1592. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  1593. request.ContentType = "application/json; charset=utf-8";
  1594. byte[] buffer = encoding.GetBytes(body);
  1595. request.ContentLength = buffer.Length;
  1596. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  1597. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  1598. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  1599. {
  1600. return reader.ReadToEnd();
  1601. }
  1602. }
  1603. catch (WebException ex)
  1604. {
  1605. throw new Exception(ex.Message);
  1606. }
  1607. }
  1608. }
  1609. /// <summary>
  1610. /// api/Rd1011/Approve 调用产成品入库单、材料出库单审核
  1611. /// </summary>
  1612. public class ApproveInput
  1613. {
  1614. public string ID { get; set; }//ID
  1615. public string User { get; set; }//用户
  1616. public string Type { get; set; }//类型(入库,倒冲)
  1617. public string WorkPoint { get; set; }//站点
  1618. public bool UpdateStock { get; set; }//是否更新现存量
  1619. public DateTime MTime { get; set; }//时间
  1620. public bool UpdateTodoQuantity { get; set; }//更新待出入数量
  1621. }
  1622. }