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

418 lines
20 KiB

  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 生产退料单(红字材料出库)
  15. /// </summary>
  16. public class MOIssueDocNegatives
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  20. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  21. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  22. 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. string sql = string.Empty;
  92. string iFatherIdTwo = string.Empty;
  93. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  94. conn.Open();
  95. VouchKey key = new VouchKey();
  96. int num = 0;
  97. SqlTransaction sqlTran = conn.BeginTransaction();
  98. SqlCommand cmd = new SqlCommand();
  99. cmd.Transaction = sqlTran;
  100. cmd.Connection = conn;
  101. cmd.CommandTimeout = 300;
  102. try
  103. {
  104. if (Bills.Count <= 0)
  105. {
  106. throw new Exception("传送数据为空!");
  107. }
  108. LogInfo(Bills);
  109. //MergeObject(Bills, cmd);
  110. LogInfo(Bills);
  111. foreach (ICSMOIssueDocNegative head in Bills)
  112. {
  113. num = head.details.Count();
  114. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  115. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  116. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  117. DateTime date = DateTime.Now;
  118. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  119. string DEF_ID = DBHelper.GetDefaultTemplate(Type, cmd);
  120. // #region 判断工单是否已关闭
  121. // sql = @"SELECT isnull(CloseUser,'') as CloseUser from mom_orderdetail a
  122. // inner join mom_order b on a.MoId=b.MoId where b.MoId='" + head.DetailID + "' ";
  123. // DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  124. // if (dtClose != null && dtClose.Rows.Count > 0)
  125. // {
  126. // if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString()))
  127. // {
  128. // throw new Exception("该工单行已关闭,无法领料!");
  129. // }
  130. // }
  131. // #endregion
  132. #region 材料出库单表头
  133. sql = @"INSERT INTO dbo.rdrecord11
  134. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  135. cCode ,cRdCode ,cDepCode,cHandler ,bTransFlag ,cMaker ,
  136. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc
  137. ,cPsPcode ,cMPoCode ,iproorderid ,bFromPreYear ,
  138. bIsLsQuery ,bIsComplement ,iDiscountTaxType ,ireturncount ,iverifystate ,
  139. iswfcontrolled ,dnmaketime ,dnverifytime ,bredvouch ,iPrintCount,
  140. iMQuantity)
  141. SELECT @ID,0,'11','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),
  142. @cCode,'23',a.MDeptCode,@cHandler,0,@cMaker,
  143. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,
  144. a.InvCode,b.MoCode,a.MoId,0,
  145. 0,0,0,0,0,
  146. 0,GETDATE(),GETDATE(),1,1,
  147. a.Qty
  148. FROM dbo.mom_orderdetail a
  149. left JOIN dbo.mom_order b ON a.MoId=b.MoId
  150. left join mom_morder c on a.modid=c.modid
  151. WHERE a.MoDId='" + head.MODetailID + "'";
  152. cmd.Parameters.Clear();
  153. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  154. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  155. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  156. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  157. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  158. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  159. //cmd.Parameters.Add(new SqlParameter("@bredvouch",0));
  160. cmd.CommandText = sql;
  161. try
  162. {
  163. int count = cmd.ExecuteNonQuery();
  164. if (count <= 0)
  165. {
  166. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  167. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  168. }
  169. }
  170. catch (Exception ex)
  171. {
  172. log.Error("生成材料出库单表头失败" + sql, ex);
  173. throw new Exception("生成材料出库单表头失败" + sql, ex);
  174. }
  175. #endregion
  176. #region 材料出库单表体
  177. foreach (ICSMOIssueDocNegatives body in head.details)
  178. {
  179. sql = @"INSERT INTO dbo.rdrecords11
  180. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,
  181. cItemCode ,cName ,iNQuantity ,iMPoIds ,bLPUseFree ,
  182. iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,invcode ,imoseq ,
  183. iopseq ,iExpiratDateCalcu ,iorderdid ,iordertype ,isotype ,
  184. ipesodid ,ipesotype ,cpesocode ,ipesoseq ,irowno ,
  185. bcanreplace ,iposflag)
  186. SELECT @AutoID,@ID,a.InvCode,@iQuantity,@cBatch,0,
  187. null,null,a.Qty,a.AllocateId,0,
  188. 0,1,0,c.MoCode,b.InvCode,b.SortSeq,
  189. '0000',0,0,0,0,
  190. a.AllocateId,'7',c.MoCode,b.SortSeq,@irowno,
  191. 0,null
  192. FROM dbo.mom_moallocate a
  193. INNER JOIN dbo.mom_orderdetail b ON a.MoDId=b.MoDId
  194. INNER JOIN dbo.mom_order c ON b.MoId=c.MoId
  195. WHERE a.AllocateId='" + body.SourceDetailID + "'";
  196. cmd.Parameters.Clear();
  197. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  198. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  199. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  200. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  201. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  202. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  203. cmd.CommandText = sql;
  204. try
  205. {
  206. int count = cmd.ExecuteNonQuery();
  207. if (count <= 0)
  208. {
  209. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  210. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  211. }
  212. }
  213. catch (Exception ex)
  214. {
  215. log.Error("生成材料出库单表体失败" + sql, ex);
  216. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  217. }
  218. //更新现存量
  219. key.cBustypeUN = "领料";
  220. key.cVouchTypeUN = "11";
  221. key.TableName = "IA_ST_UnAccountVouch11";
  222. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  223. #region 回写工单子件已领数量
  224. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + " where AllocateId='" + body.SourceDetailID + "' ";
  225. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  226. #endregion
  227. iChildId--;
  228. }
  229. #endregion
  230. iFatherIdTwo += "'" + iFatherId + "',";
  231. }
  232. #region 查询
  233. sql = @"
  234. select a.ID as ID,a.ID as IDs,cMPoCode as SourceCode ,
  235. cCode as IssueNEGCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  236. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  237. cHandler as Checker,dnverifytime as CheckDateTime
  238. from rdrecord11 a
  239. left join Department c on a.cDepCode=c.cDepCode
  240. left join Warehouse d on a.cWhCode=d.cWhCode
  241. WHERE a.ID in ({0})
  242. select a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  243. ,iQuantity as Quantity,e.AllocateId as SourceDetailID,iNum as Amount
  244. from rdrecord11 a
  245. left join rdrecords11 b on a.ID=b.ID
  246. left join Department c on a.cDepCode=c.cDepCode
  247. left join Warehouse d on a.cWhCode=d.cWhCode
  248. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  249. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  250. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({0})";
  251. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  252. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  253. string RelationName = "details";
  254. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  255. ds.Relations.Add(dr);
  256. msg = DBHelper.DataSetToJson(ds, RelationName);
  257. cmd.Transaction.Commit();
  258. return msg;
  259. #endregion
  260. }
  261. catch (Exception ex)
  262. {
  263. cmd.Transaction.Rollback();
  264. log.Error(ex.Message);
  265. throw new Exception(ex.Message);
  266. }
  267. finally
  268. {
  269. if (conn.State == ConnectionState.Open)
  270. {
  271. conn.Close();
  272. }
  273. conn.Dispose();
  274. }
  275. }
  276. public string Approve(List<ICSMOIssueDocNegative> infos)
  277. {
  278. List<ICSMOIssueDocNegative> szJson = new List<ICSMOIssueDocNegative>();
  279. DataTable dt = null;
  280. string json = "";
  281. if (infos.Count <= 0)
  282. {
  283. throw new Exception("传送数据为空!");
  284. }
  285. string res = string.Empty;
  286. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  287. conn.Open();
  288. SqlTransaction sqlTran = conn.BeginTransaction();
  289. SqlCommand cmd = new SqlCommand();
  290. cmd.Transaction = sqlTran;
  291. cmd.Connection = conn;
  292. try
  293. {
  294. string sql = string.Empty;
  295. foreach (ICSMOIssueDocNegative info in infos)
  296. {
  297. if (info.MTime < new DateTime(2000, 01, 01))
  298. throw new Exception("请输入正确的操作时间:" + info.MTime);
  299. sql = @"UPDATE dbo.rdrecord11 SET cHandler ='" + info.User + @"' ,
  300. dnmodifytime =CONVERT(VARCHAR(50),GETDATE(),112),dModifyDate =GETDATE() WHERE ID='{0}'";
  301. sql = string.Format(sql, info.ID);
  302. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核生产退料单失败!");
  303. }
  304. cmd.Transaction.Commit();
  305. return json;
  306. }
  307. catch (Exception ex)
  308. {
  309. cmd.Transaction.Rollback();
  310. log.Error(ex.Message);
  311. throw new Exception(ex.Message);
  312. }
  313. finally
  314. {
  315. if (conn.State == ConnectionState.Open)
  316. {
  317. conn.Close();
  318. }
  319. conn.Dispose();
  320. }
  321. }
  322. /// <summary>
  323. /// 删除请购单
  324. /// </summary>
  325. /// <param name="infos"></param>
  326. /// <returns></returns>
  327. public string Delete(List<ICSMOIssueDocNegative> infos)
  328. {
  329. List<ICSMOIssueDocNegative> szJson = new List<ICSMOIssueDocNegative>();
  330. if (infos.Count <= 0)
  331. {
  332. throw new Exception("传送数据为空!");
  333. }
  334. string res = string.Empty;
  335. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  336. conn.Open();
  337. SqlTransaction sqlTran = conn.BeginTransaction();
  338. SqlCommand cmd = new SqlCommand();
  339. cmd.Transaction = sqlTran;
  340. cmd.Connection = conn;
  341. try
  342. {
  343. string sql = string.Empty;
  344. foreach (ICSMOIssueDocNegative info in infos)
  345. {
  346. if (info.MTime < new DateTime(2000, 01, 01))
  347. throw new Exception("请输入正确的操作时间:" + info.MTime);
  348. sql = @"delete rdrecord11 where rdrecord11.ID='" + info.ID + "'";
  349. sql += @"delete rdrecords11 where rdrecords11.ID='" + info.ID + "'";
  350. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  351. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  352. // WHERE a.ID='" + info.ID + "'";
  353. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除材料出库单失败!");
  354. }
  355. cmd.Transaction.Commit();
  356. return res;
  357. ;
  358. }
  359. catch (Exception ex)
  360. {
  361. cmd.Transaction.Rollback();
  362. log.Error(ex.Message);
  363. throw new Exception(ex.Message);
  364. }
  365. finally
  366. {
  367. if (conn.State == ConnectionState.Open)
  368. {
  369. conn.Close();
  370. }
  371. conn.Dispose();
  372. }
  373. }
  374. private void LogInfo(List<ICSMOIssueDocNegative> Bills)
  375. {
  376. string HeadList = string.Empty;
  377. string BodyList = string.Empty;
  378. foreach (ICSMOIssueDocNegative head in Bills)
  379. {
  380. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User;
  381. foreach (ICSMOIssueDocNegatives body in head.details)
  382. {
  383. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  384. }
  385. }
  386. log.Info(HeadList);
  387. log.Info(BodyList);
  388. }
  389. /// <summary>
  390. /// 表体合并
  391. /// </summary>
  392. /// <param name="Bills"></param>
  393. private void MergeObject(List<ICSMOIssueDocNegative> Bills, SqlCommand cmd)
  394. {
  395. foreach (ICSMOIssueDocNegative head in Bills)
  396. {
  397. DataTable dt = DBHelper.ToDataTable(head.details.ToList());
  398. DataTable dtNew = dt.Clone();
  399. dtNew = DBHelper.MergeDataTable(dt, "AllocateId", "cBatch", "iQuantity", "InvCode", cmd);
  400. List<ICSMOIssueDocNegatives> list = DBHelper.ConvertTo<ICSMOIssueDocNegatives>(dtNew).ToList();
  401. head.details = list;
  402. }
  403. }
  404. }
  405. }