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

589 lines
33 KiB

3 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 采购入库红字
  15. /// </summary>
  16. public class PurchaseReceiveDoctNegative
  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. /// <summary>
  23. /// 获取采购入库单(红字)
  24. /// </summary>
  25. /// <param name="infos">传入参数</param>
  26. /// <returns></returns>
  27. public string Get(List<ICSPurchaseReceiveDoc> infos)
  28. {
  29. List<ICSPurchaseReceiveDoc> szJson = new List<ICSPurchaseReceiveDoc>();
  30. DataTable dt = null;
  31. string json = "";
  32. if (infos.Count <= 0)
  33. {
  34. throw new Exception("传送数据为空!");
  35. }
  36. string res = string.Empty;
  37. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  38. conn.Open();
  39. SqlTransaction sqlTran = conn.BeginTransaction();
  40. SqlCommand cmd = new SqlCommand();
  41. cmd.Transaction = sqlTran;
  42. cmd.Connection = conn;
  43. try
  44. {
  45. string sql = string.Empty;
  46. foreach (ICSPurchaseReceiveDoc info in infos)
  47. {
  48. if (info.MTime < new DateTime(2000, 01, 01))
  49. throw new Exception("请输入正确的操作时间:" + info.MTime);
  50. sql = @"select a.ID,a.cCode,a.cVenCode,''as cVenName,cWhCode,''as cWhName,cARVCode,cMaker,dnmaketime,cHandler,dVeriDate,
  51. AutoID,irowno,cInvCode,iQuantity,iNum,iUnitCost,iArrsId
  52. FROM RdRecord01 a
  53. LEFT JOIN rdrecords01 b on a.ID=b.ID
  54. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  55. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and iQuantity<0";
  56. if (!string.IsNullOrWhiteSpace(info.RCVTCode))
  57. {
  58. sql += " and a.cCode='{0}'";
  59. }
  60. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  61. {
  62. sql += " and ISNULL(b.cbCloseTime,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime))))>='{2}'";
  63. }
  64. if (!string.IsNullOrWhiteSpace(info.User))
  65. {
  66. sql += "and a.CMAKER='{3}'";
  67. }
  68. sql = string.Format(sql, info.RCVTCode, ERPDB, info.MTime, info.User);
  69. dt = DBHelper.SQlReturnData(sql, cmd);
  70. json = JsonConvert.SerializeObject(dt);
  71. }
  72. cmd.Transaction.Commit();
  73. return json;
  74. }
  75. catch (Exception ex)
  76. {
  77. cmd.Transaction.Rollback();
  78. log.Error(ex.Message);
  79. throw new Exception(ex.Message);
  80. }
  81. finally
  82. {
  83. if (conn.State == ConnectionState.Open)
  84. {
  85. conn.Close();
  86. }
  87. conn.Dispose();
  88. }
  89. }
  90. ///<summary>
  91. //创建采购入库单(红字)
  92. ///</summary>
  93. ///<param name="Bills"></param>
  94. ///<returns></returns>
  95. public string CreatePurchaseReceiveDoctNegative(List<ICSPurchaseReceiveDoc> Bills)
  96. {
  97. string szJson = "";
  98. string sql = "";
  99. int iFatherId=0;
  100. int iChildId=0;
  101. string iFatherIdTwo = "";
  102. DateTime time = DateTime.Now;
  103. int num = 0;
  104. DataTable dt = null;
  105. decimal iTaxRate = 0;
  106. VouchKey key = new VouchKey();
  107. string json = "";
  108. if (Bills.Count <= 0)
  109. {
  110. throw new Exception("传送数据为空!");
  111. }
  112. string res = string.Empty;
  113. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  114. conn.Open();
  115. SqlTransaction sqlTran = conn.BeginTransaction();
  116. SqlCommand cmd = new SqlCommand();
  117. cmd.Transaction = sqlTran;
  118. cmd.Connection = conn;
  119. foreach (ICSPurchaseReceiveDoc head in Bills)
  120. {
  121. num = head.details.Count();
  122. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd01red", "" + num + "");
  123. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  124. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  125. DateTime dates = DateTime.Now;
  126. string iBaseCodeLen = DBHelper.GetAllRDCode("24", "" + time + "", "admin");
  127. if (head.MTime < new DateTime(2000, 01, 01))
  128. throw new Exception("请输入正确的操作时间:" + head.MTime);
  129. #region 验证入库数量不能大于采购订单数量
  130. // foreach (ICSPurchaseReceiveDocs body in head.details)
  131. // {
  132. // sql = @"SELECT a.iQuantity,a.ID,isnull(a.iReceivedQTY,0) as iReceivedQTY,isnull(a.freceivedqty,0) as freceivedqty,ISNULL(c.fInExcess,0) AS fInExcess
  133. // FROM dbo.PO_Podetails a
  134. // INNER JOIN dbo.PO_Pomain b ON a.POID=b.POID
  135. // LEFT JOIN dbo.Inventory c ON a.cInvCode=c.cInvCode
  136. // where a.ID='" + head.ID + "'";
  137. // cmd.CommandText = sql;
  138. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  139. // if (dtQty != null && dtQty.Rows.Count > 0)
  140. // {
  141. // decimal fInExcess = decimal.Parse(dtQty.Rows[0]["fInExcess"].ToString());//入库超额上限
  142. // decimal PoQty = decimal.Parse(dtQty.Rows[0]["iQuantity"].ToString());
  143. // decimal iReceicedQty = decimal.Parse(dtQty.Rows[0]["iReceivedQTY"].ToString());//累计到货数量
  144. // decimal freceivedqty = decimal.Parse(dtQty.Rows[0]["freceivedqty"].ToString());//合格品入库数量
  145. // PoQty = PoQty * (1 + fInExcess);
  146. // log.Info("PO数量:" + PoQty.ToString() + "入库超额上限:" + fInExcess.ToString() + ",累计到货数量:" + iReceicedQty.ToString() + ",合格品入库数量:" + freceivedqty.ToString());
  147. // //2021-11-05 回写字段变更,见行408
  148. // //decimal to = PoQty - iReceicedQty - freceivedqty;
  149. // decimal to = PoQty - freceivedqty;
  150. // if (body.Quantity > to)
  151. // {
  152. // throw new Exception("入库数量" + body.Quantity.ToString("f3") + "大于采购订单可入库数量" + to.ToString("f3")
  153. // );
  154. // }
  155. // }
  156. //}
  157. #endregion
  158. #region 采购入库单表头
  159. string BusType = "";//业务类型
  160. sql = @"select b.POID,b.cPOID,a.cVenCode,a.cDepCode,a.cexch_name,a.iTaxRate,a.cMaker,a.cpocode,
  161. b.nflat,a.cPersonCode,a.cptcode ,a.ID,a.cCode,a.cBusType
  162. FROM PU_ArrivalVouch a
  163. LEFT JOIN PO_Pomain b ON b.cPOID=a.cpocode
  164. WHERE a.cCode ='" + head.DNRTCode + "'";
  165. cmd.CommandText = sql;
  166. DataTable dtPOMain = DBHelper.SQlReturnData(sql, cmd);
  167. if (dtPOMain != null && dtPOMain.Rows.Count > 0)
  168. {
  169. string cSource = "";
  170. string cCode = "";
  171. int bredvouch = 0;
  172. string cRdCode = "";
  173. string WHCode = "";
  174. BusType = dtPOMain.Rows[0]["cBusType"].ToString();
  175. //校验采购订单类型是固定资产仓库
  176. if (BusType.Equals("固定资产"))
  177. {
  178. string sqlwh = @"SELECT bWhAsset FROM Warehouse WHERE cWhCode='" + head.WHCode + "'";
  179. cmd.CommandText = sqlwh;
  180. DataTable dtwhcode = DBHelper.SQlReturnData(sqlwh, cmd);
  181. log.Info(dtwhcode.Rows[0]["bWhAsset"].ToString());
  182. if (!dtwhcode.Rows[0]["bWhAsset"].ToString().Equals("True"))
  183. {
  184. throw new Exception("采购订单:" + dtPOMain.Rows[0]["cPOID"].ToString() + ",业务类型:固定资产,只能入固定资产仓库!");
  185. }
  186. else
  187. {
  188. WHCode = head.WHCode;
  189. }
  190. }
  191. else
  192. {
  193. WHCode = head.WHCode;
  194. }
  195. cSource = "采购退货单";
  196. cCode = dtPOMain.Rows[0]["cCode"].ToString();
  197. bredvouch = 1;
  198. cRdCode = "205";
  199. sql = @"
  200. INSERT INTO dbo.RdRecord01
  201. (ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  202. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode ,
  203. cPersonCode ,cVenCode ,cOrderCode ,cARVCode,
  204. cHandler ,bTransFlag ,cMaker,cDefine1,cDefine2,
  205. dVeriDate ,bpufirst ,biafirst , dARVDate ,VT_ID ,
  206. bIsSTQc ,ipurorderid ,iTaxRate ,iExchRate ,cExch_Name ,
  207. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  208. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,dnverifytime ,
  209. bredvouch ,bCredit ,iPrintCount,ipurarriveid,cPTCode,cVenPUOMProtocol)
  210. SELECT
  211. @ID,1,'01',b.cBusType,@cSource,
  212. @cWhCode,@dDate,@cCode,@cRdCode,@cDepCode,
  213. @cPersonCode,b.cVenCode,b.cPOID,@cARVCode,
  214. @cHandler,0,@cMaker,b.cDefine1,b.cDefine2,
  215. @cVeriDate,0,0,@dARVDate,@VT_ID,
  216. 0,@ipurorderid,b.iTaxRate,b.nflat,b.cexch_name,
  217. 0,0,0,0,0,
  218. 0,0,'',@dnmakertime,@dnverifytime,
  219. @bredvouch,0,0,@ipurarriveid,b.cPTCode,b.cPayCode
  220. FROM PU_ArrivalVouch a
  221. LEFT JOIN PO_Pomain b ON b.cPOID=a.cpocode
  222. WHERE a.cCode ='" + head.DNRTCode + "'";
  223. cmd.Parameters.Clear();
  224. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  225. cmd.Parameters.Add(new SqlParameter("@cSource", cSource));
  226. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  227. cmd.Parameters.Add(new SqlParameter("@dDate", Convert.ToDateTime(DateTime.Now).ToString("yyyy -MM-dd")));
  228. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  229. cmd.Parameters.Add(new SqlParameter("@cDepCode", dtPOMain.Rows[0]["cDepCode"].ToString()));
  230. cmd.Parameters.Add(new SqlParameter("@cPersonCode", dtPOMain.Rows[0]["cPersonCode"].ToString()));
  231. cmd.Parameters.Add(new SqlParameter("@cARVCode", cCode));
  232. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  233. cmd.Parameters.Add(new SqlParameter("@cVeriDate", Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd")));
  234. cmd.Parameters.Add(new SqlParameter("@dARVDate", Convert.ToDateTime(head.MTime)));
  235. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  236. cmd.Parameters.Add(new SqlParameter("@ipurorderid", dtPOMain.Rows[0]["POID"].ToString()));
  237. cmd.Parameters.Add(new SqlParameter("@dnmakertime", Convert.ToDateTime(DateTime.Now).ToString()));
  238. cmd.Parameters.Add(new SqlParameter("@dnverifytime", Convert.ToDateTime(DateTime.Now).ToString()));
  239. cmd.Parameters.Add(new SqlParameter("@bredvouch", bredvouch));
  240. cmd.Parameters.Add(new SqlParameter("@ipurarriveid", dtPOMain.Rows[0]["ID"].ToString()));
  241. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  242. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRdCode));
  243. ;
  244. cmd.CommandText = sql;
  245. try
  246. {
  247. int count = cmd.ExecuteNonQuery();
  248. if (count <= 0)
  249. {
  250. log.Error("生成红字采购入库单表头失败,受影响行数<=0;");
  251. throw new Exception("生成红字采购入库单表头失败,受影响行数<=0;");
  252. }
  253. }
  254. catch (Exception ex)
  255. {
  256. log.Error("生成红字采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  257. throw new Exception("生成红字采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  258. }
  259. }
  260. else
  261. {
  262. throw new Exception("对应采购订单不存在!采购订单号:" + dtPOMain.Rows[0]["cPOID"].ToString());
  263. }
  264. #endregion
  265. #region 采购入库单表体
  266. int irowno = 0;
  267. foreach (ICSPurchaseReceiveDocs body in head.details)
  268. {
  269. //iChildId -= 1;
  270. //if (head.IsReturn == "1")
  271. //{
  272. // body.iQuantity = -body.iQuantity;
  273. //}
  274. sql = @"SELECT c.id,c.cInvCode,c.iUnitPrice ,c.iMoney AS 原币无税金额 ,c.ivouchrowno,
  275. c.iTax AS ,c.iSum AS ,c.iNatUnitPrice ,
  276. c.iNatMoney AS ,c.iNatTax AS ,c.iNatSum AS ,
  277. c.iTaxPrice ,c.freceivedqty AS ,c.iPerTaxRate,b.Autoid,a.cCode,a.dDate
  278. FROM dbo.PU_ArrivalVouch a
  279. LEFT JOIN dbo.PU_ArrivalVouchs b ON a.ID=b.ID
  280. LEFT JOIN dbo.PO_Podetails c ON b.iPOsID=c.ID
  281. left JOIN dbo.PO_Pomain d ON c.POID=d.POID
  282. where b.AutoID='" + body.DNRTDetailID + "'";
  283. cmd.CommandText = sql;
  284. DataTable dtPODetails = DBHelper.SQlReturnData(sql, cmd);
  285. decimal iUnitPrice = 0;
  286. decimal iNatUnitPrice = 0;
  287. decimal iTaxPrice = 0;
  288. string id = "";
  289. string code = "";
  290. string date = "";
  291. if (dtPODetails != null && dtPODetails.Rows.Count > 0)
  292. {
  293. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iUnitPrice"].ToString()))
  294. iUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iUnitPrice"].ToString());//原币无税单价
  295. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iNatUnitPrice"].ToString()))
  296. iNatUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iNatUnitPrice"].ToString());//本币无税单价
  297. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iTaxPrice"].ToString()))
  298. iTaxPrice = decimal.Parse(dtPODetails.Rows[0]["iTaxPrice"].ToString());//原币含税单价
  299. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iPerTaxRate"].ToString()))
  300. iTaxRate = Convert.ToDecimal(dtPODetails.Rows[0]["iPerTaxRate"]);
  301. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["Autoid"].ToString()))//采购退货单字体标识
  302. id = dtPODetails.Rows[0]["Autoid"].ToString();
  303. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["cCode"].ToString()))//采购退货单号
  304. code = dtPODetails.Rows[0]["cCode"].ToString();
  305. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["dDate"].ToString()))//采购退货单日期
  306. date = dtPODetails.Rows[0]["dDate"].ToString();
  307. sql = @"INSERT INTO dbo.rdrecords01
  308. (
  309. AutoID ,ID ,cInvCode ,iNum ,iQuantity ,
  310. iUnitCost ,iPrice ,iAPrice ,cBatch ,iFlag ,
  311. dSDate ,iSQuantity ,iSNum ,iMoney ,iPOsID ,
  312. fACost ,iNQuantity ,chVencode ,iOriTaxCost ,iOriCost ,
  313. iOriMoney ,iOriTaxPrice ,ioriSum ,iTaxRate ,iTaxPrice ,
  314. iSum ,bTaxCost ,cPOID ,iMatSettleState ,iBillSettleCount ,
  315. bLPUseFree ,iOriTrackID ,cbaccounter ,dbKeepDate ,bCosting ,
  316. iSumBillQuantity ,bVMIUsed ,iExpiratDateCalcu ,isotype ,irowno,
  317. iArrsId,cbarvcode,dbarvdate
  318. )
  319. select
  320. @AutoID,@ID,@cInvCode,0,@iQuantity,
  321. @iUnitCost,@iPrice,@iAPrice,@cBatch,0,
  322. null,0,0,0,b.ID,
  323. @fACost,@iNQuantity,c.cVenCode,@iOriTaxCost,@iOriCost,
  324. @iOriMoney,@iOriTaxPrice,@ioriSum,@iTaxRate,@iTaxPrice,
  325. @iSum,1,@cPOID,0,0,
  326. 0,0,null,null,d.bInCost,
  327. null,0,0,0,@irowno,
  328. @iArrsId,@cbarvcode,@dbarvdate
  329. FROM dbo.PU_ArrivalVouchs a
  330. left JOIN dbo.PO_Podetails b ON a.iPOsID = b.ID
  331. left JOIN dbo.PO_Pomain c ON b.POID = c.POID
  332. left JOIN dbo.Warehouse d ON d.cWhCode ='{0}'
  333. WHERE a.Autoid = '{1}'";
  334. sql = string.Format(sql, head.WHCode, body.DNRTDetailID);
  335. cmd.Parameters.Clear();
  336. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  337. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  338. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  339. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  340. cmd.Parameters.Add(new SqlParameter("@iUnitCost", iUnitPrice));
  341. cmd.Parameters.Add(new SqlParameter("@iPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  342. cmd.Parameters.Add(new SqlParameter("@iAPrice",Math.Round(body.Quantity * iUnitPrice, 2)));
  343. cmd.Parameters.Add(new SqlParameter("@cBatch", "''"));
  344. //cmd.Parameters.Add(new SqlParameter("@iSQuantity", 0));
  345. //cmd.Parameters.Add(new SqlParameter("@iMoney", 0));
  346. cmd.Parameters.Add(new SqlParameter("@fACost", iNatUnitPrice));
  347. cmd.Parameters.Add(new SqlParameter("@iNQuantity", body.Quantity));
  348. cmd.Parameters.Add(new SqlParameter("@iOriTaxCost", iUnitPrice));
  349. cmd.Parameters.Add(new SqlParameter("@iOriCost", iTaxPrice));
  350. cmd.Parameters.Add(new SqlParameter("@iOriMoney", Math.Round(body.Quantity * iUnitPrice, 2)));
  351. cmd.Parameters.Add(new SqlParameter("@iOriTaxPrice", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  352. cmd.Parameters.Add(new SqlParameter("@ioriSum", Math.Round(iTaxPrice * body.Quantity, 2)));
  353. cmd.Parameters.Add(new SqlParameter("@iTaxRate", iTaxRate));
  354. cmd.Parameters.Add(new SqlParameter("@iTaxPrice", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  355. cmd.Parameters.Add(new SqlParameter("@iSum", Math.Round(iTaxPrice * body.Quantity, 2)));
  356. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  357. cmd.Parameters.Add(new SqlParameter("@cPOID", head.POCode));
  358. cmd.Parameters.Add(new SqlParameter("@iArrsId", id));
  359. cmd.Parameters.Add(new SqlParameter("@cbarvcode", code));
  360. cmd.Parameters.Add(new SqlParameter("@dbarvdate", Convert.ToDateTime(date)));
  361. cmd.CommandText = sql;
  362. try
  363. {
  364. int count = cmd.ExecuteNonQuery();
  365. if (count <= 0)
  366. {
  367. log.Error("生成采购入库单表体失败,受影响行数<=0;");
  368. throw new Exception("生成采购入库单表体失败,受影响行数<=0;");
  369. }
  370. }
  371. catch (Exception ex)
  372. {
  373. log.Error("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  374. throw new Exception("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  375. }
  376. #region 更新[采购到货单子表]PU_ArrivalVouchs 合格品入库数量20211019
  377. sql = @"UPDATE PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+@iQty WHERE Autoid=@Autoid";
  378. cmd.CommandText = sql;
  379. cmd.Parameters.Clear();
  380. cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  381. cmd.Parameters.Add(new SqlParameter("@Autoid", body.DNRTDetailID));
  382. try
  383. {
  384. log.Info("PU_Autoid:" + body.DNDetailID);
  385. cmd.ExecuteNonQuery();
  386. }
  387. catch (Exception ex)
  388. {
  389. log.Error("回写采购到货单子表合格品入库数量失败:Autoid:" + body.DNDetailID + ";异常:" + ex.Message);
  390. throw new Exception("程序异常,请联系开发人员!");
  391. }
  392. #endregion
  393. #region 更新[采购订单子表]PO_PODetails累计到货数量和累计到货金额
  394. //sql = @"Update a set iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty,iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney FROM dbo.PO_Podetails a where a.ID='" + body.iPOsID + "'";
  395. sql = @"
  396. Update a set
  397. iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney
  398. ,freceivedqty=isnull(freceivedqty ,0)+ @iQty
  399. FROM dbo.PO_Podetails a where a.ID=@ID"
  400. ;
  401. //2021-11-05
  402. //1.移除 iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty 由采购到货,创建到货单CreatePU时写入iReceivedQTY,此处不再回写,累计到货数量
  403. //2.加入 freceivedqty =isnull(freceivedqty ,0) + @iQty 合格品入库数量
  404. cmd.CommandText = sql;
  405. cmd.Parameters.Clear();
  406. cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  407. cmd.Parameters.Add(new SqlParameter("@iMoney", body.Quantity * iUnitPrice));
  408. cmd.Parameters.Add(new SqlParameter("@ID", body.DNRTDetailID));
  409. try
  410. {
  411. log.Info("iPOsID:" + body.DNDetailID);
  412. cmd.ExecuteNonQuery();
  413. }
  414. catch (Exception ex)
  415. {
  416. log.Error("回写采购订单累计到货数量和累计到货金额失败:采购订单表体行号:" + body.DNDetailID + ";异常:" + ex.Message);
  417. throw new Exception("程序异常,请联系开发人员!");
  418. }
  419. #endregion
  420. //更新现存量
  421. key.cBustypeUN = "普通采购";
  422. key.cVouchTypeUN = "01";
  423. key.TableName = "IA_ST_UnAccountVouch01";
  424. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, body.InvCode, body.Quantity, key);
  425. }
  426. iChildId--;
  427. ////更新主键ID,DID
  428. //DBHelper.UpdateIDandDID("rd", head.list.Count(), cmd);
  429. }
  430. #endregion
  431. #region 判断现存量是否超入库
  432. sql = @"IF EXISTS(SELECT ID FROM dbo.PO_Podetails WHERE isnull(ireceivedqty,0)+isnull(freceivedqty,0)>iQuantity AND ID>'1000043932')
  433. BEGIN
  434. DECLARE @MSG NVARCHAR(100)
  435. SELECT @MSG='ERP入库数量不能大于订单数量ID'+CAST(ID AS NVARCHAR(100)) FROM dbo.PO_Podetails WHERE isnull(ireceivedqty,0)+isnull(freceivedqty,0)>iQuantity AND ID>'1000043932'
  436. RAISERROR(@MSG,16,1)
  437. END";
  438. cmd.CommandText = sql;
  439. cmd.ExecuteNonQuery();
  440. iFatherIdTwo += "'" + iFatherId + "',";
  441. #endregion
  442. }
  443. #region 查询
  444. sql = @"select a.ID,a.cCode,a.cVenCode,''as cVenName,cWhCode,''as cWhName,cARVCode,cMaker,dnmaketime,cHandler,dVeriDate,
  445. AutoID,irowno,cInvCode,iQuantity,iNum,iUnitCost,iArrsId
  446. FROM RdRecord01 a
  447. LEFT JOIN rdrecords01 b on a.ID=b.ID
  448. LEFT JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  449. LEFT JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and iQuantity<0 and a.ID in ({0})";
  450. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  451. dt = DBHelper.SQlReturnData(sql, cmd);
  452. #endregion
  453. cmd.Transaction.Commit();
  454. szJson = JsonConvert.SerializeObject(dt);
  455. return szJson;
  456. }
  457. /// <summary>
  458. /// 审核采购入库单(红字)
  459. /// </summary>
  460. /// <param name="infos"></param>
  461. /// <returns></returns>
  462. public string Approve(List<ICSPurchaseReceiveDoc> infos)
  463. {
  464. List<ICSPurchaseReceiveDoc> szJson = new List<ICSPurchaseReceiveDoc>();
  465. DataTable dt = null;
  466. string json = "";
  467. if (infos.Count <= 0)
  468. {
  469. throw new Exception("传送数据为空!");
  470. }
  471. string res = string.Empty;
  472. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  473. conn.Open();
  474. SqlTransaction sqlTran = conn.BeginTransaction();
  475. SqlCommand cmd = new SqlCommand();
  476. cmd.Transaction = sqlTran;
  477. cmd.Connection = conn;
  478. try
  479. {
  480. string sql = string.Empty;
  481. foreach (ICSPurchaseReceiveDoc info in infos)
  482. {
  483. if (info.MTime < new DateTime(2000, 01, 01))
  484. throw new Exception("请输入正确的操作时间:" + info.MTime);
  485. sql = @"UPDATE [{0}].dbo.RdRecord01 SET cVerifier ='" + info.User + @"' ,
  486. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE ID='" + info.ID + "'";
  487. sql = string.Format(sql, ERPDB);
  488. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核采购红字入库单失败!");
  489. }
  490. cmd.Transaction.Commit();
  491. return json;
  492. }
  493. catch (Exception ex)
  494. {
  495. cmd.Transaction.Rollback();
  496. log.Error(ex.Message);
  497. throw new Exception(ex.Message);
  498. }
  499. finally
  500. {
  501. if (conn.State == ConnectionState.Open)
  502. {
  503. conn.Close();
  504. }
  505. conn.Dispose();
  506. }
  507. }
  508. /// <summary>
  509. /// 删除采购入库单(红字)
  510. /// </summary>
  511. /// <param name="infos"></param>
  512. /// <returns></returns>
  513. public string Delete(List<ICSPurchaseReceiveDoc> infos)
  514. {
  515. List<ICSPurchaseReceiveDoc> szJson = new List<ICSPurchaseReceiveDoc>();
  516. if (infos.Count <= 0)
  517. {
  518. throw new Exception("传送数据为空!");
  519. }
  520. string res = string.Empty;
  521. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  522. conn.Open();
  523. SqlTransaction sqlTran = conn.BeginTransaction();
  524. SqlCommand cmd = new SqlCommand();
  525. cmd.Transaction = sqlTran;
  526. cmd.Connection = conn;
  527. try
  528. {
  529. string sql = string.Empty;
  530. foreach (ICSPurchaseReceiveDoc info in infos)
  531. {
  532. if (info.MTime < new DateTime(2000, 01, 01))
  533. throw new Exception("请输入正确的操作时间:" + info.MTime);
  534. sql = @"delete rdrecord11 where rdrecord11.ID='" + info.ID + "'";
  535. sql += @"delete rdrecords11 where rdrecords11.ID='" + info.ID + "'";
  536. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  537. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  538. // WHERE a.ID='" + info.ID + "'";
  539. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除采购红字入库单失败!");
  540. }
  541. cmd.Transaction.Commit();
  542. return res;
  543. ;
  544. }
  545. catch (Exception ex)
  546. {
  547. cmd.Transaction.Rollback();
  548. log.Error(ex.Message);
  549. throw new Exception(ex.Message);
  550. }
  551. finally
  552. {
  553. if (conn.State == ConnectionState.Open)
  554. {
  555. conn.Close();
  556. }
  557. conn.Dispose();
  558. }
  559. }
  560. public string GetSerialCode(string ID)
  561. {
  562. string sql = "EXEC Addins_GetSerialCode '{0}'";
  563. sql = string.Format(sql, new object[] { ID });
  564. return DBHelper.ExecuteScalar(CommandType.Text, sql, connString).ToString();
  565. }
  566. }
  567. }