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

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