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

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