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

427 lines
19 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
  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 CreatePuArrivalVouch
  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. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  19. //参数示例
  20. //[{
  21. // "ASNNO": "DN510852000001",
  22. // "WHCode": "11",
  23. // "User": "CC001",
  24. // "MTime": "2021-08-26 17:20:13",
  25. // "details": [
  26. // {
  27. // "LOTNO": "PO200219004100005",
  28. // "ReceiveQty": "2"
  29. // },{
  30. // "LOTNO": "PO200219004100001",
  31. // "ReceiveQty": "1"
  32. // }
  33. // ]
  34. //}]
  35. //返回参数
  36. //{
  37. //"Success": true,
  38. //"Message": "接口调用成功!",
  39. //"Data": null
  40. //}
  41. public string Get(List<ICSPUArrivalVouch> infos)
  42. {
  43. List<ICSPUArrivalVouch> szJson = new List<ICSPUArrivalVouch>();
  44. DataTable dt = null;
  45. DataTable dtNew = null;
  46. string connS = "";
  47. string json = "";
  48. if (infos.Count <= 0)
  49. {
  50. throw new Exception("传送数据为空!");
  51. }
  52. string res = string.Empty;
  53. SqlConnection conn = new SqlConnection();
  54. SqlCommand cmd = new SqlCommand();
  55. string sql = string.Empty;
  56. foreach (ICSPUArrivalVouch info in infos)
  57. {
  58. try
  59. {
  60. connS = string.Format(connString, info.WorkPoint);
  61. conn = new System.Data.SqlClient.SqlConnection(connS);
  62. conn.Open();
  63. SqlTransaction sqlTran = conn.BeginTransaction();
  64. cmd = new SqlCommand();
  65. cmd.Transaction = sqlTran;
  66. cmd.Connection = conn;
  67. if (info.MTime < new DateTime(2000, 01, 01))
  68. throw new Exception("请输入正确的操作时间:" + info.MTime);
  69. sql = @"SELECT A.ID,A.cCode,A. cVenCode ,D.CVENNAME,A.cDepCode,C.CDEPNAME, A.CMAKER,A.CMAKETIME,A.CVERIFIER,A.CAUDITTIME,
  70. B.Autoid ,B.irowno ,B.IQUANTITY ,B.INUM,B.cInvCode ,B.iCost ,B.irejectautoid
  71. FROM PU_ArrivalVouch A
  72. INNER JOIN PU_ArrivalVouchs B ON A.ID=B.ID
  73. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  74. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and a.iBillType=2 and b.IQUANTITY<0 ";
  75. if (!string.IsNullOrWhiteSpace(info.RJTCode))
  76. {
  77. sql += " and a.cCode='{0}'";
  78. }
  79. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  80. {
  81. sql += " and ISNULL(A.dclosedate,ISNULL(A.caudittime ,ISNULL(A.cAuditTime, ISNULL(A.cModifyTime, A.cMakeTime))))>='{1}'";
  82. }
  83. if (!string.IsNullOrWhiteSpace(info.User))
  84. {
  85. sql += "and a.CMAKER='{2}'";
  86. }
  87. sql = string.Format(sql, info.RJTCode, info.MTime, info.User);
  88. dt = DBHelper.SQlReturnData(sql, cmd);
  89. if (dt.Rows.Count <= 0 || dt == null)
  90. throw new Exception("拒收单单:" + info.RJTCode + ",无信息!");
  91. dtNew.Merge(dt);
  92. cmd.Transaction.Commit();
  93. }
  94. catch (Exception ex)
  95. {
  96. cmd.Transaction.Rollback();
  97. log.Error(ex.Message);
  98. throw new Exception(ex.Message);
  99. }
  100. finally
  101. {
  102. if (conn.State == ConnectionState.Open)
  103. {
  104. conn.Close();
  105. }
  106. conn.Dispose();
  107. }
  108. }
  109. json = JsonConvert.SerializeObject(dtNew);
  110. return json;
  111. }
  112. /// <summary>
  113. /// 回写U8到货单,
  114. /// </summary>
  115. /// <param name="cmd"></param>
  116. /// <param name="Lotnos"></param>
  117. /// <param name="info"></param>
  118. public string CreatePUArrive(List<ICSPUArrivalVouch> Bills)
  119. {
  120. string msg = "";
  121. bool ResultFlag = false;
  122. int num = 0;
  123. foreach (ICSPUArrivalVouch head in Bills)
  124. {
  125. num = head.Vouchs.Count();
  126. }
  127. DataTable dtNew = null;
  128. string connS = "";
  129. SqlConnection conn = new SqlConnection();
  130. SqlCommand cmd = new SqlCommand();
  131. if (Bills.Count <= 0)
  132. {
  133. throw new Exception("传送数据为空!");
  134. }
  135. bool isNG = false;
  136. LogInfo(Bills);
  137. DateTime dVDate = new DateTime();
  138. DateTime dPDate = new DateTime();
  139. foreach (ICSPUArrivalVouch head in Bills)
  140. {
  141. foreach (ICSPUArrivalVouchs body in head.Vouchs)
  142. {
  143. if (body.Quantity > 0)
  144. {
  145. isNG = true;
  146. }
  147. }
  148. }
  149. if (isNG)
  150. {
  151. int sum = 0;
  152. foreach (ICSPUArrivalVouch head in Bills)
  153. {
  154. try
  155. {
  156. connS = string.Format(connString, head.WorkPoint);
  157. conn = new System.Data.SqlClient.SqlConnection(connS);
  158. conn.Open();
  159. SqlTransaction sqlTran = conn.BeginTransaction();
  160. cmd = new SqlCommand();
  161. cmd.Transaction = sqlTran;
  162. cmd.Connection = conn;
  163. string[] ss = head.WorkPoint.Split('_');
  164. ERPDB = ss[1];
  165. string sql = "";
  166. VouchKey key = new VouchKey();
  167. //取得out库单的默认显示模版
  168. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  169. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  170. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  171. DateTime date = DateTime.Now;
  172. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  173. #region 主表
  174. sql = @"INSERT INTO PU_ArrivalVouch
  175. (iVTid, ID, cCode, cPTCode, dDate, cVenCode, cDepCode, cPersonCode, cPayCode, cexch_name, iExchRate, iTaxRate, cMemo, cBusType, cMaker, bNegative,
  176. iDiscountTaxType, iBillType, cMakeTime, cAuditDate, caudittime, cverifier, iverifystateex, IsWfControlled, iflowid, iPrintCount, cpocode)
  177. SELECT @iVTid, @ID, @cCode, cPTCode, CONVERT(NVARCHAR(10), GETDATE(), 23), cVenCode, cDepCode, cPersonCode, cPayCode, cexch_name, iExchRate, iTaxRate, cMemo, '', @User, 1,
  178. iDiscountTaxType, 2, GETDATE(), CONVERT(NVARCHAR(10), GETDATE(), 23), GETDATE(), @User, 2, 0, 0, iPrintCount, null
  179. FROM PU_ArrivalVouch WHERE 1=1 AND ID = @headID ";
  180. cmd.Parameters.Clear();
  181. cmd.Parameters.Add(new SqlParameter("@iVTid", iChildId));
  182. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  183. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  184. cmd.Parameters.Add(new SqlParameter("@User", head.User));
  185. cmd.Parameters.Add(new SqlParameter("@headID", head.ID));
  186. cmd.CommandText = sql;
  187. try
  188. {
  189. int count = cmd.ExecuteNonQuery();
  190. if (count <= 0)
  191. {
  192. log.Error("生成采购拒收单表头失败,受影响行数<=0;");
  193. throw new Exception("生成采购拒收单表头失败,受影响行数<=0;");
  194. }
  195. }
  196. catch (Exception ex)
  197. {
  198. log.Error("生成采购拒收单表头失败!到货单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  199. throw new Exception("生成采购拒收单表头失败!到货单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  200. }
  201. #endregion
  202. #region 表体
  203. foreach (ICSPUArrivalVouchs body in head.Vouchs)
  204. {
  205. iChildId -= 1;
  206. sql = @"INSERT INTO PU_ArrivalVouchs
  207. (Autoid, ID, cInvCode, iQuantity, iOriCost, iOriTaxCost, iOriMoney, iOriTaxPrice, iOriSum, iCost, iMoney, iTaxPrice, iSum, iTaxRate,
  208. iPOsID, fValidInQuan, fRealQuantity, fValidQuantity, fRetQuantity, fInValidInQuan, bGsp, fValidNum, fValidInNum, fRealNum, bTaxCost,
  209. SoType, fInvalidInNum, iinvexchrate, cordercode, RejectSource, iExpiratDateCalcu, iordertype, ivouchrowno, bgift,
  210. cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8,
  211. cBatch, cDefine37, fKPQuantity, finValidQuantity, fRefuseQuantity, iProductType,bexigency,inum,iCorId,carrivalcode )
  212. SELECT @Autoid, @ID, @cInvCode, @iQuantity,pd.iUnitPrice,pd.iTaxPrice,round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2),
  213. round(pd.iTaxPrice*@iQuantity,2)-round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2),round(pd.iTaxPrice*@iQuantity,2),round((pd.iTaxPrice/(1+pd.iPerTaxRate/100)*b.nflat),2),
  214. round(round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2)*b.nflat,2),
  215. round((round(pd.iTaxPrice*@iQuantity,2)-round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2))*b.nflat,2),round(round(pd.iTaxPrice*3402,2)*b.nflat,2),pd.iPerTaxRate,
  216. pd.ID, 0, @iQuantity, @iQuantity, 0, 0, 0, null, null, null, 1,
  217. 0, null, 0, b.cPOID, 0, 0, 0, @ivouchrowno, 0,
  218. pd.cFree1, pd.cFree2, pd.cFree3, pd.cFree4, pd.cFree5, pd.cFree6, pd.cFree7, pd.cFree8,
  219. @Batch, @cDefine37, null, null, null, null, null,0,a.Autoid,c.cCode
  220. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.PO_Podetails pd ON a.iPOsID= pd.ID
  221. INNER JOIN dbo.PO_Pomain b ON pd.POID=b.POID
  222. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.DNDetailID + "'";
  223. cmd.Parameters.Clear();
  224. cmd.Parameters.Add(new SqlParameter("@Autoid", iChildId));
  225. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  226. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  227. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  228. cmd.Parameters.Add(new SqlParameter("@ivouchrowno", body.Sequence));
  229. cmd.Parameters.Add(new SqlParameter("@Batch", ""));
  230. cmd.Parameters.Add(new SqlParameter("@cDefine37", DateTime.Now.Date.AddDays(1 - DateTime.Now.Day).AddMonths(1).AddSeconds(-1)));
  231. cmd.CommandText = sql;
  232. try
  233. {
  234. int count = cmd.ExecuteNonQuery();
  235. if (count <= 0)
  236. {
  237. log.Error("生成采购拒收单表体失败,受影响行数<=0;");
  238. throw new Exception("生成采购拒收单表体失败,受影响行数<=0;");
  239. }
  240. }
  241. catch (Exception ex)
  242. {
  243. log.Error("生成采购拒收单表体失败!采购到货单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  244. throw new Exception("生成采购拒收单表体失败!采购到货单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  245. }
  246. sql = string.Format(@"update pd set pd.fPoRefuseQuantity = isnull(fPoValidQuantity,0)+{0}
  247. from dbo.PU_ArrivalVouchs a LEFT JOIN dbo.PO_Podetails pd ON a.iPOsID= pd.ID
  248. INNER JOIN dbo.PO_Pomain b ON pd.POID=b.POID
  249. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.DNDetailID + "' ", body.Quantity);
  250. cmd.CommandText = sql;
  251. cmd.ExecuteNonQuery();
  252. }
  253. #endregion
  254. cmd.Transaction.Commit();
  255. }
  256. catch (Exception ex)
  257. {
  258. cmd.Transaction.Rollback();
  259. log.Error(ex.Message);
  260. throw new Exception(ex.Message);
  261. }
  262. finally
  263. {
  264. if (conn.State == ConnectionState.Open)
  265. {
  266. conn.Close();
  267. }
  268. conn.Dispose();
  269. }
  270. }
  271. }
  272. ResultFlag = true;
  273. return msg;
  274. }
  275. public string Approve(List<ICSPUArrivalVouch> infos)
  276. {
  277. List<ICSPUArrivalVouch> szJson = new List<ICSPUArrivalVouch>();
  278. DataTable dt = null;
  279. DataTable dtNew = null;
  280. string connS = "";
  281. string json = "";
  282. if (infos.Count <= 0)
  283. {
  284. throw new Exception("传送数据为空!");
  285. }
  286. string res = string.Empty;
  287. SqlConnection conn = new SqlConnection();
  288. SqlCommand cmd = new SqlCommand();
  289. string sql = string.Empty;
  290. foreach (ICSPUArrivalVouch info in infos)
  291. {
  292. try
  293. {
  294. connS = string.Format(connString, info.WorkPoint);
  295. conn = new System.Data.SqlClient.SqlConnection(connS);
  296. conn.Open();
  297. SqlTransaction sqlTran = conn.BeginTransaction();
  298. cmd = new SqlCommand();
  299. cmd.Transaction = sqlTran;
  300. cmd.Connection = conn;
  301. if (info.MTime < new DateTime(2000, 01, 01))
  302. throw new Exception("请输入正确的操作时间:" + info.MTime);
  303. sql = @"UPDATE PU_ArrivalVouch SET cVerifier ='" + info.User + @"' ,
  304. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE ID='{0}'";
  305. sql = string.Format(sql, info.ID);
  306. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  307. cmd.Transaction.Commit();
  308. }
  309. catch (Exception ex)
  310. {
  311. cmd.Transaction.Rollback();
  312. log.Error(ex.Message);
  313. throw new Exception(ex.Message);
  314. }
  315. finally
  316. {
  317. if (conn.State == ConnectionState.Open)
  318. {
  319. conn.Close();
  320. }
  321. conn.Dispose();
  322. }
  323. }
  324. return json;
  325. }
  326. /// <summary>
  327. /// 删除请购单
  328. /// </summary>
  329. /// <param name="infos"></param>
  330. /// <returns></returns>
  331. public string Delete(List<ICSPUArrivalVouch> infos)
  332. {
  333. List<ICSPUArrivalVouch> szJson = new List<ICSPUArrivalVouch>();
  334. if (infos.Count <= 0)
  335. {
  336. throw new Exception("传送数据为空!");
  337. }
  338. string res = string.Empty;
  339. DataTable dtNew = null;
  340. string connS = "";
  341. SqlConnection conn = new SqlConnection();
  342. SqlCommand cmd = new SqlCommand();
  343. string sql = string.Empty;
  344. foreach (ICSPUArrivalVouch info in infos)
  345. {
  346. try
  347. {
  348. connS = string.Format(connString, info.WorkPoint);
  349. conn = new System.Data.SqlClient.SqlConnection(connS);
  350. conn.Open();
  351. SqlTransaction sqlTran = conn.BeginTransaction();
  352. cmd = new SqlCommand();
  353. cmd.Transaction = sqlTran;
  354. cmd.Connection = conn;
  355. if (info.MTime < new DateTime(2000, 01, 01))
  356. throw new Exception("请输入正确的操作时间:" + info.MTime);
  357. sql = @" DELETE PU_AppVouch WHERE ID={0}";
  358. sql = string.Format(sql, info.ID);
  359. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  360. cmd.Transaction.Commit();
  361. }
  362. catch (Exception ex)
  363. {
  364. cmd.Transaction.Rollback();
  365. log.Error(ex.Message);
  366. throw new Exception(ex.Message);
  367. }
  368. finally
  369. {
  370. if (conn.State == ConnectionState.Open)
  371. {
  372. conn.Close();
  373. }
  374. conn.Dispose();
  375. }
  376. }
  377. return res;
  378. }
  379. /// <summary>
  380. ///
  381. /// 记录日志
  382. /// </summary>
  383. /// <param name="Bills"></param>
  384. private void LogInfo(List<ICSPUArrivalVouch> Bills)
  385. {
  386. string HeadList = string.Empty;
  387. string BodyList = string.Empty;
  388. foreach (ICSPUArrivalVouch head in Bills)
  389. {
  390. HeadList += "\r\n 表头主键ID:" + head.ID + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  391. foreach (ICSPUArrivalVouchs body in head.Vouchs)
  392. {
  393. BodyList += "\r\n 表体主键ID: " + body.DNDetailID + ",数量:" + body.Quantity;
  394. }
  395. }
  396. log.Info(HeadList);
  397. log.Info(BodyList);
  398. }
  399. }
  400. }