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

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