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

768 lines
42 KiB

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