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

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