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

415 lines
18 KiB

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