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

764 lines
42 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 SalesShipmentDoc
  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<ICSSalesShipmentDoc> infos)
  30. {
  31. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  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 (ICSSalesShipmentDoc 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 ";
  71. if (!string.IsNullOrWhiteSpace(info.SSDCode))
  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.SSDCode, info.MTime, userInfo.UserName);
  84. dt = DBHelper.SQlReturnData(sql, cmd);
  85. if (dt.Rows.Count <= 0 || dt == null)
  86. throw new Exception("销售出库单号:" + info.SSDCode + ",无信息!");
  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 CreateSalesShipmentDoc(List<ICSSalesShipmentDoc> Bills)
  118. {
  119. string sql = "";
  120. string msg = "";
  121. DataSet ds = null;
  122. string connS = "";
  123. int iFatherId = 0;
  124. string status = "";
  125. int iChildId = 0;
  126. string iFatherIdTwo = "";
  127. //bool ResultFlag = false;
  128. SqlConnection conn = new SqlConnection();
  129. SqlCommand cmd = new SqlCommand();
  130. VouchKey key = new VouchKey();
  131. if (Bills.Count <= 0)
  132. {
  133. throw new Exception("传送数据为空!");
  134. }
  135. LogInfo(Bills);
  136. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  137. foreach (string WorkPoint in result)
  138. {
  139. iFatherIdTwo = "";
  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 (ICSSalesShipmentDoc head in Bills)
  150. {
  151. if (WorkPoint != head.WorkPoint)
  152. continue;
  153. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  154. throw new Exception("U8正在整理现存量,请稍后再试");
  155. ICSUserInfo userInfo = new ICSUserInfo();
  156. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  157. string[] ss = head.WorkPoint.Split('_');
  158. ERPDB = ss[1];
  159. #region 检查 发货单状态
  160. sql = @"SELECT a.DLID,a.cBusType,c.cRdCode,a.cSTCode,b.AutoID,b.irowno,a.cPersonCode,a.cCusCode,b.iDLsID,
  161. a.cMemo,a.cShipAddress,a.cDepCode,cCusInvName,b.cBatch,a.caddcode,a.cCloser,a.cVerifier FROM dbo.DispatchList a INNER JOIN
  162. dbo.DispatchLists b ON a.DLID=b.DLID
  163. LEFT JOIN dbo.SaleType c ON c.cSTCode=a.cSTCode
  164. WHERE a.cDLCode ='" + head.SDNCode + "'";
  165. cmd.CommandText = sql;
  166. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  167. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  168. {
  169. foreach (DataRow item in dtDisCheck.Rows)
  170. {
  171. if (!string.IsNullOrEmpty(item["cCloser"].ToString()))
  172. {
  173. log.Info("DLID:" + head.ID);
  174. throw new Exception("ERP发货单已关闭,无法发货!发货单号:" + head.details);
  175. }
  176. }
  177. bustype = dtDisCheck.Rows[0]["cBusType"].ToString();
  178. }
  179. else
  180. {
  181. throw new Exception("销售发货单单号:" + head.ID + "在ERP内不存在!");
  182. }
  183. #endregion
  184. #region 回写发货单字段
  185. sql = @"select (case when cvalue='TRUE' THEN 'SA' ELSE 'ST' END) status FROM AccInformation WHERE cSysID='ST' AND cName='BSAcreat'";
  186. cmd.CommandText = sql;
  187. dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  188. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  189. {
  190. status = dtDisCheck.Rows[0]["status"].ToString();
  191. }
  192. else
  193. {
  194. status = "";
  195. }
  196. #endregion
  197. #region 检验发货单数量
  198. foreach (ICSSalesShipmentDocs body in head.details)
  199. {
  200. sql = "SELECT isnull(fOutQuantity,0) as fOutQuantity,ABS(iQuantity) as iQuantity FROM dbo.DispatchLists WHERE AutoID='" + body.SDNDetailID + "'";
  201. DataTable dtfOutQty = DBHelper.SQlReturnData(sql, cmd);
  202. if (dtfOutQty != null && dtfOutQty.Rows.Count > 0)
  203. {
  204. decimal fOutQty = Convert.ToDecimal(dtfOutQty.Rows[0]["fOutQuantity"]);
  205. decimal iQuantity = Convert.ToDecimal(dtfOutQty.Rows[0]["iQuantity"]);
  206. if (fOutQty + body.Quantity > iQuantity)
  207. {
  208. throw new Exception("出库数量超过发货单表体可出库数量!");
  209. }
  210. }
  211. else
  212. {
  213. throw new Exception("发货单表体不存在!");
  214. }
  215. }
  216. #endregion
  217. #region 销售出库单表头
  218. int num = 0;
  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.SDNCode + "'";
  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.SSDCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  276. throw new Exception("生成销售出库单表头失败!销售出库单号:" + head.SSDCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  277. }
  278. #endregion
  279. #region 销售出库单表体
  280. foreach (ICSSalesShipmentDocs body in head.details)
  281. {
  282. int fotnum = 0;
  283. if (head.IsFot == true)
  284. {
  285. sql = @"SELECT bService from Inventory WHERE cInvCode='" + body.InvCode + "'";
  286. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  287. if (dtItem != null && dtItem.Rows.Count > 0)
  288. {
  289. if (dtItem.Rows[0]["bService"].ToString() == "True")
  290. {
  291. fotnum++;
  292. #region 回写销售订单累计发货数量,回写销售发货单累计出库数量
  293. sql = @"UPDATE a SET a.foutquantity=ISNULL(a.foutquantity,0)+" + body.Quantity + @"
  294. FROM dbo.SO_SODetails a LEFT JOIN dbo.DispatchLists b ON a.iSOsID=b.iSOsID
  295. WHERE b.AutoID='" + body.SDNDetailID + "'";
  296. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售订单累计发货数量失败!");
  297. sql = @"Update DispatchList set cSaleOut='" + status + "' where DLID=(select DLID from DispatchLists where AutoID='" + body.SDNDetailID + @"')
  298. Update DispatchLists set fOutQuantity=isnull(fOutQuantity,0)+" + body.Quantity + " where AutoID='" + body.SDNDetailID + "' ";
  299. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售发货单累计出库数量失败!");
  300. #endregion
  301. }
  302. else
  303. {
  304. #region 回写销售订单累计发货数量,回写销售发货单累计出库数量
  305. sql = @"UPDATE a SET a.foutquantity=ISNULL(a.foutquantity,0)+" + body.Quantity + @"
  306. FROM dbo.SO_SODetails a LEFT JOIN dbo.DispatchLists b ON a.iSOsID=b.iSOsID
  307. WHERE b.AutoID='" + body.SDNDetailID + "'";
  308. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售订单累计发货数量失败!");
  309. sql = @"Update DispatchList set cSaleOut='" + status + "' where DLID=(select DLID from DispatchLists where AutoID='" + body.SDNDetailID + @"')
  310. Update DispatchLists set fOutQuantity=isnull(fOutQuantity,0)+" + body.Quantity + " where AutoID='" + body.SDNDetailID + "' ";
  311. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售发货单累计出库数量失败!");
  312. #endregion
  313. }
  314. }
  315. }
  316. else
  317. {
  318. #region 回写销售订单累计发货数量,回写销售发货单累计出库数量
  319. sql = @"UPDATE a SET a.foutquantity=ISNULL(a.foutquantity,0)+" + body.Quantity + @"
  320. FROM dbo.SO_SODetails a LEFT JOIN dbo.DispatchLists b ON a.iSOsID=b.iSOsID
  321. WHERE b.AutoID='" + body.SDNDetailID + "'";
  322. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售订单累计发货数量失败!");
  323. sql = @"Update DispatchList set cSaleOut='" + status + "' where DLID=(select DLID from DispatchLists where AutoID='" + body.SDNDetailID + @"')
  324. Update DispatchLists set fOutQuantity=isnull(fOutQuantity,0)+" + body.Quantity + " where AutoID='" + body.SDNDetailID + "' ";
  325. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售发货单累计出库数量失败!");
  326. #endregion
  327. }
  328. if (fotnum != 0)
  329. {
  330. continue;
  331. }
  332. string Batch = "";
  333. string socode = "";
  334. sql = @" SELECT d.cSOCode,a.cBatch
  335. FROM dbo.DispatchLists a
  336. INNER JOIN dbo.DispatchList b ON a.DLID=b.DLID
  337. LEFT JOIN dbo.SO_SODetails c ON a.iSOsID=c.iSOsID
  338. LEFT JOIN dbo.SO_SOMain d ON c.ID=d.ID WHERE a.AutoID='" + body.SDNDetailID + "'";
  339. DataTable dtfOutQty = DBHelper.SQlReturnData(sql, cmd);
  340. if (dtfOutQty != null && dtfOutQty.Rows.Count > 0)
  341. {
  342. socode = dtfOutQty.Rows[0]["cSOCode"].ToString();
  343. Batch = dtfOutQty.Rows[0]["cBatch"].ToString();
  344. }
  345. else
  346. socode = "";
  347. //if (head.IsReturn == "1")
  348. //{
  349. // body.iQuantity = -body.iQuantity;
  350. //}
  351. //else
  352. //{
  353. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  354. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch='" + "" + "'and cWhCode='" + head.WHCode + "'";
  355. sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch='" + body.BatchCode + @"'AND cFree1='" + body.cFree1 + @"' AND cFree2='" + body.cFree2 + @"' AND cFree3='" + body.cFree3 + @"' AND cFree4='" + body.cFree4 + @"' AND cFree5='" + body.cFree5 + @"' AND cFree6='" + body.cFree6 + @"' AND cFree7='" + body.cFree7 + @"' AND cFree8='" + body.cFree8 + @"' AND cFree9='" + body.cFree9 + @"' AND cFree10='" + body.cFree10 + @"' and cWhCode='" + head.WHCode + "'";
  356. DataTable dtItem1 = DBHelper.SQlReturnData(sql, cmd);
  357. if (dtItem1 != null && dtItem1.Rows.Count > 0)
  358. {
  359. //if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  360. //{
  361. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  362. //}
  363. if (Convert.ToDecimal(dtItem1.Rows[0]["iQuantity"].ToString()) < body.Quantity)
  364. {
  365. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.InvCode);
  366. }
  367. }
  368. else
  369. {
  370. throw new Exception("物料:" + body.InvCode + "在现存量表中不存在!");
  371. }
  372. //}
  373. sql = @"INSERT INTO dbo.rdrecords32
  374. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,iNum,
  375. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,
  376. cDefine27 ,cItem_class ,cItemCode ,iDLsID ,iNQuantity ,
  377. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  378. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  379. bLPUseFree ,iRSRowNO ,iOriTrackID ,ccusinvcode,ccusinvname,bCosting ,bVMIUsed ,
  380. cbdlcode ,iExpiratDateCalcu ,iorderdid ,iordertype ,
  381. iordercode ,iorderseq ,ipesodid ,ipesotype ,cpesocode ,
  382. ipesoseq ,isodid ,isotype ,csocode ,isoseq ,irowno ,
  383. bIAcreatebill ,bsaleoutcreatebill ,isaleoutid ,bneedbill,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  384. SELECT @AutoID,@ID,@cInvCode,@iQuantity,@cBatch,0,@iNum,
  385. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,
  386. a.cDefine27,a.cItem_class,@cItemCode,a.iDLsID,a.iQuantity,
  387. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  388. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  389. 0,0,0,a.cCusInvCode,a.cCusInvName,1,0,
  390. b.cDLCode,0,a.iSOsID,1,
  391. d.cSOCode,c.iRowNo,a.iSOsID,1,d.cSOCode,
  392. 1,a.iSOsID,1,d.cSOCode,c.iRowNo,@irowno,
  393. 1,1,@AutoID,1,null,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  394. FROM dbo.DispatchLists a
  395. INNER JOIN dbo.DispatchList b ON a.DLID=b.DLID
  396. LEFT JOIN dbo.SO_SODetails c ON a.iSOsID=c.iSOsID
  397. LEFT JOIN dbo.SO_SOMain d ON c.ID=d.ID WHERE a.AutoID='" + body.SDNDetailID + "'";
  398. cmd.Parameters.Clear();
  399. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  400. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  401. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  402. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  403. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  404. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  405. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  406. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  407. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  408. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  409. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  410. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  411. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  412. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  413. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  414. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  415. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  416. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  417. cmd.CommandText = sql;
  418. try
  419. {
  420. int count = cmd.ExecuteNonQuery();
  421. if (count <= 0)
  422. {
  423. log.Error("生成销售出库单表体失败,受影响行数<=0;");
  424. throw new Exception("生成销售出库单表体失败,受影响行数<=0;");
  425. }
  426. }
  427. catch (Exception ex)
  428. {
  429. log.Error("生成销售出库单表体失败!销售出库单号:" + head.SSDCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  430. throw new Exception("生成销售出库单表体失败!销售出库单号:" + head.SSDCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  431. }
  432. #region 回写销售订单累计发货数量,回写销售发货单累计出库数量
  433. // sql = @"UPDATE a SET a.foutquantity=ISNULL(a.foutquantity,0)+" + body.Quantity + @"
  434. // FROM dbo.SO_SODetails a LEFT JOIN dbo.DispatchLists b ON a.iSOsID=b.iSOsID
  435. // WHERE b.AutoID='" + body.SDNDetailID + "'";
  436. // DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售订单累计发货数量失败!");
  437. // sql = @"Update DispatchList set cSaleOut='" + status + "' where DLID=(select DLID from DispatchLists where AutoID='" + body.SDNDetailID + @"')
  438. // Update DispatchLists set fOutQuantity=isnull(fOutQuantity,0)+" + body.Quantity + " where AutoID='" + body.SDNDetailID + "' ";
  439. // DBHelper.CmdExecuteNonQuery(sql, cmd, "回写销售发货单累计出库数量失败!");
  440. 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
  441. LEFT JOIN (select sum(iNum) NiNum,iDLsID from rdrecords32 GROUP BY iDLsID) b on a.iDLsID=b.iDLsID
  442. where a.AutoID='" + body.SDNDetailID + "'";
  443. dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  444. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  445. {
  446. foreach (DataRow item in dtDisCheck.Rows)
  447. {
  448. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  449. {
  450. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  451. {
  452. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  453. {
  454. sql = @"Update rdrecords32 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  455. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  456. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  457. }
  458. }
  459. }
  460. }
  461. }
  462. #region 更新现存量 现存量表待出库数量扣减
  463. key.cBustypeUN = bustype;
  464. key.cVouchTypeUN = "32";
  465. key.TableName = "IA_ST_UnAccountVouch32";
  466. //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);
  467. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  468. if (Batch != "")
  469. {
  470. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, -body.Amount, body.cFree1,
  471. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  472. "", key, "", "", "", "", -body.Quantity, -body.Amount, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  473. }
  474. else
  475. {
  476. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, "", 0, 0, body.cFree1,
  477. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  478. "", key, "", "", "", "", -body.Quantity, -body.Amount, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  479. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, -body.Amount, body.cFree1,
  480. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  481. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  482. }
  483. #endregion
  484. iFatherIdTwo = "'" + iFatherId + "',";
  485. iChildId--;
  486. #endregion
  487. log.Debug(sql);
  488. }
  489. #endregion
  490. #region 查询
  491. sql = @"
  492. select top 1 a.ID as ID, a.ID as IDs,a.cCode as SSDCode,a.cCusCode as CusCode,
  493. c.cCusName as CusName,a.cWhCode as WHCode,d.cWhName as WHName,e.cordercode ,
  494. a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime ,a.cHandler as Checker ,
  495. a.dnverifytime as CheckDateTime
  496. from rdrecord32 a
  497. left join DispatchLists e on a.cDLCode=e.DLID
  498. left join Customer c on a.cCusCode=c.cCusCode
  499. left join Warehouse d on a.cWhCode=d.cWhCode WHERE 1=1 and a.ID in({0})
  500. select distinct a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode ,b.iQuantity as Quantity ,
  501. b.iNum as Amount,e.autoid as SDNDetailID
  502. ,isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  503. isnull(b.cFree1,'') as cFree1,
  504. isnull(b.cFree2,'') as cFree2,
  505. isnull(b.cFree3,'') as cFree3,
  506. isnull(b.cFree4,'') as cFree4,
  507. isnull(b.cFree5,'') as cFree5,
  508. isnull(b.cFree6,'') as cFree6,
  509. isnull(b.cFree7,'') as cFree7,
  510. isnull(b.cFree8,'') as cFree8,
  511. isnull(b.cFree9,'') as cFree9,
  512. isnull(b.cFree10,'') as cFree10
  513. from rdrecord32 a
  514. inner join rdrecords32 b on a.ID=b.ID
  515. left join DispatchLists e on b.iDLsID=e.iDLsID
  516. left join Customer c on a.cCusCode=c.cCusCode
  517. left join Warehouse d on a.cWhCode=d.cWhCode WHERE 1=1 and a.ID in({0})";
  518. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  519. if (ds != null)
  520. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  521. else
  522. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  523. #endregion
  524. }
  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. msg = JSON.DataSetToJson(ds, "details", "IDs");
  543. return msg;
  544. }
  545. /// <summary>
  546. /// 审核销售出库单
  547. /// </summary>
  548. /// <param name="infos"></param>
  549. /// <returns></returns>
  550. public string Approve(List<ICSSalesShipmentDoc> infos)
  551. {
  552. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  553. string connS = "";
  554. string json = "";
  555. if (infos.Count <= 0)
  556. {
  557. throw new Exception("传送数据为空!");
  558. }
  559. string res = string.Empty;
  560. SqlConnection conn = new SqlConnection();
  561. SqlCommand cmd = new SqlCommand();
  562. string sql = string.Empty;
  563. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  564. foreach (string WorkPoint in result)
  565. {
  566. try
  567. {
  568. connS = string.Format(connString, WorkPoint);
  569. conn = new System.Data.SqlClient.SqlConnection(connS);
  570. conn.Open();
  571. SqlTransaction sqlTran = conn.BeginTransaction();
  572. cmd = new SqlCommand();
  573. cmd.Transaction = sqlTran;
  574. cmd.Connection = conn;
  575. foreach (ICSSalesShipmentDoc info in infos)
  576. {
  577. if (WorkPoint != info.WorkPoint)
  578. continue;
  579. ICSUserInfo userInfo = new ICSUserInfo();
  580. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  581. if (info.MTime < new DateTime(2000, 01, 01))
  582. throw new Exception("请输入正确的操作时间:" + info.MTime);
  583. sql = @"UPDATE rdrecord32 SET cHandler ='" + userInfo.UserName + @"' ,
  584. dnverifytime=CONVERT(VARCHAR(50),GETDATE(),112),dVeriDate=GETDATE() WHERE ID='{0}'";
  585. sql = string.Format(sql, info.ID);
  586. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核销售出单失败!");
  587. }
  588. cmd.Transaction.Commit();
  589. }
  590. catch (Exception ex)
  591. {
  592. cmd.Transaction.Rollback();
  593. log.Error(ex.Message);
  594. throw new Exception(ex.Message);
  595. }
  596. finally
  597. {
  598. if (conn.State == ConnectionState.Open)
  599. {
  600. conn.Close();
  601. }
  602. conn.Dispose();
  603. }
  604. }
  605. return json;
  606. }
  607. /// <summary>
  608. /// 删除销售出库单
  609. /// </summary>
  610. /// <param name="infos"></param>
  611. /// <returns></returns>
  612. public string Delete(List<ICSSalesShipmentDoc> infos)
  613. {
  614. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  615. if (infos.Count <= 0)
  616. {
  617. throw new Exception("传送数据为空!");
  618. }
  619. string res = string.Empty;
  620. string connS = "";
  621. SqlConnection conn = new SqlConnection();
  622. SqlCommand cmd = new SqlCommand();
  623. string sql = string.Empty;
  624. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  625. foreach (string WorkPoint in result)
  626. {
  627. try
  628. {
  629. connS = string.Format(connString, WorkPoint);
  630. conn = new System.Data.SqlClient.SqlConnection(connS);
  631. conn.Open();
  632. SqlTransaction sqlTran = conn.BeginTransaction();
  633. cmd = new SqlCommand();
  634. cmd.Transaction = sqlTran;
  635. cmd.Connection = conn;
  636. foreach (ICSSalesShipmentDoc info in infos)
  637. {
  638. if (WorkPoint != info.WorkPoint)
  639. continue;
  640. if (info.MTime < new DateTime(2000, 01, 01))
  641. throw new Exception("请输入正确的操作时间:" + info.MTime);
  642. sql = @"delete rdrecord11 where rdrecord32.ID='" + info.ID + "'";
  643. sql += @"delete rdrecords11 where rdrecords32.ID='" + info.ID + "'";
  644. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  645. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  646. // WHERE a.ID='" + info.ID + "'";
  647. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除销售出单失败!");
  648. }
  649. cmd.Transaction.Commit();
  650. }
  651. catch (Exception ex)
  652. {
  653. cmd.Transaction.Rollback();
  654. log.Error(ex.Message);
  655. throw new Exception(ex.Message);
  656. }
  657. finally
  658. {
  659. if (conn.State == ConnectionState.Open)
  660. {
  661. conn.Close();
  662. }
  663. conn.Dispose();
  664. }
  665. }
  666. return res;
  667. }
  668. /// <summary>
  669. ///
  670. /// 记录日志
  671. /// </summary>
  672. /// <param name="Bills"></param>
  673. private void LogInfo(List<ICSSalesShipmentDoc> Bills)
  674. {
  675. string HeadList = string.Empty;
  676. string BodyList = string.Empty;
  677. foreach (ICSSalesShipmentDoc head in Bills)
  678. {
  679. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  680. foreach (ICSSalesShipmentDocs body in head.details)
  681. {
  682. BodyList += "\r\n 表体主键ID: " + body.SDNDetailID + ",数量:" + body.Quantity;
  683. }
  684. }
  685. log.Info(HeadList);
  686. log.Info(BodyList);
  687. }
  688. }
  689. }