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

559 lines
34 KiB

3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 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 ManufactureOrder
  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 GET(List<ICSManufactureOrder> infos)
  28. {
  29. DataTable dt = null;
  30. DataTable dtNew = null;
  31. string connS = "";
  32. string json = "";
  33. if (infos.Count <= 0)
  34. {
  35. throw new Exception("传送数据为空!");
  36. }
  37. string res = string.Empty;
  38. SqlConnection conn = new SqlConnection();
  39. SqlCommand cmd = new SqlCommand();
  40. string sql = string.Empty;
  41. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  42. foreach (string WorkPoint in result)
  43. {
  44. try
  45. {
  46. connS = string.Format(connString, WorkPoint);
  47. conn = new System.Data.SqlClient.SqlConnection(connS);
  48. conn.Open();
  49. SqlTransaction sqlTran = conn.BeginTransaction();
  50. cmd = new SqlCommand();
  51. cmd.Transaction = sqlTran;
  52. cmd.Connection = conn;
  53. foreach (ICSManufactureOrder info in infos)
  54. {
  55. if (WorkPoint != info.WorkPoint)
  56. {
  57. continue;
  58. }
  59. ICSUserInfo userInfo = new ICSUserInfo();
  60. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  61. if (info.MTime < new DateTime(2000, 01, 01))
  62. throw new Exception("请输入正确的操作时间:" + info.MTime);
  63. sql = @" select a.MoId ,a.MoCode ,a.CreateUser ,a.CreateTime ,b.RelsUser ,b.RelsTime ,b.MoDId ,b.SortSeq,b.InvCode,b.Qty,b.AuxQty,b.QualifiedInQty,b.MDeptCode,
  64. e.CDEPNAME, b.WhCode cWhCode,f.cWhName,c.StartDate,c.DueDate FROM dbo.mom_order a
  65. INNER JOIN dbo.mom_orderdetail b ON a.MoId = b.MoId
  66. INNER JOIN dbo.mom_morder c ON b.MoDId = c.MoDId
  67. LEFT JOIN dbo.mom_orderdetail_extradefine d ON b.MoDId = d.MoDId
  68. INNER JOIN DEPARTMENT e ON b.MDeptCode=e.CDEPCODE
  69. INNER JOIN Warehouse f ON b.WhCode=f.cWhCode WHERE 1=1";
  70. if (!string.IsNullOrWhiteSpace(info.MOCode))
  71. {
  72. sql += " and a.MoCode='{0}'";
  73. }
  74. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  75. {
  76. sql += " and ISNULL(b.cbCloseTime,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime))))>='{1}'";
  77. }
  78. if (!string.IsNullOrWhiteSpace(info.User))
  79. {
  80. sql += "and a.CMAKER='{2}'";
  81. }
  82. sql = string.Format(sql, info.MOCode, info.MTime, userInfo.UserName);
  83. dt = DBHelper.SQlReturnData(sql, cmd);
  84. if (dt.Rows.Count <= 0 || dt == null)
  85. throw new Exception("生产订单:" + info.MOCode + ",无信息!");
  86. if (dtNew == null)
  87. dtNew = dt;
  88. else
  89. dtNew.Merge(dt);
  90. }
  91. cmd.Transaction.Commit();
  92. }
  93. catch (Exception ex)
  94. {
  95. if (cmd.Transaction != null)
  96. cmd.Transaction.Rollback();
  97. log.Error(ex.Message);
  98. throw new Exception(ex.Message);
  99. }
  100. finally
  101. {
  102. if (conn.State == ConnectionState.Open)
  103. {
  104. conn.Close();
  105. }
  106. conn.Dispose();
  107. }
  108. }
  109. json = JsonConvert.SerializeObject(dtNew);
  110. return json;
  111. }
  112. /// <summary>
  113. /// 创建生产订单
  114. /// </summary>
  115. /// <param name="infos"></param>
  116. /// <returns></returns>
  117. public string CreateManufactureOrder(List<ICSManufactureOrder> Bills)
  118. {
  119. string msg = "";
  120. List<ICSManufactureOrder> szJson = new List<ICSManufactureOrder>();
  121. string connS = "";
  122. DateTime time = DateTime.Now;
  123. int num = 0;
  124. VouchKey key = new VouchKey();
  125. //int iBaseCodeLen = Convert.ToInt32(dic["iBaseCodeLen"].ToString());
  126. //int cVouchCodeBase = Convert.ToInt32(dic["cVouchCodeBase"].ToString());
  127. List<ICSPOArrive> datas = new List<ICSPOArrive>();
  128. if (Bills.Count <= 0)
  129. {
  130. throw new Exception("传送数据为空!");
  131. }
  132. string res = string.Empty;
  133. SqlConnection conn = new SqlConnection();
  134. SqlCommand cmd = new SqlCommand();
  135. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  136. foreach (string WorkPoint in result)
  137. {
  138. try
  139. {
  140. connS = string.Format(connString, WorkPoint);
  141. conn = new System.Data.SqlClient.SqlConnection(connS);
  142. conn.Open();
  143. SqlTransaction sqlTran = conn.BeginTransaction();
  144. cmd = new SqlCommand();
  145. cmd.Transaction = sqlTran;
  146. cmd.Connection = conn;
  147. foreach (ICSManufactureOrder head in Bills)
  148. {
  149. if (WorkPoint != head.WorkPoint)
  150. {
  151. continue;
  152. }
  153. ICSUserInfo userInfo = new ICSUserInfo();
  154. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  155. num = head.details.Count();
  156. string[] ss = head.WorkPoint.Split('_');
  157. ERPDB = ss[1];
  158. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "mom_order", "" + num + "", head.WorkPoint, cmd);
  159. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  160. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  161. DateTime date = DateTime.Now;
  162. string iBaseCodeLen = DBHelper.GetAllRDCode("MO21", "" + time + "", "admin", "", head.WorkPoint, cmd);
  163. if (head.MTime < new DateTime(2000, 01, 01))
  164. throw new Exception("请输入正确的操作时间:" + head.MTime);
  165. string sql = "";
  166. #region 生产订单
  167. sql = @"INSERT INTO mom_order(MoId, MoCode,cSysBarCode, CreateDate, CreateTime,
  168. CreateUser, ModifyDate, ModifyTime, ModifyUser, UpdCount ,
  169. VTid,RelsVTid)
  170. values (@MoId,@MoCode,NULL, CONVERT(VARCHAR(10),GETDATE(),23), GETDATE(),
  171. '@CreateUser', NULL, NULL, NULL, 0 ,
  172. @VTID,0)" + Environment.NewLine;
  173. cmd.Parameters.Clear();
  174. cmd.Parameters.Add(new SqlParameter("@MoId", iFatherId));
  175. cmd.Parameters.Add(new SqlParameter("@MoCode", iBaseCodeLen));
  176. cmd.Parameters.Add(new SqlParameter("@CreateUser", userInfo.UserName));
  177. cmd.Parameters.Add(new SqlParameter("@VTID", iChildId));
  178. cmd.CommandText = sql;
  179. try
  180. {
  181. int count = cmd.ExecuteNonQuery();
  182. if (count <= 0)
  183. {
  184. log.Error("生成生产顶订单表头失败,受影响行数<=0;");
  185. throw new Exception("生成生产顶订单表头失败,受影响行数<=0;");
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. log.Error("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  191. throw new Exception("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  192. }
  193. #endregion
  194. #region 采购入库单表体
  195. foreach (ICSManufactureOrders body in head.details)
  196. {
  197. sql += @"INSERT INTO mom_orderdetail(MoDId, MoId, SortSeq, MoClass, MoTypeId,
  198. Qty, MrpQty, AuxUnitCode, AuxQty, ChangeRate,
  199. MoLotCode, WhCode, MDeptCode, OrderType, OrderDId,
  200. OrderCode, OrderSeq, CustCode, DeclaredQty, QualifiedInQty,
  201. Status, OrgStatus, BomId, RoutingId, CustBomId,
  202. DemandId, PlanCode, PartId, InvCode,cbSysBarCode ,
  203. Free1, Free2, Free3, Free4, Free5, Free6, Free7, Free8, Free9, Free10 ,
  204. SfcFlag, CrpFlag, QcFlag,LeadTime,WIPType,
  205. SupplyWhCode,OpScheduleType,ReasonCode,Remark ,SourceQCId,
  206. SourceQCDId,SourceMoId,SourceMoDId,SourceQCVouchType,SourceSvcVouchType,
  207. PAllocateId ,RelsTime,AuditStatus,Iswfcontrolled,iVerifyState,
  208. iReturnCount ,CollectiveFlag,SoType,ReformFlag, RelsUser,
  209. RelsDate, FmFlag,BomType,RoutingType,OrdFlag,
  210. RunCardFlag,RequisitionFlag,AlloVTid,RelsAlloVTid)
  211. SELECT @MoDId, @MoId, @SortSeq, '1', NULL,
  212. @Qty, 0, NULL, @AuxQty, NULL,
  213. NULL, NULL, @MDeptCode, '0', '0',
  214. NULL, NULL,NULL, '0', '0',
  215. '3', '2', a.BomId, '0', '0',
  216. '0', NULL, c.PartId, @InvCode,'' ,
  217. '', '', '', '', '', '', '', '', '', '' ,
  218. '0', '0', '0', '0','',
  219. NULL,'3',NULL,'','0',
  220. '0','0','0','0','0',
  221. '0',GETDATE(),'1','0','0',
  222. '0' ,'0','0','0',@RelsUser,
  223. NULL, '0' ,a.BomType,'0','0',
  224. '0','0',NULL,NULL
  225. FROM dbo.bom_bom a
  226. INNER JOIN dbo.bom_parent b ON a.BomId = b.BomId
  227. INNER JOIN dbo.bas_part c ON b.ParentId = c.PartId
  228. WHERE c.InvCode=@InvCode
  229. " + Environment.NewLine;
  230. cmd.Parameters.Clear();
  231. cmd.Parameters.Add(new SqlParameter("@MoDId", iChildId));
  232. cmd.Parameters.Add(new SqlParameter("@SortSeq", body.Sequence));
  233. cmd.Parameters.Add(new SqlParameter("@MDeptCode", body.DepCode));
  234. cmd.Parameters.Add(new SqlParameter("@Qty", body.Quantity));
  235. cmd.Parameters.Add(new SqlParameter("@AuxQty", body.Amount.ToString("0.00")));
  236. cmd.Parameters.Add(new SqlParameter("@InvCode", body.InvCode));
  237. cmd.Parameters.Add(new SqlParameter("@RelsUser", head.User));
  238. cmd.Parameters.Add(new SqlParameter("@MoId", iFatherId));
  239. //cmd.Parameters.Add(new SqlParameter("@RelsDate", body.Quantity));
  240. cmd.CommandText = sql;
  241. try
  242. {
  243. int count = cmd.ExecuteNonQuery();
  244. if (count <= 0)
  245. {
  246. log.Error("生成生产顶订单表头失败,受影响行数<=0;");
  247. throw new Exception("生成生产顶订单表头失败,受影响行数<=0;");
  248. }
  249. }
  250. catch (Exception ex)
  251. {
  252. log.Error("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  253. throw new Exception("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  254. }
  255. #region 生产订单资料
  256. sql += @"INSERT INTO mom_morder(MoDId, MoId, StartDate, DueDate)
  257. values (" + iChildId + ", " + iFatherId + ", '" + body.StartDate + "', '" + body.DueDate + "') " + Environment.NewLine;
  258. cmd.CommandText = sql;
  259. try
  260. {
  261. int count = cmd.ExecuteNonQuery();
  262. if (count <= 0)
  263. {
  264. log.Error("生产订单资料表失败,受影响行数<=0;");
  265. throw new Exception("生产订单资料表失败,受影响行数<=0;");
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. log.Error("生产订单资料表失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  271. throw new Exception("生产订单资料表失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  272. }
  273. #endregion
  274. #region 考虑虚拟件,如果包含虚拟件,取虚拟件的下一级,如果虚拟件的下级bom未维护,不做处理
  275. sql += @"SELECT c.InvCode AS PInvCode,0 AS [Level], CAST(1 AS nvarchar(MAX)) AS SEQ,CAST(1 AS nvarchar(MAX)) AS Sort,
  276. d.MoDId,a.SortSeq,a.OpSeq,a.ComponentId,
  277. a.FVFlag,a.BaseQtyN,a.BaseQtyD,'0' AS ParentScrap,a.CompScrap,
  278. CAST(d.Qty AS DECIMAL(28,6)) AS Qty,'0' AS IssQty,'0' AS DeclaredQty, '@StartDate' AS StartDemDate, '@DueDate' AS EndDemDate,
  279. NULL AS WhCode,NULL AS LotNo,e.WIPType,a.ByproductFlag,'0' AS QcFlag,
  280. '0' AS Offset,a.Free1,a.Free2,a.Free3,
  281. a.Free4,a.Free5,a.Free6,a.Free7,a.Free8,
  282. a.Free9,a.Free10,a.OpComponentId,a.Define22,a.Define23,
  283. a.Define24,a.Define25,a.Define26,a.Define27,a.Define28,
  284. a.Define29,a.Define30,a.Define31,a.Define32,a.Define33,
  285. a.Define34,a.Define35,a.Define36,a.Define37,'0' AS ReplenishQty,
  286. a.Remark,'0' AS TransQty,a.ProductType,'0' AS SoType,NULL AS SoDId,
  287. NULL AS SoCode,NULL AS SoSeq,NULL AS DemandCode,'0' AS QmFlag,'0' AS OrgQty,
  288. '0' AS OrgAuxQty,NULL AS CostItemCode,NULL AS CostItemName,'0' AS RequisitionFlag,'0' AS RequisitionQty,
  289. '0' AS RequisitionIssQty,'0' AS CostWIPRel,NULL AS MoallocateSubId,NULL AS cSubSysBarCode,'0' AS PickingQty,
  290. '0' AS PickingAuxQty,'0' AS UpperMoQty,'0' AS InvAlloeFlag,NULL AS FactoryCode
  291. INTO #TempBOMP" + iFatherId + @"
  292. FROM bas_part c
  293. INNER JOIN mom_orderdetail d ON c.PartId=d.PartId
  294. LEFT JOIN bom_parent b ON 1<>1 --b.ParentId=c.PartId
  295. LEFT JOIN bom_opcomponent a ON a.BomId=b.BomId
  296. LEFT JOIN bom_opcomponentopt e ON a.OptionsId=e.OptionsId
  297. WHERE d.MoDId=" + iChildId + @"
  298. ORDER BY d.MoDId
  299. ;
  300. SELECT row_number() OVER (partition by c.InvCode ORDER BY c.InvCode,d.SortSeq) AS SEQ,c.InvCode AS PInvCode,f.InvCode AS InvCode,
  301. d.SortSeq,d.OpSeq,d.ComponentId,
  302. d.FVFlag,d.BaseQtyN,d.BaseQtyD,'0' AS ParentScrap,d.CompScrap,
  303. CAST(d.BaseQtyN/d.BaseQtyD AS DECIMAL(28,6)) AS Qty,'0' AS IssQty,'0' AS DeclaredQty, '@StartDate' AS StartDemDate, '@DueDate' AS EndDemDate,
  304. NULL AS WhCode,NULL AS LotNo,e.WIPType,d.ByproductFlag,'0' AS QcFlag,
  305. '0' AS Offset,d.Free1,d.Free2,d.Free3,
  306. d.Free4,d.Free5,d.Free6,d.Free7,d.Free8,
  307. d.Free9,d.Free10,d.OpComponentId,d.Define22,d.Define23,
  308. d.Define24,d.Define25,d.Define26,d.Define27,d.Define28,
  309. d.Define29,d.Define30,d.Define31,d.Define32,d.Define33,
  310. d.Define34,d.Define35,d.Define36,d.Define37,'0' AS ReplenishQty,
  311. d.Remark,'0' AS TransQty,d.ProductType,'0' AS SoType,NULL AS SoDId,
  312. NULL AS SoCode,NULL AS SoSeq,NULL AS DemandCode,'0' AS QmFlag,'0' AS OrgQty,
  313. '0' AS OrgAuxQty,NULL AS CostItemCode,NULL AS CostItemName,'0' AS RequisitionFlag,'0' AS RequisitionQty,
  314. '0' AS RequisitionIssQty,'0' AS CostWIPRel,NULL AS MoallocateSubId,NULL AS cSubSysBarCode,'0' AS PickingQty,
  315. '0' AS PickingAuxQty,'0' AS UpperMoQty,'0' AS InvAlloeFlag,NULL AS FactoryCode
  316. INTO #TempBOM" + iFatherId + @"
  317. FROM dbo.bom_bom a
  318. INNER JOIN dbo.bom_parent b on a.BomId = b.BomId
  319. INNER JOIN dbo.bas_part c on b.ParentId = c.PartId
  320. INNER JOIN dbo.bom_opcomponent d on a.BomId=d.BomId
  321. INNER JOIN dbo.bom_opcomponentopt e ON d.OptionsId=e.OptionsId
  322. INNER JOIN dbo.bas_part f on d.ComponentId = f.PartId
  323. WHERE a.VersionEndDate>=CONVERT(VARCHAR(10),GETDATE(),23)
  324. ORDER BY c.InvCode,d.SortSeq
  325. ;
  326. WITH ICSBomALL AS
  327. (
  328. SELECT
  329. PInvCode AS TInvCode,
  330. PInvCode AS PInvCode,
  331. PInvCode AS InvCode,
  332. --CAST('1' AS tinyint) AS WIPType,
  333. [Level],
  334. SEQ,
  335. CAST(REPLICATE('0',5-len(Sort))+Sort AS nvarchar(MAX)) AS Sort,
  336. MoDId,SortSeq,OpSeq,ComponentId,
  337. FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap,
  338. CAST(Qty AS DECIMAL(28,6)) AS Qty,IssQty,DeclaredQty, StartDemDate, EndDemDate,
  339. WhCode,LotNo,WIPType,ByproductFlag,QcFlag,
  340. Offset,Free1,Free2,Free3,
  341. Free4,Free5,Free6,Free7,Free8,
  342. Free9,Free10,OpComponentId,Define22,Define23,
  343. Define24,Define25,Define26,Define27,Define28,
  344. Define29,Define30,Define31,Define32,Define33,
  345. Define34,Define35,Define36,Define37,ReplenishQty,
  346. Remark,TransQty,ProductType,SoType,SoDId,
  347. SoCode,SoSeq,DemandCode,QmFlag,OrgQty,
  348. OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty,
  349. RequisitionIssQty,CostWIPRel,MoallocateSubId,cSubSysBarCode,PickingQty,
  350. PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode
  351. FROM
  352. #TempBOMP" + iFatherId + @"
  353. UNION ALL
  354. SELECT
  355. b.TInvCode,
  356. a.PInvCode,
  357. a.InvCode,
  358. --a.WIPType,
  359. b.[Level]+1 AS [Level],
  360. CAST(b.SEQ AS nvarchar(MAX))+'.'+CAST(a.SEQ AS nvarchar(MAX)) AS SEQ,
  361. CAST(b.Sort+'.'+REPLICATE('0',5-len(a.SEQ))+CAST(a.SEQ AS nvarchar(MAX)) AS nvarchar(MAX)) AS Sort,
  362. b.MoDId,a.SortSeq,a.OpSeq,a.ComponentId,
  363. a.FVFlag,a.BaseQtyN,a.BaseQtyD,a.ParentScrap,a.CompScrap,
  364. CAST(a.Qty*b.Qty AS DECIMAL(28,6)) AS Qty,a.IssQty,a.DeclaredQty, a.StartDemDate, a.EndDemDate,
  365. a.WhCode,a.LotNo,a.WIPType,a.ByproductFlag,a.QcFlag,
  366. a.Offset,a.Free1,a.Free2,a.Free3,
  367. a.Free4,a.Free5,a.Free6,a.Free7,a.Free8,
  368. a.Free9,a.Free10,a.OpComponentId,a.Define22,a.Define23,
  369. a.Define24,a.Define25,a.Define26,a.Define27,a.Define28,
  370. a.Define29,a.Define30,a.Define31,a.Define32,a.Define33,
  371. a.Define34,a.Define35,a.Define36,a.Define37,a.ReplenishQty,
  372. a.Remark,a.TransQty,a.ProductType,a.SoType,a.SoDId,
  373. a.SoCode,a.SoSeq,a.DemandCode,a.QmFlag,a.OrgQty,
  374. a.OrgAuxQty,a.CostItemCode,a.CostItemName,a.RequisitionFlag,a.RequisitionQty,
  375. a.RequisitionIssQty,a.CostWIPRel,a.MoallocateSubId,a.cSubSysBarCode,a.PickingQty,
  376. a.PickingAuxQty,a.UpperMoQty,a.InvAlloeFlag,a.FactoryCode
  377. FROM
  378. #TempBOM" + iFatherId + @" a
  379. INNER JOIN ICSBomALL b ON a.PInvCode=b.InvCode
  380. --
  381. WHERE b.[Level]='0' OR b.WIPType='4'
  382. )
  383. SELECT row_number() OVER (ORDER BY Sort) AS RowIndex,* INTO #TempMOAll" + iFatherId + @" FROM ICSBomALL WHERE [Level]<>'0' AND WIPType<>'4' ORDER BY Sort";
  384. //更新临时表的ID
  385. // sql += @" DECLARE @CurrentRow" + iFatherId + @" INT,@TotalRow" + iFatherId + @" INT
  386. // SET @TotalRow" + iFatherId + @"=@@rowcount
  387. // SET @CurrentRow" + iFatherId + @"=1
  388. // WHILE @CurrentRow" + iFatherId + @"<=@TotalRow" + iFatherId + @"
  389. // BEGIN
  390. // " + ICSHelper.GetIDSql(cCompanyCode, "mom_moallocate", appmokey, GetID.ALL) + @"
  391. // UPDATE #TempMOAll" + appskey + @" SET RowIndex=" + baseInt + "+@DID" + appmokey + @" WHERE RowIndex=@CurrentRow" + appskey + @"
  392. // SET @CurrentRow" + appskey + @"+=1
  393. // END
  394. // " + Environment.NewLine;
  395. sql += @" INSERT INTO mom_moallocate (AllocateId,MoDId,SortSeq,OpSeq,ComponentId,
  396. FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap,
  397. Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate,
  398. WhCode,LotNo,WIPType,ByproductFlag,QcFlag,
  399. Offset,InvCode,Free1,Free2,Free3,
  400. Free4,Free5,Free6,Free7,Free8,
  401. Free9,Free10,OpComponentId,Define22,Define23,
  402. Define24,Define25,Define26,Define27,Define28,
  403. Define29,Define30,Define31,Define32,Define33,
  404. Define34,Define35,Define36,Define37,ReplenishQty,
  405. Remark,TransQty,ProductType,SoType,SoDId,
  406. SoCode,SoSeq,DemandCode,QmFlag,OrgQty,
  407. OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty,
  408. RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty,
  409. PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode)
  410. SELECT RowIndex,MoDId,SortSeq,OpSeq,ComponentId,
  411. FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap,
  412. Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate,
  413. WhCode,LotNo,WIPType,ByproductFlag,QcFlag,
  414. Offset,InvCode,Free1,Free2,Free3,
  415. Free4,Free5,Free6,Free7,Free8,
  416. Free9,Free10,OpComponentId,Define22,Define23,
  417. Define24,Define25,Define26,Define27,Define28,
  418. Define29,Define30,Define31,Define32,Define33,
  419. Define34,Define35,Define36,Define37,ReplenishQty,
  420. Remark,TransQty,ProductType,SoType,SoDId,
  421. SoCode,SoSeq,DemandCode,QmFlag,OrgQty,
  422. OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty,
  423. RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty,
  424. PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode
  425. FROM #TempMOAll" + iFatherId + Environment.NewLine;
  426. sql += @" DROP TABLE #TempMOAll" + iFatherId + Environment.NewLine;
  427. sql += @" DROP TABLE #TempBOMP" + iFatherId + Environment.NewLine;
  428. sql += @" DROP TABLE #TempBOM" + iFatherId + Environment.NewLine;
  429. cmd.CommandText = sql;
  430. try
  431. {
  432. int count = cmd.ExecuteNonQuery();
  433. if (count <= 0)
  434. {
  435. log.Error("生成生产顶订单表头失败,受影响行数<=0;");
  436. throw new Exception("生成生产顶订单表头失败,受影响行数<=0;");
  437. }
  438. }
  439. catch (Exception ex)
  440. {
  441. log.Error("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  442. throw new Exception("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  443. }
  444. #endregion
  445. }
  446. #endregion
  447. ////更新主键ID,DID
  448. //DBHelper.UpdateIDandDID("rd", head.list.Count(), cmd);
  449. }
  450. cmd.Transaction.Commit();
  451. }
  452. catch (Exception ex)
  453. {
  454. if (cmd.Transaction != null)
  455. cmd.Transaction.Rollback();
  456. log.Error(ex.Message);
  457. throw new Exception(ex.Message);
  458. }
  459. finally
  460. {
  461. if (conn.State == ConnectionState.Open)
  462. {
  463. conn.Close();
  464. }
  465. conn.Dispose();
  466. }
  467. }
  468. return msg;
  469. }
  470. /// <summary>
  471. /// 删除生产订单
  472. /// </summary>
  473. /// <param name="infos"></param>
  474. /// <returns></returns>
  475. public string Delete(List<ICSManufactureOrder> infos)
  476. {
  477. List<ICSManufactureOrder> szJson = new List<ICSManufactureOrder>();
  478. if (infos.Count <= 0)
  479. {
  480. throw new Exception("传送数据为空!");
  481. }
  482. string res = string.Empty;
  483. string connS = "";
  484. SqlConnection conn = new SqlConnection();
  485. SqlCommand cmd = new SqlCommand();
  486. string sql = string.Empty;
  487. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  488. foreach (string WorkPoint in result)
  489. {
  490. try
  491. {
  492. connS = string.Format(connString, WorkPoint);
  493. conn = new System.Data.SqlClient.SqlConnection(connS);
  494. conn.Open();
  495. SqlTransaction sqlTran = conn.BeginTransaction();
  496. cmd = new SqlCommand();
  497. cmd.Transaction = sqlTran;
  498. cmd.Connection = conn;
  499. foreach (ICSManufactureOrder info in infos)
  500. {
  501. if (WorkPoint != info.WorkPoint)
  502. {
  503. continue;
  504. }
  505. if (info.MTime < new DateTime(2000, 01, 01))
  506. throw new Exception("请输入正确的操作时间:" + info.MTime);
  507. sql = @" DELETE [{1}].dbo.mom_order WHERE ID={0}";
  508. sql = string.Format(sql, info.ID, ERPDB);
  509. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  510. }
  511. cmd.Transaction.Commit();
  512. }
  513. catch (Exception ex)
  514. {
  515. if (cmd.Transaction != null)
  516. cmd.Transaction.Rollback();
  517. log.Error(ex.Message);
  518. throw new Exception(ex.Message);
  519. }
  520. finally
  521. {
  522. if (conn.State == ConnectionState.Open)
  523. {
  524. conn.Close();
  525. }
  526. conn.Dispose();
  527. }
  528. }
  529. return res;
  530. }
  531. }
  532. }