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

378 lines
17 KiB

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