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

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