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

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