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

369 lines
16 KiB

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