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

1065 lines
65 KiB

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