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

404 lines
19 KiB

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