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

342 lines
16 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. public class CreatePoMain
  14. {
  15. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  16. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
  17. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  18. /// <summary>
  19. /// 查找采购订单
  20. /// </summary>
  21. /// <param name="infos"></param>
  22. /// <returns></returns>
  23. public string GET(List<ICSPOMain> infos)
  24. {
  25. List<ICSPOMain> szJson = new List<ICSPOMain>();
  26. DataTable dt = null;
  27. string json = "";
  28. if (infos.Count <= 0)
  29. {
  30. throw new Exception("传送数据为空!");
  31. }
  32. string res = string.Empty;
  33. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  34. conn.Open();
  35. SqlTransaction sqlTran = conn.BeginTransaction();
  36. SqlCommand cmd = new SqlCommand();
  37. cmd.Transaction = sqlTran;
  38. cmd.Connection = conn;
  39. try
  40. {
  41. string sql = string.Empty;
  42. foreach (ICSPOMain info in infos)
  43. {
  44. if (info.MTime < new DateTime(2000, 01, 01))
  45. throw new Exception("请输入正确的操作时间:" + info.MTime);
  46. sql = @" SELECT A.CPOID,A.CVENCODE,A.CDEPCODE,A.CAPPCODE,C.CDEPNAME,D.CVENNAME,A.CMAKER,A.CMAKETIME,A.CVERIFIER,A.CAUDITTIME,
  47. B.IROWNO,B.CINVCODE ,B.IQUANTITY ,B.INUM,B.IRECEIVEDQTY,B.DARRIVEDATE,A.CEXCH_NAME,B.ITAXPRICE,B.INATUNITPRICE,B.IUNITPRICE
  48. FROM [{1}].DBO.PO_POMAIN A
  49. INNER JOIN [{1}].DBO.PO_PODETAILS B ON A.POID=B.POID
  50. INNER JOIN [{1}].DBO.DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  51. INNER JOIN [{1}].DBO.VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1";
  52. if (!string.IsNullOrWhiteSpace(info.POCode))
  53. {
  54. sql += " and a.CPOID='{0}'";
  55. }
  56. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  57. {
  58. sql += " and ISNULL(b.cbCloseTime,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime))))>='{2}'";
  59. }
  60. if(!string.IsNullOrWhiteSpace(info.User)){
  61. sql+="and a.CMAKER='{3}'";
  62. }
  63. sql = string.Format(sql, info.POCode, ERPDB,info.MTime,info.User);
  64. dt = DBHelper.SQlReturnData(sql, cmd);
  65. json = JsonConvert.SerializeObject(dt);
  66. }
  67. cmd.Transaction.Commit();
  68. return json;
  69. }
  70. catch (Exception ex)
  71. {
  72. cmd.Transaction.Rollback();
  73. log.Error(ex.Message);
  74. throw new Exception(ex.Message);
  75. }
  76. finally
  77. {
  78. if (conn.State == ConnectionState.Open)
  79. {
  80. conn.Close();
  81. }
  82. conn.Dispose();
  83. }
  84. }
  85. /// <summary>
  86. /// 创建请购单
  87. /// </summary>
  88. /// <param name="infos"></param>
  89. /// <returns></returns>
  90. public List<AppVouch> Create(List<AppVouch> infos)
  91. {
  92. List<AppVouch> szJson = new List<AppVouch>();
  93. DataTable dt = null;
  94. string sql = "";
  95. if (infos.Count <= 0)
  96. {
  97. throw new Exception("传送数据为空!");
  98. }
  99. string res = string.Empty;
  100. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  101. conn.Open();
  102. SqlTransaction sqlTran = conn.BeginTransaction();
  103. SqlCommand cmd = new SqlCommand();
  104. cmd.Transaction = sqlTran;
  105. cmd.Connection = conn;
  106. try
  107. {
  108. foreach (AppVouch info in infos)
  109. {
  110. string sqlID = @"SELECT * FROM UFSystem..UA_Identity where cVouchType='PU_AppVouch'";
  111. dt = DBHelper.SQlReturnData(sqlID, null);
  112. string ID = dt.Rows[0]["iFatherId"].ToString();
  113. string DID = dt.Rows[0]["iChildId"].ToString();
  114. sql += @"Insert Into [{1}].DBO.PO_POMAIN
  115. (ivtid,id,ccode,ddate,cdepcode,
  116. cpersoncode,cptcode,cbustype,cmaker,cverifier,
  117. iverifystateex,ireturncount,iswfcontrolled,cAuditDate,iPrintCount,
  118. cMakeTime,cAuditTime,cDefine10,cDefine1,cDefine2
  119. )
  120. Values
  121. ('8171','@ID',Substring(Convert( varchar(100),GetDate(),112),1,6)+REPLICATE('0',4-len(@Num" + ID + @"))+CAST(@Num" + ID + @" AS nvarchar(10)),CONVERT(VARCHAR(10),GETDATE(),23),'@cdepcode',
  122. null,'99','','@cmaker','@cverifier',
  123. '2','0','0','@cAuditDate','0',
  124. GETDATE(),GETDATE(),'job','@cDefine1','@cDefine2'
  125. )" + Environment.NewLine;
  126. cmd.Parameters.Clear();
  127. cmd.Parameters.Add("@ID", ID);
  128. cmd.Parameters.Add("@cdepcode", info.DepCode);
  129. //sql = sql.Replace("@cpersoncode", cMaker);
  130. cmd.Parameters.Add("@cmaker", info.User);
  131. //sql = sql.Replace("@cverifier", cCurrentAuditor);
  132. cmd.Parameters.Add("@cAuditDate", info.MTime.ToString());
  133. cmd.Parameters.Add("@cDefine1", info.PRCode);
  134. //sql = sql.Replace("@cDefine2", SourceCode);
  135. cmd.CommandText = sql;
  136. try
  137. {
  138. cmd.ExecuteNonQuery();
  139. }
  140. catch (Exception ex)
  141. {
  142. log.Error("表头失败!");
  143. throw new Exception("程序异常,请联系开发人员!");
  144. }
  145. foreach (var detail in info.details)
  146. {
  147. // string SortSeq = ch["SortSeq"].ToString();
  148. // string cInvCode = ch["cInvCode"].ToString();
  149. // string cInvName = ch["cInvName"].ToString();
  150. // string cInvStd = ch["cInvStd"].ToString();
  151. // string cComUnitName = ch["cComUnitName"].ToString();
  152. // string fQuantity = ch["fQuantity"].ToString();
  153. // string dRequirDate = ch["dRequirDate"].ToString();
  154. // string cVenName = ch["cVenName"].ToString();
  155. // #region 判断存货编码是否存在
  156. // sql += @"IF NOT EXISTS(
  157. // SELECT cInvCode
  158. // FROM Inventory
  159. // WHERE cInvCode='@cInvCode'
  160. // )
  161. // BEGIN
  162. // RAISERROR('存货编码:@cInvCode 不存在!',16,0)
  163. // END" + Environment.NewLine;
  164. // #endregion
  165. // #region PU_AppVouchs
  166. // #region 获取ID
  167. // string appskey = cCompanyCode + i + SortSeq;
  168. // sql += ICSHelper.GetIDSql(cCompanyCode, "PuApp", appskey, GetID.CHILD) + Environment.NewLine;
  169. // #endregion
  170. sql += @"Insert Into PU_AppVouchs
  171. (id,autoid,cvencode,cinvcode,fquantity,
  172. funitprice,ipertaxrate,ftaxprice,fmoney,drequirdate,
  173. darrivedate,iReceivedQTY,cdefine22,cdefine23,btaxcost,
  174. iReceivedNum,cpersoncodeexec,cdepcodeexec,cexch_name,iexchrate,
  175. ioricost,ioritaxcost,iorimoney,ioritaxprice,iorisum,
  176. imoney,itaxprice,ivouchrowno)
  177. VALUES
  178. ('@ID', '@DID',NULL,'@cInvCode','@fQuantity',
  179. NULL,17,NULL,NULL,'@drequirdate',
  180. '@darrivedate','0',NULL,NULL,1,
  181. 0,NULL,NULL,N'',1,
  182. NULL,NULL,NULL,NULL,NULL,
  183. NULL,NULL,'@ivouchrowno')" + Environment.NewLine;
  184. //sql = sql.Replace("@cInvCode", cInvCode);
  185. //sql = sql.Replace("@fQuantity", fQuantity);
  186. //sql = sql.Replace("@drequirdate", dRequirDate);
  187. //sql = sql.Replace("@darrivedate", dRequirDate);
  188. //sql = sql.Replace("@ivouchrowno", SortSeq);
  189. cmd.Parameters.Clear();
  190. cmd.Parameters.Add("@cInvCode", detail.InvCode);
  191. cmd.Parameters.Add("@fQuantity", detail.Amount);
  192. //sql = sql.Replace("@cpersoncode", cMaker);
  193. cmd.Parameters.Add("@darrivedate", detail.ArriveDate);
  194. //sql = sql.Replace("@cverifier", cCurrentAuditor);
  195. cmd.Parameters.Add("@drequirdate", info.MTime);
  196. cmd.Parameters.Add("@ivouchrowno", detail.Sequence);
  197. try
  198. {
  199. cmd.ExecuteNonQuery();
  200. }
  201. catch (Exception ex)
  202. {
  203. log.Error("表体失败!");
  204. throw new Exception("程序异常,请联系开发人员!");
  205. }
  206. //sql = sql.Replace("@cDefine2", SourceCode);
  207. }
  208. // #endregion
  209. // sqls.Add(cCompanyCode + "~" + PRCode, sql);
  210. // if (i == parent.Rows.Count - 1)//循环到最后保存到公司
  211. // {
  212. // Dictionary<string, string> result = ICSHelper.InsertDate(dictionary[cCompanyCode], sqls);
  213. // foreach (var res in result)
  214. // {
  215. // errors.Add(res.Key, res.Value);
  216. // }
  217. // }
  218. // }
  219. cmd.Transaction.Commit();
  220. }
  221. return szJson;
  222. }
  223. catch (Exception ex)
  224. {
  225. cmd.Transaction.Rollback();
  226. log.Error(ex.Message);
  227. throw new Exception(ex.Message);
  228. }
  229. finally
  230. {
  231. if (conn.State == ConnectionState.Open)
  232. {
  233. conn.Close();
  234. }
  235. conn.Dispose();
  236. }
  237. }
  238. /// <summary>
  239. /// 审核采购订单
  240. /// </summary>
  241. /// <param name="infos"></param>
  242. /// <returns></returns>
  243. public string Approve(List<AppVouch> infos)
  244. {
  245. List<AppVouch> szJson = new List<AppVouch>();
  246. DataTable dt = null;
  247. if (infos.Count <= 0)
  248. {
  249. throw new Exception("传送数据为空!");
  250. }
  251. string res = string.Empty;
  252. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  253. conn.Open();
  254. SqlTransaction sqlTran = conn.BeginTransaction();
  255. SqlCommand cmd = new SqlCommand();
  256. cmd.Transaction = sqlTran;
  257. cmd.Connection = conn;
  258. try
  259. {
  260. string sql = string.Empty;
  261. foreach (AppVouch info in infos)
  262. {
  263. if (info.MTime < new DateTime(2000, 01, 01))
  264. throw new Exception("请输入正确的操作时间:" + info.MTime);
  265. sql = @"UPDATE [{0}].dbo.PO_POMAIN SET cVerifier ='" + info.User + @"' ,
  266. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE POID='" + info.ID + "'";
  267. sql = string.Format(sql, ERPDB);
  268. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  269. }
  270. cmd.Transaction.Commit();
  271. return res;
  272. }
  273. catch (Exception ex)
  274. {
  275. cmd.Transaction.Rollback();
  276. log.Error(ex.Message);
  277. throw new Exception(ex.Message);
  278. }
  279. finally
  280. {
  281. if (conn.State == ConnectionState.Open)
  282. {
  283. conn.Close();
  284. }
  285. conn.Dispose();
  286. }
  287. }
  288. /// <summary>
  289. /// 删除采购订单
  290. /// </summary>
  291. /// <param name="infos"></param>
  292. /// <returns></returns>
  293. public string Delete(List<AppVouch> infos)
  294. {
  295. List<AppVouch> szJson = new List<AppVouch>();
  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 (AppVouch info in infos)
  311. {
  312. if (info.MTime < new DateTime(2000, 01, 01))
  313. throw new Exception("请输入正确的操作时间:" + info.MTime);
  314. sql = @" DELETE [{1}].dbo.PO_POMAIN WHERE POID='{0}'";
  315. sql = string.Format(sql, info.ID, ERPDB);
  316. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  317. }
  318. cmd.Transaction.Commit();
  319. return res;
  320. ;
  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. }
  338. }