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

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