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

1530 lines
95 KiB

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