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

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