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

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