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

468 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
2 years ago
3 years ago
3 years ago
2 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
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,iNum)
  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,@iNum 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("@iNum", body.Amount));
  248. cmd.Parameters.Add(new SqlParameter("@cordercode", head.OOCode));
  249. cmd.Parameters.Add(new SqlParameter("@ivouchrowno",body.Sequence));
  250. cmd.Parameters.Add(new SqlParameter("@dPDate", head.MTime));
  251. if (cMassUnit == 1)
  252. {
  253. cmd.Parameters.Add(new SqlParameter("@dVDate", head.MTime.AddYears(iMassDate).Date));
  254. }
  255. if (cMassUnit == 2)
  256. {
  257. cmd.Parameters.Add(new SqlParameter("@dVDate", head.MTime.AddMonths(iMassDate).Date));
  258. }
  259. if (cMassUnit == 3)
  260. {
  261. cmd.Parameters.Add(new SqlParameter("@dVDate", head.MTime.AddDays(iMassDate).Date));
  262. }
  263. cmd.Parameters.Add(new SqlParameter("@dVDate", ""));
  264. cmd.Parameters.Add(new SqlParameter("@imassdate", iMassDate));
  265. cmd.Parameters.Add(new SqlParameter("@cmassunit", cMassUnit));
  266. cmd.Parameters.Add(new SqlParameter("@Batch", body.BatchCode));
  267. cmd.Parameters.Add(new SqlParameter("@cDefine37", head.MTime.AddDays(1 - DateTime.Now.Day).AddMonths(1).AddSeconds(-1)));
  268. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  269. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  270. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  271. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  272. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  273. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  274. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  275. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  276. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  277. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  278. cmd.CommandText = sql;
  279. try
  280. {
  281. int count = cmd.ExecuteNonQuery();
  282. if (count <= 0)
  283. {
  284. log.Error("生成委外到货单表体失败,受影响行数<=0;");
  285. throw new Exception("生成委外到货单表体失败,受影响行数<=0;");
  286. }
  287. }
  288. catch (Exception ex)
  289. {
  290. log.Error("生成委外到货单表体失败!委外订单号:" + head.ODNCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  291. throw new Exception("生成委外到货单表体失败!委外订单号:" + head.ODNCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  292. }
  293. sql = string.Format(@"UPDATE B SET B.iArrMoney = ISNULL(B.iArrMoney, 0) + A.iOriSum, B.iNatArrMoney = ISNULL(B.iNatArrMoney, 0) + A.iSum
  294. FROM dbo.PU_ArrivalVouchs A LEFT JOIN dbo.PO_Podetails B ON A.iPOsID = B.ID
  295. WHERE A.Autoid = '{0}'", iChildId);
  296. cmd.CommandText = sql;
  297. cmd.ExecuteNonQuery();
  298. iFatherIdTwo += "'" + iFatherId + "',";
  299. iChildId--;
  300. }
  301. sql = @" SELECT A.ID as ID, A.ID as IDs,A.cCode as ODNCode,A.cVenCode,D.cVenName,
  302. A.cDepCode,C.cDepName,A.cpocode as OOCode,A.cMaker,A.cMakeTime,A.cverifier,A.caudittime
  303. FROM PU_ArrivalVouch A
  304. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  305. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE
  306. WHERE 1=1 and A.cBusType='' AND A.ID in ({0})
  307. SELECT A.ID as IDs, B.Autoid as DetailID ,B.ivouchrowno as Sequence,B.cInvCode,B.INUM,B.iQuantity,
  308. B.fValidInQuan,B.iPOsID as OODetailID
  309. FROM PU_ArrivalVouch A
  310. INNER JOIN PU_ArrivalVouchs B ON A.ID=B.ID
  311. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  312. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and A.cBusType='' AND A.ID in ({0}) ";
  313. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  314. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  315. if (dtNew1 == null)
  316. dtNew1 = ds.Tables[0];
  317. else
  318. dtNew1.Merge(ds.Tables[0]);
  319. if (dtNew2 == null)
  320. dtNew2 = ds.Tables[1];
  321. else
  322. dtNew2.Merge(ds.Tables[1]);
  323. cmd.Transaction.Commit();
  324. }
  325. catch (Exception ex)
  326. {
  327. cmd.Transaction.Rollback();
  328. log.Error(ex.Message);
  329. throw new Exception(ex.Message);
  330. }
  331. finally
  332. {
  333. if (conn.State == ConnectionState.Open)
  334. {
  335. conn.Close();
  336. }
  337. conn.Dispose();
  338. }
  339. }
  340. string RelationName = "details";
  341. DataSet dz = new DataSet();
  342. dtNew1.TableName = "Table0";
  343. dtNew2.TableName = "Table1";
  344. dz.Tables.Add(dtNew1.Copy());
  345. dz.Tables.Add(dtNew2.Copy());
  346. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  347. dz.Relations.Add(dh);
  348. str = DBHelper.DataSetToJson(dz, RelationName);
  349. return str;
  350. }
  351. public string Approve(List<ICSDeliveryNotice> infos)
  352. {
  353. List<ICSDeliveryNotice> szJson = new List<ICSDeliveryNotice>();
  354. string json = "";
  355. string connS = "";
  356. if (infos.Count <= 0)
  357. {
  358. throw new Exception("传送数据为空!");
  359. }
  360. string res = string.Empty;
  361. SqlConnection conn = new SqlConnection();
  362. SqlCommand cmd = new SqlCommand();
  363. string sql = string.Empty;
  364. foreach (ICSDeliveryNotice info in infos)
  365. {
  366. try
  367. {
  368. connS = string.Format(connString, info.WorkPoint);
  369. conn = new System.Data.SqlClient.SqlConnection(connS);
  370. conn.Open();
  371. SqlTransaction sqlTran = conn.BeginTransaction();
  372. cmd = new SqlCommand();
  373. cmd.Transaction = sqlTran;
  374. cmd.Connection = conn;
  375. if (info.MTime < new DateTime(2000, 01, 01))
  376. throw new Exception("请输入正确的操作时间:" + info.MTime);
  377. sql = @"UPDATE dbo.PU_ArrivalVouch SET cVerifier ='" + info.User + @"' ,
  378. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE ID='{0}'";
  379. sql = string.Format(sql, info.ID);
  380. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  381. cmd.Transaction.Commit();
  382. }
  383. catch (Exception ex)
  384. {
  385. cmd.Transaction.Rollback();
  386. log.Error(ex.Message);
  387. throw new Exception(ex.Message);
  388. }
  389. finally
  390. {
  391. if (conn.State == ConnectionState.Open)
  392. {
  393. conn.Close();
  394. }
  395. conn.Dispose();
  396. }
  397. }
  398. return json;
  399. }
  400. /// <summary>
  401. /// 删除请购单
  402. /// </summary>
  403. /// <param name="infos"></param>
  404. /// <returns></returns>
  405. public string Delete(List<ICSDeliveryNotice> infos)
  406. {
  407. List<ICSDeliveryNotice> szJson = new List<ICSDeliveryNotice>();
  408. if (infos.Count <= 0)
  409. {
  410. throw new Exception("传送数据为空!");
  411. }
  412. string res = string.Empty;
  413. string connS = "";
  414. SqlConnection conn = new SqlConnection();
  415. SqlCommand cmd = new SqlCommand();
  416. string sql = string.Empty;
  417. foreach (ICSDeliveryNotice info in infos)
  418. {
  419. try
  420. {
  421. connS = string.Format(connString, info.WorkPoint);
  422. conn = new System.Data.SqlClient.SqlConnection(connS);
  423. conn.Open();
  424. SqlTransaction sqlTran = conn.BeginTransaction();
  425. cmd = new SqlCommand();
  426. cmd.Transaction = sqlTran;
  427. cmd.Connection = conn;
  428. if (info.MTime < new DateTime(2000, 01, 01))
  429. throw new Exception("请输入正确的操作时间:" + info.MTime);
  430. sql = @" DELETE dbo.PU_ArrivalVouch WHERE ID='{0}'";
  431. sql = string.Format(sql, info.ID);
  432. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  433. cmd.Transaction.Commit();
  434. }
  435. catch (Exception ex)
  436. {
  437. cmd.Transaction.Rollback();
  438. log.Error(ex.Message);
  439. throw new Exception(ex.Message);
  440. }
  441. finally
  442. {
  443. if (conn.State == ConnectionState.Open)
  444. {
  445. conn.Close();
  446. }
  447. conn.Dispose();
  448. }
  449. }
  450. return res;
  451. }
  452. }
  453. }