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

483 lines
22 KiB

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