爱思开
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.

487 lines
23 KiB

2 years ago
  1. using ICSSoft.Entity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using Newtonsoft.Json;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using ICSSoft.Common;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 采购订单
  15. /// </summary>
  16. public class PurchaseOrder
  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<ICSPurchaseOrder> infos)
  28. {
  29. DataTable dt = null;
  30. DataTable dtNew = null;
  31. string connS = "";
  32. string sql = "";
  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. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  42. foreach (string WorkPoint in result)
  43. {
  44. try
  45. {
  46. connS = string.Format(connString, 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. foreach (ICSPurchaseOrder info in infos)
  54. {
  55. if (WorkPoint != info.WorkPoint)
  56. continue;
  57. ICSUserInfo userInfo = new ICSUserInfo();
  58. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  59. if (info.MTime < new DateTime(2000, 01, 01))
  60. throw new Exception("请输入正确的操作时间:" + info.MTime);
  61. sql = @" SELECT A.CPOID,A.CVENCODE,A.CDEPCODE,A.CAPPCODE,C.CDEPNAME,D.CVENNAME,A.CMAKER,A.CMAKETIME,A.CVERIFIER,A.CAUDITTIME,
  62. B.IROWNO,B.CINVCODE ,B.IQUANTITY ,B.INUM,B.IRECEIVEDQTY,B.DARRIVEDATE,A.CEXCH_NAME,B.ITAXPRICE,B.INATUNITPRICE,B.IUNITPRICE
  63. FROM PO_POMAIN A
  64. INNER JOIN PO_PODETAILS B ON A.POID=B.POID
  65. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  66. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1";
  67. if (!string.IsNullOrWhiteSpace(info.POCode))
  68. {
  69. sql += " and a.CPOID='{0}'";
  70. }
  71. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  72. {
  73. sql += " and ISNULL(b.cbCloseTime,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.POCode, 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. cmd.CommandType = CommandType.Text;
  88. cmd.Parameters.Clear();
  89. }
  90. cmd.Transaction.Commit();
  91. }
  92. catch (Exception ex)
  93. {
  94. if (cmd.Transaction != null)
  95. cmd.Transaction.Rollback();
  96. log.Error(ex.Message);
  97. throw new Exception(ex.Message);
  98. }
  99. finally
  100. {
  101. if (conn.State == ConnectionState.Open)
  102. {
  103. conn.Close();
  104. }
  105. conn.Dispose();
  106. }
  107. }
  108. json = JsonConvert.SerializeObject(dtNew);
  109. return json;
  110. }
  111. /// <summary>
  112. /// 创建采购订单
  113. /// </summary>
  114. /// <param name="infos"></param>
  115. /// <returns></returns>
  116. public string CreateICSPurchaseOrder(List<ICSPurchaseOrder> infos)
  117. {
  118. int num = 0;
  119. DataTable dtNew = null;
  120. string connS = "";
  121. string szJson = "";
  122. DataTable dt = null;
  123. string sql = "";
  124. if (infos.Count <= 0)
  125. {
  126. throw new Exception("传送数据为空!");
  127. }
  128. string res = string.Empty;
  129. SqlConnection conn = new SqlConnection();
  130. SqlCommand cmd = new SqlCommand();
  131. #region 采购订单表头
  132. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  133. foreach (string WorkPoint in result)
  134. {
  135. try
  136. {
  137. connS = string.Format(connString, WorkPoint);
  138. conn = new System.Data.SqlClient.SqlConnection(connS);
  139. conn.Open();
  140. SqlTransaction sqlTran = conn.BeginTransaction();
  141. cmd = new SqlCommand();
  142. cmd.Transaction = sqlTran;
  143. cmd.Connection = conn;
  144. foreach (ICSPurchaseOrder info in infos)
  145. {
  146. num = info.details.Count();
  147. if (WorkPoint != info.WorkPoint)
  148. continue;
  149. ICSUserInfo userInfo = new ICSUserInfo();
  150. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  151. string[] ss = info.WorkPoint.Split('_');
  152. ERPDB = ss[1];
  153. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PoMain", "" + num + "", info.WorkPoint, cmd);
  154. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  155. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  156. DateTime date = DateTime.Now;
  157. string iBaseCodeLen = DBHelper.GetAllRDCode("88", "" + date + "", "admin", "", info.WorkPoint, cmd);
  158. if (string.IsNullOrWhiteSpace(info.PRCode))
  159. {
  160. sql = @"Insert Into dbo.PO_Pomain
  161. (cPOID ,dPODate ,cVenCode ,cexch_name ,
  162. nflat ,iTaxRate ,iCost ,iBargain ,
  163. cMemo ,cState ,cMaker ,cVerifier ,
  164. POID ,iVTid ,cBusType ,iDiscountTaxType ,
  165. iverifystateex ,ireturncount ,IsWfControlled ,cmaketime ,
  166. cAuditTime ,cAuditDate ,iPrintCount,cDefine14,
  167. cPersonCode,cDepCode)
  168. Values
  169. ( @cPOID,CONVERT(NVARCHAR(15),GETDATE(),23),@cVenCode,'',
  170. '','',0,0,
  171. '',1,@cMaker,'',
  172. @POID,'','',0,
  173. 2,0,0,GETDATE(),
  174. GETDATE() ,CONVERT(NVARCHAR(15),GETDATE(),23) ,0 ,'',
  175. null,@cDepCode)" + Environment.NewLine;
  176. cmd.Parameters.Clear();
  177. cmd.Parameters.Add("@cPOID", iBaseCodeLen);
  178. cmd.Parameters.Add("@cVenCode", info.VenCode);
  179. cmd.Parameters.Add("@POID", iFatherId);
  180. cmd.Parameters.Add("@cMaker", userInfo.UserName);
  181. cmd.Parameters.Add("@cDepCode", "''");
  182. }
  183. else
  184. {
  185. sql += @"INSERT INTO dbo.PO_Pomain
  186. (cPOID ,dPODate ,cVenCode ,cexch_name ,
  187. nflat ,iTaxRate ,iCost ,iBargain ,
  188. cMemo ,cState ,cMaker ,cVerifier ,
  189. POID ,iVTid ,cBusType ,iDiscountTaxType ,
  190. iverifystateex ,ireturncount ,IsWfControlled ,cmaketime ,
  191. cAuditTime ,cAuditDate ,iPrintCount,cDefine14,
  192. cPersonCode,cDepCode)
  193. SELECT
  194. @cPOID,CONVERT(NVARCHAR(15),GETDATE(),23),@cVenCode ,'' ,
  195. '' AS nflat ,'' ,0 AS iCost ,0 AS iBargain,
  196. cMemo , '1' AS cState ,cMaker ,cVerifier ,
  197. @POID ,'' AS iVTid ,'' AS cBusType ,0 AS iDiscountTaxType ,
  198. 2 AS iverifystateex ,0 AS ireturncount ,0 AS IsWfControlled ,GETDATE() AS cmaketime ,
  199. GETDATE() AS cAuditTime ,CONVERT(NVARCHAR(15),GETDATE(),23) AS cAuditDate ,0 AS iPrintCount ,ID,
  200. cPersonCode,@cDepCode FROM dbo.PU_AppVouch where ccode='" + info.PRCode + "'";
  201. cmd.Parameters.Clear();
  202. cmd.Parameters.Add("@cPOID", iBaseCodeLen);
  203. cmd.Parameters.Add("@cVenCode", info.VenCode);
  204. cmd.Parameters.Add("@POID", iFatherId);
  205. cmd.Parameters.Add("@cDepCode", info.DepCode);
  206. }
  207. cmd.CommandText = sql;
  208. try
  209. {
  210. int count = cmd.ExecuteNonQuery();
  211. if (count <= 0)
  212. {
  213. log.Error("采购订单表头<=0;");
  214. throw new Exception("采购订单表头败,受影响行数<=0;");
  215. }
  216. }
  217. catch (Exception ex)
  218. {
  219. log.Error("采购订单表头失败" + sql, ex);
  220. throw new Exception("采购订单表头失败" + sql, ex);
  221. }
  222. #endregion
  223. #region 采购订单表体
  224. foreach (var detail in info.details)
  225. {
  226. if (string.IsNullOrWhiteSpace(info.PRCode))
  227. {
  228. sql = @"
  229. Insert Into PO_Podetails
  230. (ID,cInvCode ,iQuantity ,iNum ,iUnitPrice ,iMoney ,iTax ,iSum ,
  231. iNatUnitPrice ,iNatMoney ,iNatTax ,iNatSum ,dArriveDate ,
  232. iReceivedQTY ,iReceivedNum ,iReceivedMoney ,iPerTaxRate ,
  233. bGsp ,POID ,iTaxPrice ,bTaxCost ,cSource ,SoType ,cupsocode ,
  234. iordertype ,ivouchrowno ,bgift)
  235. VALUES
  236. ('',@cInvCode,@iQuantity,@iNum,@iUnitPrice,'','','',
  237. 0,0,'','',@dArriveDate,
  238. '','',0,'',
  239. 0,@ID,'',0,'','','' NULL,@ivouchrowno,'')" + Environment.NewLine;
  240. cmd.Parameters.Clear();
  241. cmd.Parameters.Add("@ID", iFatherId);
  242. cmd.Parameters.Add("@cInvCode", detail.InvCode);
  243. cmd.Parameters.Add("@Code", info.PRCode);
  244. cmd.Parameters.Add("@ivouchrowno", detail.Sequence);
  245. cmd.Parameters.Add("@iQuantity", detail.Quantity);
  246. cmd.Parameters.Add("@iNum", detail.Amount);
  247. cmd.Parameters.Add("@iUnitPrice", detail.UnitPrice);
  248. cmd.Parameters.Add("@dArriveDate", detail.ArriveDate);
  249. }
  250. sql = @"INSERT INTO PO_Podetails
  251. (ID,cInvCode ,iQuantity ,iNum ,iUnitPrice ,iMoney ,iTax ,iSum ,
  252. iNatUnitPrice ,iNatMoney ,iNatTax ,iNatSum ,dArriveDate ,
  253. iReceivedQTY ,iReceivedNum ,iReceivedMoney ,iPerTaxRate ,
  254. bGsp ,POID ,iTaxPrice ,bTaxCost ,cSource ,SoType ,cupsocode ,
  255. iordertype ,ivouchrowno ,bgift)
  256. SELECT AutoID ,@cInvCode ,@iQuantity ,@iNum ,@iUnitPrice ,iMoney ,iOriTaxPrice,iOriSum ,
  257. 0 ,0 ,iTaxPrice ,'' ,@dArriveDate ,
  258. iReceivedQTY ,iReceivedNum ,0 ,iPerTaxRate ,
  259. 0 ,@ID ,iOriTaxCost ,0 ,cSource ,SoType ,@Code ,
  260. iordertype ,@ivouchrowno ,''
  261. FROM PU_AppVouchs WHERE AutoID='" + detail.PRDetailID + "'";
  262. cmd.Parameters.Clear();
  263. cmd.Parameters.Add("@ID", iFatherId);
  264. cmd.Parameters.Add("@cInvCode", detail.InvCode);
  265. cmd.Parameters.Add("@Code", info.PRCode);
  266. cmd.Parameters.Add("@ivouchrowno", detail.Sequence);
  267. cmd.Parameters.Add("@iQuantity", detail.Quantity);
  268. cmd.Parameters.Add("@iNum", detail.Amount);
  269. cmd.Parameters.Add("@iUnitPrice", detail.UnitPrice);
  270. cmd.Parameters.Add("@dArriveDate", detail.ArriveDate);
  271. cmd.CommandText = sql;
  272. try
  273. {
  274. int count = cmd.ExecuteNonQuery();
  275. if (count <= 0)
  276. {
  277. log.Error("采购订单表体<=0;");
  278. throw new Exception("采购订单表体败,受影响行数<=0;");
  279. }
  280. }
  281. catch (Exception ex)
  282. {
  283. log.Error("采购订单表体失败" + sql, ex);
  284. throw new Exception("采购订单表体失败" + sql, ex);
  285. }
  286. }
  287. #endregion
  288. #region 查询
  289. cmd.Transaction.Commit();
  290. sql = @"select a.POID,a.cPOID,a.cVenCode,c.cVenName,
  291. a.cDepCode,d.cDepName,a.cappcode,a.cMaker,
  292. a.cmaketime ,a.cVerifier,a.cAuditDate,b.ID,
  293. b.ivouchrowno ,b.cInvCode , b.iQuantity, b.iNum,
  294. b.fPoArrQuantity, b.dArriveDate ,'' ,b.iTaxPrice ,
  295. b.iAppIds from po_pomain a
  296. left join PO_Podetails b on a.POID=b.POID
  297. left join Vendor c on a.cVenCode=c.cVenCode
  298. left join Department d on a.cDepCode=d.cDepCode where a.poid='{0}' ";
  299. sql = string.Format(sql, iFatherId);
  300. dt = DBHelper.SQlReturnData(sql, cmd);
  301. #endregion
  302. if (dtNew == null)
  303. dtNew = dt;
  304. else
  305. dtNew.Merge(dt);
  306. cmd.CommandType = CommandType.Text;
  307. cmd.Parameters.Clear();
  308. }
  309. cmd.Transaction.Commit();
  310. }
  311. catch (Exception ex)
  312. {
  313. if (cmd.Transaction != null)
  314. cmd.Transaction.Rollback();
  315. log.Error(ex.Message);
  316. throw new Exception(ex.Message);
  317. }
  318. finally
  319. {
  320. if (conn.State == ConnectionState.Open)
  321. {
  322. conn.Close();
  323. }
  324. conn.Dispose();
  325. }
  326. }
  327. szJson = JsonConvert.SerializeObject(dtNew);
  328. return szJson;
  329. }
  330. /// <summary>
  331. /// 审核采购订单
  332. /// </summary>
  333. /// <param name="infos"></param>
  334. /// <returns></returns>
  335. public string Approve(List<ICSPurchaseOrder> infos)
  336. {
  337. List<ICSPurchaseOrder> szJson = new List<ICSPurchaseOrder>();
  338. string connS = "";
  339. if (infos.Count <= 0)
  340. {
  341. throw new Exception("传送数据为空!");
  342. }
  343. string res = string.Empty;
  344. SqlConnection conn = new SqlConnection();
  345. SqlCommand cmd = new SqlCommand();
  346. string sql = string.Empty;
  347. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  348. foreach (string WorkPoint in result)
  349. {
  350. try
  351. {
  352. connS = string.Format(connString, WorkPoint);
  353. conn = new System.Data.SqlClient.SqlConnection(connS);
  354. conn.Open();
  355. SqlTransaction sqlTran = conn.BeginTransaction();
  356. cmd = new SqlCommand();
  357. cmd.Transaction = sqlTran;
  358. cmd.Connection = conn;
  359. foreach (ICSPurchaseOrder info in infos)
  360. {
  361. if (WorkPoint != info.WorkPoint)
  362. continue;
  363. ICSUserInfo userInfo = new ICSUserInfo();
  364. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  365. if (info.MTime < new DateTime(2000, 01, 01))
  366. throw new Exception("请输入正确的操作时间:" + info.MTime);
  367. sql = @"UPDATE PO_POMAIN SET cVerifier ='" + userInfo.UserName + @"' ,
  368. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE POID='" + info.ID + "'";
  369. sql = string.Format(sql, ERPDB);
  370. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  371. cmd.CommandType = CommandType.Text;
  372. cmd.Parameters.Clear();
  373. }
  374. cmd.Transaction.Commit();
  375. }
  376. catch (Exception ex)
  377. {
  378. if (cmd.Transaction != null)
  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. /// <summary>
  395. /// 删除采购订单
  396. /// </summary>
  397. /// <param name="infos"></param>
  398. /// <returns></returns>
  399. public string Delete(List<ICSPurchaseOrder> infos)
  400. {
  401. List<ICSPurchaseOrder> szJson = new List<ICSPurchaseOrder>();
  402. if (infos.Count <= 0)
  403. {
  404. throw new Exception("传送数据为空!");
  405. }
  406. string res = string.Empty;
  407. string connS = "";
  408. SqlConnection conn = new SqlConnection();
  409. SqlCommand cmd = new SqlCommand();
  410. string sql = string.Empty;
  411. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  412. foreach (string WorkPoint in result)
  413. {
  414. try
  415. {
  416. connS = string.Format(connString, WorkPoint);
  417. conn = new System.Data.SqlClient.SqlConnection(connS);
  418. conn.Open();
  419. SqlTransaction sqlTran = conn.BeginTransaction();
  420. cmd = new SqlCommand();
  421. cmd.Transaction = sqlTran;
  422. cmd.Connection = conn;
  423. foreach (ICSPurchaseOrder info in infos)
  424. {
  425. if (WorkPoint != info.WorkPoint)
  426. continue;
  427. if (info.MTime < new DateTime(2000, 01, 01))
  428. throw new Exception("请输入正确的操作时间:" + info.MTime);
  429. sql = @" DELETE PO_POMAIN WHERE POID='{0}'";
  430. sql = string.Format(sql, info.ID);
  431. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  432. cmd.CommandType = CommandType.Text;
  433. cmd.Parameters.Clear();
  434. }
  435. cmd.Transaction.Commit();
  436. }
  437. catch (Exception ex)
  438. {
  439. if (cmd.Transaction != null)
  440. cmd.Transaction.Rollback();
  441. log.Error(ex.Message);
  442. throw new Exception(ex.Message);
  443. }
  444. finally
  445. {
  446. if (conn.State == ConnectionState.Open)
  447. {
  448. conn.Close();
  449. }
  450. conn.Dispose();
  451. }
  452. }
  453. return res;
  454. }
  455. }
  456. }