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

1547 lines
96 KiB

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