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

281 lines
18 KiB

2 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 生产订单备料表
  15. /// </summary>
  16. public class ManufactureOrderPick
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  20. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  21. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  22. /// <summary>
  23. /// 创建生产订单备料表
  24. /// </summary>
  25. /// <param name="infos"></param>
  26. /// <returns></returns>
  27. public string CreateManufactureOrderPick(List<ICSManufactureOrderPick> Bills)
  28. {
  29. string msg = "";
  30. string connS = "";
  31. DataSet ds = null;
  32. if (Bills.Count <= 0)
  33. {
  34. throw new Exception("传送数据为空!");
  35. }
  36. string res = string.Empty;
  37. SqlConnection conn = new SqlConnection();
  38. SqlCommand cmd = new SqlCommand();
  39. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  40. foreach (string WorkPoint in result)
  41. {
  42. try
  43. {
  44. connS = string.Format(connString, WorkPoint);
  45. conn = new System.Data.SqlClient.SqlConnection(connS);
  46. conn.Open();
  47. SqlTransaction sqlTran = conn.BeginTransaction();
  48. cmd = new SqlCommand();
  49. cmd.Transaction = sqlTran;
  50. cmd.Connection = conn;
  51. var list = Bills.Where(a => a.WorkPoint == WorkPoint);
  52. foreach (ICSManufactureOrderPick head in list)
  53. {
  54. ICSUserInfo userInfo = new ICSUserInfo();
  55. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  56. string[] ss = head.WorkPoint.Split('_');
  57. ERPDB = ss[1];
  58. Dictionary<string, int> dic;
  59. string sql = "";
  60. List<int> ids = new List<int>();
  61. #region 备料表
  62. foreach (ICSManufactureOrderPicks body in head.details)
  63. {
  64. if (body.ParentAllocateId > 0 && body.Factor <= 0)
  65. throw new Exception("需要替代时替代比例必须大于零!");
  66. int iFatherId =0;
  67. int iChildId = 0;
  68. if (body.AllocateId <= 0)
  69. {
  70. dic = DBHelper.GetAllCode(ERPDB, "mom_moallocate", "1", head.WorkPoint, cmd);
  71. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  72. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  73. ids.Add(iChildId);
  74. }
  75. else
  76. {
  77. ids.Add(body.AllocateId);
  78. }
  79. #region 生产备料表
  80. sql = @"UPDATE a SET ModifyTime=GETDATE() --更新工单修改时间,以便再次同步
  81. FROM mom_order a
  82. INNER JOIN mom_orderdetail b ON a.MoId=b.MoId
  83. WHERE MoDId='{0}'
  84. IF {10}>0
  85. BEGIN
  86. IF NOT EXISTS (SELECT AllocateId FROM mom_moallocate WHERE AllocateId='{10}')
  87. BEGIN
  88. RAISERROR('AllocateId{10}',16,1)
  89. END
  90. ELSE IF NOT EXISTS (SELECT AllocateId FROM mom_moallocate WHERE AllocateId='{10}' AND MoDId='{0}')
  91. BEGIN
  92. RAISERROR('MoDId{0}AllocateId{10}',16,1)
  93. END
  94. UPDATE mom_moallocate SET BaseQtyN='{3}',BaseQtyD='{4}',Qty='{5}',AuxQty='{11}',WhCode='{7}',WIPType='{8}',Define33='{12}'
  95. WHERE AllocateId='{10}'
  96. END
  97. ELSE
  98. BEGIN
  99. SELECT '{6}' AS AllocateId,d.MoDId,ISNULL((SELECT MAX(SortSeq) FROM mom_moallocate WHERE MoDId=d.MoDId), 0)+10 AS SortSeq,ISNULL(a.OpSeq, '0000') AS OpSeq,ISNULL(a.ComponentId,c.PartId) AS ComponentId,
  100. ISNULL(a.FVFlag,'1') AS FVFlag,'{3}' AS BaseQtyN,'{4}' AS BaseQtyD,'0' AS ParentScrap,ISNULL(a.CompScrap,'0') AS CompScrap,
  101. '{5}' AS Qty,'0' AS IssQty,'0' AS DeclaredQty, GETDATE() AS StartDemDate, GETDATE() AS EndDemDate,
  102. '{7}' AS WhCode,NULL AS LotNo,'{8}' AS WIPType,ISNULL(a.ByproductFlag,'0') AS ByproductFlag,'0' AS QcFlag,
  103. '0' AS Offset,c.InvCode,ISNULL(a.Free1,'') AS Free1,ISNULL(a.Free2,'') AS Free2,ISNULL(a.Free3,'') AS Free3
  104. ,ISNULL(a.Free4,'') AS Free4,ISNULL(a.Free5,'') AS Free5,ISNULL(a.Free6,'') AS Free6,ISNULL(a.Free7,'') AS Free7,ISNULL(a.Free8,'') AS Free8
  105. ,ISNULL(a.Free9,'') AS Free9,ISNULL(a.Free10,'') AS Free10,ISNULL(a.OpComponentId,'0') AS OpComponentId,a.Define22,a.Define23,
  106. a.Define24,a.Define25,a.Define26,a.Define27,a.Define28,
  107. a.Define29,a.Define30,a.Define31,a.Define32,'{12}' AS Define33,
  108. a.Define34,a.Define35,a.Define36,a.Define37,'0' AS ReplenishQty,
  109. a.Remark,'0' AS TransQty,ISNULL(a.ProductType,'1') AS ProductType,'0' AS SoType,NULL AS SoDId,
  110. NULL AS SoCode,NULL AS SoSeq,NULL AS DemandCode,'0' AS QmFlag,'0' AS OrgQty,
  111. '0' AS OrgAuxQty,NULL AS CostItemCode,NULL AS CostItemName,(SELECT TOP 1 RequisitionFlag FROM mom_moallocate WHERE MoDId=d.MoDId ORDER BY RequisitionFlag DESC) AS RequisitionFlag,'0' AS RequisitionQty,
  112. '0' AS RequisitionIssQty,'0' AS CostWIPRel,NULL AS cSubSysBarCode,'0' AS PickingQty,
  113. '0' AS PickingAuxQty,'0' AS UpperMoQty,'0' AS InvAlloeFlag,NULL AS FactoryCode,'{11}' AS AuxQty
  114. ,ISNULL((SELECT MAX(Sequence) FROM mom_moallocatesub WHERE AllocateId=o.AllocateId), 0)+1 AS Sequence,o.AllocateId AS SubAllocateId,n.MoallocateSubId AS SubMoallocateSubId,ISNULL(n.MoallocateSubId, NEWID()) AS MoallocateSubId,c.PartId,'{9}' AS Factor,'2000-01-01' AS EffBegDate,'2099-12-31' AS EffEndDate,'0' AS ReplaceFlag
  115. INTO #TempMOAll
  116. FROM mom_orderdetail d
  117. INNER JOIN bas_part p on d.PartId = p.PartId
  118. LEFT JOIN bom_parent q on q.ParentId = p.PartId
  119. LEFT JOIN bom_bom b ON q.BomId=b.BomId AND b.VersionEndDate>=CONVERT(VARCHAR(10),GETDATE(),23)
  120. --
  121. INNER JOIN bas_part c ON c.InvCode='{1}'
  122. LEFT JOIN bom_opcomponent a ON a.ComponentId = c.PartId AND a.BomId=b.BomId
  123. LEFT JOIN bom_opcomponentopt e ON a.OptionsId=e.OptionsId
  124. LEFT JOIN mom_moallocate o on d.MoDId = o.MoDId AND o.AllocateId='{2}'
  125. LEFT JOIN mom_moallocatesub n on o.AllocateId = n.AllocateId AND c.PartId=n.PartId
  126. WHERE d.MoDId='{0}'
  127. ORDER BY d.MoDId
  128. IF @@ROWCOUNT<=0
  129. BEGIN
  130. RAISERROR('{1}MoDId{0}',16,1)
  131. END
  132. IF {2}>0
  133. BEGIN
  134. IF EXISTS (SELECT SubAllocateId FROM #TempMOAll WHERE SubAllocateId IS NULL)
  135. BEGIN
  136. RAISERROR('ParentAllocateId{2}',16,1)
  137. END
  138. IF EXISTS (SELECT SubMoallocateSubId FROM #TempMOAll WHERE SubMoallocateSubId IS NULL)
  139. BEGIN
  140. INSERT INTO mom_moallocatesub(MoallocateSubId,AllocateId,Sequence,PartId,Factor,EffBegDate,EffEndDate,ReplaceFlag)
  141. SELECT MoallocateSubId,SubAllocateId,Sequence,PartId,Factor,EffBegDate,EffEndDate,ReplaceFlag FROM #TempMOAll
  142. END
  143. ELSE
  144. BEGIN
  145. UPDATE a SET Factor = b.Factor
  146. FROM mom_moallocatesub a
  147. INNER JOIN #TempMOAll b ON a.MoallocateSubId=b.MoallocateSubId
  148. END
  149. END
  150. INSERT INTO mom_moallocate (AllocateId,MoDId,SortSeq,OpSeq,ComponentId,
  151. FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap,
  152. Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate,
  153. WhCode,LotNo,WIPType,ByproductFlag,QcFlag,
  154. Offset,InvCode,Free1,Free2,Free3,
  155. Free4,Free5,Free6,Free7,Free8,
  156. Free9,Free10,OpComponentId,Define22,Define23,
  157. Define24,Define25,Define26,Define27,Define28,
  158. Define29,Define30,Define31,Define32,Define33,
  159. Define34,Define35,Define36,Define37,ReplenishQty,
  160. Remark,TransQty,ProductType,SoType,SoDId,
  161. SoCode,SoSeq,DemandCode,QmFlag,OrgQty,
  162. OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty,
  163. RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty,
  164. PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode,MoallocateSubId,AuxQty)
  165. SELECT AllocateId,MoDId,SortSeq,OpSeq,ComponentId,
  166. FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap,
  167. Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate,
  168. WhCode,LotNo,WIPType,ByproductFlag,QcFlag,
  169. Offset,InvCode,Free1,Free2,Free3,
  170. Free4,Free5,Free6,Free7,Free8,
  171. Free9,Free10,OpComponentId,Define22,Define23,
  172. Define24,Define25,Define26,Define27,Define28,
  173. Define29,Define30,Define31,Define32,Define33,
  174. Define34,Define35,Define36,Define37,ReplenishQty,
  175. Remark,TransQty,ProductType,SoType,SoDId,
  176. SoCode,SoSeq,DemandCode,QmFlag,OrgQty,
  177. OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty,
  178. RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty,
  179. PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode,MoallocateSubId,AuxQty
  180. FROM #TempMOAll
  181. DROP TABLE #TempMOAll
  182. END
  183. IF EXISTS (SELECT AllocateId FROM mom_moallocate WHERE AllocateId='{10}' AND Qty<IssQty)
  184. BEGIN
  185. RAISERROR('AllocateId{10}',16,1)
  186. END";
  187. //根据替代料数量更新主料数量,主料数量作为参数传递,此处不需要更新
  188. //UPDATE a SET Qty = Qty-b.Qty
  189. // FROM mom_moallocate a
  190. // INNER JOIN(
  191. // SELECT d.AllocateId, SUM(c.Qty / d.Factor) AS Qty
  192. // FROM mom_moallocate c
  193. // INNER JOIN mom_moallocatesub d ON c.MoallocateSubId = d.MoallocateSubId
  194. // WHERE c.MoDId = '{0}'
  195. // GROUP BY d.AllocateId
  196. // ) b ON a.AllocateId = b.AllocateId
  197. sql = string.Format(sql, head.DetailID, body.InvCode, body.ParentAllocateId, body.BaseQtyN, body.BaseQtyD, body.Quantity, iChildId, body.WHCode, body.SupplyType, body.Factor, body.AllocateId, body.Amount, body.Define33);
  198. cmd.CommandText = sql;
  199. try
  200. {
  201. int count = cmd.ExecuteNonQuery();
  202. if (count <= 0)
  203. {
  204. throw new Exception("生成生产订单备料表失败,受影响行数<=0;");
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. log.Error("生成生产订单备料表失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  210. throw new Exception("生成生产订单备料表失败!异常:" + ex.Message);
  211. }
  212. #endregion
  213. }
  214. #endregion
  215. #region 查询
  216. sql = @" SELECT DISTINCT a.MoDId AS IDs,a.MoDId AS DetailID,a.SortSeq AS DetailSequence,a.InvCode AS DetailInvCode,a.Qty AS DetailQuantity
  217. FROM mom_orderdetail a
  218. INNER JOIN mom_moallocate b ON a.MoDId=b.MoDId
  219. WHERE b.AllocateId in ({0})
  220. SELECT DISTINCT a.MoDId as IDs, AllocateId as PickID,SortSeq as Sequence,
  221. InvCode,Qty as Quantity,AuxQty as Amount,a.WhCode,b.cWhName AS WHName,WIPType AS SupplyType,
  222. '' ProjectCode,'' cBatch,'' version ,'' brand,
  223. isnull(a.Define33,'') as Define33,
  224. isnull(a.Free1,'') as cFree1,
  225. isnull(a.Free2,'') as cFree2,
  226. isnull(a.Free3,'') as cFree3,
  227. isnull(a.Free4,'') as cFree4,
  228. isnull(a.Free5,'') as cFree5,
  229. isnull(a.Free6,'') as cFree6,
  230. isnull(a.Free7,'') as cFree7,
  231. isnull(a.Free8,'') as cFree8,
  232. isnull(a.Free9,'') as cFree9,
  233. isnull(a.Free10,'') as cFree10
  234. FROM mom_moallocate a
  235. LEFT JOIN Warehouse b on a.WhCode=b.cWhCode
  236. WHERE a.AllocateId in ({0}) ";
  237. sql = string.Format(sql, "'" + string.Join("','", ids) + "'");
  238. if (ds != null)
  239. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  240. else
  241. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  242. #endregion
  243. cmd.CommandType = CommandType.Text;
  244. cmd.Parameters.Clear();
  245. }
  246. cmd.Transaction.Commit();
  247. }
  248. catch (Exception ex)
  249. {
  250. if (cmd.Transaction != null)
  251. cmd.Transaction.Rollback();
  252. log.Error(ex.Message);
  253. throw new Exception(ex.Message);
  254. }
  255. finally
  256. {
  257. if (conn.State == ConnectionState.Open)
  258. {
  259. conn.Close();
  260. }
  261. conn.Dispose();
  262. }
  263. }
  264. msg = JSON.DataSetToJson(ds, "details", "IDs");
  265. return msg;
  266. }
  267. }
  268. }