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

462 lines
22 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 CreatePOArrive
  14. {
  15. private log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  16. private string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  17. private string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  18. private 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<ICSPOArrive> infos)
  42. {
  43. List<ICSPOArrive> szJson = new List<ICSPOArrive>();
  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 (ICSPOArrive 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,C.CDEPNAME,A.cDepCode,C.cDepName,A.cpocode,A.cMaker,A.cMakeTime,A.cverifier,A.caudittime,
  65. B.Autoid,B.irowno ,B.cInvCode ,B.INUM,B.iQuantity,B.fValidInQuan,B.iPOsID
  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=0";
  70. if (!string.IsNullOrWhiteSpace(info.DNCode))
  71. {
  72. sql += " and a.cCode='{0}'";
  73. }
  74. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  75. {
  76. sql += " and ISNULL(a.cModifyTime,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.DNCode, info.MTime, info.User);
  83. dt = DBHelper.SQlReturnData(sql, cmd);
  84. json = JsonConvert.SerializeObject(dt);
  85. }
  86. cmd.Transaction.Commit();
  87. return json;
  88. }
  89. catch (Exception ex)
  90. {
  91. cmd.Transaction.Rollback();
  92. log.Error(ex.Message);
  93. throw new Exception(ex.Message);
  94. }
  95. finally
  96. {
  97. if (conn.State == ConnectionState.Open)
  98. {
  99. conn.Close();
  100. }
  101. conn.Dispose();
  102. }
  103. }
  104. /// <summary>
  105. /// 生成到货单
  106. /// </summary>
  107. /// <param name="infos"></param>
  108. /// <returns></returns>
  109. public string Create(List<ICSPOArrive> infos)
  110. {
  111. if (infos.Count <= 0)
  112. {
  113. throw new Exception("传送数据为空!");
  114. }
  115. string res = string.Empty;
  116. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  117. conn.Open();
  118. SqlTransaction sqlTran = conn.BeginTransaction();
  119. SqlCommand cmd = new SqlCommand();
  120. cmd.Transaction = sqlTran;
  121. cmd.Connection = conn;
  122. try
  123. {
  124. string sql = string.Empty;
  125. foreach (ICSPOArrive info in infos)
  126. {
  127. if (info.MTime<new DateTime(2000,01,01))
  128. throw new Exception("请输入正确的操作时间:" + info.MTime);
  129. string Lotnos = "";
  130. foreach (var detail in info.details)
  131. {
  132. sql = @"IF EXISTS(SELECT LOTNO FROM ICSASNDETAIL WHERE LOTNO='{1}' AND STNO='{2}' AND ISNULL(RECEIVEQTY,0)>0)
  133. RAISERROR('{1}',16,1)
  134. UPDATE ICSASNDETAIL SET RECEIVEQTY=ISNULL(RECEIVEQTY,0)+{0} WHERE LOTNO='{1}' AND STNO='{2}'
  135. --IF EXISTS(SELECT LOTNO FROM ICSASNDETAIL WHERE LOTNO='{1}' AND STNO='{2}' AND RECEIVEQTY>PLANQTY)
  136. -- RAISERROR('{1}',16,1)";
  137. sql = string.Format(sql, detail.Quantity, detail.LOTNO,info.ASNNO);
  138. DBHelper.CmdExecuteNonQuery(sql, cmd, "送货单号:"+ info.ASNNO+",条码:"+ detail.LOTNO+"未查询到对应数据!");
  139. Lotnos += "'" + detail.LOTNO + "',";
  140. }
  141. if (string.IsNullOrWhiteSpace(Lotnos))
  142. throw new Exception("送货单号:" + info.ASNNO+",无条码信息!");
  143. CreatePUArrive( infos);
  144. }
  145. cmd.Transaction.Commit();
  146. return res;
  147. }
  148. catch (Exception ex)
  149. {
  150. cmd.Transaction.Rollback();
  151. log.Error(ex.Message);
  152. throw new Exception(ex.Message);
  153. }
  154. finally
  155. {
  156. if (conn.State == ConnectionState.Open)
  157. {
  158. conn.Close();
  159. }
  160. conn.Dispose();
  161. }
  162. }
  163. /// <summary>
  164. /// 回写U8到货单,
  165. /// </summary>
  166. /// <param name="cmd"></param>
  167. /// <param name="Lotnos"></param>
  168. /// <param name="info"></param>
  169. public string CreatePUArrive(List<ICSPOArrive> Bills)
  170. {
  171. string res = string.Empty;
  172. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  173. conn.Open();
  174. string str = "";
  175. DataTable dt = null;
  176. int iFatherId = 0;
  177. int iChildId = 0;
  178. SqlTransaction sqlTran = conn.BeginTransaction();
  179. SqlCommand cmd = new SqlCommand();
  180. cmd.Transaction = sqlTran;
  181. cmd.Connection = conn;
  182. int num = 0;
  183. foreach (ICSPOArrive head in Bills)
  184. {
  185. num= head.details.Count();
  186. }
  187. List<ICSPOArrive> datas = new List<ICSPOArrive>();
  188. string sql = "";
  189. try
  190. {
  191. foreach (ICSPOArrive head in Bills)
  192. {
  193. num = head.details.Count();
  194. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  195. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  196. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  197. DateTime date = DateTime.Now;
  198. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  199. string DEF_ID = DBHelper.GetDefaultTemplate(Type, cmd);
  200. #region 主表
  201. sql = @"INSERT INTO PU_ArrivalVouch
  202. (iVTid, ID, cCode, cPTCode, dDate, cVenCode, cDepCode, cPersonCode, cPayCode, cexch_name, iExchRate, iTaxRate, cMemo, cBusType, cMaker, bNegative,
  203. iDiscountTaxType, iBillType, cMakeTime, cAuditDate, caudittime, cverifier, iverifystateex, IsWfControlled, iflowid, iPrintCount, cpocode)
  204. SELECT @iVTid, @ID, @cCode, cPTCode, CONVERT(NVARCHAR(10), GETDATE(), 23), cVenCode, cDepCode, cPersonCode, cPayCode, cexch_name, nflat, iTaxRate, cMemo, '', @User, 0,
  205. iDiscountTaxType, 0, GETDATE(), CONVERT(NVARCHAR(10), GETDATE(), 23), GETDATE(), @User, 2, 0, 0, iPrintCount, cPOID
  206. FROM PO_Pomain WHERE 1=1 AND cPOID = @POID ";
  207. cmd.Parameters.Clear();
  208. cmd.Parameters.Add(new SqlParameter("@iVTid", DEF_ID));
  209. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  210. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  211. cmd.Parameters.Add(new SqlParameter("@User", head.User));
  212. cmd.Parameters.Add(new SqlParameter("@POID", head.POCode));
  213. //cmd.Parameters.Add(new SqlParameter("@dDate", data.dMate));
  214. cmd.CommandText = sql;
  215. try
  216. {
  217. int count = cmd.ExecuteNonQuery();
  218. if (count <= 0)
  219. {
  220. log.Error("生成采购到货单表头失败,受影响行数<=0;");
  221. throw new Exception("生成采购到货单表头失败,受影响行数<=0;");
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. log.Error("生成采购到货单表头失败!采购订单号:" + head.POCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  227. throw new Exception("生成采购到货单表头失败!采购订单号:" + head.POCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  228. }
  229. #endregion
  230. #region 子表
  231. foreach (ICSPODetail body in head.details)
  232. {
  233. int iMassDate = 0;
  234. int cMassUnit = 0;
  235. sql = @"SELECT cInvCode, ISNULL(iMassDate, 0) iMassDate, ISNULL(cMassUnit, 0) cMassUnit FROM Inventory WHERE cInvCode = '{0}' ";
  236. sql = string.Format(sql, body.InvCode);
  237. DataTable dts = DBHelper.SQlReturnData(sql, cmd);
  238. if (dts.Rows.Count == 0)
  239. {
  240. log.Error("存货档案不存在");
  241. throw new Exception("存货档案不存在");
  242. }
  243. else
  244. {
  245. iMassDate = Convert.ToInt16(dts.Rows[0]["iMassDate"].ToString());
  246. cMassUnit = Convert.ToInt16(dts.Rows[0]["cMassUnit"].ToString());
  247. }
  248. sql = @"
  249. INSERT INTO PU_ArrivalVouchs
  250. (Autoid, ID, cInvCode, iQuantity, iOriCost, iOriTaxCost, iOriMoney, iOriTaxPrice, iOriSum, iCost, iMoney, iTaxPrice, iSum, iTaxRate,
  251. iPOsID, fValidInQuan, fRealQuantity, fValidQuantity, fRetQuantity, fInValidInQuan, bGsp, fValidNum, fValidInNum, fRealNum, bTaxCost,
  252. SoType, fInvalidInNum, iinvexchrate, cordercode, RejectSource, iExpiratDateCalcu, iordertype, ivouchrowno, bgift,
  253. dPDate, dVDate, imassdate, cmassunit, cExpirationdate, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8,
  254. cBatch, cDefine37, fKPQuantity, finValidQuantity, fRefuseQuantity, iProductType,bexigency)
  255. SELECT @Autoid, @ID, @cInvCode, @iQuantity,pd.iUnitPrice,pd.iTaxPrice,round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2),
  256. 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),
  257. round(round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2)*b.nflat,2),
  258. round((round(pd.iTaxPrice*@iQuantity,2)-round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2))*b.nflat,2),round(round(pd.iTaxPrice*@iQuantity,2)*b.nflat,2),pd.iPerTaxRate,
  259. pd.ID, 0, @iQuantity, @iQuantity, 0, 0, 0, null, null, null, 1,
  260. 0, null, 0, @cordercode, 0, 0, 0, @ivouchrowno, 0,
  261. @dPDate, @dVDate, @imassdate, @cmassunit, null, @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8,
  262. @Batch, @cDefine37, null, null, null, null, null FROM PO_Podetails pd
  263. left join po_pomain b on pd.poid=b.poid
  264. WHERE pd.ID = @iPOsID ";
  265. cmd.Parameters.Clear();
  266. cmd.Parameters.Add(new SqlParameter("@iPOsID", body.PODetailID));
  267. cmd.Parameters.Add(new SqlParameter("@Autoid", iChildId));
  268. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  269. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  270. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  271. cmd.Parameters.Add(new SqlParameter("@cordercode", head.POCode));
  272. cmd.Parameters.Add(new SqlParameter("@ivouchrowno", body.Sequence));
  273. cmd.Parameters.Add(new SqlParameter("@dPDate", head.MTime));
  274. if (cMassUnit == 1)
  275. {
  276. cmd.Parameters.Add(new SqlParameter("@dVDate", head.MTime.AddYears(iMassDate).Date));
  277. }
  278. if (cMassUnit == 2)
  279. {
  280. cmd.Parameters.Add(new SqlParameter("@dVDate", head.MTime.AddMonths(iMassDate).Date));
  281. }
  282. if (cMassUnit == 3)
  283. {
  284. cmd.Parameters.Add(new SqlParameter("@dVDate", head.MTime.AddDays(iMassDate).Date));
  285. }
  286. cmd.Parameters.Add(new SqlParameter("@dVDate", ""));
  287. cmd.Parameters.Add(new SqlParameter("@imassdate", iMassDate));
  288. cmd.Parameters.Add(new SqlParameter("@cmassunit", cMassUnit));
  289. cmd.Parameters.Add(new SqlParameter("@Batch", ""));
  290. cmd.Parameters.Add(new SqlParameter("@cDefine37", head.MTime.AddDays(1 - DateTime.Now.Day).AddMonths(1).AddSeconds(-1)));
  291. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  292. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  293. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  294. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  295. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  296. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  297. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  298. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  299. cmd.CommandText = sql;
  300. try
  301. {
  302. int count = cmd.ExecuteNonQuery();
  303. if (count <= 0)
  304. {
  305. log.Error("生成采购到货单表体失败,受影响行数<=0;");
  306. throw new Exception("生成采购到货单表体失败,受影响行数<=0;");
  307. }
  308. }
  309. catch (Exception ex)
  310. {
  311. log.Error("生成采购到货单表体失败!采购订单号:" + head.POCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  312. throw new Exception("生成采购到货单表体失败!采购订单号:" + head.POCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  313. }
  314. sql = string.Format(@"UPDATE B SET B.iArrMoney = ISNULL(B.iArrMoney, 0) + A.iOriSum, B.iNatArrMoney = ISNULL(B.iNatArrMoney, 0) + A.iSum
  315. FROM dbo.PU_ArrivalVouchs A LEFT JOIN dbo.PO_Podetails B ON A.iPOsID = B.ID
  316. WHERE A.Autoid = '{0}'", iChildId);
  317. cmd.CommandText = sql;
  318. cmd.ExecuteNonQuery();
  319. iChildId--;
  320. }
  321. #endregion
  322. sql = @" SELECT A.ID as ID,A.ID as IDs,A.cCode as DNCode ,A.cVenCode,D.cVenName,A.cDepCode,C.cDepName,
  323. A.cpocode as POCode,A.cMaker as CreateUser,A.cMakeTime as CreateDateTime,
  324. A.cverifier as Checker,A.caudittime as CheckDateTime
  325. FROM PU_ArrivalVouch A
  326. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  327. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and A.iBillType=0 and A.ID in('{0}')
  328. SELECT A.ID as IDs,B.Autoid as DetailID,B.ivouchrowno as Sequence,B.cInvCode as InvCode,
  329. B.INUM as Amount,B.iQuantity as Quantity,B.fValidInQuan as RCVQuantity,B.iPOsID as PODetailID
  330. FROM PU_ArrivalVouch A
  331. INNER JOIN PU_ArrivalVouchs B ON A.ID=B.ID
  332. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  333. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and A.iBillType=0 and A.ID in('{0}')";
  334. sql = string.Format(sql, iFatherId);
  335. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  336. string RelationName = "details";
  337. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  338. ds.Relations.Add(dr);
  339. str = DBHelper.DataSetToJson(ds, RelationName);
  340. }
  341. cmd.Transaction.Commit();
  342. }
  343. catch (Exception ex)
  344. {
  345. cmd.Transaction.Rollback();
  346. log.Error(ex.Message);
  347. throw new Exception(ex.Message);
  348. }
  349. finally
  350. {
  351. if (conn.State == ConnectionState.Open)
  352. {
  353. conn.Close();
  354. }
  355. conn.Dispose();
  356. }
  357. return str;
  358. }
  359. public string Approve(List<ICSPOArrive> infos)
  360. {
  361. List<ICSPOArrive> szJson = new List<ICSPOArrive>();
  362. DataTable dt = null;
  363. string json = "";
  364. if (infos.Count <= 0)
  365. {
  366. throw new Exception("传送数据为空!");
  367. }
  368. string res = string.Empty;
  369. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  370. conn.Open();
  371. SqlTransaction sqlTran = conn.BeginTransaction();
  372. SqlCommand cmd = new SqlCommand();
  373. cmd.Transaction = sqlTran;
  374. cmd.Connection = conn;
  375. try
  376. {
  377. string sql = string.Empty;
  378. foreach (ICSPOArrive info in infos)
  379. {
  380. if (info.MTime < new DateTime(2000, 01, 01))
  381. throw new Exception("请输入正确的操作时间:" + info.MTime);
  382. sql = @"UPDATE dbo.PU_ArrivalVouch SET cVerifier ='" + info.User + @"' ,
  383. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE ID='{0}'";
  384. sql = string.Format(sql,info.ID);
  385. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  386. }
  387. cmd.Transaction.Commit();
  388. return json;
  389. }
  390. catch (Exception ex)
  391. {
  392. cmd.Transaction.Rollback();
  393. log.Error(ex.Message);
  394. throw new Exception(ex.Message);
  395. }
  396. finally
  397. {
  398. if (conn.State == ConnectionState.Open)
  399. {
  400. conn.Close();
  401. }
  402. conn.Dispose();
  403. }
  404. }
  405. /// <summary>
  406. /// 删除请购单
  407. /// </summary>
  408. /// <param name="infos"></param>
  409. /// <returns></returns>
  410. public string Delete(List<ICSPOArrive> infos)
  411. {
  412. List<ICSPOArrive> szJson = new List<ICSPOArrive>();
  413. if (infos.Count <= 0)
  414. {
  415. throw new Exception("传送数据为空!");
  416. }
  417. string res = string.Empty;
  418. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  419. conn.Open();
  420. SqlTransaction sqlTran = conn.BeginTransaction();
  421. SqlCommand cmd = new SqlCommand();
  422. cmd.Transaction = sqlTran;
  423. cmd.Connection = conn;
  424. try
  425. {
  426. string sql = string.Empty;
  427. foreach (ICSPOArrive info in infos)
  428. {
  429. if (info.MTime < new DateTime(2000, 01, 01))
  430. throw new Exception("请输入正确的操作时间:" + info.MTime);
  431. sql = @" DELETE dbo.PU_ArrivalVouch WHERE ID='{0}'";
  432. sql = string.Format(sql, info.ID);
  433. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  434. }
  435. cmd.Transaction.Commit();
  436. return res;
  437. ;
  438. }
  439. catch (Exception ex)
  440. {
  441. cmd.Transaction.Rollback();
  442. log.Error(ex.Message);
  443. throw new Exception(ex.Message);
  444. }
  445. finally
  446. {
  447. if (conn.State == ConnectionState.Open)
  448. {
  449. conn.Close();
  450. }
  451. conn.Dispose();
  452. }
  453. }
  454. }
  455. }