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

1027 lines
62 KiB

  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 PurchaseReceiveDoc
  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";
  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(a.dnverifytime , ISNULL(a.dnmodifytime , a.dnmaketime ))>='{1}'";
  63. }
  64. if (!string.IsNullOrWhiteSpace(info.User))
  65. {
  66. sql += "and a.CMAKER='{2}'";
  67. }
  68. sql = string.Format(sql, info.RCVTCode, info.MTime, info.User);
  69. dt = DBHelper.SQlReturnData(sql, cmd);
  70. if (dt.Rows.Count <= 0 || dt == null)
  71. throw new Exception("采购入库单:" + info.RCVTCode + ",无信息!");
  72. json = JsonConvert.SerializeObject(dt);
  73. }
  74. cmd.Transaction.Commit();
  75. return json;
  76. }
  77. catch (Exception ex)
  78. {
  79. cmd.Transaction.Rollback();
  80. log.Error(ex.Message);
  81. throw new Exception(ex.Message);
  82. }
  83. finally
  84. {
  85. if (conn.State == ConnectionState.Open)
  86. {
  87. conn.Close();
  88. }
  89. conn.Dispose();
  90. }
  91. }
  92. ///<summary>
  93. //创建采购入库单(采购订单)
  94. ///</summary>
  95. ///<param name="Bills"></param>
  96. ///<returns></returns>
  97. public string CreateRdRecord01sByPO(List<ICSPurchaseReceiveDoc> Bills)
  98. {
  99. string szJson = "";
  100. string iFatherIdTwo = "";
  101. string sql = "";
  102. DataTable dt = null;
  103. decimal iTaxRate = 0;
  104. DateTime time = DateTime.Now;
  105. int num = 0;
  106. VouchKey key = new VouchKey();
  107. //int iBaseCodeLen = Convert.ToInt32(dic["iBaseCodeLen"].ToString());
  108. //int cVouchCodeBase = Convert.ToInt32(dic["cVouchCodeBase"].ToString());
  109. List<ICSPOArrive> datas = new List<ICSPOArrive>();
  110. if (Bills.Count <= 0)
  111. {
  112. throw new Exception("传送数据为空!");
  113. }
  114. string res = string.Empty;
  115. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  116. conn.Open();
  117. SqlTransaction sqlTran = conn.BeginTransaction();
  118. SqlCommand cmd = new SqlCommand();
  119. cmd.Transaction = sqlTran;
  120. cmd.Connection = conn;
  121. try
  122. {
  123. foreach (ICSPurchaseReceiveDoc head in Bills)
  124. {
  125. num = head.details.Count();
  126. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  127. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  128. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  129. DateTime date = DateTime.Now;
  130. string iBaseCodeLen = DBHelper.GetAllRDCode("24", "" + time + "", "admin");
  131. if (head.MTime < new DateTime(2000, 01, 01))
  132. throw new Exception("请输入正确的操作时间:" + head.MTime);
  133. #region 验证入库数量不能大于采购订单数量
  134. // foreach (ICSPurchaseReceiveDocs body in head.details)
  135. // {
  136. // sql = @"SELECT a.iQuantity,a.ID,isnull(a.iReceivedQTY,0) as iReceivedQTY,isnull(a.freceivedqty,0) as freceivedqty,ISNULL(c.fInExcess,0) AS fInExcess
  137. // FROM dbo.PO_Podetails a
  138. // INNER JOIN dbo.PO_Pomain b ON a.POID=b.POID
  139. // LEFT JOIN PU_ArrivalVouch d ON b.cPOID=d.cpocode
  140. // LEFT JOIN dbo.Inventory c ON a.cInvCode=c.cInvCode
  141. // WHERE d.cCode ='" + head.DNCode + "'";
  142. // cmd.CommandText = sql;
  143. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  144. // if (dtQty != null && dtQty.Rows.Count > 0)
  145. // {
  146. // decimal fInExcess = decimal.Parse(dtQty.Rows[0]["fInExcess"].ToString());//入库超额上限
  147. // decimal PoQty = decimal.Parse(dtQty.Rows[0]["iQuantity"].ToString());
  148. // decimal iReceicedQty = decimal.Parse(dtQty.Rows[0]["iReceivedQTY"].ToString());//累计到货数量
  149. // decimal freceivedqty = decimal.Parse(dtQty.Rows[0]["freceivedqty"].ToString());//合格品入库数量
  150. // PoQty = PoQty * (1 + fInExcess);
  151. // log.Info("PO数量:" + PoQty.ToString()+ "入库超额上限:" + fInExcess.ToString() + ",累计到货数量:" + iReceicedQty.ToString() + ",合格品入库数量:" + freceivedqty.ToString());
  152. // //2021-11-05 回写字段变更,见行408
  153. // //decimal to = PoQty - iReceicedQty - freceivedqty;
  154. // decimal to = PoQty - freceivedqty;
  155. // if (body.Quantity > to)
  156. // {
  157. // throw new Exception("入库数量" + body.Quantity.ToString("f3") + "大于采购订单可入库数量" + to.ToString("f3")
  158. // );
  159. // }
  160. // }
  161. // }
  162. #endregion
  163. #region 采购入库单表头
  164. string BusType = "";//业务类型
  165. sql = @"select a.POID,a.cPOID,a.cVenCode,a.cDepCode,a.cexch_name,a.iTaxRate,a.cMaker,
  166. a.nflat,a.cPersonCode,a.cptcode ,b.ID,b.cCode,a.cBusType
  167. FROM dbo.PO_Pomain a
  168. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode
  169. WHERE a.cPOID ='" + head.POCode + "'";
  170. cmd.CommandText = sql;
  171. DataTable dtPOMain = DBHelper.SQlReturnData(sql, cmd);
  172. if (dtPOMain != null && dtPOMain.Rows.Count > 0)
  173. {
  174. string cSource="";
  175. string cCode="";
  176. int bredvouch = 0;
  177. string cRdCode="";
  178. string WHCode="";
  179. BusType = dtPOMain.Rows[0]["cBusType"].ToString();
  180. //校验采购订单类型是固定资产仓库
  181. if (BusType.Equals("固定资产"))
  182. {
  183. string sqlwh = @"SELECT bWhAsset FROM Warehouse WHERE cWhCode='" + head.WHCode + "'";
  184. cmd.CommandText = sqlwh;
  185. DataTable dtwhcode = DBHelper.SQlReturnData(sqlwh, cmd);
  186. log.Info(dtwhcode.Rows[0]["bWhAsset"].ToString());
  187. if (!dtwhcode.Rows[0]["bWhAsset"].ToString().Equals("True"))
  188. {
  189. throw new Exception("到货单:" + dtPOMain.Rows[0]["cPOID"].ToString() + ",业务类型:固定资产,只能入固定资产仓库!");
  190. }
  191. else
  192. {
  193. WHCode = head.WHCode;
  194. }
  195. }
  196. else
  197. {
  198. WHCode = head.WHCode;
  199. }
  200. cSource = "采购到货单";
  201. cCode = dtPOMain.Rows[0]["cCode"].ToString();
  202. bredvouch = 0;
  203. cRdCode = "200";
  204. sql = @"
  205. INSERT INTO dbo.RdRecord01
  206. (ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  207. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode ,
  208. cPersonCode ,cVenCode ,cOrderCode ,cARVCode,
  209. cHandler ,bTransFlag ,cMaker,cDefine1,cDefine2,
  210. dVeriDate ,bpufirst ,biafirst , dARVDate ,VT_ID ,
  211. bIsSTQc ,ipurorderid ,iTaxRate ,iExchRate ,cExch_Name ,
  212. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  213. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,dnverifytime ,
  214. bredvouch ,bCredit ,iPrintCount,ipurarriveid,cPTCode,cVenPUOMProtocol)
  215. SELECT
  216. @ID,1,'01',a.cBusType,@cSource,
  217. @cWhCode,@dDate,@cCode,@cRdCode,@cDepCode,
  218. @cPersonCode,a.cVenCode,a.cPOID,@cARVCode,
  219. @cHandler,0,@cMaker,a.cDefine1,a.cDefine2,
  220. @cVeriDate,0,0,@dARVDate,@VT_ID,
  221. 0,@ipurorderid,a.iTaxRate,a.nflat,a.cexch_name,
  222. 0,0,0,0,0,
  223. 0,0,'',@dnmakertime,@dnverifytime,
  224. @bredvouch,0,0,@ipurarriveid,a.cPTCode,a.cPayCode
  225. FROM dbo.PO_Pomain a
  226. --LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode
  227. WHERE a.cPOID ='" + head.POCode + "'";
  228. cmd.Parameters.Clear();
  229. cmd.Parameters.Add(new SqlParameter("@ID",iFatherId));
  230. cmd.Parameters.Add(new SqlParameter("@cSource",cSource));
  231. cmd.Parameters.Add(new SqlParameter("@cWhCode", WHCode));
  232. cmd.Parameters.Add(new SqlParameter("@dDate",Convert.ToDateTime(DateTime.Now).ToString("yyyy -MM-dd")));
  233. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  234. cmd.Parameters.Add(new SqlParameter("@cDepCode",dtPOMain.Rows[0]["cDepCode"].ToString()));
  235. cmd.Parameters.Add(new SqlParameter("@cPersonCode",dtPOMain.Rows[0]["cPersonCode"].ToString()));
  236. cmd.Parameters.Add(new SqlParameter("@cARVCode",cCode));
  237. cmd.Parameters.Add(new SqlParameter("@cMaker",head.User));
  238. cmd.Parameters.Add(new SqlParameter("@cVeriDate",Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd")));
  239. cmd.Parameters.Add(new SqlParameter("@dARVDate",Convert.ToDateTime(head.MTime)));
  240. cmd.Parameters.Add(new SqlParameter("@VT_ID",iChildId));
  241. cmd.Parameters.Add(new SqlParameter("@ipurorderid",dtPOMain.Rows[0]["POID"].ToString()));
  242. cmd.Parameters.Add(new SqlParameter("@dnmakertime",Convert.ToDateTime(DateTime.Now).ToString()));
  243. cmd.Parameters.Add(new SqlParameter("@dnverifytime",Convert.ToDateTime(DateTime.Now).ToString()));
  244. cmd.Parameters.Add(new SqlParameter("@bredvouch",bredvouch));
  245. cmd.Parameters.Add(new SqlParameter("@ipurarriveid",dtPOMain.Rows[0]["ID"].ToString()));
  246. cmd.Parameters.Add(new SqlParameter("@cHandler",head.User));
  247. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRdCode));
  248. ;
  249. cmd.CommandText = sql;
  250. try
  251. {
  252. int count = cmd.ExecuteNonQuery();
  253. if (count <= 0)
  254. {
  255. log.Error("生成采购入库单表头失败,受影响行数<=0;");
  256. throw new Exception("生成采购入库单表头失败,受影响行数<=0;");
  257. }
  258. }
  259. catch (Exception ex)
  260. {
  261. log.Error("生成采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  262. throw new Exception("生成采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  263. }
  264. }
  265. else
  266. {
  267. throw new Exception("对应采购订单不存在!采购订单号:" +head.POCode);
  268. }
  269. #endregion
  270. #region 采购入库单表体
  271. foreach (ICSPurchaseReceiveDocs body in head.details)
  272. {
  273. //if (head.IsReturn == "1")
  274. //{
  275. // body.iQuantity = -body.iQuantity;
  276. //}
  277. sql = @"SELECT a.id,a.cInvCode,a.iUnitPrice ,a.iMoney AS 原币无税金额 ,a.ivouchrowno,
  278. a.iTax AS ,a.iSum AS ,a.iNatUnitPrice ,
  279. a.iNatMoney AS ,a.iNatTax AS ,a.iNatSum AS ,
  280. a.iTaxPrice ,a.freceivedqty AS ,a.iPerTaxRate,c.Autoid,d.cCode,d.dDate
  281. FROM dbo.PO_Podetails a
  282. INNER JOIN dbo.PO_Pomain b ON a.POID=b.POID
  283. LEFT JOIN dbo.PU_ArrivalVouchs c ON a.ID=c.iPOsID
  284. LEFT JOIN dbo.PU_ArrivalVouch d ON c.ID=d.ID
  285. WHERE b.cPOID ='" + head.POCode + "'";
  286. cmd.CommandText = sql;
  287. DataTable dtPODetails = DBHelper.SQlReturnData(sql, cmd);
  288. decimal iUnitPrice = 0;
  289. decimal iNatUnitPrice = 0;
  290. decimal iTaxPrice = 0;
  291. string id = "";
  292. string code = "";
  293. string dates = "";
  294. if (dtPODetails != null && dtPODetails.Rows.Count > 0)
  295. {
  296. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iUnitPrice"].ToString()))
  297. iUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iUnitPrice"].ToString());//原币无税单价
  298. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iNatUnitPrice"].ToString()))
  299. iNatUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iNatUnitPrice"].ToString());//本币无税单价
  300. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iTaxPrice"].ToString()))
  301. iTaxPrice = decimal.Parse(dtPODetails.Rows[0]["iTaxPrice"].ToString());//原币含税单价
  302. iTaxRate = Convert.ToDecimal(dtPODetails.Rows[0]["iPerTaxRate"]);
  303. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["Autoid"].ToString()))//采购退货单字体标识
  304. id = dtPODetails.Rows[0]["Autoid"].ToString();
  305. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["cCode"].ToString()))//采购退货单号
  306. code = dtPODetails.Rows[0]["cCode"].ToString();
  307. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["dDate"].ToString()))//采购退货单日期
  308. dates = dtPODetails.Rows[0]["dDate"].ToString();
  309. sql = @"INSERT INTO dbo.rdrecords01
  310. (
  311. AutoID ,ID ,cInvCode ,iNum ,iQuantity ,
  312. iUnitCost ,iPrice ,iAPrice ,cBatch ,iFlag ,
  313. dSDate ,iSQuantity ,iSNum ,iMoney ,iPOsID ,
  314. fACost ,iNQuantity ,chVencode ,iOriTaxCost ,iOriCost ,
  315. iOriMoney ,iOriTaxPrice ,ioriSum ,iTaxRate ,iTaxPrice ,
  316. iSum ,bTaxCost ,cPOID ,iMatSettleState ,iBillSettleCount ,
  317. bLPUseFree ,iOriTrackID ,cbaccounter ,dbKeepDate ,bCosting ,
  318. iSumBillQuantity ,bVMIUsed ,iExpiratDateCalcu ,isotype ,irowno,
  319. iArrsId,cbarvcode,dbarvdate
  320. )
  321. select
  322. @AutoID,@ID,@cInvCode,0,@iQuantity,
  323. @iUnitCost,@iPrice,@iAPrice,@cBatch,0,
  324. null,0,0,0,a.ID,
  325. @fACost,@iNQuantity,b.cVenCode,@iOriTaxCost,@iOriCost,
  326. @iOriMoney,@iOriTaxPrice,@ioriSum,@iTaxRate,@iTaxPrice,
  327. @iSum,1,@cPOID,0,0,
  328. 0,0,null,null,d.bInCost,
  329. null,0,0,0,@irowno,
  330. @iArrsId,@cbarvcode,@dbarvdate
  331. FROM dbo.PO_Podetails a
  332. INNER JOIN dbo.PO_Pomain b ON a.POID = b.POID
  333. INNER JOIN dbo.Warehouse d ON d.cWhCode = '{0}'
  334. WHERE a.ID ='" + body.PODetailID + "'";
  335. sql = string.Format(sql, head.WHCode);
  336. cmd.Parameters.Clear();
  337. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  338. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  339. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  340. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  341. cmd.Parameters.Add(new SqlParameter("@iUnitCost", iUnitPrice));
  342. cmd.Parameters.Add(new SqlParameter("@iPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  343. cmd.Parameters.Add(new SqlParameter("@iAPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  344. cmd.Parameters.Add(new SqlParameter("@cBatch", "''"));
  345. //cmd.Parameters.Add(new SqlParameter("@iSQuantity", 0));
  346. //cmd.Parameters.Add(new SqlParameter("@iMoney", 0));
  347. cmd.Parameters.Add(new SqlParameter("@fACost", iNatUnitPrice));
  348. cmd.Parameters.Add(new SqlParameter("@iNQuantity", body.Quantity));
  349. cmd.Parameters.Add(new SqlParameter("@iOriTaxCost", iTaxPrice));
  350. cmd.Parameters.Add(new SqlParameter("@iOriCost", body.Quantity * iUnitPrice));
  351. cmd.Parameters.Add(new SqlParameter("@iOriMoney", iUnitPrice));
  352. cmd.Parameters.Add(new SqlParameter("@iOriTaxPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  353. cmd.Parameters.Add(new SqlParameter("@ioriSum", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  354. cmd.Parameters.Add(new SqlParameter("@iTaxRate", Math.Round(iTaxPrice * body.Quantity, 2)));
  355. cmd.Parameters.Add(new SqlParameter("@iTaxPrice", iTaxRate));
  356. cmd.Parameters.Add(new SqlParameter("@iSum", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  357. cmd.Parameters.Add(new SqlParameter("@irowno",body.Sequence));
  358. cmd.Parameters.Add(new SqlParameter("@cPOID", dtPODetails.Rows[0]["cCode"].ToString()));
  359. cmd.Parameters.Add(new SqlParameter("@iArrsId", id));
  360. cmd.Parameters.Add(new SqlParameter("@cbarvcode", code));
  361. cmd.Parameters.Add(new SqlParameter("@dbarvdate", dates));
  362. cmd.CommandText = sql;
  363. try
  364. {
  365. int count = cmd.ExecuteNonQuery();
  366. if (count <= 0)
  367. {
  368. log.Error("生成采购入库单表体失败,受影响行数<=0;");
  369. throw new Exception("生成采购入库单表体失败,受影响行数<=0;");
  370. }
  371. }
  372. catch (Exception ex)
  373. {
  374. log.Error("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  375. throw new Exception("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  376. }
  377. #endregion
  378. #region 更新[采购到货单子表]PU_ArrivalVouchs 合格品入库数量20211019
  379. // sql = @"UPDATE PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+@iQty WHERE Autoid=@Autoid";
  380. //cmd.CommandText = sql;
  381. //cmd.Parameters.Clear();
  382. //cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  383. //cmd.Parameters.Add(new SqlParameter("@Autoid", body.DNDetailID));
  384. //try
  385. //{
  386. // log.Info("PU_Autoid:" + body.DNDetailID);
  387. // cmd.ExecuteNonQuery();
  388. //}
  389. //catch (Exception ex)
  390. //{
  391. // log.Error("回写采购到货单子表合格品入库数量失败:Autoid:" + body.DNDetailID + ";异常:" + ex.Message);
  392. // throw new Exception("程序异常,请联系开发人员!");
  393. //}
  394. #endregion
  395. #region 更新[采购订单子表]PO_PODetails累计到货数量和累计到货金额
  396. //sql = @"Update a set iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty,iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney FROM dbo.PO_Podetails a where a.ID='" + body.iPOsID + "'";
  397. sql = @"
  398. Update a set
  399. iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney
  400. ,freceivedqty=isnull(freceivedqty ,0)+ @iQty
  401. FROM dbo.PO_Podetails a where a.ID=@ID"
  402. ;
  403. //2021-11-05
  404. //1.移除 iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty 由采购到货,创建到货单CreatePU时写入iReceivedQTY,此处不再回写,累计到货数量
  405. //2.加入 freceivedqty =isnull(freceivedqty ,0) + @iQty 合格品入库数量
  406. cmd.CommandText = sql;
  407. cmd.Parameters.Clear();
  408. cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  409. cmd.Parameters.Add(new SqlParameter("@iMoney", body.Quantity * iUnitPrice));
  410. cmd.Parameters.Add(new SqlParameter("@ID", body.PODetailID));
  411. try
  412. {
  413. log.Info("iPOsID:" + body.PODetailID);
  414. int i=cmd.ExecuteNonQuery();
  415. }
  416. catch (Exception ex)
  417. {
  418. log.Error("回写采购订单累计到货数量和累计到货金额失败:采购订单表体行号:" + body.DNDetailID + ";异常:" + ex.Message);
  419. throw new Exception("程序异常,请联系开发人员!");
  420. }
  421. #endregion
  422. //string sqls = @"SELECT freceivedqty,iQuantity FROM dbo.PO_Podetails WHERE ID={0} ";
  423. //sqls = string.Format(sqls, body.PODetailID);
  424. //DataTable dtPODetailss = DBHelper.SQlReturnData(sqls, cmd);
  425. #region 判断现存量是否超入库
  426. sql = @"IF EXISTS(SELECT ID FROM dbo.PO_Podetails WHERE isnull(freceivedqty,0)>iQuantity AND ID={0} )
  427. BEGIN
  428. DECLARE @MSG NVARCHAR(100)
  429. SELECT @MSG='ERP入库数量不能大于订单数量ID'+CAST(ID AS NVARCHAR(100)) FROM dbo.PO_Podetails WHERE isnull(freceivedqty,0)>iQuantity AND ID={0}
  430. RAISERROR(@MSG,16,1)
  431. END";
  432. sql = string.Format(sql, body.PODetailID);
  433. cmd.CommandText = sql;
  434. cmd.ExecuteNonQuery();
  435. #endregion
  436. //更新现存量
  437. key.cBustypeUN = "普通采购";
  438. key.cVouchTypeUN = "01";
  439. key.TableName = "IA_ST_UnAccountVouch01";
  440. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, body.InvCode, body.Quantity, key);
  441. }
  442. ////更新主键ID,DID
  443. //DBHelper.UpdateIDandDID("rd", head.list.Count(), cmd);
  444. iChildId--;
  445. }
  446. iFatherIdTwo += "'" + iFatherId + "',";
  447. }
  448. #region 查询
  449. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as RCVTCode,a.cVenCode,D.cVenName,
  450. a.cWhCode,c.cWhName,cARVCode as DNCode,cMaker as CreateUser,
  451. dnmaketime as CreateDateTime,cHandler as Checker,dVeriDate as CheckDateTime
  452. FROM RdRecord01 a
  453. INNER JOIN Warehouse C ON a.cWhCode=C.cWhCode
  454. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and a.ID in ({0})
  455. select a.ID as IDs, AutoID as DetailID,irowno as Sequence,
  456. cInvCode as InvCode,iQuantity as Quantity,iNum as Amount,iPOsID as PODetailID
  457. FROM RdRecord01 a
  458. LEFT JOIN rdrecords01 b on a.ID=b.ID
  459. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  460. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and a.ID in ({0}) ";
  461. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  462. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  463. string RelationName = "details";
  464. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  465. ds.Relations.Add(dr);
  466. szJson = DBHelper.DataSetToJson(ds, RelationName);
  467. #endregion
  468. cmd.Transaction.Commit();
  469. return szJson;
  470. }
  471. catch (Exception ex)
  472. {
  473. cmd.Transaction.Rollback();
  474. log.Error(ex.Message);
  475. throw new Exception(ex.Message);
  476. }
  477. finally
  478. {
  479. if (conn.State == ConnectionState.Open)
  480. {
  481. conn.Close();
  482. }
  483. conn.Dispose();
  484. }
  485. }
  486. ///<summary>
  487. //创建采购入库单(到货单)
  488. ///</summary>
  489. ///<param name="Bills"></param>
  490. ///<returns></returns>
  491. public string CreateRdRecord01sByPOArrive(List<ICSPurchaseReceiveDoc> Bills)
  492. {
  493. string szJson = "";
  494. DataTable dt = null;
  495. string iFatherIdTwo = "";
  496. decimal iTaxRate = 0;
  497. DateTime time = DateTime.Now;
  498. int num = 0;
  499. string sql = "";
  500. VouchKey key = new VouchKey();
  501. //int iBaseCodeLen = Convert.ToInt32(dic["iBaseCodeLen"].ToString());
  502. //int cVouchCodeBase = Convert.ToInt32(dic["cVouchCodeBase"].ToString());
  503. List<ICSPOArrive> datas = new List<ICSPOArrive>();
  504. if (Bills.Count <= 0)
  505. {
  506. throw new Exception("传送数据为空!");
  507. }
  508. string res = string.Empty;
  509. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  510. conn.Open();
  511. SqlTransaction sqlTran = conn.BeginTransaction();
  512. SqlCommand cmd = new SqlCommand();
  513. cmd.Transaction = sqlTran;
  514. cmd.Connection = conn;
  515. try
  516. {
  517. foreach (ICSPurchaseReceiveDoc head in Bills)
  518. {
  519. num = head.details.Count;
  520. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  521. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  522. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  523. DateTime date = DateTime.Now;
  524. string iBaseCodeLen = DBHelper.GetAllRDCode("24", "" + time + "", "admin");
  525. if (head.MTime < new DateTime(2000, 01, 01))
  526. throw new Exception("请输入正确的操作时间:" + head.MTime);
  527. #region 验证入库数量不能大于采购订单数量
  528. // foreach (ICSPurchaseReceiveDocs body in head.details)
  529. // {
  530. // sql = @"SELECT a.iQuantity,a.ID,isnull(a.iReceivedQTY,0) as iReceivedQTY,isnull(a.freceivedqty,0) as freceivedqty,ISNULL(c.fInExcess,0) AS fInExcess
  531. // FROM dbo.PO_Podetails a
  532. // INNER JOIN dbo.PO_Pomain b ON a.POID=b.POID
  533. // LEFT JOIN PU_ArrivalVouch d ON b.cPOID=d.cpocode
  534. // LEFT JOIN dbo.Inventory c ON a.cInvCode=c.cInvCode
  535. // WHERE d.cCode ='" + head.DNCode + "'";
  536. // cmd.CommandText = sql;
  537. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  538. // if (dtQty != null && dtQty.Rows.Count > 0)
  539. // {
  540. // decimal fInExcess = decimal.Parse(dtQty.Rows[0]["fInExcess"].ToString());//入库超额上限
  541. // decimal PoQty = decimal.Parse(dtQty.Rows[0]["iQuantity"].ToString());
  542. // decimal iReceicedQty = decimal.Parse(dtQty.Rows[0]["iReceivedQTY"].ToString());//累计到货数量
  543. // decimal freceivedqty = decimal.Parse(dtQty.Rows[0]["freceivedqty"].ToString());//合格品入库数量
  544. // PoQty = PoQty * (1 + fInExcess);
  545. // log.Info("PO数量:" + PoQty.ToString()+ "入库超额上限:" + fInExcess.ToString() + ",累计到货数量:" + iReceicedQty.ToString() + ",合格品入库数量:" + freceivedqty.ToString());
  546. // //2021-11-05 回写字段变更,见行408
  547. // //decimal to = PoQty - iReceicedQty - freceivedqty;
  548. // decimal to = PoQty - freceivedqty;
  549. // if (body.Quantity > to)
  550. // {
  551. // throw new Exception("入库数量" + body.Quantity.ToString("f3") + "大于采购订单可入库数量" + to.ToString("f3")
  552. // );
  553. // }
  554. // }
  555. // }
  556. #endregion
  557. #region 采购入库单表头
  558. string BusType = "";//业务类型
  559. sql = @"select a.POID,a.cPOID,a.cVenCode,a.cDepCode,a.cexch_name,a.iTaxRate,a.cMaker,
  560. a.nflat,a.cPersonCode,a.cptcode ,b.ID,b.cCode,a.cBusType
  561. FROM dbo.PO_Pomain a
  562. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode
  563. WHERE b.cCode ='" + head.DNCode + "'";
  564. cmd.CommandText = sql;
  565. DataTable dtPOMain = DBHelper.SQlReturnData(sql, cmd);
  566. if (dtPOMain != null && dtPOMain.Rows.Count > 0)
  567. {
  568. string cSource = "";
  569. string cCode = "";
  570. int bredvouch = 0;
  571. string cRdCode = "";
  572. string WHCode = "";
  573. BusType = dtPOMain.Rows[0]["cBusType"].ToString();
  574. //校验采购订单类型是固定资产仓库
  575. if (BusType.Equals("固定资产"))
  576. {
  577. string sqlwh = @"SELECT bWhAsset FROM Warehouse WHERE cWhCode='" + head.WHCode + "'";
  578. cmd.CommandText = sqlwh;
  579. DataTable dtwhcode = DBHelper.SQlReturnData(sqlwh, cmd);
  580. log.Info(dtwhcode.Rows[0]["bWhAsset"].ToString());
  581. if (!dtwhcode.Rows[0]["bWhAsset"].ToString().Equals("True"))
  582. {
  583. throw new Exception("到货单:" + dtPOMain.Rows[0]["cPOID"].ToString() + ",业务类型:固定资产,只能入固定资产仓库!");
  584. }
  585. else
  586. {
  587. WHCode = head.WHCode;
  588. }
  589. }
  590. else
  591. {
  592. WHCode = head.WHCode;
  593. }
  594. cSource = "采购到货单";
  595. cCode = dtPOMain.Rows[0]["cCode"].ToString();
  596. bredvouch = 0;
  597. cRdCode = "200";
  598. sql = @"
  599. INSERT INTO dbo.RdRecord01
  600. (ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  601. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode ,
  602. cPersonCode ,cVenCode ,cOrderCode ,cARVCode,
  603. cHandler ,bTransFlag ,cMaker,cDefine1,cDefine2,
  604. dVeriDate ,bpufirst ,biafirst , dARVDate ,VT_ID ,
  605. bIsSTQc ,ipurorderid ,iTaxRate ,iExchRate ,cExch_Name ,
  606. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  607. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,dnverifytime ,
  608. bredvouch ,bCredit ,iPrintCount,ipurarriveid,cPTCode,cVenPUOMProtocol)
  609. SELECT
  610. @ID,1,'01',a.cBusType,@cSource,
  611. @cWhCode,@dDate,@cCode,@cRdCode,@cDepCode,
  612. @cPersonCode,a.cVenCode,a.cPOID,@cARVCode,
  613. @cHandler,0,@cMaker,a.cDefine1,a.cDefine2,
  614. @cVeriDate,0,0,@dARVDate,@VT_ID,
  615. 0,@ipurorderid,a.iTaxRate,a.nflat,a.cexch_name,
  616. 0,0,0,0,0,
  617. 0,0,'',@dnmakertime,@dnverifytime,
  618. @bredvouch,0,0,@ipurarriveid,a.cPTCode,a.cPayCode
  619. FROM dbo.PO_Pomain a
  620. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode
  621. WHERE b.cCode ='" + head.DNCode + "'";
  622. cmd.Parameters.Clear();
  623. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  624. cmd.Parameters.Add(new SqlParameter("@cSource", cSource));
  625. cmd.Parameters.Add(new SqlParameter("@cWhCode",head.WHCode));
  626. cmd.Parameters.Add(new SqlParameter("@dDate", Convert.ToDateTime(DateTime.Now).ToString("yyyy -MM-dd")));
  627. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  628. cmd.Parameters.Add(new SqlParameter("@cDepCode", dtPOMain.Rows[0]["cDepCode"].ToString()));
  629. cmd.Parameters.Add(new SqlParameter("@cPersonCode", dtPOMain.Rows[0]["cPersonCode"].ToString()));
  630. cmd.Parameters.Add(new SqlParameter("@cARVCode", cCode));
  631. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  632. cmd.Parameters.Add(new SqlParameter("@cVeriDate", Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd")));
  633. cmd.Parameters.Add(new SqlParameter("@dARVDate", Convert.ToDateTime(head.MTime)));
  634. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  635. cmd.Parameters.Add(new SqlParameter("@ipurorderid", dtPOMain.Rows[0]["POID"].ToString()));
  636. cmd.Parameters.Add(new SqlParameter("@dnmakertime", Convert.ToDateTime(DateTime.Now).ToString()));
  637. cmd.Parameters.Add(new SqlParameter("@dnverifytime", Convert.ToDateTime(DateTime.Now).ToString()));
  638. cmd.Parameters.Add(new SqlParameter("@bredvouch", bredvouch));
  639. cmd.Parameters.Add(new SqlParameter("@ipurarriveid", dtPOMain.Rows[0]["ID"].ToString()));
  640. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  641. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRdCode));
  642. ;
  643. cmd.CommandText = sql;
  644. try
  645. {
  646. int count = cmd.ExecuteNonQuery();
  647. if (count <= 0)
  648. {
  649. log.Error("生成采购入库单表头失败,受影响行数<=0;");
  650. throw new Exception("生成采购入库单表头失败,受影响行数<=0;");
  651. }
  652. }
  653. catch (Exception ex)
  654. {
  655. log.Error("生成采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  656. throw new Exception("生成采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  657. }
  658. }
  659. else
  660. {
  661. throw new Exception("对应采购订单不存在!采购订单号:" + head.DNCode);
  662. }
  663. #endregion
  664. #region 采购入库单表体
  665. foreach (ICSPurchaseReceiveDocs body in head.details)
  666. {
  667. sql = @"SELECT a.id,a.cInvCode,a.iUnitPrice ,a.iMoney AS 原币无税金额 ,a.ivouchrowno,
  668. a.iTax AS ,a.iSum AS ,a.iNatUnitPrice ,
  669. a.iNatMoney AS ,a.iNatTax AS ,a.iNatSum AS ,
  670. a.iTaxPrice ,a.freceivedqty AS ,a.iPerTaxRate,c.Autoid,d.cCode,d.dDate
  671. FROM dbo.PO_Podetails a
  672. INNER JOIN dbo.PO_Pomain b ON a.POID=b.POID
  673. LEFT JOIN dbo.PU_ArrivalVouchs c ON a.ID=c.iPOsID
  674. LEFT JOIN dbo.PU_ArrivalVouch d ON c.ID=d.ID
  675. where c.AutoID='" + body.DNDetailID + "'";
  676. cmd.CommandText = sql;
  677. DataTable dtPODetails = DBHelper.SQlReturnData(sql, cmd);
  678. decimal iUnitPrice = 0;
  679. decimal iNatUnitPrice = 0;
  680. decimal iTaxPrice = 0;
  681. string id = "";
  682. string code = "";
  683. string dates = "";
  684. if (dtPODetails != null && dtPODetails.Rows.Count > 0)
  685. {
  686. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iUnitPrice"].ToString()))
  687. iUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iUnitPrice"].ToString());//原币无税单价
  688. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iNatUnitPrice"].ToString()))
  689. iNatUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iNatUnitPrice"].ToString());//本币无税单价
  690. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iTaxPrice"].ToString()))
  691. iTaxPrice = decimal.Parse(dtPODetails.Rows[0]["iTaxPrice"].ToString());//原币含税单价
  692. iTaxRate = Convert.ToDecimal(dtPODetails.Rows[0]["iPerTaxRate"]);
  693. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["Autoid"].ToString()))//采购退货单字体标识
  694. id = dtPODetails.Rows[0]["Autoid"].ToString();
  695. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["cCode"].ToString()))//采购退货单号
  696. code = dtPODetails.Rows[0]["cCode"].ToString();
  697. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["dDate"].ToString()))//采购退货单日期
  698. dates = dtPODetails.Rows[0]["dDate"].ToString();
  699. sql = @"INSERT INTO dbo.rdrecords01
  700. (
  701. AutoID ,ID ,cInvCode ,iNum ,iQuantity ,
  702. iUnitCost ,iPrice ,iAPrice ,cBatch ,iFlag ,
  703. dSDate ,iSQuantity ,iSNum ,iMoney ,iPOsID ,
  704. fACost ,iNQuantity ,chVencode ,iOriTaxCost ,iOriCost ,
  705. iOriMoney ,iOriTaxPrice ,ioriSum ,iTaxRate ,iTaxPrice ,
  706. iSum ,bTaxCost ,cPOID ,iMatSettleState ,iBillSettleCount ,
  707. bLPUseFree ,iOriTrackID ,cbaccounter ,dbKeepDate ,bCosting ,
  708. iSumBillQuantity ,bVMIUsed ,iExpiratDateCalcu ,isotype ,irowno,
  709. iArrsId,cbarvcode,dbarvdate
  710. )
  711. select
  712. @AutoID,@ID,@cInvCode,0,@iQuantity,
  713. @iUnitCost,@iPrice,@iAPrice,@cBatch,0,
  714. null,0,0,0,a.ID,
  715. @fACost,@iNQuantity,b.cVenCode,@iOriTaxCost,@iOriCost,
  716. @iOriMoney,@iOriTaxPrice,@ioriSum,@iTaxRate,@iTaxPrice,
  717. @iSum,1,@cPOID,0,0,
  718. 0,0,null,null,d.bInCost,
  719. null,0,0,0,@irowno,
  720. @iArrsId,@cbarvcode,@dbarvdate
  721. FROM dbo.PO_Podetails a
  722. INNER JOIN dbo.PO_Pomain b ON a.POID = b.POID
  723. INNER JOIN dbo.PU_ArrivalVouchs c ON a.ID = c.iPOsID
  724. INNER JOIN dbo.Warehouse d ON d.cWhCode = '{0}'
  725. WHERE c.Autoid = '{1}'
  726. ";
  727. sql = string.Format(sql, head.WHCode, body.DNDetailID);
  728. cmd.Parameters.Clear();
  729. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  730. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  731. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  732. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  733. cmd.Parameters.Add(new SqlParameter("@iUnitCost", iUnitPrice));
  734. cmd.Parameters.Add(new SqlParameter("@iPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  735. cmd.Parameters.Add(new SqlParameter("@iAPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  736. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  737. //cmd.Parameters.Add(new SqlParameter("@iSQuantity", 0));
  738. //cmd.Parameters.Add(new SqlParameter("@iMoney", 0));
  739. cmd.Parameters.Add(new SqlParameter("@fACost", iNatUnitPrice));
  740. cmd.Parameters.Add(new SqlParameter("@iNQuantity", body.Quantity));
  741. cmd.Parameters.Add(new SqlParameter("@iOriTaxCost", iTaxPrice));
  742. cmd.Parameters.Add(new SqlParameter("@iOriCost", body.Quantity * iUnitPrice));
  743. cmd.Parameters.Add(new SqlParameter("@iOriMoney", iUnitPrice));
  744. cmd.Parameters.Add(new SqlParameter("@iOriTaxPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  745. cmd.Parameters.Add(new SqlParameter("@ioriSum", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  746. cmd.Parameters.Add(new SqlParameter("@iTaxRate", Math.Round(iTaxPrice * body.Quantity, 2)));
  747. cmd.Parameters.Add(new SqlParameter("@iTaxPrice", iTaxRate));
  748. cmd.Parameters.Add(new SqlParameter("@iSum", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  749. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  750. cmd.Parameters.Add(new SqlParameter("@cPOID", dtPODetails.Rows[0]["cCode"].ToString()));
  751. cmd.Parameters.Add(new SqlParameter("@iArrsId", id));
  752. cmd.Parameters.Add(new SqlParameter("@cbarvcode", code));
  753. cmd.Parameters.Add(new SqlParameter("@dbarvdate", dates));
  754. cmd.CommandText = sql;
  755. try
  756. {
  757. int count = cmd.ExecuteNonQuery();
  758. if (count <= 0)
  759. {
  760. log.Error("生成采购入库单表体失败,受影响行数<=0;");
  761. throw new Exception("生成采购入库单表体失败,受影响行数<=0;");
  762. }
  763. }
  764. catch (Exception ex)
  765. {
  766. log.Error("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  767. throw new Exception("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  768. }
  769. #region 更新[采购到货单子表]PU_ArrivalVouchs 合格品入库数量20211019
  770. sql = @"UPDATE PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+@iQty WHERE Autoid=@Autoid";
  771. cmd.CommandText = sql;
  772. cmd.Parameters.Clear();
  773. cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  774. cmd.Parameters.Add(new SqlParameter("@Autoid", body.DNDetailID));
  775. try
  776. {
  777. log.Info("PU_Autoid:" + body.DNDetailID);
  778. cmd.ExecuteNonQuery();
  779. }
  780. catch (Exception ex)
  781. {
  782. log.Error("回写采购到货单子表合格品入库数量失败:Autoid:" + body.DNDetailID + ";异常:" + ex.Message);
  783. throw new Exception("程序异常,请联系开发人员!");
  784. }
  785. #endregion
  786. #region 更新[采购订单子表]PO_PODetails累计到货数量和累计到货金额
  787. // //sql = @"Update a set iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty,iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney FROM dbo.PO_Podetails a where a.ID='" + body.iPOsID + "'";
  788. // sql = @"
  789. // Update a set
  790. // iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney
  791. // ,freceivedqty=isnull(freceivedqty ,0)+ @iQty
  792. // FROM dbo.PO_Podetails a where a.ID=@ID"
  793. // ;
  794. // //2021-11-05
  795. // //1.移除 iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty 由采购到货,创建到货单CreatePU时写入iReceivedQTY,此处不再回写,累计到货数量
  796. // //2.加入 freceivedqty =isnull(freceivedqty ,0) + @iQty 合格品入库数量
  797. // cmd.CommandText = sql;
  798. // cmd.Parameters.Clear();
  799. // cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  800. // cmd.Parameters.Add(new SqlParameter("@iMoney", body.Quantity * iUnitPrice));
  801. // cmd.Parameters.Add(new SqlParameter("@ID", body.DNDetailID));
  802. // try
  803. // {
  804. // log.Info("iPOsID:" + body.DNDetailID);
  805. // cmd.ExecuteNonQuery();
  806. // }
  807. // catch (Exception ex)
  808. // {
  809. // log.Error("回写采购订单累计到货数量和累计到货金额失败:采购订单表体行号:" + body.DNDetailID + ";异常:" + ex.Message);
  810. // throw new Exception("程序异常,请联系开发人员!");
  811. // }
  812. #endregion
  813. //更新现存量
  814. key.cBustypeUN = "普通采购";
  815. key.cVouchTypeUN = "01";
  816. key.TableName = "IA_ST_UnAccountVouch01";
  817. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, body.InvCode, body.Quantity, key);
  818. iChildId--;
  819. }
  820. ////更新主键ID,DID
  821. //DBHelper.UpdateIDandDID("rd", head.list.Count(), cmd);
  822. }
  823. #endregion
  824. #region 判断现存量是否超入库
  825. sql = @"IF EXISTS(SELECT ID FROM dbo.PO_Podetails WHERE isnull(ireceivedqty,0)+isnull(freceivedqty,0)>iQuantity AND ID>'1000043932')
  826. BEGIN
  827. DECLARE @MSG NVARCHAR(100)
  828. SELECT @MSG='ERP入库数量不能大于订单数量ID'+CAST(ID AS NVARCHAR(100)) FROM dbo.PO_Podetails WHERE isnull(ireceivedqty,0)+isnull(freceivedqty,0)>iQuantity AND ID>'1000043932'
  829. RAISERROR(@MSG,16,1)
  830. END";
  831. cmd.CommandText = sql;
  832. cmd.ExecuteNonQuery();
  833. #endregion
  834. //GetSerialCode(iFatherId.ToString());
  835. iFatherIdTwo += "'" + iFatherId + "',";
  836. }
  837. #region 查询
  838. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as RCVTCode,a.cVenCode,D.cVenName,
  839. a.cWhCode,c.cWhName,cARVCode as DNCode,cMaker as CreateUser,
  840. dnmaketime as CreateDateTime,cHandler as Checker,dVeriDate as CheckDateTime
  841. FROM RdRecord01 a
  842. INNER JOIN Warehouse C ON a.cWhCode=C.cWhCode
  843. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and a.ID in ({0})
  844. select a.ID as IDs, AutoID as DetailID,irowno as Sequence,
  845. cInvCode as InvCode,iQuantity as Quantity,iNum as Amount,iArrsId as DNDetailID
  846. FROM RdRecord01 a
  847. LEFT JOIN rdrecords01 b on a.ID=b.ID
  848. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  849. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and a.ID in ({0}) ";
  850. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  851. log.Debug(sql);
  852. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  853. string RelationName = "details";
  854. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"], false);
  855. ds.Relations.Add(dr);
  856. szJson = DBHelper.DataSetToJson(ds, RelationName);
  857. #endregion
  858. cmd.Transaction.Commit();
  859. return szJson;
  860. }
  861. catch (Exception ex)
  862. {
  863. cmd.Transaction.Rollback();
  864. log.Error(ex.Message);
  865. throw new Exception(ex.Message);
  866. }
  867. finally
  868. {
  869. if (conn.State == ConnectionState.Open)
  870. {
  871. conn.Close();
  872. }
  873. conn.Dispose();
  874. }
  875. }
  876. /// <summary>
  877. /// 审核采购入库单
  878. /// </summary>
  879. /// <param name="infos"></param>
  880. /// <returns></returns>
  881. public string Approve(List<ICSPurchaseReceiveDoc> infos)
  882. {
  883. List<ICSPurchaseReceiveDoc> szJson = new List<ICSPurchaseReceiveDoc>();
  884. DataTable dt = null;
  885. string json = "";
  886. if (infos.Count <= 0)
  887. {
  888. throw new Exception("传送数据为空!");
  889. }
  890. string res = string.Empty;
  891. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  892. conn.Open();
  893. SqlTransaction sqlTran = conn.BeginTransaction();
  894. SqlCommand cmd = new SqlCommand();
  895. cmd.Transaction = sqlTran;
  896. cmd.Connection = conn;
  897. try
  898. {
  899. string sql = string.Empty;
  900. foreach (ICSPurchaseReceiveDoc info in infos)
  901. {
  902. if (info.MTime < new DateTime(2000, 01, 01))
  903. throw new Exception("请输入正确的操作时间:" + info.MTime);
  904. sql = @"UPDATE [{0}].dbo.RdRecord01 SET cVerifier ='" + info.User + @"' ,
  905. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE ID='" + info.ID + "'";
  906. sql = string.Format(sql, ERPDB);
  907. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  908. }
  909. cmd.Transaction.Commit();
  910. return json;
  911. }
  912. catch (Exception ex)
  913. {
  914. cmd.Transaction.Rollback();
  915. log.Error(ex.Message);
  916. throw new Exception(ex.Message);
  917. }
  918. finally
  919. {
  920. if (conn.State == ConnectionState.Open)
  921. {
  922. conn.Close();
  923. }
  924. conn.Dispose();
  925. }
  926. }
  927. /// <summary>
  928. /// 删除采购入库单
  929. /// </summary>
  930. /// <param name="infos"></param>
  931. /// <returns></returns>
  932. public string Delete(List<ICSPurchaseReceiveDoc> infos)
  933. {
  934. List<ICSPurchaseReceiveDoc> szJson = new List<ICSPurchaseReceiveDoc>();
  935. if (infos.Count <= 0)
  936. {
  937. throw new Exception("传送数据为空!");
  938. }
  939. string res = string.Empty;
  940. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  941. conn.Open();
  942. SqlTransaction sqlTran = conn.BeginTransaction();
  943. SqlCommand cmd = new SqlCommand();
  944. cmd.Transaction = sqlTran;
  945. cmd.Connection = conn;
  946. try
  947. {
  948. string sql = string.Empty;
  949. foreach (ICSPurchaseReceiveDoc info in infos)
  950. {
  951. if (info.MTime < new DateTime(2000, 01, 01))
  952. throw new Exception("请输入正确的操作时间:" + info.MTime);
  953. sql = @" DELETE [{1}].dbo.RdRecord01 WHERE ID={0}";
  954. sql = string.Format(sql, info.ID, ERPDB);
  955. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  956. }
  957. cmd.Transaction.Commit();
  958. return res;
  959. ;
  960. }
  961. catch (Exception ex)
  962. {
  963. cmd.Transaction.Rollback();
  964. log.Error(ex.Message);
  965. throw new Exception(ex.Message);
  966. }
  967. finally
  968. {
  969. if (conn.State == ConnectionState.Open)
  970. {
  971. conn.Close();
  972. }
  973. conn.Dispose();
  974. }
  975. }
  976. //public string GetSerialCode(string ID)
  977. //{
  978. // string sql = "EXEC PROC_Update_PO_RdRecord01 '{0}'";
  979. // sql = string.Format(sql, new object[] { ID });
  980. // return DBHelper.ExecuteScalar(CommandType.Text, sql, connString).ToString();
  981. //}
  982. }
  983. }