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

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