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

430 lines
20 KiB

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