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

1059 lines
64 KiB

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