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

803 lines
39 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. public class CreatePuArrivalVouch
  14. {
  15. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  16. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  17. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  18. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  19. private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["PUARRIVALRCRDNAME"];
  20. //参数示例
  21. //[{
  22. // "ASNNO": "DN510852000001",
  23. // "WHCode": "11",
  24. // "User": "CC001",
  25. // "MTime": "2021-08-26 17:20:13",
  26. // "details": [
  27. // {
  28. // "LOTNO": "PO200219004100005",
  29. // "ReceiveQty": "2"
  30. // },{
  31. // "LOTNO": "PO200219004100001",
  32. // "ReceiveQty": "1"
  33. // }
  34. // ]
  35. //}]
  36. //返回参数
  37. //{
  38. //"Success": true,
  39. //"Message": "接口调用成功!",
  40. //"Data": null
  41. //}
  42. public string Get(List<ICSPUArrivalVouch> infos)
  43. {
  44. List<ICSPUArrivalVouch> szJson = new List<ICSPUArrivalVouch>();
  45. DataTable dt = null;
  46. DataTable dtNew = null;
  47. string connS = "";
  48. string json = "";
  49. if (infos.Count <= 0)
  50. {
  51. throw new Exception("传送数据为空!");
  52. }
  53. string res = string.Empty;
  54. SqlConnection conn = new SqlConnection();
  55. SqlCommand cmd = new SqlCommand();
  56. string sql = string.Empty;
  57. foreach (ICSPUArrivalVouch info in infos)
  58. {
  59. try
  60. {
  61. connS = string.Format(connString, info.WorkPoint);
  62. conn = new System.Data.SqlClient.SqlConnection(connS);
  63. conn.Open();
  64. SqlTransaction sqlTran = conn.BeginTransaction();
  65. cmd = new SqlCommand();
  66. cmd.Transaction = sqlTran;
  67. cmd.Connection = conn;
  68. ICSUserInfo userInfo = new ICSUserInfo();
  69. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  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,A.cDepCode,C.CDEPNAME, A.CMAKER,A.CMAKETIME,A.CVERIFIER,A.CAUDITTIME,
  73. B.Autoid ,B.irowno ,B.IQUANTITY ,B.INUM,B.cInvCode ,B.iCost ,B.irejectautoid
  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.iBillType=2 and b.IQUANTITY<0 ";
  78. if (!string.IsNullOrWhiteSpace(info.RJTCode))
  79. {
  80. sql += " and a.cCode='{0}'";
  81. }
  82. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  83. {
  84. sql += " and ISNULL(A.dclosedate,ISNULL(A.caudittime ,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.RJTCode, info.MTime, userInfo.UserName);
  91. dt = DBHelper.SQlReturnData(sql, cmd);
  92. if (dt.Rows.Count <= 0 || dt == null)
  93. throw new Exception("拒收单单:" + info.RJTCode + ",无信息!");
  94. if (dtNew == null)
  95. dtNew = dt;
  96. else
  97. dtNew.Merge(dt);
  98. cmd.Transaction.Commit();
  99. }
  100. catch (Exception ex)
  101. {
  102. cmd.Transaction.Rollback();
  103. log.Error(ex.Message);
  104. throw new Exception(ex.Message);
  105. }
  106. finally
  107. {
  108. if (conn.State == ConnectionState.Open)
  109. {
  110. conn.Close();
  111. }
  112. conn.Dispose();
  113. }
  114. }
  115. json = JsonConvert.SerializeObject(dtNew);
  116. return json;
  117. }
  118. /// <summary>
  119. /// 回写U8到货单,
  120. /// </summary>
  121. /// <param name="cmd"></param>
  122. /// <param name="Lotnos"></param>
  123. /// <param name="info"></param>
  124. public string CreatePUArrive(List<ICSPUArrivalVouch> Bills)
  125. {
  126. string msg = "";
  127. string szJson = "";
  128. DataSet ds = null;
  129. string iFatherIdTwo = "";
  130. int num = 0;
  131. string connS = "";
  132. SqlConnection conn = new SqlConnection();
  133. SqlCommand cmd = new SqlCommand();
  134. if (Bills.Count <= 0)
  135. {
  136. throw new Exception("传送数据为空!");
  137. }
  138. bool isNG = false;
  139. LogInfo(Bills);
  140. foreach (ICSPUArrivalVouch head in Bills)
  141. {
  142. foreach (ICSPUArrivalVouchs body in head.Vouchs)
  143. {
  144. if (body.Quantity > 0)
  145. {
  146. isNG = true;
  147. }
  148. }
  149. }
  150. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  151. foreach (string WorkPoint in result)
  152. {
  153. try
  154. {
  155. connS = string.Format(connString, WorkPoint);
  156. conn = new System.Data.SqlClient.SqlConnection(connS);
  157. conn.Open();
  158. SqlTransaction sqlTran = conn.BeginTransaction();
  159. cmd = new SqlCommand();
  160. cmd.Transaction = sqlTran;
  161. cmd.Connection = conn;
  162. if (isNG)
  163. {
  164. int sum = 0;
  165. foreach (ICSPUArrivalVouch head in Bills)
  166. {
  167. iFatherIdTwo = "";
  168. num = head.Vouchs.Count();
  169. if (WorkPoint != head.WorkPoint)
  170. {
  171. continue;
  172. }
  173. string[] ss = head.WorkPoint.Split('_');
  174. ERPDB = ss[1];
  175. string sql = "";
  176. VouchKey key = new VouchKey();
  177. //取得out库单的默认显示模版
  178. string[] dd = CRNALL.Split('~');
  179. string crdname = dd[0];
  180. string carname = dd[1];
  181. string surface = dd[2];
  182. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  183. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  184. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  185. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  186. DateTime date = DateTime.Now;
  187. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", "", head.WorkPoint);
  188. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  189. ICSUserInfo userInfo = new ICSUserInfo();
  190. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  191. #region 主表
  192. sql = @"INSERT INTO PU_ArrivalVouch
  193. (iVTid, ID, cCode, cPTCode, dDate, cVenCode, cDepCode, cPersonCode, cPayCode, cexch_name, iExchRate, iTaxRate, cMemo, cBusType, cMaker, bNegative,
  194. iDiscountTaxType, iBillType, cMakeTime, cAuditDate, caudittime, cverifier, iverifystateex, IsWfControlled, iflowid, iPrintCount, cpocode)
  195. SELECT @iVTid, @ID, @cCode, cPTCode, CONVERT(NVARCHAR(10), GETDATE(), 23), cVenCode, cDepCode, cPersonCode, cPayCode, cexch_name, iExchRate, iTaxRate, cMemo, '', @User, 1,
  196. iDiscountTaxType, 2, GETDATE(), CONVERT(NVARCHAR(10), GETDATE(), 23), GETDATE(), @User, 2, 0, 0, iPrintCount, null
  197. FROM PU_ArrivalVouch WHERE 1=1 AND cCode = @headID ";
  198. cmd.Parameters.Clear();
  199. cmd.Parameters.Add(new SqlParameter("@iVTid", DEF_ID));
  200. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  201. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  202. cmd.Parameters.Add(new SqlParameter("@User", userInfo.UserName));
  203. cmd.Parameters.Add(new SqlParameter("@headID", head.DNCode));
  204. cmd.CommandText = sql;
  205. try
  206. {
  207. int count = cmd.ExecuteNonQuery();
  208. if (count <= 0)
  209. {
  210. log.Error("生成采购拒收单表头失败,受影响行数<=0;");
  211. throw new Exception("生成采购拒收单表头失败,受影响行数<=0;");
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. log.Error("生成采购拒收单表头失败!到货单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  217. throw new Exception("生成采购拒收单表头失败!到货单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  218. }
  219. #endregion
  220. iFatherIdTwo = "'" + iFatherId + "',";
  221. #region 表体
  222. foreach (ICSPUArrivalVouchs body in head.Vouchs)
  223. {
  224. sql = @"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. cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cItemCode,
  229. cBatch, cDefine37, fKPQuantity, finValidQuantity, fRefuseQuantity, iProductType,bexigency,inum,iCorId,carrivalcode )
  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*3402,2)*b.nflat,2),pd.iPerTaxRate,
  234. pd.ID, 0, @iQuantity, @iQuantity, 0, 0, 0, null, null, null, 1,
  235. 0, null, 0, b.cPOID, 0, 0, 0, @ivouchrowno, 0,
  236. @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8, @cFree9, @cFree10,@cItemCode,
  237. @Batch, @cDefine37, null, null, null, null, null,0,a.Autoid,c.cCode
  238. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.PO_Podetails pd ON a.iPOsID= pd.ID
  239. INNER JOIN dbo.PO_Pomain b ON pd.POID=b.POID
  240. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.DNDetailID + @"'
  241. EXEC PROC_Update_PO_PUArrivalVouchs @ID= @ID1";
  242. cmd.Parameters.Clear();
  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("@ivouchrowno", body.Sequence));
  248. cmd.Parameters.Add(new SqlParameter("@Batch", body.BatchCode));
  249. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  250. cmd.Parameters.Add(new SqlParameter("@cDefine37", DateTime.Now.Date.AddDays(1 - DateTime.Now.Day).AddMonths(1).AddSeconds(-1)));
  251. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  252. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  253. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  254. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  255. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  256. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  257. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  258. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  259. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  260. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  261. cmd.CommandText = sql;
  262. try
  263. {
  264. int count = cmd.ExecuteNonQuery();
  265. if (count <= 0)
  266. {
  267. log.Error("生成采购拒收单表体失败,受影响行数<=0;");
  268. throw new Exception("生成采购拒收单表体失败,受影响行数<=0;");
  269. }
  270. }
  271. catch (Exception ex)
  272. {
  273. log.Error("生成采购拒收单表体失败!采购到货单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  274. throw new Exception("生成采购拒收单表体失败!采购到货单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  275. }
  276. sql = string.Format(@"update pd set pd.fPoRefuseQuantity = isnull(pd.fPoRefuseQuantity,0)+{0}
  277. from dbo.PU_ArrivalVouchs a LEFT JOIN dbo.PO_Podetails pd ON a.iPOsID= pd.ID
  278. INNER JOIN dbo.PO_Pomain b ON pd.POID=b.POID
  279. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.DNDetailID + "' ", body.Quantity);
  280. cmd.CommandText = sql;
  281. cmd.ExecuteNonQuery();
  282. iChildId -= 1;
  283. }
  284. #endregion
  285. #region 查询
  286. sql = @"select DISTINCT a.ID as ID,a.ID as IDs,a.cCode as RJTCode,a.cVenCode VenCode,d.cVenName VenName,a.cDepCode DepCode,c.cDepName DepName
  287. ,b.carrivalcode as DNCode ,a.cMaker as CreateUser ,a.cMakeTime as CreateDateTime
  288. ,a.cverifier as Checker ,a.caudittime as CheckDateTime
  289. from PU_ArrivalVouch a
  290. left join PU_ArrivalVouchs b on a.ID=b.ID
  291. left join Department c on a.cDepCode=c.cDepCode
  292. left join Vendor d on a.cVenCode=d.cVenCode
  293. WHERE A.cverifier IS NOT NULL AND A.ID in({0})
  294. select DISTINCT a.ID as IDs, b.AutoID as DetailID,b.ivouchrowno as Sequence ,b.cInvCode as InvCode ,
  295. b.iQuantity as Quantity ,b.iNum as Amount,a.cexch_name Currency,iOriCost UnitPrice,b.iCorId as DNDetailID
  296. ,isnull(b.cItemCode,'') ProjectCode,
  297. isnull(b.cbatch,'') cBatch,
  298. '' version ,'' brand,
  299. isnull(b.cFree1,'') as cFree1,
  300. isnull(b.cFree2,'') as cFree2,
  301. isnull(b.cFree3,'') as cFree3,
  302. isnull(b.cFree4,'') as cFree4,
  303. isnull(b.cFree5,'') as cFree5,
  304. isnull(b.cFree6,'') as cFree6,
  305. isnull(b.cFree7,'') as cFree7,
  306. isnull(b.cFree8,'') as cFree8,
  307. isnull(b.cFree9,'') as cFree9,
  308. isnull(b.cFree10,'') as cFree10
  309. from PU_ArrivalVouch a
  310. left join PU_ArrivalVouchs b on a.ID=b.ID
  311. WHERE A.cverifier IS NOT NULL AND A.ID in({0}) ";
  312. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  313. if (ds != null)
  314. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  315. else
  316. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  317. #endregion
  318. }
  319. }
  320. cmd.Transaction.Commit();
  321. }
  322. catch (Exception ex)
  323. {
  324. cmd.Transaction.Rollback();
  325. log.Error(ex.Message);
  326. throw new Exception(ex.Message);
  327. }
  328. finally
  329. {
  330. if (conn.State == ConnectionState.Open)
  331. {
  332. conn.Close();
  333. }
  334. conn.Dispose();
  335. }
  336. }
  337. szJson = JSON.DataSetToJson(ds, "details", "IDs");
  338. return szJson;
  339. }
  340. /// <summary>
  341. /// 回写委外拒收单
  342. /// </summary>
  343. /// <param name="cmd"></param>
  344. /// <param name="Lotnos"></param>
  345. /// <param name="info"></param>
  346. public string CreateWPUArrive(List<ICSOPUArrivalVouch> Bills)
  347. {
  348. string msg = "";
  349. string szJson = "";
  350. DataSet ds = null;
  351. string iFatherIdTwo = "";
  352. int num = 0;
  353. string connS = "";
  354. SqlConnection conn = new SqlConnection();
  355. SqlCommand cmd = new SqlCommand();
  356. if (Bills.Count <= 0)
  357. {
  358. throw new Exception("传送数据为空!");
  359. }
  360. bool isNG = false;
  361. LogInfo(Bills);
  362. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  363. foreach (string WorkPoint in result)
  364. {
  365. try
  366. {
  367. connS = string.Format(connString, 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. foreach (ICSOPUArrivalVouch head in Bills)
  375. {
  376. foreach (ICSOPUArrivalVouchs body in head.Vouchs)
  377. {
  378. if (body.Quantity > 0)
  379. {
  380. isNG = true;
  381. }
  382. }
  383. }
  384. if (isNG)
  385. {
  386. int sum = 0;
  387. foreach (ICSOPUArrivalVouch head in Bills)
  388. {
  389. iFatherIdTwo = "";
  390. if (WorkPoint != head.WorkPoint)
  391. {
  392. continue;
  393. }
  394. ICSUserInfo userInfo = new ICSUserInfo();
  395. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  396. string[] ss = head.WorkPoint.Split('_');
  397. ERPDB = ss[1];
  398. string sql = "";
  399. VouchKey key = new VouchKey();
  400. //取得out库单的默认显示模版
  401. string[] dd = CRNALL.Split('~');
  402. string crdname = dd[0];
  403. string carname = dd[1];
  404. string surface = dd[2];
  405. num = head.Vouchs.Count();
  406. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  407. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  408. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  409. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  410. DateTime date = DateTime.Now;
  411. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", "", head.WorkPoint);
  412. #region 主表
  413. sql = @"INSERT INTO PU_ArrivalVouch
  414. (iVTid, ID, cCode, cPTCode, dDate, cVenCode, cDepCode, cPersonCode, cPayCode, cexch_name, iExchRate, iTaxRate, cMemo, cBusType, cMaker, bNegative,
  415. iDiscountTaxType, iBillType, cMakeTime, cAuditDate, caudittime, cverifier, iverifystateex, IsWfControlled, iflowid, iPrintCount, cpocode)
  416. SELECT @iVTid, @ID, @cCode, cPTCode, CONVERT(NVARCHAR(10), GETDATE(), 23), cVenCode, cDepCode, cPersonCode, cPayCode, cexch_name, iExchRate, iTaxRate, cMemo, '', @User, 1,
  417. iDiscountTaxType, 2, GETDATE(), CONVERT(NVARCHAR(10), GETDATE(), 23), GETDATE(), @User, 2, 0, 0, iPrintCount, null
  418. FROM PU_ArrivalVouch WHERE 1=1 AND cCode = @headID ";
  419. cmd.Parameters.Clear();
  420. cmd.Parameters.Add(new SqlParameter("@iVTid", iChildId));
  421. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  422. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  423. cmd.Parameters.Add(new SqlParameter("@User", userInfo.UserName));
  424. cmd.Parameters.Add(new SqlParameter("@headID", head.ODNCode));
  425. cmd.CommandText = sql;
  426. try
  427. {
  428. int count = cmd.ExecuteNonQuery();
  429. if (count <= 0)
  430. {
  431. log.Error("生成委外拒收单表头失败,受影响行数<=0;");
  432. throw new Exception("生成委外拒收单表头失败,受影响行数<=0;");
  433. }
  434. }
  435. catch (Exception ex)
  436. {
  437. log.Error("生成委外拒收单表头失败!到货单:" + head.ODNCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  438. throw new Exception("生成委外拒收单表头失败!到货单:" + head.ODNCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  439. }
  440. #endregion
  441. iFatherIdTwo = "'" + iFatherId + "',";
  442. #region 表体
  443. foreach (ICSOPUArrivalVouchs body in head.Vouchs)
  444. {
  445. iChildId -= 1;
  446. sql = @"INSERT INTO PU_ArrivalVouchs
  447. (Autoid, ID, cInvCode, iQuantity, iOriCost, iOriTaxCost, iOriMoney, iOriTaxPrice, iOriSum, iCost, iMoney, iTaxPrice, iSum, iTaxRate,
  448. iPOsID, fValidInQuan, fRealQuantity, fValidQuantity, fRetQuantity, fInValidInQuan, bGsp, fValidNum, fValidInNum, fRealNum, bTaxCost,
  449. SoType, fInvalidInNum, iinvexchrate, cordercode, RejectSource, iExpiratDateCalcu, iordertype, ivouchrowno, bgift,
  450. cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cItemCode,
  451. cBatch, cDefine37, fKPQuantity, finValidQuantity, fRefuseQuantity, iProductType,bexigency,inum,iCorId,carrivalcode )
  452. SELECT top 1 @Autoid, @ID1, @cInvCode, @iQuantity,pd.iUnitPrice,pd.iTaxPrice,round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2),
  453. 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),
  454. round(round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2)*b.nflat,2),
  455. round((round(pd.iTaxPrice*@iQuantity,2)-round((pd.iTaxPrice*@iQuantity)/(1+pd.iPerTaxRate/100),2))*b.nflat,2),round(round(pd.iTaxPrice*3402,2)*b.nflat,2),pd.iPerTaxRate,
  456. pd.MODetailsID, 0, @iQuantity, @iQuantity, 0, 0, 0, null, null, null, 1,
  457. 0, null, 0, b.ccode, 0, 0, 0, @ivouchrowno, 0,
  458. @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8, @cFree9, @cFree10,@cItemCode,
  459. @Batch, @cDefine37, null, null, null, null, null,0,a.Autoid,c.cCode
  460. from dbo.PU_ArrivalVouchs a
  461. LEFT JOIN dbo.OM_MODetails pd ON a.iPOsID = pd.MODetailsID
  462. left JOIN dbo.OM_MOMain b ON pd.MOID = b.MOID
  463. left JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNDetailID + @"'
  464. EXEC PROC_Update_OM_PUArrivalVouchs @ID= @ID1
  465. ";
  466. cmd.Parameters.Clear();
  467. cmd.Parameters.Add(new SqlParameter("@Autoid", iChildId));
  468. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  469. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  470. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  471. cmd.Parameters.Add(new SqlParameter("@ivouchrowno", body.Sequence));
  472. cmd.Parameters.Add(new SqlParameter("@Batch", body.BatchCode));
  473. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  474. cmd.Parameters.Add(new SqlParameter("@cDefine37", DateTime.Now.Date.AddDays(1 - DateTime.Now.Day).AddMonths(1).AddSeconds(-1)));
  475. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  476. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  477. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  478. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  479. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  480. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  481. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  482. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  483. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  484. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  485. cmd.CommandText = sql;
  486. try
  487. {
  488. int count = cmd.ExecuteNonQuery();
  489. if (count <= 0)
  490. {
  491. log.Error("生成委外拒收单表体失败,受影响行数<=0;");
  492. throw new Exception("生成委外拒收单表体失败,受影响行数<=0;");
  493. }
  494. }
  495. catch (Exception ex)
  496. {
  497. log.Error("生成委外拒收单表体失败!委外到货单:" + head.ODNCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  498. throw new Exception("生成委外拒收单表体失败!委外到货单:" + head.ODNCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  499. }
  500. sql = string.Format(@"update pd set pd.fPoRefuseQuantity = isnull(pd.fPoRefuseQuantity,0)+{0}
  501. from dbo.PU_ArrivalVouchs a LEFT JOIN dbo.PO_Podetails pd ON a.iPOsID= pd.ID
  502. where a.Autoid='" + body.ODNDetailID + "' ", body.Quantity);
  503. cmd.CommandText = sql;
  504. cmd.ExecuteNonQuery();
  505. }
  506. #endregion
  507. #region 查询
  508. sql = @"select DISTINCT a.ID as ID,a.ID as IDs,a.cCode as RJTCode,a.cVenCode VenCode,d.cVenName VenName,a.cDepCode DepCode,c.cDepName DepName
  509. ,b.carrivalcode as DNCode ,a.cMaker as CreateUser ,a.cMakeTime as CreateDateTime
  510. ,a.cverifier as Checker ,a.caudittime as CheckDateTime
  511. from PU_ArrivalVouch a
  512. left join PU_ArrivalVouchs b on a.ID=b.ID
  513. left join Department c on a.cDepCode=c.cDepCode
  514. left join Vendor d on a.cVenCode=d.cVenCode
  515. WHERE A.cverifier IS NOT NULL AND A.ID in({0})
  516. select DISTINCT a.ID as IDs, b.AutoID as DetailID,b.ivouchrowno as Sequence ,b.cInvCode as InvCode ,
  517. b.iQuantity as Quantity ,b.iNum as Amount,a.cexch_name Currency,iOriCost UnitPrice,b.iCorId as DNDetailID
  518. ,isnull(b.cItemCode,'') ProjectCode,
  519. isnull(b.cbatch,'') cBatch,
  520. '' version ,'' brand,
  521. isnull(b.cFree1,'') as cFree1,
  522. isnull(b.cFree2,'') as cFree2,
  523. isnull(b.cFree3,'') as cFree3,
  524. isnull(b.cFree4,'') as cFree4,
  525. isnull(b.cFree5,'') as cFree5,
  526. isnull(b.cFree6,'') as cFree6,
  527. isnull(b.cFree7,'') as cFree7,
  528. isnull(b.cFree8,'') as cFree8,
  529. isnull(b.cFree9,'') as cFree9,
  530. isnull(b.cFree10,'') as cFree10
  531. from PU_ArrivalVouch a
  532. left join PU_ArrivalVouchs b on a.ID=b.ID
  533. WHERE A.cverifier IS NOT NULL AND A.ID in({0}) ";
  534. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  535. if (ds != null)
  536. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  537. else
  538. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  539. #endregion
  540. }
  541. }
  542. cmd.Transaction.Commit();
  543. }
  544. catch (Exception ex)
  545. {
  546. cmd.Transaction.Rollback();
  547. log.Error(ex.Message);
  548. throw new Exception(ex.Message);
  549. }
  550. finally
  551. {
  552. if (conn.State == ConnectionState.Open)
  553. {
  554. conn.Close();
  555. }
  556. conn.Dispose();
  557. }
  558. }
  559. szJson = JSON.DataSetToJson(ds, "details", "IDs");
  560. return szJson;
  561. }
  562. public string Approve(List<ICSPUArrivalVouch> infos)
  563. {
  564. List<ICSPUArrivalVouch> szJson = new List<ICSPUArrivalVouch>();
  565. DataTable dt = null;
  566. DataTable dtNew = null;
  567. string connS = "";
  568. string json = "";
  569. if (infos.Count <= 0)
  570. {
  571. throw new Exception("传送数据为空!");
  572. }
  573. string res = string.Empty;
  574. SqlConnection conn = new SqlConnection();
  575. SqlCommand cmd = new SqlCommand();
  576. string sql = string.Empty;
  577. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  578. foreach (string WorkPoint in result)
  579. {
  580. try
  581. {
  582. connS = string.Format(connString, WorkPoint);
  583. conn = new System.Data.SqlClient.SqlConnection(connS);
  584. conn.Open();
  585. SqlTransaction sqlTran = conn.BeginTransaction();
  586. cmd = new SqlCommand();
  587. cmd.Transaction = sqlTran;
  588. cmd.Connection = conn;
  589. foreach (ICSPUArrivalVouch info in infos)
  590. {
  591. if (WorkPoint != info.WorkPoint)
  592. {
  593. continue;
  594. }
  595. ICSUserInfo userInfo = new ICSUserInfo();
  596. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  597. if (info.MTime < new DateTime(2000, 01, 01))
  598. throw new Exception("请输入正确的操作时间:" + info.MTime);
  599. sql = @"UPDATE PU_ArrivalVouch SET cVerifier ='" + userInfo.UserName + @"' ,
  600. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE ID='{0}'";
  601. sql = string.Format(sql, info.ID);
  602. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  603. }
  604. cmd.Transaction.Commit();
  605. }
  606. catch (Exception ex)
  607. {
  608. cmd.Transaction.Rollback();
  609. log.Error(ex.Message);
  610. throw new Exception(ex.Message);
  611. }
  612. finally
  613. {
  614. if (conn.State == ConnectionState.Open)
  615. {
  616. conn.Close();
  617. }
  618. conn.Dispose();
  619. }
  620. }
  621. return json;
  622. }
  623. /// <summary>
  624. /// 删除请购单
  625. /// </summary>
  626. /// <param name="infos"></param>
  627. /// <returns></returns>
  628. public string Delete(List<ICSPUArrivalVouch> infos)
  629. {
  630. List<ICSPUArrivalVouch> szJson = new List<ICSPUArrivalVouch>();
  631. if (infos.Count <= 0)
  632. {
  633. throw new Exception("传送数据为空!");
  634. }
  635. string res = string.Empty;
  636. DataTable dtNew = null;
  637. string connS = "";
  638. SqlConnection conn = new SqlConnection();
  639. SqlCommand cmd = new SqlCommand();
  640. string sql = string.Empty;
  641. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  642. foreach (string WorkPoint in result)
  643. {
  644. try
  645. {
  646. connS = string.Format(connString, WorkPoint);
  647. conn = new System.Data.SqlClient.SqlConnection(connS);
  648. conn.Open();
  649. SqlTransaction sqlTran = conn.BeginTransaction();
  650. cmd = new SqlCommand();
  651. cmd.Transaction = sqlTran;
  652. cmd.Connection = conn;
  653. foreach (ICSPUArrivalVouch info in infos)
  654. {
  655. if (WorkPoint != info.WorkPoint)
  656. {
  657. continue;
  658. }
  659. if (info.MTime < new DateTime(2000, 01, 01))
  660. throw new Exception("请输入正确的操作时间:" + info.MTime);
  661. sql = @" DELETE PU_AppVouch WHERE ID={0}";
  662. sql = string.Format(sql, info.ID);
  663. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  664. }
  665. cmd.Transaction.Commit();
  666. }
  667. catch (Exception ex)
  668. {
  669. cmd.Transaction.Rollback();
  670. log.Error(ex.Message);
  671. throw new Exception(ex.Message);
  672. }
  673. finally
  674. {
  675. if (conn.State == ConnectionState.Open)
  676. {
  677. conn.Close();
  678. }
  679. conn.Dispose();
  680. }
  681. }
  682. return res;
  683. }
  684. /// <summary>
  685. ///
  686. /// 记录日志
  687. /// </summary>
  688. /// <param name="Bills"></param>
  689. private void LogInfo(List<ICSPUArrivalVouch> Bills)
  690. {
  691. string HeadList = string.Empty;
  692. string BodyList = string.Empty;
  693. foreach (ICSPUArrivalVouch head in Bills)
  694. {
  695. HeadList += "\r\n 表头主键ID:" + head.ID + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  696. foreach (ICSPUArrivalVouchs body in head.Vouchs)
  697. {
  698. BodyList += "\r\n 表体主键ID: " + body.DNDetailID + ",数量:" + body.Quantity;
  699. }
  700. }
  701. log.Info(HeadList);
  702. log.Info(BodyList);
  703. }
  704. /// <summary>
  705. ///
  706. /// 记录日志
  707. /// </summary>
  708. /// <param name="Bills"></param>
  709. private void LogInfo(List<ICSOPUArrivalVouch> Bills)
  710. {
  711. string HeadList = string.Empty;
  712. string BodyList = string.Empty;
  713. foreach (ICSOPUArrivalVouch head in Bills)
  714. {
  715. HeadList += "\r\n 表头主键ID:" + head.ID + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  716. foreach (ICSOPUArrivalVouchs body in head.Vouchs)
  717. {
  718. BodyList += "\r\n 表体主键ID: " + body.ODNDetailID + ",数量:" + body.Quantity;
  719. }
  720. }
  721. log.Info(HeadList);
  722. log.Info(BodyList);
  723. }
  724. }
  725. }