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

1104 lines
68 KiB

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