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

204 lines
12 KiB

  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. dic = DBHelper.GetAllCode(ERPDB, "mom_moallocate", "1", head.WorkPoint);
  65. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  66. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  67. ids.Add(iChildId);
  68. #region 生产备料表
  69. sql = @"SELECT '{6}' AS AllocateId,d.MoDId,'{2}' AS SortSeq,ISNULL(a.OpSeq, '0000') AS OpSeq,ISNULL(a.ComponentId,c.PartId) AS ComponentId,
  70. ISNULL(a.FVFlag,'1') AS FVFlag,'{3}' AS BaseQtyN,'{4}' AS BaseQtyD,'0' AS ParentScrap,ISNULL(a.CompScrap,'0') AS CompScrap,
  71. '{5}' AS Qty,'0' AS IssQty,'0' AS DeclaredQty, GETDATE() AS StartDemDate, GETDATE() AS EndDemDate,
  72. '{7}' AS WhCode,NULL AS LotNo,'{8}' AS WIPType,ISNULL(a.ByproductFlag,'0') AS ByproductFlag,'0' AS QcFlag,
  73. '0' AS Offset,c.InvCode,ISNULL(a.Free1,'') AS Free1,ISNULL(a.Free2,'') AS Free2,ISNULL(a.Free3,'') AS Free3
  74. ,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
  75. ,ISNULL(a.Free9,'') AS Free9,ISNULL(a.Free10,'') AS Free10,ISNULL(a.OpComponentId,'0') AS OpComponentId,a.Define22,a.Define23,
  76. a.Define24,a.Define25,a.Define26,a.Define27,a.Define28,
  77. a.Define29,a.Define30,a.Define31,a.Define32,a.Define33,
  78. a.Define34,a.Define35,a.Define36,a.Define37,'0' AS ReplenishQty,
  79. a.Remark,'0' AS TransQty,ISNULL(a.ProductType,'1') AS ProductType,'0' AS SoType,NULL AS SoDId,
  80. NULL AS SoCode,NULL AS SoSeq,NULL AS DemandCode,'0' AS QmFlag,'0' AS OrgQty,
  81. '0' AS OrgAuxQty,NULL AS CostItemCode,NULL AS CostItemName,'0' AS RequisitionFlag,'0' AS RequisitionQty,
  82. '0' AS RequisitionIssQty,'0' AS CostWIPRel,NULL AS MoallocateSubId,NULL AS cSubSysBarCode,'0' AS PickingQty,
  83. '0' AS PickingAuxQty,'0' AS UpperMoQty,'0' AS InvAlloeFlag,NULL AS FactoryCode
  84. INTO #TempMOAll
  85. FROM mom_orderdetail d
  86. INNER JOIN bas_part p on d.InvCode = p.InvCode
  87. LEFT JOIN bom_parent q on q.ParentId = p.PartId
  88. LEFT JOIN bom_bom b ON q.BomId=b.BomId AND b.VersionEndDate>=CONVERT(VARCHAR(10),GETDATE(),23)
  89. --
  90. INNER JOIN bas_part c ON c.InvCode='{1}'
  91. LEFT JOIN bom_opcomponent a ON a.ComponentId = c.PartId AND a.BomId=b.BomId
  92. LEFT JOIN bom_opcomponentopt e ON a.OptionsId=e.OptionsId
  93. WHERE d.MoDId='{0}'
  94. ORDER BY d.MoDId";
  95. sql = string.Format(sql, head.DetailID, body.InvCode, body.Sequence, body.BaseQtyN, body.BaseQtyD, body.Quantity, iChildId, body.WHCode, body.SupplyType);
  96. sql += @" INSERT INTO mom_moallocate (AllocateId,MoDId,SortSeq,OpSeq,ComponentId,
  97. FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap,
  98. Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate,
  99. WhCode,LotNo,WIPType,ByproductFlag,QcFlag,
  100. Offset,InvCode,Free1,Free2,Free3,
  101. Free4,Free5,Free6,Free7,Free8,
  102. Free9,Free10,OpComponentId,Define22,Define23,
  103. Define24,Define25,Define26,Define27,Define28,
  104. Define29,Define30,Define31,Define32,Define33,
  105. Define34,Define35,Define36,Define37,ReplenishQty,
  106. Remark,TransQty,ProductType,SoType,SoDId,
  107. SoCode,SoSeq,DemandCode,QmFlag,OrgQty,
  108. OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty,
  109. RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty,
  110. PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode)
  111. SELECT AllocateId,MoDId,SortSeq,OpSeq,ComponentId,
  112. FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap,
  113. Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate,
  114. WhCode,LotNo,WIPType,ByproductFlag,QcFlag,
  115. Offset,InvCode,Free1,Free2,Free3,
  116. Free4,Free5,Free6,Free7,Free8,
  117. Free9,Free10,OpComponentId,Define22,Define23,
  118. Define24,Define25,Define26,Define27,Define28,
  119. Define29,Define30,Define31,Define32,Define33,
  120. Define34,Define35,Define36,Define37,ReplenishQty,
  121. Remark,TransQty,ProductType,SoType,SoDId,
  122. SoCode,SoSeq,DemandCode,QmFlag,OrgQty,
  123. OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty,
  124. RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty,
  125. PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode
  126. FROM #TempMOAll";
  127. sql += @" DROP TABLE #TempMOAll" + Environment.NewLine;
  128. cmd.CommandText = sql;
  129. try
  130. {
  131. int count = cmd.ExecuteNonQuery();
  132. if (count <= 0)
  133. {
  134. throw new Exception("生成生产订单备料表失败,受影响行数<=0;");
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. log.Error("生成生产订单备料表失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  140. throw new Exception("生成生产订单备料表失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  141. }
  142. #endregion
  143. }
  144. #endregion
  145. #region 查询
  146. sql = @" SELECT DISTINCT a.MoDId AS IDs,a.MoDId AS DetailID,a.SortSeq AS DetailSequence,a.InvCode AS DetailInvCode,a.Qty AS DetailQuantity
  147. FROM mom_orderdetail a
  148. INNER JOIN mom_moallocate b ON a.MoDId=b.MoDId
  149. WHERE b.AllocateId in ({0})
  150. SELECT DISTINCT a.MoDId as IDs, AllocateId as PickID,SortSeq as Sequence,
  151. InvCode,Qty as Quantity,0 as Amount,a.WhCode,b.cWhName AS WHName,WIPType AS SupplyType,
  152. '' ProjectCode,'' cBatch,'' version ,'' brand,
  153. isnull(a.Free1,'') as cFree1,
  154. isnull(a.Free2,'') as cFree2,
  155. isnull(a.Free3,'') as cFree3,
  156. isnull(a.Free4,'') as cFree4,
  157. isnull(a.Free5,'') as cFree5,
  158. isnull(a.Free6,'') as cFree6,
  159. isnull(a.Free7,'') as cFree7,
  160. isnull(a.Free8,'') as cFree8,
  161. isnull(a.Free9,'') as cFree9,
  162. isnull(a.Free10,'') as cFree10
  163. FROM mom_moallocate a
  164. LEFT JOIN Warehouse b on a.WhCode=b.cWhCode
  165. WHERE a.AllocateId in ({0}) ";
  166. sql = string.Format(sql, "'" + string.Join("','", ids) + "'");
  167. if (ds != null)
  168. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  169. else
  170. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  171. #endregion
  172. }
  173. cmd.Transaction.Commit();
  174. }
  175. catch (Exception ex)
  176. {
  177. if(cmd.Transaction!=null)
  178. cmd.Transaction.Rollback();
  179. log.Error(ex.Message);
  180. throw new Exception(ex.Message);
  181. }
  182. finally
  183. {
  184. if (conn.State == ConnectionState.Open)
  185. {
  186. conn.Close();
  187. }
  188. conn.Dispose();
  189. }
  190. }
  191. msg = JSON.DataSetToJson(ds, "details", "IDs");
  192. return msg;
  193. }
  194. }
  195. }