爱思开
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.

813 lines
40 KiB

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