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

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