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

403 lines
19 KiB

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