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

1573 lines
98 KiB

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