爱思开
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.

461 lines
20 KiB

2 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 PurchaseRequisition
  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. /// <summary>
  23. /// 获取请购单
  24. /// </summary>
  25. /// <param name="infos"></param>
  26. /// <returns></returns>
  27. public string Get(List<ICSPurchaseRequisition> infos)
  28. {
  29. List<ICSPurchaseRequisition> szJson = new List<ICSPurchaseRequisition>();
  30. DataTable dt = null;
  31. DataTable dtNew = null;
  32. string connS = "";
  33. string json = "";
  34. if (infos.Count <= 0)
  35. {
  36. throw new Exception("传送数据为空!");
  37. }
  38. string res = string.Empty;
  39. SqlConnection conn = new SqlConnection();
  40. SqlCommand cmd = new SqlCommand();
  41. string sql = string.Empty;
  42. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  43. foreach (string WorkPoint in result)
  44. {
  45. try
  46. {
  47. connS = string.Format(connString, WorkPoint);
  48. conn = new System.Data.SqlClient.SqlConnection(connS);
  49. conn.Open();
  50. SqlTransaction sqlTran = conn.BeginTransaction();
  51. cmd = new SqlCommand();
  52. cmd.Transaction = sqlTran;
  53. cmd.Connection = conn;
  54. foreach (ICSPurchaseRequisition info in infos)
  55. {
  56. if (WorkPoint != info.WorkPoint)
  57. continue;
  58. ICSUserInfo userInfo = new ICSUserInfo();
  59. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  60. if (info.MTime < new DateTime(2000, 01, 01))
  61. throw new Exception("请输入正确的操作时间:" + info.MTime);
  62. sql = @" select a.ID,a.cCode,a.cDepCode ,c.cDepName,a.cMaker ,a.cMakeTime ,a.cVerifier ,a.cAuditTime,
  63. b.AutoID ,b.irowno ,b.cInvCode ,b.fQuantity ,b.dRequirDate ,b.dArriveDate ,b.cexch_name ,b.fUnitPrice
  64. from PU_AppVouch a
  65. inner join PU_AppVouchs b on a.ID =b.ID
  66. left join Department c on a.cDepCode=c.cDepCode WHERE 1=1";
  67. if (!string.IsNullOrWhiteSpace(info.PRCode))
  68. {
  69. sql += " and a.cCode='{0}'";
  70. }
  71. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  72. {
  73. sql += " and ISNULL(a.dCloseTime ,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime))))>='{1}'";
  74. }
  75. if (!string.IsNullOrWhiteSpace(info.User))
  76. {
  77. sql += "and a.CMAKER='{2}'";
  78. }
  79. sql = string.Format(sql, info.PRCode, info.MTime, userInfo.UserName);
  80. dt = DBHelper.SQlReturnData(sql, cmd);
  81. if (dt.Rows.Count <= 0 || dt == null)
  82. throw new Exception("请购单:" + info.PRCode + ",无信息!");
  83. if (dtNew == null)
  84. dtNew = dt;
  85. else
  86. dtNew.Merge(dt);
  87. cmd.CommandType = CommandType.Text;
  88. cmd.Parameters.Clear();
  89. }
  90. cmd.Transaction.Commit();
  91. }
  92. catch (Exception ex)
  93. {
  94. if (cmd.Transaction != null)
  95. cmd.Transaction.Rollback();
  96. log.Error(ex.Message);
  97. throw new Exception(ex.Message);
  98. }
  99. finally
  100. {
  101. if (conn.State == ConnectionState.Open)
  102. {
  103. conn.Close();
  104. }
  105. conn.Dispose();
  106. }
  107. }
  108. json = JsonConvert.SerializeObject(dtNew);
  109. return json;
  110. }
  111. /// <summary>
  112. /// 创建请购单
  113. /// </summary>
  114. /// <param name="Bills"></param>
  115. /// <returns></returns>
  116. public string CreatePurchaseRequisition(List<ICSPurchaseRequisition> Bills)
  117. {
  118. string msg = "";
  119. DataTable dt = null;
  120. DataTable dtNew = null;
  121. string connS = "";
  122. SqlConnection conn = new SqlConnection();
  123. VouchKey key = new VouchKey();
  124. int num = 0;
  125. SqlCommand cmd = new SqlCommand();
  126. if (Bills.Count <= 0)
  127. {
  128. throw new Exception("传送数据为空!");
  129. }
  130. LogInfo(Bills);
  131. //MergeObject(Bills, cmd);
  132. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  133. foreach (string WorkPoint in result)
  134. {
  135. try
  136. {
  137. connS = string.Format(connString, WorkPoint);
  138. conn = new System.Data.SqlClient.SqlConnection(connS);
  139. conn.Open();
  140. SqlTransaction sqlTran = conn.BeginTransaction();
  141. cmd = new SqlCommand();
  142. cmd.Transaction = sqlTran;
  143. cmd.Connection = conn;
  144. foreach (ICSPurchaseRequisition head in Bills)
  145. {
  146. num = head.details.Count();
  147. if (WorkPoint != head.WorkPoint)
  148. continue;
  149. cmd.CommandTimeout = 300;
  150. string[] ss = head.WorkPoint.Split('_');
  151. ERPDB = ss[1];
  152. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuApp", "" + num + "", head.WorkPoint, cmd);
  153. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  154. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  155. DateTime date = DateTime.Now;
  156. string iBaseCodeLen = DBHelper.GetAllRDCode("27", "" + date + "", "admin", "", head.WorkPoint, cmd);
  157. string sql = string.Empty;
  158. #region 请购单表头
  159. sql = @"Insert Into PU_AppVouch
  160. (ivtid,id,ccode,ddate,cdepcode,
  161. cpersoncode,cbustype,cmaker,cverifier,
  162. iverifystateex,ireturncount,iswfcontrolled,cAuditDate,iPrintCount,
  163. cMakeTime,cAuditTime
  164. )
  165. Values
  166. ('8171',@ID,@ccode,CONVERT(VARCHAR(10),GETDATE(),23),@cdepcode,
  167. null,'',@cmaker,@cverifier,
  168. '2','0','0','','0',
  169. GETDATE(),GETDATE()
  170. )" + Environment.NewLine;
  171. cmd.Parameters.Clear();
  172. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  173. cmd.Parameters.Add(new SqlParameter("@ccode", iBaseCodeLen));
  174. cmd.Parameters.Add(new SqlParameter("@cdepcode", head.DepCode));
  175. cmd.Parameters.Add(new SqlParameter("@cverifier", head.User));
  176. cmd.Parameters.Add(new SqlParameter("@cmaker", head.User));
  177. //cmd.Parameters.Add(new SqlParameter("@bredvouch",0));
  178. cmd.CommandText = sql;
  179. try
  180. {
  181. int count = cmd.ExecuteNonQuery();
  182. if (count <= 0)
  183. {
  184. log.Error("请购单表头失败,受影响行数<=0;");
  185. throw new Exception("请购单表头失败,受影响行数<=0;");
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. log.Error("请购单表头失败" + sql, ex);
  191. throw new Exception("请购单表头失败" + sql, ex);
  192. }
  193. #endregion
  194. #region 请购单表体
  195. int irowno = 0;
  196. foreach (ICSPurchaseRequisitions body in head.details)
  197. {
  198. iChildId -= 1;
  199. sql += @"IF NOT EXISTS(
  200. SELECT cInvCode
  201. FROM Inventory
  202. WHERE cInvCode='@cInvCode'
  203. )
  204. BEGIN
  205. RAISERROR('@cInvCode ',16,0)
  206. END" + Environment.NewLine;
  207. sql = @"Insert Into PU_AppVouchs
  208. (id,autoid,cvencode,cinvcode,fquantity,
  209. funitprice,ipertaxrate,ftaxprice,fmoney,drequirdate,
  210. darrivedate,iReceivedQTY,cdefine22,cdefine23,btaxcost,
  211. iReceivedNum,cpersoncodeexec,cdepcodeexec,cexch_name,iexchrate,
  212. ioricost,ioritaxcost,iorimoney,ioritaxprice,iorisum,
  213. imoney,itaxprice,ivouchrowno)
  214. VALUES
  215. (@ID,@AutoID,NULL,@cInvCode,@fQuantity,
  216. NULL,17,NULL,NULL,@drequirdate,
  217. @darrivedate,'0',NULL,NULL,1,
  218. 0,NULL,NULL,N'',1,
  219. NULL,NULL,NULL,NULL,NULL,
  220. NULL,NULL,@ivouchrowno)" + Environment.NewLine;
  221. cmd.Parameters.Clear();
  222. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  223. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  224. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  225. cmd.Parameters.Add(new SqlParameter("@fQuantity", body.Quantity));
  226. cmd.Parameters.Add(new SqlParameter("@drequirdate", body.RequirDate));
  227. cmd.Parameters.Add(new SqlParameter("@darrivedate", body.ArriveDate));
  228. cmd.Parameters.Add(new SqlParameter("@ivouchrowno", body.Sequence));
  229. cmd.CommandText = sql;
  230. try
  231. {
  232. int count = cmd.ExecuteNonQuery();
  233. if (count <= 0)
  234. {
  235. log.Error("请购单表体失败,受影响行数<=0;");
  236. throw new Exception("请购单表体失败,受影响行数<=0;");
  237. }
  238. }
  239. catch (Exception ex)
  240. {
  241. log.Error("请购单表体失败" + sql, ex);
  242. throw new Exception("请购单表体失败 " + sql, ex);
  243. }
  244. }
  245. sql = @"
  246. select a.ID,a.cCode,a.cDepCode ,c.cDepName,a.cMaker ,a.cMakeTime ,a.cVerifier ,a.cAuditTime,
  247. b.AutoID ,b.irowno ,b.cInvCode ,b.fQuantity ,b.dRequirDate ,b.dArriveDate ,b.cexch_name ,b.fUnitPrice
  248. from PU_AppVouch a
  249. inner join PU_AppVouchs b on a.ID =b.ID
  250. left join Department c on a.cDepCode=c.cDepCode where a.id='{0}' ";
  251. sql = string.Format(sql, iFatherId);
  252. dt = DBHelper.SQlReturnData(sql, cmd);
  253. #endregion
  254. if (dtNew == null)
  255. dtNew = dt;
  256. else
  257. dtNew.Merge(dt);
  258. cmd.CommandType = CommandType.Text;
  259. cmd.Parameters.Clear();
  260. }
  261. cmd.Transaction.Commit();
  262. }
  263. catch (Exception ex)
  264. {
  265. if (cmd.Transaction != null)
  266. cmd.Transaction.Rollback();
  267. log.Error(ex.Message);
  268. throw new Exception(ex.Message);
  269. }
  270. finally
  271. {
  272. if (conn.State == ConnectionState.Open)
  273. {
  274. conn.Close();
  275. }
  276. conn.Dispose();
  277. }
  278. }
  279. msg = JsonConvert.SerializeObject(dtNew);
  280. return msg;
  281. }
  282. /// <summary>
  283. /// 审核请购单
  284. /// </summary>
  285. /// <param name="infos"></param>
  286. /// <returns></returns>
  287. public string Approve(List<ICSPurchaseRequisition> infos)
  288. {
  289. List<ICSPurchaseRequisition> szJson = new List<ICSPurchaseRequisition>();
  290. string connS = "";
  291. string json = "";
  292. if (infos.Count <= 0)
  293. {
  294. throw new Exception("传送数据为空!");
  295. }
  296. string res = string.Empty;
  297. SqlConnection conn = new SqlConnection();
  298. SqlCommand cmd = new SqlCommand();
  299. string sql = string.Empty;
  300. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  301. foreach (string WorkPoint in result)
  302. {
  303. try
  304. {
  305. connS = string.Format(connString, WorkPoint);
  306. conn = new System.Data.SqlClient.SqlConnection(connS);
  307. conn.Open();
  308. SqlTransaction sqlTran = conn.BeginTransaction();
  309. cmd = new SqlCommand();
  310. cmd.Transaction = sqlTran;
  311. cmd.Connection = conn;
  312. foreach (ICSPurchaseRequisition info in infos)
  313. {
  314. if (WorkPoint != info.WorkPoint)
  315. continue;
  316. if (info.MTime < new DateTime(2000, 01, 01))
  317. throw new Exception("请输入正确的操作时间:" + info.MTime);
  318. sql = @"UPDATE dbo.PU_AppVouch SET cVerifier ='" + info.User + @"' ,
  319. cAuditTime =CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate =GETDATE() WHERE ID='{0}'";
  320. sql = string.Format(sql, info.ID);
  321. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  322. cmd.CommandType = CommandType.Text;
  323. cmd.Parameters.Clear();
  324. }
  325. cmd.Transaction.Commit();
  326. }
  327. catch (Exception ex)
  328. {
  329. if (cmd.Transaction != null)
  330. cmd.Transaction.Rollback();
  331. log.Error(ex.Message);
  332. throw new Exception(ex.Message);
  333. }
  334. finally
  335. {
  336. if (conn.State == ConnectionState.Open)
  337. {
  338. conn.Close();
  339. }
  340. conn.Dispose();
  341. }
  342. }
  343. return json;
  344. }
  345. /// <summary>
  346. /// 删除请购单
  347. /// </summary>
  348. /// <param name="infos"></param>
  349. /// <returns></returns>
  350. public string Delete(List<ICSPurchaseRequisition> infos)
  351. {
  352. List<ICSPurchaseRequisition> szJson = new List<ICSPurchaseRequisition>();
  353. if (infos.Count <= 0)
  354. {
  355. throw new Exception("传送数据为空!");
  356. }
  357. string res = string.Empty;
  358. string connS = "";
  359. SqlConnection conn = new SqlConnection();
  360. SqlCommand cmd = new SqlCommand();
  361. string sql = string.Empty;
  362. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  363. foreach (string WorkPoint in result)
  364. {
  365. try
  366. {
  367. connS = string.Format(connString, WorkPoint);
  368. conn = new System.Data.SqlClient.SqlConnection(connS);
  369. conn.Open();
  370. SqlTransaction sqlTran = conn.BeginTransaction();
  371. cmd = new SqlCommand();
  372. cmd.Transaction = sqlTran;
  373. cmd.Connection = conn;
  374. foreach (ICSPurchaseRequisition info in infos)
  375. {
  376. if (WorkPoint != info.WorkPoint)
  377. continue;
  378. if (info.MTime < new DateTime(2000, 01, 01))
  379. throw new Exception("请输入正确的操作时间:" + info.MTime);
  380. sql = @"delete PU_AppVouch where PU_AppVouch.ID='" + info.ID + "'";
  381. sql += @"delete PU_AppVouchs where PU_AppVouchs.ID='" + info.ID + "'";
  382. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除请购单失败!");
  383. cmd.CommandType = CommandType.Text;
  384. cmd.Parameters.Clear();
  385. }
  386. cmd.Transaction.Commit();
  387. }
  388. catch (Exception ex)
  389. {
  390. if (cmd.Transaction != null)
  391. cmd.Transaction.Rollback();
  392. log.Error(ex.Message);
  393. throw new Exception(ex.Message);
  394. }
  395. finally
  396. {
  397. if (conn.State == ConnectionState.Open)
  398. {
  399. conn.Close();
  400. }
  401. conn.Dispose();
  402. }
  403. }
  404. return res;
  405. }
  406. private void LogInfo(List<ICSPurchaseRequisition> Bills)
  407. {
  408. string HeadList = string.Empty;
  409. string BodyList = string.Empty;
  410. foreach (ICSPurchaseRequisition head in Bills)
  411. {
  412. HeadList += "\r\n 表头主键ID:" + head.ID + ",部门:" + head.DepCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  413. foreach (ICSPurchaseRequisitions body in head.details)
  414. {
  415. // BodyList += "\r\n 表体主键ID: " + body. + ",数量:" + body.Quantity;
  416. }
  417. }
  418. log.Info(HeadList);
  419. log.Info(BodyList);
  420. }
  421. }
  422. }