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

705 lines
39 KiB

3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 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
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 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 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 = @"SELECT isnull(fOutQuantity,0) as fOutQuantity,iQuantity from MaterialAppVouchs a where a.AutoID='" + body.SourceDetailID + "' ";
  300. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  301. if (dtClose != null && dtClose.Rows.Count > 0)
  302. {
  303. if (Convert.ToDecimal(dtClose.Rows[0][0].ToString()) + body.Quantity > Convert.ToDecimal(dtClose.Rows[0][1].ToString()))
  304. {
  305. throw new Exception("退料数量不能大于领料单的已出数量!");
  306. }
  307. }
  308. sql = @"INSERT INTO dbo.rdrecords11
  309. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,cDefine22 ,iMPoIds,cmocode,iNum,
  310. cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  311. cItem_class ,cItemCode ,cName ,cItemCName ,iNQuantity ,
  312. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  313. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  314. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,iMaIDs ,
  315. iExpiratDateCalcu ,iordertype ,isotype ,ipesotype ,irowno,
  316. cBatch,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  317. SELECT @AutoID,@ID,a.cInvCode,@iQuantity,0,a.cDefine22,a.iMPoIds,a.cmocode,@iNum,
  318. a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  319. a.cItem_class,a.cItemCode,a.cName,a.cItemCName,@iQuantity,
  320. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  321. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  322. 0,0,1,0,a.AutoID,
  323. 0,0,0,0,@irowno,
  324. @cBatch,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  325. FROM dbo.MaterialAppVouchs a WHERE a.AutoID='" + body.SourceDetailID + "'";
  326. cmd.Parameters.Clear();
  327. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  328. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  329. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  330. cmd.Parameters.Add(new SqlParameter("@iNum", -Convert.ToDecimal(body.Amount.ToString("0.00"))));
  331. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  332. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  333. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  334. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  335. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  336. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  337. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  338. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  339. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  340. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  341. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  342. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  343. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  344. cmd.CommandText = sql;
  345. }
  346. try
  347. {
  348. int count = cmd.ExecuteNonQuery();
  349. if (count <= 0)
  350. {
  351. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  352. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  353. }
  354. }
  355. catch (Exception ex)
  356. {
  357. log.Error("生成材料出库单表体失败" + sql, ex);
  358. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  359. }
  360. if (head.SourceType != "领料申请单")
  361. {
  362. #region 回写工单子件已领数量
  363. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+(" + body.Quantity + ") where AllocateId='" + body.SourceDetailID + "' ";
  364. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  365. #endregion
  366. }
  367. else
  368. {
  369. #region 回写工单子件已领数量
  370. 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 + @"')
  371. update MaterialAppVouchs set fOutQuantity=isnull(fOutQuantity,0)+" + body.Quantity + @" where AutoID='" + body.SourceDetailID + @"'
  372. ";
  373. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  374. #endregion
  375. }
  376. if (head.SourceType != "领料申请单")
  377. {
  378. 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
  379. LEFT JOIN (select sum(iNum) NiNum,ipesodid from rdrecords11 GROUP BY ipesodid) b on a.AllocateId=b.ipesodid
  380. where a.AllocateId='" + body.SourceDetailID + "'";
  381. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  382. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  383. {
  384. foreach (DataRow item1 in dtDisCheck.Rows)
  385. {
  386. if (Convert.ToDecimal(item1["fOutQuantity"].ToString()) == Convert.ToDecimal(item1["iQuantity"].ToString()))
  387. {
  388. if (Convert.ToDecimal(item1["iNum"].ToString()) != 0)
  389. {
  390. if (Convert.ToDecimal(item1["SiNum"].ToString()) != 0)
  391. {
  392. sql = @"Update rdrecords11 set iNum=isnull(iNum,0)+(" + item1["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  393. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item1["SiNum"].ToString()));
  394. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  395. }
  396. }
  397. }
  398. }
  399. }
  400. }
  401. else
  402. {
  403. 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
  404. LEFT JOIN (select sum(iNum) NiNum,iMaIDs from rdrecords11 GROUP BY iMaIDs) b on a.AutoID=b.iMaIDs
  405. where a.AutoID='" + body.SourceDetailID + "'";
  406. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  407. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  408. {
  409. foreach (DataRow item in dtDisCheck.Rows)
  410. {
  411. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  412. {
  413. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  414. {
  415. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  416. {
  417. sql = @"Update rdrecords11 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  418. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  419. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  420. }
  421. }
  422. }
  423. }
  424. }
  425. }
  426. //更新现存量
  427. key.cBustypeUN = "领料";
  428. key.cVouchTypeUN = "11";
  429. key.TableName = "IA_ST_UnAccountVouch11";
  430. //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);
  431. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  432. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.Amount, body.cFree1,
  433. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  434. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  435. iFatherIdTwo = "'" + iFatherId + "',";
  436. iChildId--;
  437. }
  438. #endregion
  439. #region 查询
  440. sql = @"
  441. select DISTINCT a.ID as ID,a.ID as IDs,cMPoCode as SourceCode ,
  442. cCode as IssueNEGCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  443. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  444. cHandler as Checker,dnverifytime as CheckDateTime
  445. from rdrecord11 a
  446. left join Department c on a.cDepCode=c.cDepCode
  447. left join Warehouse d on a.cWhCode=d.cWhCode
  448. WHERE a.ID in ({0})
  449. select DISTINCT a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode
  450. ,b.iQuantity as Quantity,(case when isnull(l.AutoID,'')='' then e.AllocateId else l.AutoID end) as SourceDetailID,b.iNum as Amount
  451. ,isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  452. isnull(b.cFree1,'') as cFree1,
  453. isnull(b.cFree2,'') as cFree2,
  454. isnull(b.cFree3,'') as cFree3,
  455. isnull(b.cFree4,'') as cFree4,
  456. isnull(b.cFree5,'') as cFree5,
  457. isnull(b.cFree6,'') as cFree6,
  458. isnull(b.cFree7,'') as cFree7,
  459. isnull(b.cFree8,'') as cFree8,
  460. isnull(b.cFree9,'') as cFree9,
  461. isnull(b.cFree10,'') as cFree10
  462. from rdrecord11 a
  463. left join rdrecords11 b on a.ID=b.ID
  464. left join MaterialAppVouchs l on b.iMaIDs=l.autoid
  465. left join Department c on a.cDepCode=c.cDepCode
  466. left join Warehouse d on a.cWhCode=d.cWhCode
  467. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  468. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  469. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({0})";
  470. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  471. if (ds != null)
  472. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  473. else
  474. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  475. #endregion
  476. }
  477. cmd.Transaction.Commit();
  478. }
  479. catch (Exception ex)
  480. {
  481. cmd.Transaction.Rollback();
  482. log.Error(ex.Message);
  483. throw new Exception(ex.Message);
  484. }
  485. finally
  486. {
  487. if (conn.State == ConnectionState.Open)
  488. {
  489. conn.Close();
  490. }
  491. conn.Dispose();
  492. }
  493. }
  494. msg = JSON.DataSetToJson(ds, "details", "IDs");
  495. return msg;
  496. }
  497. public string Approve(List<ICSMOIssueDocNegative> infos)
  498. {
  499. List<ICSMOIssueDocNegative> szJson = new List<ICSMOIssueDocNegative>();
  500. string connS = "";
  501. string json = "";
  502. if (infos.Count <= 0)
  503. {
  504. throw new Exception("传送数据为空!");
  505. }
  506. string res = string.Empty;
  507. SqlConnection conn = new SqlConnection();
  508. SqlCommand cmd = new SqlCommand();
  509. string sql = string.Empty;
  510. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  511. foreach (string WorkPoint in result)
  512. {
  513. try
  514. {
  515. connS = string.Format(connString, WorkPoint);
  516. conn = new System.Data.SqlClient.SqlConnection(connS);
  517. conn.Open();
  518. SqlTransaction sqlTran = conn.BeginTransaction();
  519. cmd = new SqlCommand();
  520. cmd.Transaction = sqlTran;
  521. cmd.Connection = conn;
  522. foreach (ICSMOIssueDocNegative info in infos)
  523. {
  524. if (WorkPoint != info.WorkPoint)
  525. continue;
  526. ICSUserInfo userInfo = new ICSUserInfo();
  527. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  528. if (info.MTime < new DateTime(2000, 01, 01))
  529. throw new Exception("请输入正确的操作时间:" + info.MTime);
  530. sql = @"UPDATE dbo.rdrecord11 SET cHandler ='" + userInfo.UserName + @"' ,
  531. dnmodifytime =CONVERT(VARCHAR(50),GETDATE(),112),dModifyDate =GETDATE() WHERE ID='{0}'";
  532. sql = string.Format(sql, info.ID);
  533. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核生产退料单失败!");
  534. }
  535. cmd.Transaction.Commit();
  536. }
  537. catch (Exception ex)
  538. {
  539. cmd.Transaction.Rollback();
  540. log.Error(ex.Message);
  541. throw new Exception(ex.Message);
  542. }
  543. finally
  544. {
  545. if (conn.State == ConnectionState.Open)
  546. {
  547. conn.Close();
  548. }
  549. conn.Dispose();
  550. }
  551. }
  552. return json;
  553. }
  554. /// <summary>
  555. /// 删除请购单
  556. /// </summary>
  557. /// <param name="infos"></param>
  558. /// <returns></returns>
  559. public string Delete(List<ICSMOIssueDocNegative> infos)
  560. {
  561. List<ICSMOIssueDocNegative> szJson = new List<ICSMOIssueDocNegative>();
  562. if (infos.Count <= 0)
  563. {
  564. throw new Exception("传送数据为空!");
  565. }
  566. string res = string.Empty;
  567. string connS = "";
  568. SqlConnection conn = new SqlConnection();
  569. SqlCommand cmd = new SqlCommand();
  570. string sql = string.Empty;
  571. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  572. foreach (string WorkPoint in result)
  573. {
  574. try
  575. {
  576. connS = string.Format(connString, WorkPoint);
  577. conn = new System.Data.SqlClient.SqlConnection(connS);
  578. conn.Open();
  579. SqlTransaction sqlTran = conn.BeginTransaction();
  580. cmd = new SqlCommand();
  581. cmd.Transaction = sqlTran;
  582. cmd.Connection = conn;
  583. foreach (ICSMOIssueDocNegative info in infos)
  584. {
  585. if (WorkPoint != info.WorkPoint)
  586. continue;
  587. if (info.MTime < new DateTime(2000, 01, 01))
  588. throw new Exception("请输入正确的操作时间:" + info.MTime);
  589. sql = @"delete rdrecord11 where rdrecord11.ID='" + info.ID + "'";
  590. sql += @"delete rdrecords11 where rdrecords11.ID='" + info.ID + "'";
  591. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  592. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  593. // WHERE a.ID='" + info.ID + "'";
  594. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除材料出库单失败!");
  595. }
  596. cmd.Transaction.Commit();
  597. }
  598. catch (Exception ex)
  599. {
  600. cmd.Transaction.Rollback();
  601. log.Error(ex.Message);
  602. throw new Exception(ex.Message);
  603. }
  604. finally
  605. {
  606. if (conn.State == ConnectionState.Open)
  607. {
  608. conn.Close();
  609. }
  610. conn.Dispose();
  611. }
  612. }
  613. return res;
  614. }
  615. private void LogInfo(List<ICSMOIssueDocNegative> Bills)
  616. {
  617. string HeadList = string.Empty;
  618. string BodyList = string.Empty;
  619. foreach (ICSMOIssueDocNegative head in Bills)
  620. {
  621. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  622. foreach (ICSMOIssueDocNegatives body in head.details)
  623. {
  624. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  625. }
  626. }
  627. log.Info(HeadList);
  628. log.Info(BodyList);
  629. }
  630. /// <summary>
  631. /// 表体合并
  632. /// </summary>
  633. /// <param name="Bills"></param>
  634. private void MergeObject(List<ICSMOIssueDocNegative> Bills, SqlCommand cmd)
  635. {
  636. foreach (ICSMOIssueDocNegative head in Bills)
  637. {
  638. DataTable dt = DBHelper.ToDataTable(head.details.ToList());
  639. DataTable dtNew = dt.Clone();
  640. dtNew = DBHelper.MergeDataTable(dt, "AllocateId", "cBatch", "iQuantity", "InvCode", cmd);
  641. List<ICSMOIssueDocNegatives> list = DBHelper.ConvertTo<ICSMOIssueDocNegatives>(dtNew).ToList();
  642. head.details = list;
  643. }
  644. }
  645. }
  646. }