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

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