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

722 lines
38 KiB

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