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

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