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

1159 lines
71 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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. if (info.MTime < new DateTime(2000, 01, 01))
  55. throw new Exception("请输入正确的操作时间:" + info.MTime);
  56. sql = @"select a.ID,a.cCode,a.cVenCode,''as cVenName,cWhCode,''as cWhName,cARVCode,cMaker,dnmaketime,cHandler,dVeriDate,
  57. AutoID,irowno,cInvCode,iQuantity,iNum,iUnitCost,iArrsId
  58. FROM RdRecord01 a
  59. LEFT JOIN rdrecords01 b on a.ID=b.ID
  60. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  61. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1";
  62. if (!string.IsNullOrWhiteSpace(info.RCVTCode))
  63. {
  64. sql += " and a.cCode='{0}'";
  65. }
  66. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  67. {
  68. sql += " and ISNULL(a.dnverifytime , ISNULL(a.dnmodifytime , a.dnmaketime ))>='{1}'";
  69. }
  70. if (!string.IsNullOrWhiteSpace(info.User))
  71. {
  72. sql += "and a.CMAKER='{2}'";
  73. }
  74. sql = string.Format(sql, info.RCVTCode, info.MTime, info.User);
  75. dt = DBHelper.SQlReturnData(sql, cmd);
  76. if (dt.Rows.Count <= 0 || dt == null)
  77. throw new Exception("采购入库单:" + info.RCVTCode + ",无信息!");
  78. dtNEW.Merge(dt);
  79. cmd.Transaction.Commit();
  80. }
  81. catch (Exception ex)
  82. {
  83. cmd.Transaction.Rollback();
  84. log.Error(ex.Message);
  85. throw new Exception(ex.Message);
  86. }
  87. finally
  88. {
  89. if (conn.State == ConnectionState.Open)
  90. {
  91. conn.Close();
  92. }
  93. conn.Dispose();
  94. }
  95. }
  96. json = JsonConvert.SerializeObject(dtNEW);
  97. return json;
  98. }
  99. ///<summary>
  100. //创建采购入库单(采购订单)
  101. ///</summary>
  102. ///<param name="Bills"></param>
  103. ///<returns></returns>
  104. public string CreateRdRecord01sByPO(List<ICSPurchaseReceiveDoc> Bills)
  105. {
  106. string szJson = "";
  107. string connS = "";
  108. string sql = "";
  109. DataTable dtNew1 = null;
  110. DataTable dtNew2 = null;
  111. decimal iTaxRate = 0;
  112. DateTime time = DateTime.Now;
  113. int num = 0;
  114. VouchKey key = new VouchKey();
  115. //int iBaseCodeLen = Convert.ToInt32(dic["iBaseCodeLen"].ToString());
  116. //int cVouchCodeBase = Convert.ToInt32(dic["cVouchCodeBase"].ToString());
  117. List<ICSPOArrive> datas = new List<ICSPOArrive>();
  118. if (Bills.Count <= 0)
  119. {
  120. throw new Exception("传送数据为空!");
  121. }
  122. string res = string.Empty;
  123. SqlConnection conn = new SqlConnection();
  124. SqlCommand cmd = new SqlCommand();
  125. foreach (ICSPurchaseReceiveDoc head in Bills)
  126. {
  127. try
  128. {
  129. string iFatherIdTwo = "";
  130. connS = string.Format(connString, head.WorkPoint);
  131. conn = new System.Data.SqlClient.SqlConnection(connS);
  132. conn.Open();
  133. SqlTransaction sqlTran = conn.BeginTransaction();
  134. cmd = new SqlCommand();
  135. cmd.Transaction = sqlTran;
  136. cmd.Connection = conn;
  137. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  138. {
  139. }
  140. else
  141. {
  142. throw new Exception("U8正在整理现存量,请稍后再试");
  143. }
  144. string[] ss = head.WorkPoint.Split('_');
  145. ERPDB = ss[1];
  146. num = head.details.Count();
  147. string[] dd = CRNALL.Split('~');
  148. string crdname = dd[0];
  149. string carname = dd[1];
  150. string surface = dd[2];
  151. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  152. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  153. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  154. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  155. DateTime date = DateTime.Now;
  156. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + time + "", "admin", head.WorkPoint);
  157. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  158. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  159. if (head.MTime < new DateTime(2000, 01, 01))
  160. throw new Exception("请输入正确的操作时间:" + head.MTime);
  161. #region 验证入库数量不能大于采购订单数量
  162. // foreach (ICSPurchaseReceiveDocs body in head.details)
  163. // {
  164. // sql = @"SELECT a.iQuantity,a.ID,isnull(a.iReceivedQTY,0) as iReceivedQTY,isnull(a.freceivedqty,0) as freceivedqty,ISNULL(c.fInExcess,0) AS fInExcess
  165. // FROM dbo.PO_Podetails a
  166. // INNER JOIN dbo.PO_Pomain b ON a.POID=b.POID
  167. // LEFT JOIN PU_ArrivalVouch d ON b.cPOID=d.cpocode
  168. // LEFT JOIN dbo.Inventory c ON a.cInvCode=c.cInvCode
  169. // WHERE d.cCode ='" + head.DNCode + "'";
  170. // cmd.CommandText = sql;
  171. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  172. // if (dtQty != null && dtQty.Rows.Count > 0)
  173. // {
  174. // decimal fInExcess = decimal.Parse(dtQty.Rows[0]["fInExcess"].ToString());//入库超额上限
  175. // decimal PoQty = decimal.Parse(dtQty.Rows[0]["iQuantity"].ToString());
  176. // decimal iReceicedQty = decimal.Parse(dtQty.Rows[0]["iReceivedQTY"].ToString());//累计到货数量
  177. // decimal freceivedqty = decimal.Parse(dtQty.Rows[0]["freceivedqty"].ToString());//合格品入库数量
  178. // PoQty = PoQty * (1 + fInExcess);
  179. // log.Info("PO数量:" + PoQty.ToString()+ "入库超额上限:" + fInExcess.ToString() + ",累计到货数量:" + iReceicedQty.ToString() + ",合格品入库数量:" + freceivedqty.ToString());
  180. // //2021-11-05 回写字段变更,见行408
  181. // //decimal to = PoQty - iReceicedQty - freceivedqty;
  182. // decimal to = PoQty - freceivedqty;
  183. // if (body.Quantity > to)
  184. // {
  185. // throw new Exception("入库数量" + body.Quantity.ToString("f3") + "大于采购订单可入库数量" + to.ToString("f3")
  186. // );
  187. // }
  188. // }
  189. // }
  190. #endregion
  191. #region 采购入库单表头
  192. string BusType = "";//业务类型
  193. sql = @"select a.POID,a.cPOID,a.cVenCode,a.cDepCode,a.cexch_name,a.iTaxRate,a.cMaker,
  194. a.nflat,a.cPersonCode,a.cptcode ,b.ID,b.cCode,a.cBusType
  195. FROM dbo.PO_Pomain a
  196. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode
  197. WHERE a.cPOID ='" + head.POCode + "'";
  198. cmd.CommandText = sql;
  199. DataTable dtPOMain = DBHelper.SQlReturnData(sql, cmd);
  200. if (dtPOMain != null && dtPOMain.Rows.Count > 0)
  201. {
  202. string cSource = "";
  203. string cCode = "";
  204. int bredvouch = 0;
  205. string WHCode = "";
  206. BusType = dtPOMain.Rows[0]["cBusType"].ToString();
  207. //校验采购订单类型是固定资产仓库
  208. if (BusType.Equals("固定资产"))
  209. {
  210. string sqlwh = @"SELECT bWhAsset FROM Warehouse WHERE cWhCode='" + head.WHCode + "'";
  211. cmd.CommandText = sqlwh;
  212. DataTable dtwhcode = DBHelper.SQlReturnData(sqlwh, cmd);
  213. log.Info(dtwhcode.Rows[0]["bWhAsset"].ToString());
  214. if (!dtwhcode.Rows[0]["bWhAsset"].ToString().Equals("True"))
  215. {
  216. throw new Exception("到货单:" + dtPOMain.Rows[0]["cPOID"].ToString() + ",业务类型:固定资产,只能入固定资产仓库!");
  217. }
  218. else
  219. {
  220. WHCode = head.WHCode;
  221. }
  222. }
  223. else
  224. {
  225. WHCode = head.WHCode;
  226. }
  227. cSource = "采购到货单";
  228. cCode = dtPOMain.Rows[0]["cCode"].ToString();
  229. bredvouch = 0;
  230. sql = @"
  231. INSERT INTO dbo.RdRecord01
  232. (ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  233. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode ,
  234. cPersonCode ,cVenCode ,cOrderCode ,cARVCode,
  235. cHandler ,bTransFlag ,cMaker,cDefine1,cDefine2,
  236. dVeriDate ,bpufirst ,biafirst , dARVDate ,VT_ID ,
  237. bIsSTQc ,ipurorderid ,iTaxRate ,iExchRate ,cExch_Name ,
  238. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  239. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,dnverifytime ,
  240. bredvouch ,bCredit ,iPrintCount,ipurarriveid,cPTCode,cVenPUOMProtocol)
  241. SELECT
  242. @ID,1,'01',a.cBusType,@cSource,
  243. @cWhCode,@dDate,@cCode,@cRdCode,@cDepCode,
  244. @cPersonCode,a.cVenCode,a.cPOID,@cARVCode,
  245. @cHandler,0,@cMaker,a.cDefine1,a.cDefine2,
  246. @cVeriDate,0,0,@dARVDate,@VT_ID,
  247. 0,@ipurorderid,a.iTaxRate,a.nflat,a.cexch_name,
  248. 0,0,0,0,0,
  249. 0,0,'',@dnmakertime,@dnverifytime,
  250. @bredvouch,0,0,@ipurarriveid,a.cPTCode,a.cPayCode
  251. FROM dbo.PO_Pomain a
  252. --LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode
  253. WHERE a.cPOID ='" + head.POCode + "'";
  254. cmd.Parameters.Clear();
  255. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  256. cmd.Parameters.Add(new SqlParameter("@cSource", cSource));
  257. cmd.Parameters.Add(new SqlParameter("@cWhCode", WHCode));
  258. cmd.Parameters.Add(new SqlParameter("@dDate", Convert.ToDateTime(DateTime.Now).ToString("yyyy -MM-dd")));
  259. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  260. cmd.Parameters.Add(new SqlParameter("@cDepCode", dtPOMain.Rows[0]["cDepCode"].ToString()));
  261. cmd.Parameters.Add(new SqlParameter("@cPersonCode", dtPOMain.Rows[0]["cPersonCode"].ToString()));
  262. cmd.Parameters.Add(new SqlParameter("@cARVCode", cCode));
  263. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  264. cmd.Parameters.Add(new SqlParameter("@cVeriDate", Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd")));
  265. cmd.Parameters.Add(new SqlParameter("@dARVDate", Convert.ToDateTime(head.MTime)));
  266. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  267. cmd.Parameters.Add(new SqlParameter("@ipurorderid", dtPOMain.Rows[0]["POID"].ToString()));
  268. cmd.Parameters.Add(new SqlParameter("@dnmakertime", Convert.ToDateTime(DateTime.Now).ToString()));
  269. cmd.Parameters.Add(new SqlParameter("@dnverifytime", Convert.ToDateTime(DateTime.Now).ToString()));
  270. cmd.Parameters.Add(new SqlParameter("@bredvouch", bredvouch));
  271. cmd.Parameters.Add(new SqlParameter("@ipurarriveid", dtPOMain.Rows[0]["ID"].ToString()));
  272. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  273. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  274. ;
  275. cmd.CommandText = sql;
  276. try
  277. {
  278. int count = cmd.ExecuteNonQuery();
  279. if (count <= 0)
  280. {
  281. log.Error("生成采购入库单表头失败,受影响行数<=0;");
  282. throw new Exception("生成采购入库单表头失败,受影响行数<=0;");
  283. }
  284. }
  285. catch (Exception ex)
  286. {
  287. log.Error("生成采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  288. throw new Exception("生成采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  289. }
  290. }
  291. else
  292. {
  293. throw new Exception("对应采购订单不存在!采购订单号:" + head.POCode);
  294. }
  295. #endregion
  296. #region 采购入库单表体
  297. foreach (ICSPurchaseReceiveDocs body in head.details)
  298. {
  299. //if (head.IsReturn == "1")
  300. //{
  301. // body.iQuantity = -body.iQuantity;
  302. //}
  303. sql = @"SELECT a.id,a.cInvCode,a.iUnitPrice ,a.iMoney AS 原币无税金额 ,a.ivouchrowno,
  304. a.iTax AS ,a.iSum AS ,a.iNatUnitPrice ,
  305. a.iNatMoney AS ,a.iNatTax AS ,a.iNatSum AS ,
  306. a.iTaxPrice ,a.freceivedqty AS ,a.iPerTaxRate,c.Autoid,d.cCode,d.dDate
  307. FROM dbo.PO_Podetails a
  308. INNER JOIN dbo.PO_Pomain b ON a.POID=b.POID
  309. LEFT JOIN dbo.PU_ArrivalVouchs c ON a.ID=c.iPOsID
  310. LEFT JOIN dbo.PU_ArrivalVouch d ON c.ID=d.ID
  311. WHERE b.cPOID ='" + head.POCode + "'";
  312. cmd.CommandText = sql;
  313. DataTable dtPODetails = DBHelper.SQlReturnData(sql, cmd);
  314. decimal iUnitPrice = 0;
  315. decimal iNatUnitPrice = 0;
  316. decimal iTaxPrice = 0;
  317. string id = "";
  318. string code = "";
  319. string dates = "";
  320. if (dtPODetails != null && dtPODetails.Rows.Count > 0)
  321. {
  322. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iUnitPrice"].ToString()))
  323. iUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iUnitPrice"].ToString());//原币无税单价
  324. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iNatUnitPrice"].ToString()))
  325. iNatUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iNatUnitPrice"].ToString());//本币无税单价
  326. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iTaxPrice"].ToString()))
  327. iTaxPrice = decimal.Parse(dtPODetails.Rows[0]["iTaxPrice"].ToString());//原币含税单价
  328. iTaxRate = Convert.ToDecimal(dtPODetails.Rows[0]["iPerTaxRate"]);
  329. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["Autoid"].ToString()))//采购退货单字体标识
  330. id = dtPODetails.Rows[0]["Autoid"].ToString();
  331. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["cCode"].ToString()))//采购退货单号
  332. code = dtPODetails.Rows[0]["cCode"].ToString();
  333. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["dDate"].ToString()))//采购退货单日期
  334. dates = dtPODetails.Rows[0]["dDate"].ToString();
  335. sql = @"INSERT INTO dbo.rdrecords01
  336. (
  337. AutoID ,ID ,cInvCode ,iNum ,iQuantity ,
  338. iUnitCost ,iPrice ,iAPrice ,cBatch ,iFlag ,
  339. dSDate ,iSQuantity ,iSNum ,iMoney ,iPOsID ,
  340. fACost ,iNQuantity ,chVencode ,iOriTaxCost ,iOriCost ,
  341. iOriMoney ,iOriTaxPrice ,ioriSum ,iTaxRate ,iTaxPrice ,
  342. iSum ,bTaxCost ,cPOID ,iMatSettleState ,iBillSettleCount ,
  343. bLPUseFree ,iOriTrackID ,cbaccounter ,dbKeepDate ,bCosting ,
  344. iSumBillQuantity ,bVMIUsed ,iExpiratDateCalcu ,isotype ,irowno,
  345. iArrsId,cbarvcode,dbarvdate,cFree1,cFree2,cFree3,cFree4,cFree5,
  346. cFree6,cFree7,cFree8,cFree9,cFree10
  347. )
  348. select
  349. @AutoID,@ID1,@cInvCode,0,@iQuantity,
  350. @iUnitCost,@iPrice,@iAPrice,@cBatch,0,
  351. null,0,0,0,a.ID,
  352. @fACost,@iNQuantity,b.cVenCode,@iOriTaxCost,@iOriCost,
  353. @iOriMoney,@iOriTaxPrice,@ioriSum,@iTaxRate,@iTaxPrice,
  354. @iSum,1,@cPOID,0,0,
  355. 0,0,null,null,d.bInCost,
  356. null,0,0,0,@irowno,
  357. @iArrsId,@cbarvcode,@dbarvdate,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,
  358. @cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  359. FROM dbo.PO_Podetails a
  360. INNER JOIN dbo.PO_Pomain b ON a.POID = b.POID
  361. INNER JOIN dbo.Warehouse d ON d.cWhCode = '{0}'
  362. WHERE a.ID ='" + body.PODetailID + @"'
  363. EXEC PROC_Update_PO_RdRecord01 @ID= @ID1
  364. ";
  365. sql = string.Format(sql, head.WHCode);
  366. cmd.Parameters.Clear();
  367. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  368. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  369. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  370. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  371. cmd.Parameters.Add(new SqlParameter("@iUnitCost", iUnitPrice));
  372. cmd.Parameters.Add(new SqlParameter("@iPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  373. cmd.Parameters.Add(new SqlParameter("@iAPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  374. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  375. //cmd.Parameters.Add(new SqlParameter("@iSQuantity", 0));
  376. //cmd.Parameters.Add(new SqlParameter("@iMoney", 0));
  377. cmd.Parameters.Add(new SqlParameter("@fACost", iNatUnitPrice));
  378. cmd.Parameters.Add(new SqlParameter("@iNQuantity", body.Quantity));
  379. cmd.Parameters.Add(new SqlParameter("@iOriTaxCost", iTaxPrice));
  380. cmd.Parameters.Add(new SqlParameter("@iOriCost", body.Quantity * iUnitPrice));
  381. cmd.Parameters.Add(new SqlParameter("@iOriMoney", iUnitPrice));
  382. cmd.Parameters.Add(new SqlParameter("@iOriTaxPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  383. cmd.Parameters.Add(new SqlParameter("@ioriSum", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  384. cmd.Parameters.Add(new SqlParameter("@iTaxRate", Math.Round(iTaxPrice * body.Quantity, 2)));
  385. cmd.Parameters.Add(new SqlParameter("@iTaxPrice", iTaxRate));
  386. cmd.Parameters.Add(new SqlParameter("@iSum", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  387. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  388. cmd.Parameters.Add(new SqlParameter("@cPOID", dtPODetails.Rows[0]["cCode"].ToString()));
  389. cmd.Parameters.Add(new SqlParameter("@iArrsId", id));
  390. cmd.Parameters.Add(new SqlParameter("@cbarvcode", code));
  391. cmd.Parameters.Add(new SqlParameter("@dbarvdate", dates));
  392. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  393. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  394. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  395. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  396. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  397. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  398. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  399. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  400. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  401. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  402. cmd.CommandText = sql;
  403. try
  404. {
  405. int count = cmd.ExecuteNonQuery();
  406. if (count <= 0)
  407. {
  408. log.Error("生成采购入库单表体失败,受影响行数<=0;");
  409. throw new Exception("生成采购入库单表体失败,受影响行数<=0;");
  410. }
  411. }
  412. catch (Exception ex)
  413. {
  414. log.Error("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  415. throw new Exception("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  416. }
  417. #endregion
  418. #region 更新[采购到货单子表]PU_ArrivalVouchs 合格品入库数量20211019
  419. // sql = @"UPDATE PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+@iQty WHERE Autoid=@Autoid";
  420. //cmd.CommandText = sql;
  421. //cmd.Parameters.Clear();
  422. //cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  423. //cmd.Parameters.Add(new SqlParameter("@Autoid", body.DNDetailID));
  424. //try
  425. //{
  426. // log.Info("PU_Autoid:" + body.DNDetailID);
  427. // cmd.ExecuteNonQuery();
  428. //}
  429. //catch (Exception ex)
  430. //{
  431. // log.Error("回写采购到货单子表合格品入库数量失败:Autoid:" + body.DNDetailID + ";异常:" + ex.Message);
  432. // throw new Exception("程序异常,请联系开发人员!");
  433. //}
  434. #endregion
  435. #region 更新[采购订单子表]PO_PODetails累计到货数量和累计到货金额
  436. //sql = @"Update a set iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty,iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney FROM dbo.PO_Podetails a where a.ID='" + body.iPOsID + "'";
  437. sql = @"
  438. Update a set
  439. iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney
  440. ,freceivedqty=isnull(freceivedqty ,0)+ @iQty
  441. FROM dbo.PO_Podetails a where a.ID=@ID"
  442. ;
  443. //2021-11-05
  444. //1.移除 iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty 由采购到货,创建到货单CreatePU时写入iReceivedQTY,此处不再回写,累计到货数量
  445. //2.加入 freceivedqty =isnull(freceivedqty ,0) + @iQty 合格品入库数量
  446. cmd.CommandText = sql;
  447. cmd.Parameters.Clear();
  448. cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  449. cmd.Parameters.Add(new SqlParameter("@iMoney", body.Quantity * iUnitPrice));
  450. cmd.Parameters.Add(new SqlParameter("@ID", body.PODetailID));
  451. try
  452. {
  453. log.Info("iPOsID:" + body.PODetailID);
  454. int i = cmd.ExecuteNonQuery();
  455. }
  456. catch (Exception ex)
  457. {
  458. log.Error("回写采购订单累计到货数量和累计到货金额失败:采购订单表体行号:" + body.DNDetailID + ";异常:" + ex.Message);
  459. throw new Exception("程序异常,请联系开发人员!");
  460. }
  461. #endregion
  462. //string sqls = @"SELECT freceivedqty,iQuantity FROM dbo.PO_Podetails WHERE ID={0} ";
  463. //sqls = string.Format(sqls, body.PODetailID);
  464. //DataTable dtPODetailss = DBHelper.SQlReturnData(sqls, cmd);
  465. #region 判断现存量是否超入库
  466. sql = @"IF EXISTS(SELECT ID FROM dbo.PO_Podetails WHERE isnull(freceivedqty,0)>iQuantity AND ID={0} )
  467. BEGIN
  468. DECLARE @MSG NVARCHAR(100)
  469. SELECT @MSG='ERP入库数量不能大于订单数量ID'+CAST(ID AS NVARCHAR(100)) FROM dbo.PO_Podetails WHERE isnull(freceivedqty,0)>iQuantity AND ID={0}
  470. RAISERROR(@MSG,16,1)
  471. END";
  472. sql = string.Format(sql, body.PODetailID);
  473. cmd.CommandText = sql;
  474. cmd.ExecuteNonQuery();
  475. #endregion
  476. if (head.UpdateStock)
  477. {
  478. //更新现存量
  479. key.cBustypeUN = "普通采购";
  480. key.cVouchTypeUN = "01";
  481. key.TableName = "IA_ST_UnAccountVouch01";
  482. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, body.InvCode, body.Quantity, key);
  483. 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);
  484. }
  485. }
  486. ////更新主键ID,DID
  487. //DBHelper.UpdateIDandDID("rd", head.list.Count(), cmd);
  488. iFatherIdTwo += "'" + iFatherId + "',";
  489. iChildId--;
  490. }
  491. #region 查询
  492. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as RCVTCode,a.cVenCode,D.cVenName,
  493. a.cWhCode,c.cWhName,cARVCode as DNCode,cMaker as CreateUser,
  494. dnmaketime as CreateDateTime,cHandler as Checker,dVeriDate as CheckDateTime
  495. FROM RdRecord01 a
  496. INNER JOIN Warehouse C ON a.cWhCode=C.cWhCode
  497. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and a.ID in ({0})
  498. select a.ID as IDs, AutoID as DetailID,irowno as Sequence,
  499. cInvCode as InvCode,iQuantity as Quantity,iNum as Amount,iPOsID as PODetailID
  500. FROM RdRecord01 a
  501. LEFT JOIN rdrecords01 b on a.ID=b.ID
  502. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  503. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and a.ID in ({0}) ";
  504. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  505. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  506. if (dtNew1 == null)
  507. dtNew1 = ds.Tables[0];
  508. else
  509. dtNew1.Merge(ds.Tables[0]);
  510. if (dtNew2 == null)
  511. dtNew2 = ds.Tables[1];
  512. else
  513. dtNew2.Merge(ds.Tables[1]);
  514. #endregion
  515. cmd.Transaction.Commit();
  516. }
  517. catch (Exception ex)
  518. {
  519. cmd.Transaction.Rollback();
  520. log.Error(ex.Message);
  521. throw new Exception(ex.Message);
  522. }
  523. finally
  524. {
  525. if (conn.State == ConnectionState.Open)
  526. {
  527. conn.Close();
  528. }
  529. conn.Dispose();
  530. }
  531. }
  532. string RelationName = "details";
  533. DataSet dz = new DataSet();
  534. dtNew1.TableName = "Table0";
  535. dtNew2.TableName = "Table1";
  536. dz.Tables.Add(dtNew1.Copy());
  537. dz.Tables.Add(dtNew2.Copy());
  538. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  539. dz.Relations.Add(dh);
  540. szJson = DBHelper.DataSetToJson(dz, RelationName);
  541. return szJson;
  542. }
  543. ///<summary>
  544. //创建采购入库单(到货单)
  545. ///</summary>
  546. ///<param name="Bills"></param>
  547. ///<returns></returns>
  548. public string CreateRdRecord01sByPOArrive(List<ICSPurchaseReceiveDoc> Bills)
  549. {
  550. string szJson = "";
  551. string connS = "";
  552. DataTable dtNew1 = null;
  553. DataTable dtNew2 = null;
  554. string iFatherIdTwo = "";
  555. decimal iTaxRate = 0;
  556. DateTime time = DateTime.Now;
  557. int num = 0;
  558. string sql = "";
  559. VouchKey key = new VouchKey();
  560. //int iBaseCodeLen = Convert.ToInt32(dic["iBaseCodeLen"].ToString());
  561. //int cVouchCodeBase = Convert.ToInt32(dic["cVouchCodeBase"].ToString());
  562. List<ICSPOArrive> datas = new List<ICSPOArrive>();
  563. if (Bills.Count <= 0)
  564. {
  565. throw new Exception("传送数据为空!");
  566. }
  567. string res = string.Empty;
  568. SqlConnection conn = new SqlConnection();
  569. SqlCommand cmd = new SqlCommand();
  570. foreach (ICSPurchaseReceiveDoc head in Bills)
  571. {
  572. try
  573. {
  574. connS = string.Format(connString, head.WorkPoint);
  575. conn = new System.Data.SqlClient.SqlConnection(connS);
  576. conn.Open();
  577. SqlTransaction sqlTran = conn.BeginTransaction();
  578. cmd = new SqlCommand();
  579. cmd.Transaction = sqlTran;
  580. cmd.Connection = conn;
  581. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  582. {
  583. }
  584. else
  585. {
  586. throw new Exception("U8正在整理现存量,请稍后再试");
  587. }
  588. string[] ss = head.WorkPoint.Split('_');
  589. ERPDB = ss[1];
  590. num = head.details.Count;
  591. string[] dd = CRNALL.Split('~');
  592. string crdname = dd[0];
  593. string carname = dd[1];
  594. string surface = dd[2];
  595. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  596. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  597. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  598. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  599. DateTime date = DateTime.Now;
  600. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + time + "", "admin", head.WorkPoint);
  601. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  602. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  603. if (head.MTime < new DateTime(2000, 01, 01))
  604. throw new Exception("请输入正确的操作时间:" + head.MTime);
  605. #region 验证入库数量不能大于采购订单数量
  606. // foreach (ICSPurchaseReceiveDocs body in head.details)
  607. // {
  608. // sql = @"SELECT a.iQuantity,a.ID,isnull(a.iReceivedQTY,0) as iReceivedQTY,isnull(a.freceivedqty,0) as freceivedqty,ISNULL(c.fInExcess,0) AS fInExcess
  609. // FROM dbo.PO_Podetails a
  610. // INNER JOIN dbo.PO_Pomain b ON a.POID=b.POID
  611. // LEFT JOIN PU_ArrivalVouch d ON b.cPOID=d.cpocode
  612. // LEFT JOIN dbo.Inventory c ON a.cInvCode=c.cInvCode
  613. // WHERE d.cCode ='" + head.DNCode + "'";
  614. // cmd.CommandText = sql;
  615. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  616. // if (dtQty != null && dtQty.Rows.Count > 0)
  617. // {
  618. // decimal fInExcess = decimal.Parse(dtQty.Rows[0]["fInExcess"].ToString());//入库超额上限
  619. // decimal PoQty = decimal.Parse(dtQty.Rows[0]["iQuantity"].ToString());
  620. // decimal iReceicedQty = decimal.Parse(dtQty.Rows[0]["iReceivedQTY"].ToString());//累计到货数量
  621. // decimal freceivedqty = decimal.Parse(dtQty.Rows[0]["freceivedqty"].ToString());//合格品入库数量
  622. // PoQty = PoQty * (1 + fInExcess);
  623. // log.Info("PO数量:" + PoQty.ToString()+ "入库超额上限:" + fInExcess.ToString() + ",累计到货数量:" + iReceicedQty.ToString() + ",合格品入库数量:" + freceivedqty.ToString());
  624. // //2021-11-05 回写字段变更,见行408
  625. // //decimal to = PoQty - iReceicedQty - freceivedqty;
  626. // decimal to = PoQty - freceivedqty;
  627. // if (body.Quantity > to)
  628. // {
  629. // throw new Exception("入库数量" + body.Quantity.ToString("f3") + "大于采购订单可入库数量" + to.ToString("f3")
  630. // );
  631. // }
  632. // }
  633. // }
  634. #endregion
  635. #region 采购入库单表头
  636. string BusType = "";//业务类型
  637. sql = @"select a.POID,a.cPOID,a.cVenCode,a.cDepCode,a.cexch_name,a.iTaxRate,a.cMaker,
  638. a.nflat,a.cPersonCode,a.cptcode ,b.ID,b.cCode,a.cBusType
  639. FROM dbo.PO_Pomain a
  640. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode
  641. WHERE b.cCode ='" + head.DNCode + "'";
  642. cmd.CommandText = sql;
  643. DataTable dtPOMain = DBHelper.SQlReturnData(sql, cmd);
  644. if (dtPOMain != null && dtPOMain.Rows.Count > 0)
  645. {
  646. string cSource = "";
  647. string cCode = "";
  648. int bredvouch = 0;
  649. string WHCode = "";
  650. BusType = dtPOMain.Rows[0]["cBusType"].ToString();
  651. //校验采购订单类型是固定资产仓库
  652. if (BusType.Equals("固定资产"))
  653. {
  654. string sqlwh = @"SELECT bWhAsset FROM Warehouse WHERE cWhCode='" + head.WHCode + "'";
  655. cmd.CommandText = sqlwh;
  656. DataTable dtwhcode = DBHelper.SQlReturnData(sqlwh, cmd);
  657. log.Info(dtwhcode.Rows[0]["bWhAsset"].ToString());
  658. if (!dtwhcode.Rows[0]["bWhAsset"].ToString().Equals("True"))
  659. {
  660. throw new Exception("到货单:" + dtPOMain.Rows[0]["cPOID"].ToString() + ",业务类型:固定资产,只能入固定资产仓库!");
  661. }
  662. else
  663. {
  664. WHCode = head.WHCode;
  665. }
  666. }
  667. else
  668. {
  669. WHCode = head.WHCode;
  670. }
  671. cSource = "采购到货单";
  672. cCode = dtPOMain.Rows[0]["cCode"].ToString();
  673. bredvouch = 0;
  674. sql = @"
  675. INSERT INTO dbo.RdRecord01
  676. (ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  677. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode ,
  678. cPersonCode ,cVenCode ,cOrderCode ,cARVCode,
  679. cHandler ,bTransFlag ,cMaker,cDefine1,cDefine2,
  680. dVeriDate ,bpufirst ,biafirst , dARVDate ,VT_ID ,
  681. bIsSTQc ,ipurorderid ,iTaxRate ,iExchRate ,cExch_Name ,
  682. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  683. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,dnverifytime ,
  684. bredvouch ,bCredit ,iPrintCount,ipurarriveid,cPTCode,cVenPUOMProtocol)
  685. SELECT
  686. @ID,1,'01',a.cBusType,@cSource,
  687. @cWhCode,@dDate,@cCode,@cRdCode,@cDepCode,
  688. @cPersonCode,a.cVenCode,a.cPOID,@cARVCode,
  689. @cHandler,0,@cMaker,a.cDefine1,a.cDefine2,
  690. @cVeriDate,0,0,@dARVDate,@VT_ID,
  691. 0,@ipurorderid,a.iTaxRate,a.nflat,a.cexch_name,
  692. 0,0,0,0,0,
  693. 0,0,'',@dnmakertime,@dnverifytime,
  694. @bredvouch,0,0,@ipurarriveid,a.cPTCode,a.cPayCode
  695. FROM dbo.PO_Pomain a
  696. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode
  697. WHERE b.cCode ='" + head.DNCode + "'";
  698. cmd.Parameters.Clear();
  699. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  700. cmd.Parameters.Add(new SqlParameter("@cSource", cSource));
  701. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  702. cmd.Parameters.Add(new SqlParameter("@dDate", Convert.ToDateTime(DateTime.Now).ToString("yyyy -MM-dd")));
  703. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  704. cmd.Parameters.Add(new SqlParameter("@cDepCode", dtPOMain.Rows[0]["cDepCode"].ToString()));
  705. cmd.Parameters.Add(new SqlParameter("@cPersonCode", dtPOMain.Rows[0]["cPersonCode"].ToString()));
  706. cmd.Parameters.Add(new SqlParameter("@cARVCode", cCode));
  707. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  708. cmd.Parameters.Add(new SqlParameter("@cVeriDate", Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd")));
  709. cmd.Parameters.Add(new SqlParameter("@dARVDate", Convert.ToDateTime(head.MTime)));
  710. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  711. cmd.Parameters.Add(new SqlParameter("@ipurorderid", dtPOMain.Rows[0]["POID"].ToString()));
  712. cmd.Parameters.Add(new SqlParameter("@dnmakertime", Convert.ToDateTime(DateTime.Now).ToString()));
  713. cmd.Parameters.Add(new SqlParameter("@dnverifytime", Convert.ToDateTime(DateTime.Now).ToString()));
  714. cmd.Parameters.Add(new SqlParameter("@bredvouch", bredvouch));
  715. cmd.Parameters.Add(new SqlParameter("@ipurarriveid", dtPOMain.Rows[0]["ID"].ToString()));
  716. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  717. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  718. ;
  719. cmd.CommandText = sql;
  720. try
  721. {
  722. int count = cmd.ExecuteNonQuery();
  723. if (count <= 0)
  724. {
  725. log.Error("生成采购入库单表头失败,受影响行数<=0;");
  726. throw new Exception("生成采购入库单表头失败,受影响行数<=0;");
  727. }
  728. }
  729. catch (Exception ex)
  730. {
  731. log.Error("生成采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  732. throw new Exception("生成采购入库单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  733. }
  734. }
  735. else
  736. {
  737. throw new Exception("对应采购订单不存在!采购订单号:" + head.DNCode);
  738. }
  739. #endregion
  740. #region 采购入库单表体
  741. foreach (ICSPurchaseReceiveDocs body in head.details)
  742. {
  743. sql = @"SELECT a.id,a.cInvCode,a.iUnitPrice ,a.iMoney AS 原币无税金额 ,a.ivouchrowno,
  744. a.iTax AS ,a.iSum AS ,a.iNatUnitPrice ,
  745. a.iNatMoney AS ,a.iNatTax AS ,a.iNatSum AS ,
  746. a.iTaxPrice ,a.freceivedqty AS ,a.iPerTaxRate,c.Autoid,d.cCode,d.dDate
  747. FROM dbo.PO_Podetails a
  748. INNER JOIN dbo.PO_Pomain b ON a.POID=b.POID
  749. LEFT JOIN dbo.PU_ArrivalVouchs c ON a.ID=c.iPOsID
  750. LEFT JOIN dbo.PU_ArrivalVouch d ON c.ID=d.ID
  751. where c.AutoID='" + body.DNDetailID + "'";
  752. cmd.CommandText = sql;
  753. DataTable dtPODetails = DBHelper.SQlReturnData(sql, cmd);
  754. decimal iUnitPrice = 0;
  755. decimal iNatUnitPrice = 0;
  756. decimal iTaxPrice = 0;
  757. string id = "";
  758. string code = "";
  759. string dates = "";
  760. if (dtPODetails != null && dtPODetails.Rows.Count > 0)
  761. {
  762. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iUnitPrice"].ToString()))
  763. iUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iUnitPrice"].ToString());//原币无税单价
  764. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iNatUnitPrice"].ToString()))
  765. iNatUnitPrice = decimal.Parse(dtPODetails.Rows[0]["iNatUnitPrice"].ToString());//本币无税单价
  766. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["iTaxPrice"].ToString()))
  767. iTaxPrice = decimal.Parse(dtPODetails.Rows[0]["iTaxPrice"].ToString());//原币含税单价
  768. iTaxRate = Convert.ToDecimal(dtPODetails.Rows[0]["iPerTaxRate"]);
  769. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["Autoid"].ToString()))//采购退货单字体标识
  770. id = dtPODetails.Rows[0]["Autoid"].ToString();
  771. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["cCode"].ToString()))//采购退货单号
  772. code = dtPODetails.Rows[0]["cCode"].ToString();
  773. if (!string.IsNullOrEmpty(dtPODetails.Rows[0]["dDate"].ToString()))//采购退货单日期
  774. dates = dtPODetails.Rows[0]["dDate"].ToString();
  775. sql = @"INSERT INTO dbo.rdrecords01
  776. (
  777. AutoID ,ID ,cInvCode ,iNum ,iQuantity ,
  778. iUnitCost ,iPrice ,iAPrice ,cBatch ,iFlag ,
  779. dSDate ,iSQuantity ,iSNum ,iMoney ,iPOsID ,
  780. fACost ,iNQuantity ,chVencode ,iOriTaxCost ,iOriCost ,
  781. iOriMoney ,iOriTaxPrice ,ioriSum ,iTaxRate ,iTaxPrice ,
  782. iSum ,bTaxCost ,cPOID ,iMatSettleState ,iBillSettleCount ,
  783. bLPUseFree ,iOriTrackID ,cbaccounter ,dbKeepDate ,bCosting ,
  784. iSumBillQuantity ,bVMIUsed ,iExpiratDateCalcu ,isotype ,irowno,
  785. iArrsId,cbarvcode,dbarvdate,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
  786. )
  787. select
  788. @AutoID,@ID1,@cInvCode,0,@iQuantity,
  789. @iUnitCost,@iPrice,@iAPrice,@cBatch,0,
  790. null,0,0,0,a.ID,
  791. @fACost,@iNQuantity,b.cVenCode,@iOriTaxCost,@iOriCost,
  792. @iOriMoney,@iOriTaxPrice,@ioriSum,@iTaxRate,@iTaxPrice,
  793. @iSum,1,@cPOID,0,0,
  794. 0,0,null,null,d.bInCost,
  795. null,0,0,0,@irowno,
  796. @iArrsId,@cbarvcode,@dbarvdate,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  797. FROM dbo.PO_Podetails a
  798. INNER JOIN dbo.PO_Pomain b ON a.POID = b.POID
  799. INNER JOIN dbo.PU_ArrivalVouchs c ON a.ID = c.iPOsID
  800. INNER JOIN dbo.Warehouse d ON d.cWhCode = '{0}'
  801. WHERE c.Autoid = '{1}'
  802. EXEC PROC_Update_PO_RdRecord01 @ID= @ID1
  803. ";
  804. sql = string.Format(sql, head.WHCode, body.DNDetailID);
  805. cmd.Parameters.Clear();
  806. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  807. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  808. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  809. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  810. cmd.Parameters.Add(new SqlParameter("@iUnitCost", iUnitPrice));
  811. cmd.Parameters.Add(new SqlParameter("@iPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  812. cmd.Parameters.Add(new SqlParameter("@iAPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  813. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  814. //cmd.Parameters.Add(new SqlParameter("@iSQuantity", 0));
  815. //cmd.Parameters.Add(new SqlParameter("@iMoney", 0));
  816. cmd.Parameters.Add(new SqlParameter("@fACost", iNatUnitPrice));
  817. cmd.Parameters.Add(new SqlParameter("@iNQuantity", body.Quantity));
  818. cmd.Parameters.Add(new SqlParameter("@iOriTaxCost", iTaxPrice));
  819. cmd.Parameters.Add(new SqlParameter("@iOriCost", body.Quantity * iUnitPrice));
  820. cmd.Parameters.Add(new SqlParameter("@iOriMoney", iUnitPrice));
  821. cmd.Parameters.Add(new SqlParameter("@iOriTaxPrice", Math.Round(body.Quantity * iUnitPrice, 2)));
  822. cmd.Parameters.Add(new SqlParameter("@ioriSum", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  823. cmd.Parameters.Add(new SqlParameter("@iTaxRate", Math.Round(iTaxPrice * body.Quantity, 2)));
  824. cmd.Parameters.Add(new SqlParameter("@iTaxPrice", iTaxRate));
  825. cmd.Parameters.Add(new SqlParameter("@iSum", Math.Round(Math.Round(iTaxPrice * body.Quantity, 2) - Math.Round(body.Quantity * iUnitPrice, 2), 2)));
  826. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  827. cmd.Parameters.Add(new SqlParameter("@cPOID", dtPODetails.Rows[0]["cCode"].ToString()));
  828. cmd.Parameters.Add(new SqlParameter("@iArrsId", id));
  829. cmd.Parameters.Add(new SqlParameter("@cbarvcode", code));
  830. cmd.Parameters.Add(new SqlParameter("@dbarvdate", dates));
  831. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  832. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  833. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  834. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  835. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  836. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  837. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  838. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  839. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  840. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  841. cmd.CommandText = sql;
  842. try
  843. {
  844. int count = cmd.ExecuteNonQuery();
  845. if (count <= 0)
  846. {
  847. log.Error("生成采购入库单表体失败,受影响行数<=0;");
  848. throw new Exception("生成采购入库单表体失败,受影响行数<=0;");
  849. }
  850. }
  851. catch (Exception ex)
  852. {
  853. log.Error("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  854. throw new Exception("生成采购入库单表体失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  855. }
  856. #region 更新[采购到货单子表]PU_ArrivalVouchs 合格品入库数量20211019
  857. sql = @"UPDATE PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+@iQty WHERE Autoid=@Autoid";
  858. cmd.CommandText = sql;
  859. cmd.Parameters.Clear();
  860. cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  861. cmd.Parameters.Add(new SqlParameter("@Autoid", body.DNDetailID));
  862. try
  863. {
  864. log.Info("PU_Autoid:" + body.DNDetailID);
  865. cmd.ExecuteNonQuery();
  866. }
  867. catch (Exception ex)
  868. {
  869. log.Error("回写采购到货单子表合格品入库数量失败:单据号:" + head.DNCode + ",行号:"+body.Sequence+";异常:" + ex.Message);
  870. throw new Exception("程序异常,请联系开发人员!");
  871. }
  872. #endregion
  873. #region 更新[采购订单子表]PO_PODetails累计到货数量和累计到货金额
  874. // //sql = @"Update a set iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty,iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney FROM dbo.PO_Podetails a where a.ID='" + body.iPOsID + "'";
  875. // sql = @"
  876. // Update a set
  877. // iReceivedMoney=isnull(iReceivedMoney,0)+@iMoney
  878. // ,freceivedqty=isnull(freceivedqty ,0)+ @iQty
  879. // FROM dbo.PO_Podetails a where a.ID=@ID"
  880. // ;
  881. // //2021-11-05
  882. // //1.移除 iReceivedQTY =isnull(iReceivedQTY ,0)+ @iQty 由采购到货,创建到货单CreatePU时写入iReceivedQTY,此处不再回写,累计到货数量
  883. // //2.加入 freceivedqty =isnull(freceivedqty ,0) + @iQty 合格品入库数量
  884. // cmd.CommandText = sql;
  885. // cmd.Parameters.Clear();
  886. // cmd.Parameters.Add(new SqlParameter("@iQty", body.Quantity));
  887. // cmd.Parameters.Add(new SqlParameter("@iMoney", body.Quantity * iUnitPrice));
  888. // cmd.Parameters.Add(new SqlParameter("@ID", body.DNDetailID));
  889. // try
  890. // {
  891. // log.Info("iPOsID:" + body.DNDetailID);
  892. // cmd.ExecuteNonQuery();
  893. // }
  894. // catch (Exception ex)
  895. // {
  896. // log.Error("回写采购订单累计到货数量和累计到货金额失败:采购订单表体行号:" + body.DNDetailID + ";异常:" + ex.Message);
  897. // throw new Exception("程序异常,请联系开发人员!");
  898. // }
  899. #endregion
  900. if (head.UpdateStock)
  901. {
  902. //更新现存量
  903. key.cBustypeUN = "普通采购";
  904. key.cVouchTypeUN = "01";
  905. key.TableName = "IA_ST_UnAccountVouch01";
  906. DBHelper.UpdateCurrentStockNEW(cmd, body.InvCode, head.WHCode, body.InvCode, body.Quantity, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, key);
  907. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, body.InvCode, body.Quantity, key);
  908. }
  909. iFatherIdTwo += "'" + iFatherId + "',";
  910. iChildId--;
  911. }
  912. ////更新主键ID,DID
  913. //DBHelper.UpdateIDandDID("rd", head.list.Count(), cmd);
  914. }
  915. #endregion
  916. #region 判断现存量是否超入库
  917. sql = @"IF EXISTS(SELECT ID FROM dbo.PO_Podetails WHERE isnull(ireceivedqty,0)+isnull(freceivedqty,0)>iQuantity AND ID>'1000043932')
  918. BEGIN
  919. DECLARE @MSG NVARCHAR(100)
  920. SELECT @MSG='ERP入库数量不能大于订单数量ID'+CAST(ID AS NVARCHAR(100)) FROM dbo.PO_Podetails WHERE isnull(ireceivedqty,0)+isnull(freceivedqty,0)>iQuantity AND ID>'1000043932'
  921. RAISERROR(@MSG,16,1)
  922. END";
  923. cmd.CommandText = sql;
  924. cmd.ExecuteNonQuery();
  925. #endregion
  926. //GetSerialCode(iFatherId.ToString());
  927. #region 查询
  928. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as RCVTCode,a.cVenCode,D.cVenName,
  929. a.cWhCode,c.cWhName,cARVCode as DNCode,cMaker as CreateUser,
  930. dnmaketime as CreateDateTime,cHandler as Checker,dVeriDate as CheckDateTime
  931. FROM RdRecord01 a
  932. INNER JOIN Warehouse C ON a.cWhCode=C.cWhCode
  933. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and a.ID in ({0})
  934. select a.ID as IDs, AutoID as DetailID,irowno as Sequence,
  935. cInvCode as InvCode,iQuantity as Quantity,iNum as Amount,iArrsId as DNDetailID
  936. FROM RdRecord01 a
  937. LEFT JOIN rdrecords01 b on a.ID=b.ID
  938. INNER JOIN DEPARTMENT C ON A.CDEPCODE=C.CDEPCODE
  939. INNER JOIN VENDOR D ON A.CVENCODE=D.CVENCODE WHERE 1=1 and a.ID in ({0}) ";
  940. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  941. log.Debug(sql);
  942. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  943. if (dtNew1 == null)
  944. dtNew1 = ds.Tables[0];
  945. else
  946. dtNew1.Merge(ds.Tables[0]);
  947. if (dtNew2 == null)
  948. dtNew2 = ds.Tables[1];
  949. else
  950. dtNew2.Merge(ds.Tables[1]);
  951. #endregion
  952. cmd.Transaction.Commit();
  953. }
  954. catch (Exception ex)
  955. {
  956. cmd.Transaction.Rollback();
  957. log.Error(ex.Message);
  958. throw new Exception(ex.Message);
  959. }
  960. finally
  961. {
  962. if (conn.State == ConnectionState.Open)
  963. {
  964. conn.Close();
  965. }
  966. conn.Dispose();
  967. }
  968. }
  969. string RelationName = "details";
  970. DataSet dz = new DataSet();
  971. dtNew1.TableName = "Table0";
  972. dtNew2.TableName = "Table1";
  973. dz.Tables.Add(dtNew1.Copy());
  974. dz.Tables.Add(dtNew2.Copy());
  975. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  976. dz.Relations.Add(dh);
  977. szJson = DBHelper.DataSetToJson(dz, RelationName);
  978. return szJson;
  979. }
  980. /// <summary>
  981. /// 审核采购入库单
  982. /// </summary>
  983. /// <param name="infos"></param>
  984. /// <returns></returns>
  985. public string Approve(List<ICSPurchaseReceiveDoc> infos)
  986. {
  987. List<ICSPurchaseReceiveDoc> szJson = new List<ICSPurchaseReceiveDoc>();
  988. string json = "";
  989. string connS = "";
  990. if (infos.Count <= 0)
  991. {
  992. throw new Exception("传送数据为空!");
  993. }
  994. string res = string.Empty;
  995. SqlConnection conn = new SqlConnection();
  996. SqlCommand cmd = new SqlCommand();
  997. string sql = string.Empty;
  998. foreach (ICSPurchaseReceiveDoc info in infos)
  999. {
  1000. try
  1001. {
  1002. connS = string.Format(connString, info.WorkPoint);
  1003. conn = new System.Data.SqlClient.SqlConnection(connS);
  1004. conn.Open();
  1005. SqlTransaction sqlTran = conn.BeginTransaction();
  1006. cmd = new SqlCommand();
  1007. cmd.Transaction = sqlTran;
  1008. cmd.Connection = conn;
  1009. if (info.MTime < new DateTime(2000, 01, 01))
  1010. throw new Exception("请输入正确的操作时间:" + info.MTime);
  1011. sql = @"UPDATE [{0}].dbo.RdRecord01 SET cVerifier ='" + info.User + @"' ,
  1012. cAuditTime=CONVERT(VARCHAR(50),GETDATE(),112),cAuditDate=GETDATE() WHERE ID='" + info.ID + "'";
  1013. sql = string.Format(sql, ERPDB);
  1014. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  1015. cmd.Transaction.Commit();
  1016. }
  1017. catch (Exception ex)
  1018. {
  1019. cmd.Transaction.Rollback();
  1020. log.Error(ex.Message);
  1021. throw new Exception(ex.Message);
  1022. }
  1023. finally
  1024. {
  1025. if (conn.State == ConnectionState.Open)
  1026. {
  1027. conn.Close();
  1028. }
  1029. conn.Dispose();
  1030. }
  1031. }
  1032. return json;
  1033. }
  1034. /// <summary>
  1035. /// 删除采购入库单
  1036. /// </summary>
  1037. /// <param name="infos"></param>
  1038. /// <returns></returns>
  1039. public string Delete(List<ICSPurchaseReceiveDoc> infos)
  1040. {
  1041. List<ICSPurchaseReceiveDoc> szJson = new List<ICSPurchaseReceiveDoc>();
  1042. if (infos.Count <= 0)
  1043. {
  1044. throw new Exception("传送数据为空!");
  1045. }
  1046. string res = string.Empty;
  1047. string connS = "";
  1048. SqlConnection conn = new SqlConnection();
  1049. SqlCommand cmd = new SqlCommand();
  1050. string sql = string.Empty;
  1051. foreach (ICSPurchaseReceiveDoc info in infos)
  1052. {
  1053. try
  1054. {
  1055. connS = string.Format(connString, info.WorkPoint);
  1056. conn = new System.Data.SqlClient.SqlConnection(connS);
  1057. conn.Open();
  1058. SqlTransaction sqlTran = conn.BeginTransaction();
  1059. cmd = new SqlCommand();
  1060. cmd.Transaction = sqlTran;
  1061. cmd.Connection = conn;
  1062. if (info.MTime < new DateTime(2000, 01, 01))
  1063. throw new Exception("请输入正确的操作时间:" + info.MTime);
  1064. sql = @" DELETE [{1}].dbo.RdRecord01 WHERE ID={0}";
  1065. sql = string.Format(sql, info.ID, ERPDB);
  1066. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  1067. cmd.Transaction.Commit();
  1068. }
  1069. catch (Exception ex)
  1070. {
  1071. cmd.Transaction.Rollback();
  1072. log.Error(ex.Message);
  1073. throw new Exception(ex.Message);
  1074. }
  1075. finally
  1076. {
  1077. if (conn.State == ConnectionState.Open)
  1078. {
  1079. conn.Close();
  1080. }
  1081. conn.Dispose();
  1082. }
  1083. }
  1084. return res;
  1085. }
  1086. //public string GetSerialCode(string ID)
  1087. //{
  1088. // string sql = "EXEC PROC_Update_PO_RdRecord01 '{0}'";
  1089. // sql = string.Format(sql, new object[] { ID });
  1090. // return DBHelper.ExecuteScalar(CommandType.Text, sql, connString).ToString();
  1091. //}
  1092. }
  1093. }