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

453 lines
20 KiB

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