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

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