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

921 lines
50 KiB

2 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 委外采购红字入库单
  15. /// </summary>
  16. public class OutsourcingReceiveDocNegative
  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["OutIssRCRDNAME"];
  23. /// <summary>
  24. /// 获取委外采购红字入库单
  25. /// </summary>
  26. /// <param name="infos"></param>
  27. /// <returns></returns>
  28. public string Get(List<ICSOutsourcingReceiveDocNegative> infos)
  29. {
  30. List<ICSOutsourcingReceiveDocNegative> szJson = new List<ICSOutsourcingReceiveDocNegative>();
  31. DataTable dt = null;
  32. DataTable dtNew = null;
  33. string connS = "";
  34. string json = "";
  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. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  44. foreach (string WorkPoint in result)
  45. {
  46. try
  47. {
  48. connS = string.Format(connString, WorkPoint);
  49. conn = new System.Data.SqlClient.SqlConnection(connS);
  50. conn.Open();
  51. SqlTransaction sqlTran = conn.BeginTransaction();
  52. cmd = new SqlCommand();
  53. cmd.Transaction = sqlTran;
  54. cmd.Connection = conn;
  55. foreach (ICSOutsourcingReceiveDocNegative info in infos)
  56. {
  57. if (WorkPoint != info.WorkPoint)
  58. continue;
  59. ICSUserInfo userInfo = new ICSUserInfo();
  60. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  61. if (info.MTime < new DateTime(2000, 01, 01))
  62. throw new Exception("请输入正确的操作时间:" + info.MTime);
  63. sql = @" select a.ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  64. d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  65. b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  66. from rdrecord01 a
  67. left join rdrecords01 b on a.ID=b.ID
  68. left join Department c on a.cDepCode=c.cDepCode
  69. left join Warehouse d on a.cWhCode=d.cWhCode
  70. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  71. where a.cBusType=''and b.iQuantity<0";
  72. if (!string.IsNullOrWhiteSpace(info.ORCVNEGCode))
  73. {
  74. sql += " and a.cCode='{0}'";
  75. }
  76. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  77. {
  78. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  79. }
  80. if (!string.IsNullOrWhiteSpace(info.User))
  81. {
  82. sql += "and a.CMAKER='{2}'";
  83. }
  84. sql = string.Format(sql, info.ORCVNEGCode, info.MTime, userInfo.UserName);
  85. dt = DBHelper.SQlReturnData(sql, cmd);
  86. if (dt.Rows.Count <= 0 || dt == null)
  87. throw new Exception("委外采购红字入库单:" + info.ORCVNEGCode + ",无信息!");
  88. if (dtNew == null)
  89. dtNew = dt;
  90. else
  91. dtNew.Merge(dt);
  92. cmd.CommandType = CommandType.Text;
  93. cmd.Parameters.Clear();
  94. }
  95. cmd.Transaction.Commit();
  96. }
  97. catch (Exception ex)
  98. {
  99. if (cmd.Transaction != null)
  100. cmd.Transaction.Rollback();
  101. log.Error(ex.Message);
  102. throw new Exception(ex.Message);
  103. }
  104. finally
  105. {
  106. if (conn.State == ConnectionState.Open)
  107. {
  108. conn.Close();
  109. }
  110. conn.Dispose();
  111. }
  112. }
  113. json = JsonConvert.SerializeObject(dtNew);
  114. return json;
  115. }
  116. /// <summary>
  117. /// 创建委外采购入(到货单)
  118. /// </summary>
  119. /// <param name="Bills"></param>
  120. /// <returns></returns>
  121. public string CreateOutsourcingReceiveDocNegative(List<ICSOutsourcingReceiveDocNegative> Bills)
  122. {
  123. string msg = "";
  124. int num = 0;
  125. string sql = "";
  126. DataSet ds = null;
  127. string connS = "";
  128. int iFatherId = 0;
  129. int iChildId = 0;
  130. string iFatherIdTwo = "";
  131. //取得采购入库单单据 表头ID,表体DID
  132. VouchKey key = new VouchKey();
  133. SqlConnection conn = new SqlConnection();
  134. SqlCommand cmd = new SqlCommand();
  135. if (Bills.Count <= 0)
  136. {
  137. throw new Exception("传送数据为空!");
  138. }
  139. LogInfo(Bills);
  140. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  141. foreach (string WorkPoint in result)
  142. {
  143. try
  144. {
  145. connS = string.Format(connString, WorkPoint);
  146. conn = new System.Data.SqlClient.SqlConnection(connS);
  147. conn.Open();
  148. SqlTransaction sqlTran = conn.BeginTransaction();
  149. cmd = new SqlCommand();
  150. cmd.Transaction = sqlTran;
  151. cmd.Connection = conn;
  152. foreach (ICSOutsourcingReceiveDocNegative head in Bills)
  153. {
  154. iFatherIdTwo = "";
  155. num = head.details.Count();
  156. if (WorkPoint != head.WorkPoint)
  157. continue;
  158. string[] ss = head.WorkPoint.Split('_');
  159. ERPDB = ss[1];
  160. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  161. throw new Exception("U8正在整理现存量,请稍后再试");
  162. //取得out库单的默认显示模版
  163. ICSUserInfo userInfo = new ICSUserInfo();
  164. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  165. string[] dd = CRNALL.Split('~');
  166. string crdname = dd[0];
  167. string carname = dd[1];
  168. string surface = dd[2];
  169. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  170. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint, cmd);
  171. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  172. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  173. DateTime date = DateTime.Now;
  174. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  175. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint, cmd);
  176. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  177. #region 验证数量不能大于到货单数量
  178. foreach (ICSOutsourcingReceiveDocNegatives body in head.details)
  179. {
  180. sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNRTDetailID + "'";
  181. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  182. if (dtQty != null && dtQty.Rows.Count > 0)
  183. {
  184. //到货单可入数量
  185. decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  186. sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNRTDetailID + "'";
  187. DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  188. decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  189. if (ReceQty + body.Quantity > PUQty)
  190. {
  191. throw new Exception("入库数量大于当前到货单行可入库数量!");
  192. }
  193. }
  194. else
  195. {
  196. throw new Exception("U8不存在该到货单行!");
  197. }
  198. }
  199. #endregion
  200. string cfcode = string.Empty;
  201. string zq = string.Empty;
  202. string lz = string.Empty;
  203. string dz = string.Empty;
  204. sql = @"select a.cCode,a.iLZYJ,a.iLZFS,iPLNum,dblZQNum,c.dDate,
  205. (case when ilzfs=1 then dateadd(day,-day(getdate()),dateadd(month,1,getdate())) else DATEADD(day,isnull(iPLNum,0),c.dDate) end)
  206. lz,DATEADD(day,isnull(dblZQNum,0),DATEADD(day,isnull(iPLNum,0),c.dDate)) dz
  207. from
  208. (select b.cVenPUOMProtocol,a.cvencode,a.dDate,a.cCode from PU_ArrivalVouch a
  209. left join Vendor b on a.cvencode=b.cvencode
  210. where a.cCode='" + head.ODNRTCode + @"'
  211. ) c
  212. left join AA_Agreement a on a.cCode=c.cVenPUOMProtocol
  213. ";
  214. DataTable dtDisCheck1 = DBHelper.SQlReturnData(sql, cmd);
  215. if (dtDisCheck1 != null && dtDisCheck1.Rows.Count > 0)
  216. {
  217. foreach (DataRow item in dtDisCheck1.Rows)
  218. {
  219. if (item["iLZYJ"].ToString() == "10" & (item["iLZFS"].ToString() == "0" | item["iLZFS"].ToString() == "1"))
  220. {
  221. cfcode = item["cCode"].ToString();
  222. zq = item["dblZQNum"].ToString();
  223. lz = item["lz"].ToString();
  224. dz = item["dz"].ToString();
  225. }
  226. else
  227. {
  228. if (item["cCode"].ToString() == "")
  229. cfcode = item["cCode"].ToString();
  230. }
  231. }
  232. }
  233. #region 委外采购入库单 表头
  234. sql = @"INSERT INTO dbo.RdRecord01
  235. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  236. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  237. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  238. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  239. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  240. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  241. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  242. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  243. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  244. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  245. ireturncount ,iverifystate ,iswfcontrolled ,
  246. cModifyPerson ,dnmaketime ,dnverifytime ,
  247. bredvouch ,bCredit ,iPrintCount,cVenPUOMProtocol,iCreditPeriod,dCreditStart,dGatheringDate)
  248. SELECT @ID,1,'01','','',@cWhCode,
  249. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,@cDepCode,a.cPersonCode,'01',
  250. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  251. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  252. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  253. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  254. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  255. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  256. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  257. 0,0,0,
  258. 0,0,0,
  259. '',GETDATE(),GETDATE(),
  260. 0,0,0,@cVenPUOMProtocol,@iCreditPeriod,@dCreditStart,@dGatheringDate
  261. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode='" + head.ODNRTCode + "'";
  262. cmd.Parameters.Clear();
  263. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  264. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  265. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  266. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  267. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  268. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  269. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  270. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  271. cmd.Parameters.Add(new SqlParameter("@cVenPUOMProtocol", cfcode));
  272. cmd.Parameters.Add(new SqlParameter("@iCreditPeriod", zq));
  273. cmd.Parameters.Add(new SqlParameter("@dCreditStart", lz));
  274. cmd.Parameters.Add(new SqlParameter("@dGatheringDate", dz));
  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("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  288. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  289. }
  290. #endregion
  291. #region 采购入库单 表体
  292. foreach (ICSOutsourcingReceiveDocNegatives body in head.details)
  293. {
  294. body.Quantity = -body.Quantity;
  295. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  296. {
  297. if (body.BatchCode == "")
  298. {
  299. log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  300. throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  301. }
  302. }
  303. else
  304. {
  305. if (body.BatchCode != "")
  306. {
  307. log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  308. throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  309. }
  310. }
  311. sql = @"INSERT INTO dbo.rdrecords01
  312. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,iNum,
  313. iFlag ,iSQuantity ,iSNum ,iMoney ,cbMemo,
  314. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  315. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  316. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  317. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  318. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  319. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost ,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  320. SELECT @AutoID,@ID1,@cInvCode,@iQuantity,0,0,0,@iNum,
  321. 0,0,0,0,a.cbMemo,
  322. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  323. a.cItem_class,@cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  324. c.cVenCode,a.Autoid,0,0,0,0,
  325. 0,a.iTaxRate,0,0,1,d.cCode,0,
  326. 0,0,0,1,c.cCode,
  327. c.dDate,0,0,0,@irowno,0,@cBatch,a.iCost*convert(decimal, @iQuantity),a.iCost,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  328. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  329. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  330. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNRTDetailID + @"'
  331. EXEC ICSProcedureUpdatePriceOMToRdRecord01 @ID= @ID1
  332. ";
  333. cmd.Parameters.Clear();
  334. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  335. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  336. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  337. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  338. cmd.Parameters.Add(new SqlParameter("@iNum", -Convert.ToDecimal(body.Amount.ToString("0.00"))));
  339. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  340. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  341. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  342. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  343. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  344. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  345. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  346. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  347. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  348. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  349. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  350. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  351. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  352. cmd.CommandText = sql;
  353. try
  354. {
  355. int count = cmd.ExecuteNonQuery();
  356. if (count <= 0)
  357. {
  358. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  359. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  360. }
  361. }
  362. catch (Exception ex)
  363. {
  364. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  365. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  366. }
  367. //OM_MODetails
  368. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + -body.Quantity + @"
  369. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  370. WHERE a.Autoid='" + body.ODNRTDetailID + "'";
  371. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  372. //回写PU_ArrivalVouchs失败
  373. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + -body.Quantity + " WHERE Autoid='" + body.ODNRTDetailID + "'";
  374. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  375. 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
  376. LEFT JOIN (select sum(iNum) NiNum,iArrsId from rdrecords01 GROUP BY iArrsId) b on a.Autoid=b.iArrsId
  377. where a.AutoID='" + body.ODNRTDetailID + "'";
  378. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  379. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  380. {
  381. foreach (DataRow item in dtDisCheck.Rows)
  382. {
  383. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  384. {
  385. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  386. {
  387. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  388. {
  389. sql = @"Update rdrecords09 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  390. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  391. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  392. }
  393. }
  394. }
  395. }
  396. }
  397. ////更新待入库数量
  398. //sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + -body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  399. //if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  400. //{
  401. // if (!string.IsNullOrWhiteSpace(body.BatchCode))
  402. // {
  403. // sql += " and cBatch='" + body.BatchCode + "'";
  404. // }
  405. //}
  406. //else
  407. //{
  408. // sql += " and cBatch=''";
  409. //}
  410. //DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  411. //更新现存量
  412. key.cBustypeUN = "委外加工";
  413. key.cVouchTypeUN = "01";
  414. key.TableName = "IA_ST_UnAccountVouch01";
  415. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  416. //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);
  417. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, -body.Amount, body.cFree1,
  418. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  419. "", key, "", "", "", "", 0, 0, head.UpdateTodoQuantity ? (-1 * Convert.ToDecimal(body.Quantity)) : 0, head.UpdateTodoQuantity ? (-1 * Convert.ToDecimal(body.Amount)) : 0, 0, 0, 0, 0, iFatherId, iChildId);
  420. iFatherIdTwo = "'" + iFatherId + "',";
  421. iChildId--;
  422. }
  423. #endregion
  424. #region 查询
  425. sql = @" select DISTINCT a.ID as ID,a.ID as IDs,a.cCode as ORCVNEGCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  426. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  427. from rdrecord01 a
  428. left join Department c on a.cDepCode=c.cDepCode
  429. left join Vendor d on a.cVenCode=d.cVenCode
  430. where a.cBusType='' AND A.ID in ({0})
  431. select DISTINCT a.ID as IDs,b.AutoID as DetailID,b.iArrsId as ODNRTDetailID, b.irowno as Sequence,b.cInvCode as InvCode,b.iQuantity as Quantity,iNum as Amount,b.iOMoDID as OODetailID
  432. ,isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  433. isnull(b.cFree1,'') as cFree1,
  434. isnull(b.cFree2,'') as cFree2,
  435. isnull(b.cFree3,'') as cFree3,
  436. isnull(b.cFree4,'') as cFree4,
  437. isnull(b.cFree5,'') as cFree5,
  438. isnull(b.cFree6,'') as cFree6,
  439. isnull(b.cFree7,'') as cFree7,
  440. isnull(b.cFree8,'') as cFree8,
  441. isnull(b.cFree9,'') as cFree9,
  442. isnull(b.cFree10,'') as cFree10
  443. from rdrecord01 a
  444. left join rdrecords01 b on a.ID=b.ID
  445. left join Department c on a.cDepCode=c.cDepCode
  446. left join Vendor d on a.cVenCode=d.cVenCode
  447. where a.cBusType='' AND A.ID in ({0}) ";
  448. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  449. if (ds != null)
  450. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  451. else
  452. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  453. #endregion
  454. cmd.CommandType = CommandType.Text;
  455. cmd.Parameters.Clear();
  456. }
  457. cmd.Transaction.Commit();
  458. }
  459. catch (Exception ex)
  460. {
  461. if (cmd.Transaction != null)
  462. cmd.Transaction.Rollback();
  463. log.Error(ex.Message);
  464. throw new Exception(ex.Message);
  465. }
  466. finally
  467. {
  468. if (conn.State == ConnectionState.Open)
  469. {
  470. conn.Close();
  471. }
  472. conn.Dispose();
  473. }
  474. }
  475. msg = JSON.DataSetToJson(ds, "details", "IDs");
  476. return msg;
  477. }
  478. /// <summary>
  479. /// 创建委外采购入(委外订单)
  480. /// </summary>
  481. /// <param name="Bills"></param>
  482. /// <returns></returns>
  483. // public string CreateOutsourcingReceiveDocByPO(List<ICSOutsourcingReceiveDoc> Bills)
  484. // {
  485. // string msg = "";
  486. // int num = 0;
  487. // //取得out库单的默认显示模版
  488. // Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  489. // int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  490. // int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  491. // DateTime date = DateTime.Now;
  492. // string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  493. // //取得采购入库单单据 表头ID,表体DID
  494. // VouchKey key = new VouchKey();
  495. // foreach (ICSOutsourcingReceiveDoc head in Bills)
  496. // {
  497. // num = head.ICSoutReceDoc.Count();
  498. // }
  499. // SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  500. // conn.Open();
  501. // SqlTransaction sqlTran = conn.BeginTransaction();
  502. // SqlCommand cmd = new SqlCommand();
  503. // cmd.Transaction = sqlTran;
  504. // cmd.Connection = conn;
  505. // try
  506. // {
  507. // if (Bills.Count <= 0)
  508. // {
  509. // throw new Exception("传送数据为空!");
  510. // }
  511. // LogInfo(Bills);
  512. // foreach (ICSOutsourcingReceiveDoc head in Bills)
  513. // {
  514. // string sql = "";
  515. // #region 验证数量不能大于到货单数量
  516. // foreach (ICSOutsourcingReceiveDocs body in head.ICSoutReceDoc)
  517. // {
  518. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  519. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  520. // if (dtQty != null && dtQty.Rows.Count > 0)
  521. // {
  522. // //到货单可入数量
  523. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  524. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  525. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  526. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  527. // if (ReceQty + body.Quantity > PUQty)
  528. // {
  529. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  530. // }
  531. // }
  532. // else
  533. // {
  534. // throw new Exception("U8不存在该到货单行!");
  535. // }
  536. // }
  537. // #endregion
  538. // #region 委外采购入库单 表头
  539. // sql = @"INSERT INTO dbo.RdRecord01
  540. // ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  541. // dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  542. // cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  543. // bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  544. // cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  545. // cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  546. // dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  547. // cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  548. // ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  549. // bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  550. // ireturncount ,iverifystate ,iswfcontrolled ,
  551. // cModifyPerson ,dnmaketime ,dnverifytime ,
  552. // bredvouch ,bCredit ,iPrintCount)
  553. // SELECT @ID,1,'01','委外加工','委外到货单',@cWhCode,
  554. // CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'101',@cDepCode,a.cPersonCode,'01',
  555. // a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  556. // 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  557. // a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  558. // a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  559. // a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  560. // a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  561. // a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  562. // 0,0,0,
  563. // 0,0,0,
  564. // '',GETDATE(),GETDATE(),
  565. // 0,0,0
  566. // FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE b.cCode='" + head.ODNCode + "'";
  567. // cmd.Parameters.Clear();
  568. // cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  569. // cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  570. // cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  571. // cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  572. // cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  573. // cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  574. // cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  575. // cmd.CommandText = sql;
  576. // try
  577. // {
  578. // int count = cmd.ExecuteNonQuery();
  579. // if (count <= 0)
  580. // {
  581. // log.Error("委外采购入库单表头失败,受影响行数<=0;");
  582. // throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  583. // }
  584. // }
  585. // catch (Exception ex)
  586. // {
  587. // log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  588. // throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  589. // }
  590. // #endregion
  591. // #region 采购入库单 表体
  592. // int irowno = 0;
  593. // foreach (ICSOutsourcingReceiveDocs body in head.ICSoutReceDoc)
  594. // {
  595. // sql = @"INSERT INTO dbo.rdrecords01
  596. // ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,
  597. // iFlag ,iSQuantity ,iSNum ,iMoney ,
  598. // cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  599. // cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  600. // chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  601. // ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  602. // iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  603. // dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost )
  604. // SELECT @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,
  605. // 0,0,0,0,
  606. // a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  607. // a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  608. // c.cVenCode,a.Autoid,0,0,0,0,
  609. // 0,a.iTaxRate,0,0,1,d.cCode,0,
  610. // 0,0,0,1,c.cCode,
  611. // c.dDate,0,0,0,@irowno,0,@cBatch,a.iCost*convert(decimal, @iQuantity),a.iCost
  612. // FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  613. // INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  614. // INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where b.Autoid='" + body.ODNDetailID + "'";
  615. // cmd.Parameters.Clear();
  616. // cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  617. // cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  618. // cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  619. // cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  620. // cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  621. // cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  622. // cmd.CommandText = sql;
  623. // try
  624. // {
  625. // int count = cmd.ExecuteNonQuery();
  626. // if (count <= 0)
  627. // {
  628. // log.Error("委外采购入库单表头失败,受影响行数<=0;");
  629. // throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  630. // }
  631. // }
  632. // catch (Exception ex)
  633. // {
  634. // log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  635. // throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  636. // }
  637. // //更新现存量
  638. // key.cBustypeUN = "委外加工";
  639. // key.cVouchTypeUN = "01";
  640. // key.TableName = "IA_ST_UnAccountVouch01";
  641. // DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  642. // //OM_MODetails
  643. // sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  644. // FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  645. // WHERE a.Autoid='" + body.ODNDetailID + "'";
  646. // DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  647. // //回写PU_ArrivalVouchs失败
  648. // sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  649. // DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  650. // //更新待入库数量
  651. // sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  652. // if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  653. // {
  654. // //if (!string.IsNullOrWhiteSpace(body.cBatch))
  655. // //{
  656. // // sql += " and cBatch='" + body.cBatch + "'";
  657. // //}
  658. // }
  659. // DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  660. // #region 判断现存量是否足够
  661. // sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0)
  662. // BEGIN
  663. // DECLARE @MSG NVARCHAR(100)
  664. // SELECT @MSG='ERP待入库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0
  665. // RAISERROR(@MSG,16,1)
  666. // END";
  667. // cmd.CommandText = sql;
  668. // cmd.ExecuteNonQuery();
  669. // #endregion
  670. // }
  671. // #endregion
  672. // }
  673. // GetSerialCode(iFatherId.ToString());
  674. // cmd.Transaction.Commit();
  675. // return msg;
  676. // }
  677. // catch (Exception ex)
  678. // {
  679. // cmd.Transaction.Rollback();
  680. // log.Error(ex.Message);
  681. // throw new Exception(ex.Message);
  682. // }
  683. // finally
  684. // {
  685. // if (conn.State == ConnectionState.Open)
  686. // {
  687. // conn.Close();
  688. // }
  689. // conn.Dispose();
  690. // }
  691. // }
  692. /// <summary>
  693. /// 审核委外采购入库
  694. /// </summary>
  695. /// <param name="infos"></param>
  696. /// <returns></returns>
  697. public string Approve(List<ICSOutsourcingReceiveDocNegative> infos)
  698. {
  699. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  700. string json = "";
  701. string connS = "";
  702. if (infos.Count <= 0)
  703. {
  704. throw new Exception("传送数据为空!");
  705. }
  706. string res = string.Empty;
  707. SqlConnection conn = new SqlConnection();
  708. SqlCommand cmd = new SqlCommand();
  709. string sql = string.Empty;
  710. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  711. foreach (string WorkPoint in result)
  712. {
  713. try
  714. {
  715. connS = string.Format(connString, WorkPoint);
  716. conn = new System.Data.SqlClient.SqlConnection(connS);
  717. conn.Open();
  718. SqlTransaction sqlTran = conn.BeginTransaction();
  719. cmd = new SqlCommand();
  720. cmd.Transaction = sqlTran;
  721. cmd.Connection = conn;
  722. foreach (ICSOutsourcingReceiveDocNegative info in infos)
  723. {
  724. if (WorkPoint != info.WorkPoint)
  725. continue;
  726. ICSUserInfo userInfo = new ICSUserInfo();
  727. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  728. if (info.MTime < new DateTime(2000, 01, 01))
  729. throw new Exception("请输入正确的操作时间:" + info.MTime);
  730. sql = @"UPDATE RdRecord01 SET cHandler ='" + userInfo.UserName + @"' ,
  731. dnverifytime=CONVERT(VARCHAR(50),GETDATE(),112),dVeriDate=GETDATE() WHERE ID='{0}'";
  732. sql = string.Format(sql, info.ID);
  733. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核委外采购入库单失败!");
  734. cmd.CommandType = CommandType.Text;
  735. cmd.Parameters.Clear();
  736. }
  737. cmd.Transaction.Commit();
  738. }
  739. catch (Exception ex)
  740. {
  741. if (cmd.Transaction != null)
  742. cmd.Transaction.Rollback();
  743. log.Error(ex.Message);
  744. throw new Exception(ex.Message);
  745. }
  746. finally
  747. {
  748. if (conn.State == ConnectionState.Open)
  749. {
  750. conn.Close();
  751. }
  752. conn.Dispose();
  753. }
  754. }
  755. return json;
  756. }
  757. /// <summary>
  758. /// 删除委外采购入库单
  759. /// </summary>
  760. /// <param name="infos"></param>
  761. /// <returns></returns>
  762. public string Delete(List<ICSOutsourcingReceiveDocNegative> infos)
  763. {
  764. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  765. if (infos.Count <= 0)
  766. {
  767. throw new Exception("传送数据为空!");
  768. }
  769. string connS = "";
  770. string res = string.Empty;
  771. SqlConnection conn = new SqlConnection();
  772. SqlCommand cmd = new SqlCommand();
  773. string sql = string.Empty;
  774. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  775. foreach (string WorkPoint in result)
  776. {
  777. try
  778. {
  779. connS = string.Format(connString, WorkPoint);
  780. conn = new System.Data.SqlClient.SqlConnection(connS);
  781. conn.Open();
  782. SqlTransaction sqlTran = conn.BeginTransaction();
  783. cmd = new SqlCommand();
  784. cmd.Transaction = sqlTran;
  785. cmd.Connection = conn;
  786. foreach (ICSOutsourcingReceiveDocNegative info in infos)
  787. {
  788. if (WorkPoint != info.WorkPoint)
  789. continue;
  790. if (info.MTime < new DateTime(2000, 01, 01))
  791. throw new Exception("请输入正确的操作时间:" + info.MTime);
  792. sql = @"delete RdRecord01 where RdRecord01.ID='" + info.ID + "'";
  793. sql += @"delete RdRecords01 where RdRecords01.ID='" + info.ID + "'";
  794. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  795. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  796. // WHERE a.ID='" + info.ID + "'";
  797. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除委外采购入库单失败!");
  798. cmd.CommandType = CommandType.Text;
  799. cmd.Parameters.Clear();
  800. }
  801. cmd.Transaction.Commit();
  802. }
  803. catch (Exception ex)
  804. {
  805. if (cmd.Transaction != null)
  806. cmd.Transaction.Rollback();
  807. log.Error(ex.Message);
  808. throw new Exception(ex.Message);
  809. }
  810. finally
  811. {
  812. if (conn.State == ConnectionState.Open)
  813. {
  814. conn.Close();
  815. }
  816. conn.Dispose();
  817. }
  818. }
  819. return res;
  820. }
  821. /// <summary>
  822. ///
  823. /// 记录日志
  824. /// </summary>
  825. /// <param name="Bills"></param>
  826. private void LogInfo(List<ICSOutsourcingReceiveDocNegative> Bills)
  827. {
  828. string HeadList = string.Empty;
  829. string BodyList = string.Empty;
  830. foreach (ICSOutsourcingReceiveDocNegative head in Bills)
  831. {
  832. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  833. foreach (ICSOutsourcingReceiveDocNegatives body in head.details)
  834. {
  835. BodyList += "\r\n 表体主键ID: " + body.ODNRTDetailID + ",数量:" + body.Quantity;
  836. }
  837. }
  838. log.Info(HeadList);
  839. log.Info(BodyList);
  840. }
  841. public string GetSerialCode(string ID, string connS)
  842. {
  843. string sql = "EXEC Addins_GetSerialCode '{0}'";
  844. sql = string.Format(sql, new object[] { ID });
  845. return DBHelper.ExecuteScalar(CommandType.Text, sql, connS).ToString();
  846. }
  847. }
  848. }