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

479 lines
22 KiB

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