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

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