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

319 lines
14 KiB

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. 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 List<ICSPOMain> 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 szJson;
  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. sql += @"Insert Into PU_AppVouchs
  148. (id,autoid,cvencode,cinvcode,fquantity,
  149. funitprice,ipertaxrate,ftaxprice,fmoney,drequirdate,
  150. darrivedate,iReceivedQTY,cdefine22,cdefine23,btaxcost,
  151. iReceivedNum,cpersoncodeexec,cdepcodeexec,cexch_name,iexchrate,
  152. ioricost,ioritaxcost,iorimoney,ioritaxprice,iorisum,
  153. imoney,itaxprice,ivouchrowno)
  154. VALUES
  155. ('@ID', '@DID',NULL,'@cInvCode','@fQuantity',
  156. NULL,17,NULL,NULL,'@drequirdate',
  157. '@darrivedate','0',NULL,NULL,1,
  158. 0,NULL,NULL,N'',1,
  159. NULL,NULL,NULL,NULL,NULL,
  160. NULL,NULL,'@ivouchrowno')" + Environment.NewLine;
  161. //sql = sql.Replace("@cInvCode", cInvCode);
  162. //sql = sql.Replace("@fQuantity", fQuantity);
  163. //sql = sql.Replace("@drequirdate", dRequirDate);
  164. //sql = sql.Replace("@darrivedate", dRequirDate);
  165. //sql = sql.Replace("@ivouchrowno", SortSeq);
  166. cmd.Parameters.Clear();
  167. cmd.Parameters.Add("@cInvCode", detail.InvCode);
  168. cmd.Parameters.Add("@fQuantity", detail.Amount);
  169. //sql = sql.Replace("@cpersoncode", cMaker);
  170. cmd.Parameters.Add("@darrivedate", detail.ArriveDate);
  171. //sql = sql.Replace("@cverifier", cCurrentAuditor);
  172. cmd.Parameters.Add("@drequirdate", info.MTime);
  173. cmd.Parameters.Add("@ivouchrowno", detail.Sequence);
  174. try
  175. {
  176. cmd.ExecuteNonQuery();
  177. }
  178. catch (Exception ex)
  179. {
  180. log.Error("表体失败!");
  181. throw new Exception("程序异常,请联系开发人员!");
  182. }
  183. //sql = sql.Replace("@cDefine2", SourceCode);
  184. }
  185. // #endregion
  186. // sqls.Add(cCompanyCode + "~" + PRCode, sql);
  187. // if (i == parent.Rows.Count - 1)//循环到最后保存到公司
  188. // {
  189. // Dictionary<string, string> result = ICSHelper.InsertDate(dictionary[cCompanyCode], sqls);
  190. // foreach (var res in result)
  191. // {
  192. // errors.Add(res.Key, res.Value);
  193. // }
  194. // }
  195. // }
  196. cmd.Transaction.Commit();
  197. }
  198. return szJson;
  199. }
  200. catch (Exception ex)
  201. {
  202. cmd.Transaction.Rollback();
  203. log.Error(ex.Message);
  204. throw new Exception(ex.Message);
  205. }
  206. finally
  207. {
  208. if (conn.State == ConnectionState.Open)
  209. {
  210. conn.Close();
  211. }
  212. conn.Dispose();
  213. }
  214. }
  215. /// <summary>
  216. /// 审核采购订单
  217. /// </summary>
  218. /// <param name="infos"></param>
  219. /// <returns></returns>
  220. public string Approve(List<AppVouch> infos)
  221. {
  222. List<AppVouch> szJson = new List<AppVouch>();
  223. DataTable dt = null;
  224. if (infos.Count <= 0)
  225. {
  226. throw new Exception("传送数据为空!");
  227. }
  228. string res = string.Empty;
  229. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  230. conn.Open();
  231. SqlTransaction sqlTran = conn.BeginTransaction();
  232. SqlCommand cmd = new SqlCommand();
  233. cmd.Transaction = sqlTran;
  234. cmd.Connection = conn;
  235. try
  236. {
  237. string sql = string.Empty;
  238. foreach (AppVouch info in infos)
  239. {
  240. if (info.MTime < new DateTime(2000, 01, 01))
  241. throw new Exception("请输入正确的操作时间:" + info.MTime);
  242. sql = @"UPDATE [{0}].dbo.PO_POMAIN SET cVerifier ='" + info.User + @"' ,
  243. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE POID='" + info.ID + "'";
  244. sql = string.Format(sql, ERPDB);
  245. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  246. }
  247. cmd.Transaction.Commit();
  248. return res;
  249. }
  250. catch (Exception ex)
  251. {
  252. cmd.Transaction.Rollback();
  253. log.Error(ex.Message);
  254. throw new Exception(ex.Message);
  255. }
  256. finally
  257. {
  258. if (conn.State == ConnectionState.Open)
  259. {
  260. conn.Close();
  261. }
  262. conn.Dispose();
  263. }
  264. }
  265. /// <summary>
  266. /// 删除采购订单
  267. /// </summary>
  268. /// <param name="infos"></param>
  269. /// <returns></returns>
  270. public string Delete(List<AppVouch> infos)
  271. {
  272. List<AppVouch> szJson = new List<AppVouch>();
  273. if (infos.Count <= 0)
  274. {
  275. throw new Exception("传送数据为空!");
  276. }
  277. string res = string.Empty;
  278. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  279. conn.Open();
  280. SqlTransaction sqlTran = conn.BeginTransaction();
  281. SqlCommand cmd = new SqlCommand();
  282. cmd.Transaction = sqlTran;
  283. cmd.Connection = conn;
  284. try
  285. {
  286. string sql = string.Empty;
  287. foreach (AppVouch info in infos)
  288. {
  289. if (info.MTime < new DateTime(2000, 01, 01))
  290. throw new Exception("请输入正确的操作时间:" + info.MTime);
  291. sql = @" DELETE [{1}].dbo.PO_POMAIN WHERE POID='{0}'";
  292. sql = string.Format(sql, info.ID, ERPDB);
  293. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  294. }
  295. cmd.Transaction.Commit();
  296. return res;
  297. ;
  298. }
  299. catch (Exception ex)
  300. {
  301. cmd.Transaction.Rollback();
  302. log.Error(ex.Message);
  303. throw new Exception(ex.Message);
  304. }
  305. finally
  306. {
  307. if (conn.State == ConnectionState.Open)
  308. {
  309. conn.Close();
  310. }
  311. conn.Dispose();
  312. }
  313. }
  314. }
  315. }