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

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