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

596 lines
30 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 销售出库红字
  15. /// </summary>
  16. public class SalesShipmentDocNegative
  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["SaleSCRDNAME"];
  23. string bustype = string.Empty;
  24. /// <summary>
  25. ///获取销售出库单红字
  26. /// </summary>
  27. /// <param name="infos"></param>
  28. /// <returns></returns>
  29. public string Get(List<ICSSalesShipmentDocNegative> infos)
  30. {
  31. List<ICSSalesShipmentDocNegative> szJson = new List<ICSSalesShipmentDocNegative>();
  32. DataTable dt = null;
  33. DataTable dtNew = null;
  34. string connS = "";
  35. string json = "";
  36. if (infos.Count <= 0)
  37. {
  38. throw new Exception("传送数据为空!");
  39. }
  40. string res = string.Empty;
  41. SqlConnection conn = new SqlConnection();
  42. SqlCommand cmd = new SqlCommand();
  43. string sql = string.Empty;
  44. foreach (ICSSalesShipmentDocNegative info in infos)
  45. {
  46. try
  47. {
  48. connS = string.Format(connString, info.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. if (info.MTime < new DateTime(2000, 01, 01))
  56. throw new Exception("请输入正确的操作时间:" + info.MTime);
  57. sql = @"select a.ID,a.cCode,a.cCusCode,c.cCusName,a.cWhCode,d.cWhName,e.cordercode ,a.cMaker ,a.dnmaketime ,a.cHandler ,a.dnverifytime ,
  58. b.AutoID,b.iRSRowNO ,b.cInvCode ,b.iQuantity ,b.iNum,e.AutoID
  59. from rdrecord32 a
  60. inner join rdrecords32 b on a.ID=b.ID
  61. left join DispatchLists e on a.cDLCode=e.DLID
  62. left join Customer c on a.cCusCode=c.cCusCode
  63. left join Warehouse d on a.cWhCode=d.cWhCode WHERE 1=1 and b.iquantity<0 ";
  64. if (!string.IsNullOrWhiteSpace(info.SDNNEGCode))
  65. {
  66. sql += " and a.cCode='{0}'";
  67. }
  68. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  69. {
  70. sql += " and ISNULL(a.dnmodifytime ,ISNULL(a.dnverifytime , ISNULL(a.dnmodifytime , a.dnmaketime )))>='{1}'";
  71. }
  72. if (!string.IsNullOrWhiteSpace(info.User))
  73. {
  74. sql += "and a.CMAKER='{2}'";
  75. }
  76. sql = string.Format(sql, info.SDNNEGCode, info.MTime, info.User);
  77. dt = DBHelper.SQlReturnData(sql, cmd);
  78. if (dt.Rows.Count <= 0 || dt == null)
  79. throw new Exception("红字销售出库单号:" + info.SDNNEGCode + ",无信息!");
  80. if (dtNew == null)
  81. dtNew = dt;
  82. else
  83. dtNew.Merge(dt);
  84. cmd.Transaction.Commit();
  85. }
  86. catch (Exception ex)
  87. {
  88. cmd.Transaction.Rollback();
  89. log.Error(ex.Message);
  90. throw new Exception(ex.Message);
  91. }
  92. finally
  93. {
  94. if (conn.State == ConnectionState.Open)
  95. {
  96. conn.Close();
  97. }
  98. conn.Dispose();
  99. }
  100. }
  101. json = JsonConvert.SerializeObject(dtNew);
  102. return json;
  103. }
  104. /// <summary>
  105. /// 创建销售出库单红字
  106. /// </summary>
  107. /// <param name="Bills"></param>
  108. /// <returns></returns>
  109. public string CreateSalesShipmentDocNegative(List<ICSSalesShipmentDocNegative> Bills)
  110. {
  111. string sql = "";
  112. string msg = "";
  113. DataTable dtNew1 = null;
  114. DataTable dtNew2 = null;
  115. string connS = "";
  116. string status = "";
  117. int iFatherId = 0;
  118. int iChildId = 0;
  119. string iFatherIdTwo = "";
  120. int num = 0;
  121. //bool ResultFlag = false;
  122. SqlConnection conn = new SqlConnection();
  123. SqlCommand cmd = new SqlCommand();
  124. VouchKey key = new VouchKey();
  125. if (Bills.Count <= 0)
  126. {
  127. throw new Exception("传送数据为空!");
  128. }
  129. LogInfo(Bills);
  130. foreach (ICSSalesShipmentDocNegative head in Bills)
  131. {
  132. try
  133. {
  134. connS = string.Format(connString, head.WorkPoint);
  135. conn = new System.Data.SqlClient.SqlConnection(connS);
  136. conn.Open();
  137. SqlTransaction sqlTran = conn.BeginTransaction();
  138. cmd = new SqlCommand();
  139. cmd.Transaction = sqlTran;
  140. cmd.Connection = conn;
  141. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  142. {
  143. }
  144. else
  145. {
  146. throw new Exception("U8正在整理现存量,请稍后再试");
  147. }
  148. string[] ss = head.WorkPoint.Split('_');
  149. ERPDB = ss[1];
  150. #region 检查 发货单状态
  151. sql = @"SELECT a.DLID,a.cBusType,c.cRdCode,a.cSTCode,b.AutoID,b.irowno,a.cPersonCode,a.cCusCode,b.iDLsID,
  152. a.cMemo,a.cShipAddress,a.cDepCode,cCusInvName,b.cBatch,a.caddcode,a.cCloser,a.cVerifier FROM dbo.DispatchList a INNER JOIN
  153. dbo.DispatchLists b ON a.DLID=b.DLID
  154. LEFT JOIN dbo.SaleType c ON c.cSTCode=a.cSTCode
  155. WHERE a.cDLCode ='" + head.SDNRTCode + "'";
  156. cmd.CommandText = sql;
  157. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  158. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  159. {
  160. foreach (DataRow item in dtDisCheck.Rows)
  161. {
  162. if (!string.IsNullOrEmpty(item["cCloser"].ToString()))
  163. {
  164. log.Info("DLID:" + head.ID);
  165. throw new Exception("ERP发货单已关闭,无法发货!发货单号:" + head.SDNNEGCode);
  166. }
  167. }
  168. bustype = dtDisCheck.Rows[0]["cBusType"].ToString();
  169. }
  170. else
  171. {
  172. throw new Exception("销售发货单单号:" + head.ID + "在ERP内不存在!");
  173. }
  174. #endregion
  175. #region 回写发货单字段
  176. sql = @"select (case when cvalue='TRUE' THEN 'SA' ELSE 'ST' END) status FROM AccInformation WHERE cSysID='ST' AND cName='BSAcreat'";
  177. cmd.CommandText = sql;
  178. dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  179. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  180. {
  181. status = dtDisCheck.Rows[0]["status"].ToString();
  182. }
  183. else
  184. {
  185. status = "";
  186. }
  187. #endregion
  188. #region 检验发货单数量
  189. foreach (ICSSalesShipmentDocNegatives body in head.details)
  190. {
  191. sql = "SELECT isnull(fOutQuantity,0) as fOutQuantity,ABS(iQuantity) as iQuantity FROM dbo.DispatchLists WHERE AutoID='" + body.SDNRTDetailID + "'";
  192. DataTable dtfOutQty = DBHelper.SQlReturnData(sql, cmd);
  193. if (dtfOutQty != null && dtfOutQty.Rows.Count > 0)
  194. {
  195. decimal fOutQty = Convert.ToDecimal(dtfOutQty.Rows[0]["fOutQuantity"]);
  196. decimal iQuantity = Convert.ToDecimal(dtfOutQty.Rows[0]["iQuantity"]);
  197. if (fOutQty + body.Quantity > iQuantity)
  198. {
  199. throw new Exception("可出库数量超过发货单表体出库数量!");
  200. }
  201. }
  202. else
  203. {
  204. throw new Exception("发货单表体不存在!");
  205. }
  206. }
  207. #endregion
  208. #region 红字销售出库单表头
  209. num = head.details.Count();
  210. DateTime time = DateTime.Now;
  211. string[] dd = CRNALL.Split('~');
  212. string crdname = dd[0];
  213. string carname = dd[1];
  214. string surface = dd[2];
  215. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  216. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  217. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  218. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  219. DateTime date = DateTime.Now;
  220. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  221. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + time + "", "admin", head.WorkPoint);
  222. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  223. sql = @"INSERT INTO dbo.rdrecord32
  224. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cBusCode ,cWhCode ,
  225. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cSTCode ,cCusCode ,
  226. cDLCode ,cHandler ,cMemo ,bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,
  227. cDefine3 ,cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,cDefine9 ,
  228. cDefine10 ,dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,
  229. cDefine12 ,cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,cShipAddress ,
  230. caddcode ,bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  231. ireturncount ,iverifystate ,iswfcontrolled ,dnmaketime ,dnverifytime ,
  232. iPrintCount ,cinvoicecompany)
  233. SELECT @ID,0,32,a.cBusType,'',a.cDLCode,@cWhCode,
  234. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,a.cDepCode,a.cPersonCode,a.cSTCode,a.cCusCode,
  235. a.DLID,@cHandler,a.cMemo,0,@cMaker,a.cDefine1,a.cDefine2,
  236. a.cDefine3,a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,a.cDefine9,
  237. a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,a.cDefine11,
  238. a.cDefine12,a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,a.cShipAddress,
  239. a.caddcode,null,0,0,0,
  240. 0,0,0,GETDATE(),GETDATE(),
  241. 0,a.cinvoicecompany
  242. FROM dbo.DispatchList a WHERE a.cDLCode ='" + head.SDNRTCode + "'";
  243. cmd.Parameters.Clear();
  244. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  245. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  246. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  247. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  248. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  249. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  250. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  251. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  252. //cmd.Parameters.Add(new SqlParameter("@dDate", data.dMate));
  253. cmd.CommandText = sql;
  254. try
  255. {
  256. int count = cmd.ExecuteNonQuery();
  257. if (count <= 0)
  258. {
  259. log.Error("生成红字销售出库单表头失败,受影响行数<=0;");
  260. throw new Exception("生成红字销售出库单表头失败,受影响行数<=0;");
  261. }
  262. }
  263. catch (Exception ex)
  264. {
  265. log.Error("生成红字销售出库单表头失败!销售出库单号:" + head.SDNNEGCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  266. throw new Exception("生成红字销售出库单表头失败!销售出库单号:" + head.SDNNEGCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  267. }
  268. #endregion
  269. #region 红字销售出库单表体
  270. foreach (ICSSalesShipmentDocNegatives body in head.details)
  271. {
  272. //iChildId -= 1;
  273. ////if (head.IsReturn == "1")
  274. ////{
  275. //body.Quantity = -body.Quantity;
  276. //}
  277. //else
  278. //{
  279. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  280. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch='" + "" + "'and cWhCode='" + head.WHCode + "'";
  281. //DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  282. //if (dtItem != null && dtItem.Rows.Count > 0)
  283. //{
  284. // //if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  285. // //{
  286. // // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  287. // //}
  288. // if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.Quantity)
  289. // {
  290. // throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.InvCode);
  291. // }
  292. //}
  293. //else
  294. //{
  295. // throw new Exception("物料:" + body.InvCode + "在现存量表中不存在!");
  296. //}
  297. //}
  298. sql = @"INSERT INTO dbo.rdrecords32
  299. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,
  300. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,
  301. cDefine27 ,cItem_class ,cItemCode ,iDLsID ,iNQuantity ,
  302. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  303. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  304. bLPUseFree ,iRSRowNO ,iOriTrackID ,ccusinvcode,ccusinvname,bCosting ,bVMIUsed ,
  305. cbdlcode ,iExpiratDateCalcu ,iorderdid ,iordertype ,
  306. iordercode ,iorderseq ,ipesodid ,ipesotype ,cpesocode ,
  307. ipesoseq ,isodid ,isotype ,csocode ,isoseq ,irowno ,
  308. bIAcreatebill ,bsaleoutcreatebill ,isaleoutid ,bneedbill,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  309. SELECT @AutoID,@ID,@cInvCode,@iQuantity,@cBatch,0,
  310. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,
  311. a.cDefine27,a.cItem_class,a.cItemCode,a.iDLsID,a.iQuantity,
  312. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  313. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  314. 0,0,0,a.cCusInvCode,a.cCusInvName,1,0,
  315. b.cDLCode,0,a.iSOsID,1,
  316. d.cSOCode,c.iRowNo,a.iSOsID,1,d.cSOCode,
  317. 1,a.iSOsID,1,d.cSOCode,c.iRowNo,@irowno,
  318. 1,1,@AutoID,1,null,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  319. FROM dbo.DispatchLists a
  320. INNER JOIN dbo.DispatchList b ON a.DLID=b.DLID
  321. LEFT JOIN dbo.SO_SODetails c ON a.iSOsID=c.iSOsID
  322. LEFT JOIN dbo.SO_SOMain d ON c.ID=d.ID WHERE a.AutoID='" + body.SDNRTDetailID + "'";
  323. cmd.Parameters.Clear();
  324. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  325. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  326. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  327. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  328. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  329. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  330. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  331. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  332. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  333. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  334. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  335. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  336. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  337. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  338. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  339. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  340. cmd.CommandText = sql;
  341. try
  342. {
  343. int count = cmd.ExecuteNonQuery();
  344. if (count <= 0)
  345. {
  346. log.Error("生成红字销售出库单表体失败,受影响行数<=0;");
  347. throw new Exception("生成红字销售出库单表体失败,受影响行数<=0;");
  348. }
  349. }
  350. catch (Exception ex)
  351. {
  352. log.Error("生成红字销售出库单表体失败!销售出库单号:" + head.SDNNEGCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  353. throw new Exception("生成红字销售出库单表体失败!销售出库单号:" + head.SDNNEGCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  354. }
  355. if (head.UpdateStock)
  356. {
  357. #region 更新现存量 现存量表待出库数量增加
  358. key.cBustypeUN = bustype;
  359. key.cVouchTypeUN = "32";
  360. key.TableName = "IA_ST_UnAccountVouch32";
  361. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  362. 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);
  363. #endregion
  364. }
  365. log.Debug(sql);
  366. #region 回写销售订单累计发货数量,回写销售发货单累计出库数量
  367. sql = @"UPDATE a SET a.foutquantity=ISNULL(a.foutquantity,0)+" + -body.Quantity + @"
  368. FROM dbo.SO_SODetails a LEFT JOIN dbo.DispatchLists b ON a.iSOsID=b.iSOsID
  369. WHERE b.AutoID='" + body.SDNRTDetailID + "'";
  370. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售订单累计发货数量失败!");
  371. sql = @"Update DispatchList set cSaleOut='" + status + "' where DLID=(select DLID from DispatchLists where AutoID='" + body.SDNRTDetailID + @"')
  372. Update DispatchLists set fOutQuantity=isnull(fOutQuantity,0)+" + -body.Quantity + " where AutoID='" + body.SDNRTDetailID + "' ";
  373. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售发货单累计出库数量失败!");
  374. iFatherIdTwo += "'" + iFatherId + "',";
  375. iChildId--;
  376. #endregion
  377. }
  378. #endregion
  379. #region 查询
  380. sql = @"
  381. select distinct a.ID as ID, a.ID as IDs,a.cCode as SDNNEGCode,a.cCusCode as CusCode,
  382. c.cCusName as CusName,a.cWhCode as WHCode,d.cWhName as WHName,e.cordercode ,
  383. a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime ,a.cHandler as Checker ,
  384. a.dnverifytime as CheckDateTime
  385. from rdrecord32 a
  386. left join DispatchLists e on a.cDLCode=e.DLID
  387. left join Customer c on a.cCusCode=c.cCusCode
  388. left join Warehouse d on a.cWhCode=d.cWhCode WHERE 1=1 and a.ID in({0})
  389. select distinct a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode ,b.iQuantity as Quantity ,
  390. b.iNum as Amount,b.iDLsID as SDNRTDetailID
  391. from rdrecord32 a
  392. inner join rdrecords32 b on a.ID=b.ID
  393. left join Customer c on a.cCusCode=c.cCusCode
  394. left join Warehouse d on a.cWhCode=d.cWhCode WHERE 1=1 and a.ID in({0})";
  395. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  396. log.Debug(sql);
  397. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  398. if (dtNew1 == null)
  399. dtNew1 = ds.Tables[0];
  400. else
  401. dtNew1.Merge(ds.Tables[0]);
  402. if (dtNew2 == null)
  403. dtNew2 = ds.Tables[1];
  404. else
  405. dtNew2.Merge(ds.Tables[1]);
  406. #endregion
  407. cmd.Transaction.Commit();
  408. }
  409. catch (Exception ex)
  410. {
  411. cmd.Transaction.Rollback();
  412. log.Error(ex.Message);
  413. throw new Exception(ex.Message);
  414. }
  415. finally
  416. {
  417. if (conn.State == ConnectionState.Open)
  418. {
  419. conn.Close();
  420. }
  421. conn.Dispose();
  422. }
  423. }
  424. string RelationName = "details";
  425. DataSet dz = new DataSet();
  426. dtNew1.TableName = "Table0";
  427. dtNew2.TableName = "Table1";
  428. dz.Tables.Add(dtNew1.Copy());
  429. dz.Tables.Add(dtNew2.Copy());
  430. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  431. dz.Relations.Add(dh);
  432. msg = DBHelper.DataSetToJson(dz, RelationName);
  433. return msg;
  434. }
  435. /// <summary>
  436. /// 删除销售出库单红字
  437. /// </summary>
  438. /// <param name="infos"></param>
  439. /// <returns></returns>
  440. public string Delete(List<ICSSalesShipmentDocNegative> infos)
  441. {
  442. List<ICSSalesShipmentDocNegative> szJson = new List<ICSSalesShipmentDocNegative>();
  443. if (infos.Count <= 0)
  444. {
  445. throw new Exception("传送数据为空!");
  446. }
  447. string res = string.Empty;
  448. string connS = "";
  449. SqlConnection conn = new SqlConnection();
  450. SqlCommand cmd = new SqlCommand();
  451. string sql = string.Empty;
  452. foreach (ICSSalesShipmentDocNegative info in infos)
  453. {
  454. try
  455. {
  456. connS = string.Format(connString, info.WorkPoint);
  457. conn = new System.Data.SqlClient.SqlConnection(connS);
  458. conn.Open();
  459. SqlTransaction sqlTran = conn.BeginTransaction();
  460. cmd = new SqlCommand();
  461. cmd.Transaction = sqlTran;
  462. cmd.Connection = conn;
  463. if (info.MTime < new DateTime(2000, 01, 01))
  464. throw new Exception("请输入正确的操作时间:" + info.MTime);
  465. sql = @"delete rdrecord11 where rdrecord32.ID='" + info.ID + "'";
  466. sql += @"delete rdrecords11 where rdrecords32.ID='" + info.ID + "'";
  467. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  468. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  469. // WHERE a.ID='" + info.ID + "'";
  470. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除红字销售出单失败!");
  471. cmd.Transaction.Commit();
  472. }
  473. catch (Exception ex)
  474. {
  475. cmd.Transaction.Rollback();
  476. log.Error(ex.Message);
  477. throw new Exception(ex.Message);
  478. }
  479. finally
  480. {
  481. if (conn.State == ConnectionState.Open)
  482. {
  483. conn.Close();
  484. }
  485. conn.Dispose();
  486. }
  487. }
  488. return res;
  489. }
  490. /// <summary>
  491. /// 审核销售出库单
  492. /// </summary>
  493. /// <param name="infos"></param>
  494. /// <returns></returns>
  495. public string Approve(List<ICSSalesShipmentDocNegative> infos)
  496. {
  497. List<ICSSalesShipmentDocNegative> szJson = new List<ICSSalesShipmentDocNegative>();
  498. string connS = "";
  499. string json = "";
  500. if (infos.Count <= 0)
  501. {
  502. throw new Exception("传送数据为空!");
  503. }
  504. string res = string.Empty;
  505. SqlConnection conn = new SqlConnection();
  506. SqlCommand cmd = new SqlCommand();
  507. string sql = string.Empty;
  508. foreach (ICSSalesShipmentDocNegative info in infos)
  509. {
  510. try
  511. {
  512. connS = string.Format(connString, info.WorkPoint);
  513. conn = new System.Data.SqlClient.SqlConnection(connS);
  514. conn.Open();
  515. SqlTransaction sqlTran = conn.BeginTransaction();
  516. cmd = new SqlCommand();
  517. cmd.Transaction = sqlTran;
  518. cmd.Connection = conn;
  519. if (info.MTime < new DateTime(2000, 01, 01))
  520. throw new Exception("请输入正确的操作时间:" + info.MTime);
  521. sql = @"UPDATE rdrecord32 SET cHandler ='" + info.User + @"' ,
  522. dnverifytime=CONVERT(VARCHAR(50),GETDATE(),112),dVeriDate=GETDATE() WHERE ID='{0}'";
  523. sql = string.Format(sql, info.ID);
  524. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核红字销售出单失败!");
  525. cmd.Transaction.Commit();
  526. }
  527. catch (Exception ex)
  528. {
  529. cmd.Transaction.Rollback();
  530. log.Error(ex.Message);
  531. throw new Exception(ex.Message);
  532. }
  533. finally
  534. {
  535. if (conn.State == ConnectionState.Open)
  536. {
  537. conn.Close();
  538. }
  539. conn.Dispose();
  540. }
  541. }
  542. return json;
  543. }
  544. /// <summary>
  545. ///
  546. /// 记录日志
  547. /// </summary>
  548. /// <param name="Bills"></param>
  549. private void LogInfo(List<ICSSalesShipmentDocNegative> Bills)
  550. {
  551. string HeadList = string.Empty;
  552. string BodyList = string.Empty;
  553. foreach (ICSSalesShipmentDocNegative head in Bills)
  554. {
  555. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  556. foreach (ICSSalesShipmentDocNegatives body in head.details)
  557. {
  558. BodyList += "\r\n 表体主键ID: " + body.SDNRTDetailID + ",数量:" + body.Quantity;
  559. }
  560. }
  561. log.Info(HeadList);
  562. log.Info(BodyList);
  563. }
  564. }
  565. }