纽威

1531 lines
95 KiB

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