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

913 lines
49 KiB

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