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

1311 lines
78 KiB

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