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

713 lines
37 KiB

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