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

1540 lines
96 KiB

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