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

593 lines
32 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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 MOIssueDocNegatives
  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. private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["MoICRDNAME"];
  23. public string Get(List<ICSMOIssueDocNegative> infos)
  24. {
  25. List<ICSMOIssueDocNegative> szJson = new List<ICSMOIssueDocNegative>();
  26. DataTable dt = null;
  27. DataTable dtNew = null;
  28. string connS = "";
  29. string json = "";
  30. if (infos.Count <= 0)
  31. {
  32. throw new Exception("传送数据为空!");
  33. }
  34. string res = string.Empty;
  35. SqlConnection conn = new SqlConnection();
  36. SqlCommand cmd = new SqlCommand();
  37. string sql = string.Empty;
  38. foreach (ICSMOIssueDocNegative info in infos)
  39. {
  40. try
  41. {
  42. connS = string.Format(connString, info.WorkPoint);
  43. conn = new System.Data.SqlClient.SqlConnection(connS);
  44. conn.Open();
  45. SqlTransaction sqlTran = conn.BeginTransaction();
  46. cmd = new SqlCommand();
  47. cmd.Transaction = sqlTran;
  48. cmd.Connection = conn;
  49. if (info.MTime < new DateTime(2000, 01, 01))
  50. throw new Exception("请输入正确的操作时间:" + info.MTime);
  51. sql = @" select a.ID,iMPoIds,iordercode,cCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,cSource,cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,b.AutoID ,irowno,b.cInvCode,iQuantity,f.MoDId,iNum,g.MoCode from rdrecord11 a
  52. left join rdrecords11 b on a.ID=b.ID
  53. left join Department c on a.cDepCode=c.cDepCode
  54. left join Warehouse d on a.cWhCode=d.cWhCode
  55. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  56. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  57. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId where b.iQuantity<0";
  58. if (!string.IsNullOrWhiteSpace(info.IssueNEGCode))
  59. {
  60. sql += " and a.cCode='{0}'";
  61. }
  62. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  63. {
  64. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  65. }
  66. if (!string.IsNullOrWhiteSpace(info.User))
  67. {
  68. sql += "and a.CMAKER='{2}'";
  69. }
  70. sql = string.Format(sql, info.IssueNEGCode, info.MTime, info.User);
  71. dt = DBHelper.SQlReturnData(sql, cmd);
  72. if (dt.Rows.Count <= 0 || dt == null)
  73. throw new Exception("材料出库单:" + info.IssueNEGCode + ",无信息!");
  74. if (dtNew == null)
  75. dtNew = dt;
  76. else
  77. dtNew.Merge(dt);
  78. cmd.Transaction.Commit();
  79. }
  80. catch (Exception ex)
  81. {
  82. cmd.Transaction.Rollback();
  83. log.Error(ex.Message);
  84. throw new Exception(ex.Message);
  85. }
  86. finally
  87. {
  88. if (conn.State == ConnectionState.Open)
  89. {
  90. conn.Close();
  91. }
  92. conn.Dispose();
  93. }
  94. }
  95. json = JsonConvert.SerializeObject(dtNew);
  96. return json;
  97. }
  98. public string CreateRd11(List<ICSMOIssueDocNegative> Bills)
  99. {
  100. string msg = "";
  101. DataTable dtNew1 = null;
  102. DataTable dtNew2 = null;
  103. string connS = "";
  104. string sql = string.Empty;
  105. string iFatherIdTwo = string.Empty;
  106. VouchKey key = new VouchKey();
  107. int num = 0;
  108. SqlConnection conn = new SqlConnection();
  109. SqlCommand cmd = new SqlCommand();
  110. if (Bills.Count <= 0)
  111. {
  112. throw new Exception("传送数据为空!");
  113. }
  114. LogInfo(Bills);
  115. //MergeObject(Bills, cmd);
  116. foreach (ICSMOIssueDocNegative head in Bills)
  117. {
  118. try
  119. {
  120. num = head.details.Count();
  121. connS = string.Format(connString, head.WorkPoint);
  122. conn = new System.Data.SqlClient.SqlConnection(connS);
  123. conn.Open();
  124. SqlTransaction sqlTran = conn.BeginTransaction();
  125. cmd = new SqlCommand();
  126. cmd.Transaction = sqlTran;
  127. cmd.Connection = conn;
  128. cmd.CommandTimeout = 300;
  129. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  130. {
  131. }
  132. else
  133. {
  134. throw new Exception("U8正在整理现存量,请稍后再试");
  135. }
  136. string[] ss = head.WorkPoint.Split('_');
  137. ERPDB = ss[1];
  138. string[] dd = CRNALL.Split('~');
  139. string crdname = dd[0];
  140. string carname = dd[1];
  141. string surface = dd[2];
  142. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  143. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  144. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  145. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  146. DateTime date = DateTime.Now;
  147. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", head.WorkPoint);
  148. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  149. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  150. // #region 判断工单是否已关闭
  151. // sql = @"SELECT isnull(CloseUser,'') as CloseUser from mom_orderdetail a
  152. // inner join mom_order b on a.MoId=b.MoId where b.MoId='" + head.DetailID + "' ";
  153. // DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  154. // if (dtClose != null && dtClose.Rows.Count > 0)
  155. // {
  156. // if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString()))
  157. // {
  158. // throw new Exception("该工单行已关闭,无法领料!");
  159. // }
  160. // }
  161. // #endregion
  162. #region 材料出库单表头
  163. if (head.SourceType != "领料申请单")
  164. {
  165. sql = @"INSERT INTO dbo.rdrecord11
  166. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  167. cCode ,cRdCode ,cDepCode,cHandler ,bTransFlag ,cMaker ,
  168. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc
  169. ,cPsPcode ,cMPoCode ,iproorderid ,bFromPreYear ,
  170. bIsLsQuery ,bIsComplement ,iDiscountTaxType ,ireturncount ,iverifystate ,
  171. iswfcontrolled ,dnmaketime ,dnverifytime ,bredvouch ,iPrintCount,
  172. iMQuantity)
  173. SELECT distinct @ID,0,'11','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),
  174. @cCode,@cRdCode,a.MDeptCode,@cHandler,0,@cMaker,
  175. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,
  176. null,b.MoCode,a.MoId,0,
  177. 0,0,0,0,0,
  178. 0,GETDATE(),GETDATE(),1,1,
  179. null
  180. FROM dbo.mom_orderdetail a
  181. left JOIN dbo.mom_order b ON a.MoId=b.MoId
  182. left join mom_morder c on a.modid=c.modid
  183. WHERE b.MoCode='" + head.SourceCode + "'";
  184. cmd.Parameters.Clear();
  185. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  186. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  187. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  188. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  189. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  190. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  191. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  192. //cmd.Parameters.Add(new SqlParameter("@bredvouch",0));
  193. cmd.CommandText = sql;
  194. }
  195. else
  196. {
  197. sql = @"INSERT INTO dbo.rdrecord11
  198. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cBusCode ,cWhCode ,
  199. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cHandler ,cMemo ,
  200. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,
  201. cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,
  202. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,
  203. cDefine12 ,cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  204. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  205. ireturncount ,iverifystate ,iswfcontrolled ,cModifyPerson ,
  206. dnmaketime ,dnverifytime ,bredvouch ,bmotran ,bHYVouch ,iPrintCount
  207. )
  208. SELECT distinct @ID,0,'11','','',a.cCode,@cWhCode,
  209. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,a.cDepCode,a.cPersonCode,@cHandler,a.cMemo,
  210. 0,@cMaker,a.cDefine1,cdefine2,a.cDefine3,a.cDefine4,
  211. a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,a.cDefine9,a.cDefine10,
  212. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,a.cDefine11,
  213. a.cDefine12,a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  214. 0,0,0,0,
  215. 0,0,0,'',
  216. GETDATE(),GETDATE(),0,0,0,0
  217. FROM dbo.MaterialAppVouch a WHERE a.cCode='" + head.SourceCode + "'";
  218. cmd.Parameters.Clear();
  219. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  220. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  221. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  222. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  223. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  224. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  225. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  226. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  227. cmd.CommandText = sql;
  228. }
  229. try
  230. {
  231. int count = cmd.ExecuteNonQuery();
  232. if (count <= 0)
  233. {
  234. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  235. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. log.Error("生成材料出库单表头失败" + sql, ex);
  241. throw new Exception("生成材料出库单表头失败" + sql, ex);
  242. }
  243. #endregion
  244. #region 材料出库单表体
  245. int irowno = 0;
  246. foreach (ICSMOIssueDocNegatives body in head.details)
  247. {
  248. irowno++;
  249. if (head.SourceType != "领料申请单")
  250. {
  251. sql = @"INSERT INTO dbo.rdrecords11
  252. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,
  253. cItemCode ,cName ,iNQuantity ,iMPoIds ,bLPUseFree ,
  254. iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,invcode ,imoseq ,
  255. iopseq ,iExpiratDateCalcu ,iorderdid ,iordertype ,isotype ,
  256. ipesodid ,ipesotype ,cpesocode ,ipesoseq ,irowno ,
  257. bcanreplace ,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  258. SELECT @AutoID,@ID,a.InvCode,@iQuantity,@cBatch,0,
  259. null,null,a.Qty,a.AllocateId,0,
  260. 0,1,0,c.MoCode,b.InvCode,b.SortSeq,
  261. '0000',0,0,0,0,
  262. a.AllocateId,'7',c.MoCode,b.SortSeq,@irowno,
  263. 0,null,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  264. FROM dbo.mom_moallocate a
  265. INNER JOIN dbo.mom_orderdetail b ON a.MoDId=b.MoDId
  266. INNER JOIN dbo.mom_order c ON b.MoId=c.MoId
  267. WHERE a.AllocateId='" + body.SourceDetailID + "'";
  268. cmd.Parameters.Clear();
  269. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  270. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  271. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  272. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  273. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  274. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  275. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  276. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  277. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  278. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  279. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  280. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  281. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  282. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  283. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  284. cmd.CommandText = sql;
  285. }
  286. else
  287. {
  288. sql = @"INSERT INTO dbo.rdrecords11
  289. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,cDefine22 ,iMPoIds,cmocode,
  290. cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  291. cItem_class ,cItemCode ,cName ,cItemCName ,iNQuantity ,
  292. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  293. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  294. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,iMaIDs ,
  295. iExpiratDateCalcu ,iordertype ,isotype ,ipesotype ,irowno,
  296. cBatch,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  297. SELECT @AutoID,@ID,a.cInvCode,@iQuantity,0,a.cDefine22,a.iMPoIds,a.cmocode,
  298. a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  299. a.cItem_class,a.cItemCode,a.cName,a.cItemCName,@iQuantity,
  300. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  301. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  302. 0,0,1,0,a.AutoID,
  303. 0,0,0,0,@irowno,
  304. @cBatch,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  305. FROM dbo.MaterialAppVouchs a WHERE a.AutoID='" + body.SourceDetailID + "'";
  306. cmd.Parameters.Clear();
  307. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  308. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  309. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  310. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  311. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  312. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  313. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  314. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  315. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  316. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  317. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  318. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  319. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  320. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  321. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  322. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  323. cmd.CommandText = sql;
  324. }
  325. try
  326. {
  327. int count = cmd.ExecuteNonQuery();
  328. if (count <= 0)
  329. {
  330. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  331. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  332. }
  333. }
  334. catch (Exception ex)
  335. {
  336. log.Error("生成材料出库单表体失败" + sql, ex);
  337. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  338. }
  339. if (head.UpdateStock)
  340. {
  341. //更新现存量
  342. key.cBustypeUN = "领料";
  343. key.cVouchTypeUN = "11";
  344. key.TableName = "IA_ST_UnAccountVouch11";
  345. DBHelper.UpdateCurrentStockNEW(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, key);
  346. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  347. }
  348. if (head.SourceType != "领料申请单")
  349. {
  350. #region 回写工单子件已领数量
  351. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+(" + body.Quantity + ") where AllocateId='" + body.SourceDetailID + "' ";
  352. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  353. #endregion
  354. }
  355. else
  356. {
  357. #region 回写工单子件已领数量
  358. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+" + body.Quantity + " where AllocateId=(select iMPoIds from MaterialAppVouchs where AutoID='" + body.SourceDetailID + @"')
  359. update MaterialAppVouchs set fOutQuantity=isnull(fOutQuantity,0)+" + body.Quantity + @" where AutoID='" + body.SourceDetailID + @"'
  360. ";
  361. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  362. #endregion
  363. }
  364. iFatherIdTwo += "'" + iFatherId + "',";
  365. iChildId--;
  366. }
  367. #endregion
  368. #region 查询
  369. sql = @"
  370. select DISTINCT a.ID as ID,a.ID as IDs,cMPoCode as SourceCode ,
  371. cCode as IssueNEGCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  372. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  373. cHandler as Checker,dnverifytime as CheckDateTime
  374. from rdrecord11 a
  375. left join Department c on a.cDepCode=c.cDepCode
  376. left join Warehouse d on a.cWhCode=d.cWhCode
  377. WHERE a.ID in ({0})
  378. select DISTINCT a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode
  379. ,b.iQuantity as Quantity,(case when isnull(l.AutoID,'')='' then e.AllocateId else l.AutoID end) as SourceDetailID,b.iNum as Amount
  380. from rdrecord11 a
  381. left join rdrecords11 b on a.ID=b.ID
  382. left join MaterialAppVouchs l on b.iMaIDs=l.autoid
  383. left join Department c on a.cDepCode=c.cDepCode
  384. left join Warehouse d on a.cWhCode=d.cWhCode
  385. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  386. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  387. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({0})";
  388. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  389. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  390. if (dtNew1 == null)
  391. dtNew1 = ds.Tables[0];
  392. else
  393. dtNew1 = ds.Tables[0];
  394. if (dtNew2 == null)
  395. dtNew2 = ds.Tables[1];
  396. else
  397. dtNew2 = ds.Tables[1];
  398. #endregion
  399. cmd.Transaction.Commit();
  400. }
  401. catch (Exception ex)
  402. {
  403. cmd.Transaction.Rollback();
  404. log.Error(ex.Message);
  405. throw new Exception(ex.Message);
  406. }
  407. finally
  408. {
  409. if (conn.State == ConnectionState.Open)
  410. {
  411. conn.Close();
  412. }
  413. conn.Dispose();
  414. }
  415. }
  416. string RelationName = "details";
  417. DataSet dz = new DataSet();
  418. dtNew1.TableName = "Table0";
  419. dtNew2.TableName = "Table1";
  420. dz.Tables.Add(dtNew1.Copy());
  421. dz.Tables.Add(dtNew2.Copy());
  422. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  423. dz.Relations.Add(dh);
  424. msg = DBHelper.DataSetToJson(dz, RelationName);
  425. return msg;
  426. }
  427. public string Approve(List<ICSMOIssueDocNegative> infos)
  428. {
  429. List<ICSMOIssueDocNegative> szJson = new List<ICSMOIssueDocNegative>();
  430. string connS = "";
  431. string json = "";
  432. if (infos.Count <= 0)
  433. {
  434. throw new Exception("传送数据为空!");
  435. }
  436. string res = string.Empty;
  437. SqlConnection conn = new SqlConnection();
  438. SqlCommand cmd = new SqlCommand();
  439. string sql = string.Empty;
  440. foreach (ICSMOIssueDocNegative info in infos)
  441. {
  442. try
  443. {
  444. connS = string.Format(connString, info.WorkPoint);
  445. conn = new System.Data.SqlClient.SqlConnection(connS);
  446. conn.Open();
  447. SqlTransaction sqlTran = conn.BeginTransaction();
  448. cmd = new SqlCommand();
  449. cmd.Transaction = sqlTran;
  450. cmd.Connection = conn;
  451. if (info.MTime < new DateTime(2000, 01, 01))
  452. throw new Exception("请输入正确的操作时间:" + info.MTime);
  453. sql = @"UPDATE dbo.rdrecord11 SET cHandler ='" + info.User + @"' ,
  454. dnmodifytime =CONVERT(VARCHAR(50),GETDATE(),112),dModifyDate =GETDATE() WHERE ID='{0}'";
  455. sql = string.Format(sql, info.ID);
  456. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核生产退料单失败!");
  457. cmd.Transaction.Commit();
  458. }
  459. catch (Exception ex)
  460. {
  461. cmd.Transaction.Rollback();
  462. log.Error(ex.Message);
  463. throw new Exception(ex.Message);
  464. }
  465. finally
  466. {
  467. if (conn.State == ConnectionState.Open)
  468. {
  469. conn.Close();
  470. }
  471. conn.Dispose();
  472. }
  473. }
  474. return json;
  475. }
  476. /// <summary>
  477. /// 删除请购单
  478. /// </summary>
  479. /// <param name="infos"></param>
  480. /// <returns></returns>
  481. public string Delete(List<ICSMOIssueDocNegative> infos)
  482. {
  483. List<ICSMOIssueDocNegative> szJson = new List<ICSMOIssueDocNegative>();
  484. if (infos.Count <= 0)
  485. {
  486. throw new Exception("传送数据为空!");
  487. }
  488. string res = string.Empty;
  489. string connS = "";
  490. SqlConnection conn = new SqlConnection();
  491. SqlCommand cmd = new SqlCommand();
  492. string sql = string.Empty;
  493. foreach (ICSMOIssueDocNegative info in infos)
  494. {
  495. try
  496. {
  497. connS = string.Format(connString, info.WorkPoint);
  498. conn = new System.Data.SqlClient.SqlConnection(connS);
  499. conn.Open();
  500. SqlTransaction sqlTran = conn.BeginTransaction();
  501. cmd = new SqlCommand();
  502. cmd.Transaction = sqlTran;
  503. cmd.Connection = conn;
  504. if (info.MTime < new DateTime(2000, 01, 01))
  505. throw new Exception("请输入正确的操作时间:" + info.MTime);
  506. sql = @"delete rdrecord11 where rdrecord11.ID='" + info.ID + "'";
  507. sql += @"delete rdrecords11 where rdrecords11.ID='" + info.ID + "'";
  508. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  509. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  510. // WHERE a.ID='" + info.ID + "'";
  511. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除材料出库单失败!");
  512. cmd.Transaction.Commit();
  513. }
  514. catch (Exception ex)
  515. {
  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. private void LogInfo(List<ICSMOIssueDocNegative> Bills)
  532. {
  533. string HeadList = string.Empty;
  534. string BodyList = string.Empty;
  535. foreach (ICSMOIssueDocNegative head in Bills)
  536. {
  537. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  538. foreach (ICSMOIssueDocNegatives body in head.details)
  539. {
  540. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  541. }
  542. }
  543. log.Info(HeadList);
  544. log.Info(BodyList);
  545. }
  546. /// <summary>
  547. /// 表体合并
  548. /// </summary>
  549. /// <param name="Bills"></param>
  550. private void MergeObject(List<ICSMOIssueDocNegative> Bills, SqlCommand cmd)
  551. {
  552. foreach (ICSMOIssueDocNegative head in Bills)
  553. {
  554. DataTable dt = DBHelper.ToDataTable(head.details.ToList());
  555. DataTable dtNew = dt.Clone();
  556. dtNew = DBHelper.MergeDataTable(dt, "AllocateId", "cBatch", "iQuantity", "InvCode", cmd);
  557. List<ICSMOIssueDocNegatives> list = DBHelper.ConvertTo<ICSMOIssueDocNegatives>(dtNew).ToList();
  558. head.details = list;
  559. }
  560. }
  561. }
  562. }