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

263 lines
16 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
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. dic = DBHelper.GetAllCode(ERPDB, "mom_moallocate", "1", head.WorkPoint, cmd);
  67. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  68. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  69. ids.Add(iChildId);
  70. #region 生产备料表
  71. sql = @"
  72. IF {10}>0
  73. BEGIN
  74. IF NOT EXISTS (SELECT AllocateId FROM mom_moallocate WHERE AllocateId='{10}')
  75. BEGIN
  76. RAISERROR('AllocateId{10}',16,1)
  77. END
  78. ELSE IF NOT EXISTS (SELECT AllocateId FROM mom_moallocate WHERE AllocateId='{10}' AND MoDId='{0}')
  79. BEGIN
  80. RAISERROR('MoDId{0}AllocateId{10}',16,1)
  81. END
  82. UPDATE mom_moallocate SET BaseQtyN='{3}',BaseQtyD='{4}',Qty='{5}',AuxQty='{11}',WhCode='{7}',WIPType='{8}'
  83. WHERE AllocateId='{10}'
  84. END
  85. ELSE
  86. BEGIN
  87. 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,
  88. ISNULL(a.FVFlag,'1') AS FVFlag,'{3}' AS BaseQtyN,'{4}' AS BaseQtyD,'0' AS ParentScrap,ISNULL(a.CompScrap,'0') AS CompScrap,
  89. '{5}' AS Qty,'0' AS IssQty,'0' AS DeclaredQty, GETDATE() AS StartDemDate, GETDATE() AS EndDemDate,
  90. '{7}' AS WhCode,NULL AS LotNo,'{8}' AS WIPType,ISNULL(a.ByproductFlag,'0') AS ByproductFlag,'0' AS QcFlag,
  91. '0' AS Offset,c.InvCode,ISNULL(a.Free1,'') AS Free1,ISNULL(a.Free2,'') AS Free2,ISNULL(a.Free3,'') AS Free3
  92. ,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
  93. ,ISNULL(a.Free9,'') AS Free9,ISNULL(a.Free10,'') AS Free10,ISNULL(a.OpComponentId,'0') AS OpComponentId,a.Define22,a.Define23,
  94. a.Define24,a.Define25,a.Define26,a.Define27,a.Define28,
  95. a.Define29,a.Define30,a.Define31,a.Define32,a.Define33,
  96. a.Define34,a.Define35,a.Define36,a.Define37,'0' AS ReplenishQty,
  97. a.Remark,'0' AS TransQty,ISNULL(a.ProductType,'1') AS ProductType,'0' AS SoType,NULL AS SoDId,
  98. NULL AS SoCode,NULL AS SoSeq,NULL AS DemandCode,'0' AS QmFlag,'0' AS OrgQty,
  99. '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,
  100. '0' AS RequisitionIssQty,'0' AS CostWIPRel,NULL AS cSubSysBarCode,'0' AS PickingQty,
  101. '0' AS PickingAuxQty,'0' AS UpperMoQty,'0' AS InvAlloeFlag,NULL AS FactoryCode,'{11}' AS AuxQty
  102. ,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
  103. INTO #TempMOAll
  104. FROM mom_orderdetail d
  105. INNER JOIN bas_part p on d.PartId = p.PartId
  106. LEFT JOIN bom_parent q on q.ParentId = p.PartId
  107. LEFT JOIN bom_bom b ON q.BomId=b.BomId AND b.VersionEndDate>=CONVERT(VARCHAR(10),GETDATE(),23)
  108. --
  109. INNER JOIN bas_part c ON c.InvCode='{1}'
  110. LEFT JOIN bom_opcomponent a ON a.ComponentId = c.PartId AND a.BomId=b.BomId
  111. LEFT JOIN bom_opcomponentopt e ON a.OptionsId=e.OptionsId
  112. LEFT JOIN mom_moallocate o on d.MoDId = o.MoDId AND o.AllocateId='{2}'
  113. LEFT JOIN mom_moallocatesub n on o.AllocateId = n.AllocateId AND c.PartId=n.PartId
  114. WHERE d.MoDId='{0}'
  115. ORDER BY d.MoDId
  116. IF @@ROWCOUNT<=0
  117. BEGIN
  118. RAISERROR('{1}MoDId{0}',16,1)
  119. END
  120. IF {2}>0
  121. BEGIN
  122. IF EXISTS (SELECT SubAllocateId FROM #TempMOAll WHERE SubAllocateId IS NULL)
  123. BEGIN
  124. RAISERROR('ParentAllocateId{2}',16,1)
  125. END
  126. IF EXISTS (SELECT SubMoallocateSubId FROM #TempMOAll WHERE SubMoallocateSubId IS NULL)
  127. BEGIN
  128. INSERT INTO mom_moallocatesub(MoallocateSubId,AllocateId,Sequence,PartId,Factor,EffBegDate,EffEndDate,ReplaceFlag)
  129. SELECT MoallocateSubId,SubAllocateId,Sequence,PartId,Factor,EffBegDate,EffEndDate,ReplaceFlag FROM #TempMOAll
  130. END
  131. ELSE
  132. BEGIN
  133. UPDATE a SET Factor = b.Factor
  134. FROM mom_moallocatesub a
  135. INNER JOIN #TempMOAll b ON a.MoallocateSubId=b.MoallocateSubId
  136. END
  137. END
  138. INSERT INTO mom_moallocate (AllocateId,MoDId,SortSeq,OpSeq,ComponentId,
  139. FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap,
  140. Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate,
  141. WhCode,LotNo,WIPType,ByproductFlag,QcFlag,
  142. Offset,InvCode,Free1,Free2,Free3,
  143. Free4,Free5,Free6,Free7,Free8,
  144. Free9,Free10,OpComponentId,Define22,Define23,
  145. Define24,Define25,Define26,Define27,Define28,
  146. Define29,Define30,Define31,Define32,Define33,
  147. Define34,Define35,Define36,Define37,ReplenishQty,
  148. Remark,TransQty,ProductType,SoType,SoDId,
  149. SoCode,SoSeq,DemandCode,QmFlag,OrgQty,
  150. OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty,
  151. RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty,
  152. PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode,MoallocateSubId,AuxQty)
  153. SELECT AllocateId,MoDId,SortSeq,OpSeq,ComponentId,
  154. FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap,
  155. Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate,
  156. WhCode,LotNo,WIPType,ByproductFlag,QcFlag,
  157. Offset,InvCode,Free1,Free2,Free3,
  158. Free4,Free5,Free6,Free7,Free8,
  159. Free9,Free10,OpComponentId,Define22,Define23,
  160. Define24,Define25,Define26,Define27,Define28,
  161. Define29,Define30,Define31,Define32,Define33,
  162. Define34,Define35,Define36,Define37,ReplenishQty,
  163. Remark,TransQty,ProductType,SoType,SoDId,
  164. SoCode,SoSeq,DemandCode,QmFlag,OrgQty,
  165. OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty,
  166. RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty,
  167. PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode,MoallocateSubId,AuxQty
  168. FROM #TempMOAll
  169. DROP TABLE #TempMOAll
  170. END ";
  171. //根据替代料数量更新主料数量,主料数量作为参数传递,此处不需要更新
  172. //UPDATE a SET Qty = Qty-b.Qty
  173. // FROM mom_moallocate a
  174. // INNER JOIN(
  175. // SELECT d.AllocateId, SUM(c.Qty / d.Factor) AS Qty
  176. // FROM mom_moallocate c
  177. // INNER JOIN mom_moallocatesub d ON c.MoallocateSubId = d.MoallocateSubId
  178. // WHERE c.MoDId = '{0}'
  179. // GROUP BY d.AllocateId
  180. // ) b ON a.AllocateId = b.AllocateId
  181. 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);
  182. cmd.CommandText = sql;
  183. try
  184. {
  185. int count = cmd.ExecuteNonQuery();
  186. if (count <= 0)
  187. {
  188. throw new Exception("生成生产订单备料表失败,受影响行数<=0;");
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. log.Error("生成生产订单备料表失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  194. throw new Exception("生成生产订单备料表失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  195. }
  196. #endregion
  197. }
  198. #endregion
  199. #region 查询
  200. sql = @" SELECT DISTINCT a.MoDId AS IDs,a.MoDId AS DetailID,a.SortSeq AS DetailSequence,a.InvCode AS DetailInvCode,a.Qty AS DetailQuantity
  201. FROM mom_orderdetail a
  202. INNER JOIN mom_moallocate b ON a.MoDId=b.MoDId
  203. WHERE b.AllocateId in ({0})
  204. SELECT DISTINCT a.MoDId as IDs, AllocateId as PickID,SortSeq as Sequence,
  205. InvCode,Qty as Quantity,AuxQty as Amount,a.WhCode,b.cWhName AS WHName,WIPType AS SupplyType,
  206. '' ProjectCode,'' cBatch,'' version ,'' brand,
  207. isnull(a.Free1,'') as cFree1,
  208. isnull(a.Free2,'') as cFree2,
  209. isnull(a.Free3,'') as cFree3,
  210. isnull(a.Free4,'') as cFree4,
  211. isnull(a.Free5,'') as cFree5,
  212. isnull(a.Free6,'') as cFree6,
  213. isnull(a.Free7,'') as cFree7,
  214. isnull(a.Free8,'') as cFree8,
  215. isnull(a.Free9,'') as cFree9,
  216. isnull(a.Free10,'') as cFree10
  217. FROM mom_moallocate a
  218. LEFT JOIN Warehouse b on a.WhCode=b.cWhCode
  219. WHERE a.AllocateId in ({0}) ";
  220. sql = string.Format(sql, "'" + string.Join("','", ids) + "'");
  221. if (ds != null)
  222. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  223. else
  224. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  225. #endregion
  226. }
  227. cmd.Transaction.Commit();
  228. }
  229. catch (Exception ex)
  230. {
  231. if(cmd.Transaction!=null)
  232. cmd.Transaction.Rollback();
  233. log.Error(ex.Message);
  234. throw new Exception(ex.Message);
  235. }
  236. finally
  237. {
  238. if (conn.State == ConnectionState.Open)
  239. {
  240. conn.Close();
  241. }
  242. conn.Dispose();
  243. }
  244. }
  245. msg = JSON.DataSetToJson(ds, "details", "IDs");
  246. return msg;
  247. }
  248. }
  249. }