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

425 lines
20 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 CreateAppVouch
  14. {
  15. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  16. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  17. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  18. public string GET(List<AppVouch> infos)
  19. {
  20. List<AppVouch> szJson = new List<AppVouch>();
  21. DataTable dt = null;
  22. DataTable dtNew = null;
  23. string connS = "";
  24. string json = "";
  25. if (infos == null)
  26. {
  27. throw new Exception("传送数据为空!");
  28. }
  29. string res = string.Empty;
  30. SqlConnection conn = new SqlConnection();
  31. SqlCommand cmd = new SqlCommand();
  32. string sql = string.Empty;
  33. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  34. foreach (string WorkPoint in result)
  35. {
  36. try
  37. {
  38. connS = string.Format(connString, WorkPoint);
  39. conn = new System.Data.SqlClient.SqlConnection(connS);
  40. conn.Open();
  41. SqlTransaction sqlTran = conn.BeginTransaction();
  42. cmd = new SqlCommand();
  43. cmd.Transaction = sqlTran;
  44. cmd.Connection = conn;
  45. foreach (AppVouch info in infos)
  46. {
  47. if (WorkPoint != info.WorkPoint)
  48. {
  49. continue;
  50. }
  51. ICSUserInfo userInfo = new ICSUserInfo();
  52. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  53. if (info.MTime < new DateTime(2000, 01, 01))
  54. throw new Exception("请输入正确的操作时间:" + info.MTime);
  55. sql = @"select a.cCode,a.cDepCode,a.cMaker,a.cMakeTime,a.cVerifier,cAuditTime,b.irowno,b.cUnitID,b.fQuantity,b.cInvCode,b.dRequirDate,b.dArriveDate,b.cexch_name,b.iOriCost
  56. from dbo.PU_AppVouch a left join dbo.PU_AppVouchs b on a.ID=b.ID where 1=1";
  57. if (!string.IsNullOrWhiteSpace(info.PRCode))
  58. {
  59. sql += " and a.cCode='{0}'";
  60. }
  61. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  62. {
  63. sql += " and ISNULL(b.cbCloseTime,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime))))>='{2}'";
  64. }
  65. if (!string.IsNullOrWhiteSpace(info.User))
  66. {
  67. sql += "and a.CMAKER='{3}'";
  68. }
  69. sql = string.Format(sql, info.PRCode, ERPDB, userInfo.UserName);
  70. dt = DBHelper.SQlReturnData(sql, cmd);
  71. if (dtNew == null)
  72. dtNew = dt;
  73. else
  74. dtNew.Merge(dt);
  75. //List<AppVouch> szJson = JsonConvert.DeserializeObject<List<AppVouch>>(json);
  76. //szJson = JsonConvert.DeserializeObject<List<AppVouch>>(json);
  77. }
  78. cmd.Transaction.Commit();
  79. }
  80. catch (Exception ex)
  81. {
  82. cmd.Transaction.Rollback();
  83. log.Error(ex.Message);
  84. throw new Exception(ex.Message);
  85. }
  86. finally
  87. {
  88. if (conn.State == ConnectionState.Open)
  89. {
  90. conn.Close();
  91. }
  92. conn.Dispose();
  93. }
  94. }
  95. json = JsonConvert.SerializeObject(dtNew);
  96. return json;
  97. }
  98. /// <summary>
  99. /// 创建请购单
  100. /// </summary>
  101. /// <param name="infos"></param>
  102. /// <returns></returns>
  103. public List<AppVouch> Create(List<AppVouch> infos)
  104. {
  105. List<AppVouch> szJson = new List<AppVouch>();
  106. DataTable dt = null;
  107. string connS = "";
  108. string sql = "";
  109. if (infos.Count <= 0)
  110. {
  111. throw new Exception("传送数据为空!");
  112. }
  113. string res = string.Empty;
  114. SqlConnection conn = new SqlConnection();
  115. SqlCommand cmd = new SqlCommand();
  116. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  117. foreach (string WorkPoint in result)
  118. {
  119. try
  120. {
  121. connS = string.Format(connString, WorkPoint);
  122. conn = new System.Data.SqlClient.SqlConnection(connS);
  123. conn.Open();
  124. SqlTransaction sqlTran = conn.BeginTransaction();
  125. cmd = new SqlCommand();
  126. cmd.Transaction = sqlTran;
  127. cmd.Connection = conn;
  128. foreach (AppVouch info in infos)
  129. {
  130. if (WorkPoint != info.WorkPoint)
  131. {
  132. continue;
  133. }
  134. ICSUserInfo userInfo = new ICSUserInfo();
  135. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  136. string[] ss = info.WorkPoint.Split('_');
  137. ERPDB = ss[1];
  138. string sqlID = @"SELECT * FROM UFSystem..UA_Identity where cVouchType='PU_AppVouch' and cacc_id='" + ERPDB + @"'";
  139. dt = DBHelper.SQlReturnData(sqlID, null);
  140. string ID = dt.Rows[0]["iFatherId"].ToString();
  141. string DID = dt.Rows[0]["iChildId"].ToString();
  142. sql += @"Insert Into PU_AppVouch
  143. (ivtid,id,ccode,ddate,cdepcode,
  144. cpersoncode,cptcode,cbustype,cmaker,cverifier,
  145. iverifystateex,ireturncount,iswfcontrolled,cAuditDate,iPrintCount,
  146. cMakeTime,cAuditTime,cDefine10,cDefine1,cDefine2
  147. )
  148. Values
  149. ('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',
  150. null,'99','','@cmaker','@cverifier',
  151. '2','0','0','@cAuditDate','0',
  152. GETDATE(),GETDATE(),'job','@cDefine1','@cDefine2'
  153. )" + Environment.NewLine;
  154. cmd.Parameters.Clear();
  155. cmd.Parameters.Add("@ID", ID);
  156. cmd.Parameters.Add("@cdepcode", info.DepCode);
  157. //sql = sql.Replace("@cpersoncode", cMaker);
  158. cmd.Parameters.Add("@cmaker", userInfo.UserName);
  159. //sql = sql.Replace("@cverifier", cCurrentAuditor);
  160. cmd.Parameters.Add("@cAuditDate", info.MTime.ToString());
  161. cmd.Parameters.Add("@cDefine1", info.PRCode);
  162. //sql = sql.Replace("@cDefine2", SourceCode);
  163. cmd.CommandText = sql;
  164. try
  165. {
  166. cmd.ExecuteNonQuery();
  167. }
  168. catch (Exception ex)
  169. {
  170. log.Error("表头失败!");
  171. throw new Exception("程序异常,请联系开发人员!");
  172. }
  173. foreach (var detail in info.details)
  174. {
  175. // string SortSeq = ch["SortSeq"].ToString();
  176. // string cInvCode = ch["cInvCode"].ToString();
  177. // string cInvName = ch["cInvName"].ToString();
  178. // string cInvStd = ch["cInvStd"].ToString();
  179. // string cComUnitName = ch["cComUnitName"].ToString();
  180. // string fQuantity = ch["fQuantity"].ToString();
  181. // string dRequirDate = ch["dRequirDate"].ToString();
  182. // string cVenName = ch["cVenName"].ToString();
  183. // #region 判断存货编码是否存在
  184. // sql += @"IF NOT EXISTS(
  185. // SELECT cInvCode
  186. // FROM Inventory
  187. // WHERE cInvCode='@cInvCode'
  188. // )
  189. // BEGIN
  190. // RAISERROR('存货编码:@cInvCode 不存在!',16,0)
  191. // END" + Environment.NewLine;
  192. // #endregion
  193. // #region PU_AppVouchs
  194. // #region 获取ID
  195. // string appskey = cCompanyCode + i + SortSeq;
  196. // sql += ICSHelper.GetIDSql(cCompanyCode, "PuApp", appskey, GetID.CHILD) + Environment.NewLine;
  197. // #endregion
  198. sql += @"Insert Into PU_AppVouchs
  199. (id,autoid,cvencode,cinvcode,fquantity,
  200. funitprice,ipertaxrate,ftaxprice,fmoney,drequirdate,
  201. darrivedate,iReceivedQTY,cdefine22,cdefine23,btaxcost,
  202. iReceivedNum,cpersoncodeexec,cdepcodeexec,cexch_name,iexchrate,
  203. ioricost,ioritaxcost,iorimoney,ioritaxprice,iorisum,
  204. imoney,itaxprice,ivouchrowno)
  205. VALUES
  206. ('@ID', '@DID',NULL,'@cInvCode','@fQuantity',
  207. NULL,17,NULL,NULL,'@drequirdate',
  208. '@darrivedate','0',NULL,NULL,1,
  209. 0,NULL,NULL,N'',1,
  210. NULL,NULL,NULL,NULL,NULL,
  211. NULL,NULL,'@ivouchrowno')" + Environment.NewLine;
  212. //sql = sql.Replace("@cInvCode", cInvCode);
  213. //sql = sql.Replace("@fQuantity", fQuantity);
  214. //sql = sql.Replace("@drequirdate", dRequirDate);
  215. //sql = sql.Replace("@darrivedate", dRequirDate);
  216. //sql = sql.Replace("@ivouchrowno", SortSeq);
  217. cmd.Parameters.Clear();
  218. cmd.Parameters.Add("@cInvCode", detail.InvCode);
  219. cmd.Parameters.Add("@fQuantity", detail.Quantity);
  220. //sql = sql.Replace("@cpersoncode", cMaker);
  221. cmd.Parameters.Add("@darrivedate", detail.ArriveDate);
  222. //sql = sql.Replace("@cverifier", cCurrentAuditor);
  223. cmd.Parameters.Add("@drequirdate", info.MTime);
  224. cmd.Parameters.Add("@ivouchrowno", detail.Sequence);
  225. try
  226. {
  227. cmd.ExecuteNonQuery();
  228. }
  229. catch (Exception ex)
  230. {
  231. log.Error("表体失败!");
  232. throw new Exception("程序异常,请联系开发人员!");
  233. }
  234. //sql = sql.Replace("@cDefine2", SourceCode);
  235. }
  236. // #endregion
  237. // sqls.Add(cCompanyCode + "~" + PRCode, sql);
  238. // if (i == parent.Rows.Count - 1)//循环到最后保存到公司
  239. // {
  240. // Dictionary<string, string> result = ICSHelper.InsertDate(dictionary[cCompanyCode], sqls);
  241. // foreach (var res in result)
  242. // {
  243. // errors.Add(res.Key, res.Value);
  244. // }
  245. // }
  246. // }
  247. }
  248. cmd.Transaction.Commit();
  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. return szJson;
  266. }
  267. /// <summary>
  268. /// 审核请购单
  269. /// </summary>
  270. /// <param name="infos"></param>
  271. /// <returns></returns>
  272. public string Approve(List<AppVouch> infos)
  273. {
  274. List<AppVouch> szJson = new List<AppVouch>();
  275. if (infos.Count <= 0)
  276. {
  277. throw new Exception("传送数据为空!");
  278. }
  279. string connS = "";
  280. string res = string.Empty;
  281. SqlConnection conn = new SqlConnection();
  282. SqlCommand cmd = new SqlCommand();
  283. string sql = string.Empty;
  284. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  285. foreach (string WorkPoint in result)
  286. {
  287. try
  288. {
  289. connS = string.Format(connString, WorkPoint);
  290. conn = new System.Data.SqlClient.SqlConnection(connS);
  291. conn.Open();
  292. SqlTransaction sqlTran = conn.BeginTransaction();
  293. cmd = new SqlCommand();
  294. cmd.Transaction = sqlTran;
  295. cmd.Connection = conn;
  296. foreach (AppVouch info in infos)
  297. {
  298. if (WorkPoint!=info.WorkPoint)
  299. {
  300. continue;
  301. }
  302. ICSUserInfo userInfo = new ICSUserInfo();
  303. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  304. if (info.MTime < new DateTime(2000, 01, 01))
  305. throw new Exception("请输入正确的操作时间:" + info.MTime);
  306. sql = @"UPDATE dbo.PU_AppVouch SET cVerifier ='" + userInfo.UserName + @"' ,
  307. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE ID='" + info.ID + "'";
  308. sql = string.Format(sql, ERPDB);
  309. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  310. }
  311. cmd.Transaction.Commit();
  312. }
  313. catch (Exception ex)
  314. {
  315. cmd.Transaction.Rollback();
  316. log.Error(ex.Message);
  317. throw new Exception(ex.Message);
  318. }
  319. finally
  320. {
  321. if (conn.State == ConnectionState.Open)
  322. {
  323. conn.Close();
  324. }
  325. conn.Dispose();
  326. }
  327. }
  328. return res;
  329. }
  330. /// <summary>
  331. /// 删除请购单
  332. /// </summary>
  333. /// <param name="infos"></param>
  334. /// <returns></returns>
  335. public string Delete(List<AppVouch> infos)
  336. {
  337. List<AppVouch> szJson = new List<AppVouch>();
  338. if (infos.Count <= 0)
  339. {
  340. throw new Exception("传送数据为空!");
  341. }
  342. string res = string.Empty;
  343. string connS = "";
  344. SqlConnection conn = new SqlConnection();
  345. SqlCommand cmd = new SqlCommand();
  346. string sql = string.Empty;
  347. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  348. foreach (string WorkPoint in result)
  349. {
  350. try
  351. {
  352. connS = string.Format(connString, WorkPoint);
  353. conn = new System.Data.SqlClient.SqlConnection(connS);
  354. conn.Open();
  355. SqlTransaction sqlTran = conn.BeginTransaction();
  356. cmd = new SqlCommand();
  357. cmd.Transaction = sqlTran;
  358. cmd.Connection = conn;
  359. foreach (AppVouch info in infos)
  360. {
  361. if (WorkPoint != info.WorkPoint)
  362. {
  363. continue;
  364. }
  365. if (info.MTime < new DateTime(2000, 01, 01))
  366. throw new Exception("请输入正确的操作时间:" + info.MTime);
  367. sql = @" DELETE dbo.PU_AppVouch WHERE ID={0}";
  368. sql = string.Format(sql, info.ID, ERPDB);
  369. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  370. }
  371. cmd.Transaction.Commit();
  372. }
  373. catch (Exception ex)
  374. {
  375. cmd.Transaction.Rollback();
  376. log.Error(ex.Message);
  377. throw new Exception(ex.Message);
  378. }
  379. finally
  380. {
  381. if (conn.State == ConnectionState.Open)
  382. {
  383. conn.Close();
  384. }
  385. conn.Dispose();
  386. }
  387. }
  388. return res;
  389. }
  390. }
  391. }