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

499 lines
23 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. public class SalesOrder
  14. {
  15. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  16. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  17. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  18. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  19. string bustype = string.Empty;
  20. /// <summary>
  21. /// 获取销售出库单
  22. /// </summary>
  23. /// <param name="infos"></param>
  24. /// <returns></returns>
  25. public string Get(List<ICSSalesOrder> infos)
  26. {
  27. List<ICSSalesOrder> szJson = new List<ICSSalesOrder>();
  28. DataTable dt = null;
  29. DataTable dtNew = null;
  30. string connS = "";
  31. string json = "";
  32. if (infos.Count <= 0)
  33. {
  34. throw new Exception("传送数据为空!");
  35. }
  36. string res = string.Empty;
  37. SqlConnection conn = new SqlConnection();
  38. SqlCommand cmd = new SqlCommand();
  39. string sql = string.Empty;
  40. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  41. foreach (string WorkPoint in result)
  42. {
  43. try
  44. {
  45. connS = string.Format(connString, WorkPoint);
  46. conn = new System.Data.SqlClient.SqlConnection(connS);
  47. conn.Open();
  48. SqlTransaction sqlTran = conn.BeginTransaction();
  49. cmd = new SqlCommand();
  50. cmd.Transaction = sqlTran;
  51. cmd.Connection = conn;
  52. foreach (ICSSalesOrder info in infos)
  53. {
  54. if (WorkPoint != info.WorkPoint)
  55. continue;
  56. ICSUserInfo userInfo = new ICSUserInfo();
  57. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  58. if (info.MTime < new DateTime(2000, 01, 01))
  59. throw new Exception("请输入正确的操作时间:" + info.MTime);
  60. sql = @"select a.ID ,a.cSOCode ,a.cCusCode,c.cCusName,a.cDepCode ,
  61. d.cDepName,a.cMaker ,a.dcreatesystime ,a.cVerifier ,a.dverifydate ,
  62. b.AutoID,b.iRowNo ,b.cInvCode ,b.iQuantity ,b.iNum ,b.iFHQuantity ,
  63. b.dPreDate from SO_SOMain a
  64. inner join SO_SODetails b on a.ID =b.ID
  65. Left join Customer c on a.cCusCode=c.cCusCode
  66. left join Department d on a.cDepCode=d.cDepCode ";
  67. if (!string.IsNullOrWhiteSpace(info.SOCode))
  68. {
  69. sql += " and a.cSOCode='{0}'";
  70. }
  71. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  72. {
  73. sql += " and ISNULL(a.dnmodifytime ,ISNULL(a.dverifydate , ISNULL(a.dnmodifytime , a.dcreatesystime )))>='{1}'";
  74. }
  75. if (!string.IsNullOrWhiteSpace(info.User))
  76. {
  77. sql += "and a.CMAKER='{2}'";
  78. }
  79. sql = string.Format(sql, info.SOCode, info.MTime, userInfo.UserName);
  80. dt = DBHelper.SQlReturnData(sql, cmd);
  81. if (dt.Rows.Count <= 0 || dt == null)
  82. throw new Exception("销售出库单号:" + info.SOCode + ",无信息!");
  83. if (dtNew == null)
  84. dtNew = dt;
  85. else
  86. dtNew.Merge(dt);
  87. cmd.CommandType = CommandType.Text;
  88. cmd.Parameters.Clear();
  89. }
  90. cmd.Transaction.Commit();
  91. }
  92. catch (Exception ex)
  93. {
  94. if (cmd.Transaction != null)
  95. cmd.Transaction.Rollback();
  96. log.Error(ex.Message);
  97. throw new Exception(ex.Message);
  98. }
  99. finally
  100. {
  101. if (conn.State == ConnectionState.Open)
  102. {
  103. conn.Close();
  104. }
  105. conn.Dispose();
  106. }
  107. }
  108. json = JsonConvert.SerializeObject(dtNew);
  109. return json;
  110. }
  111. /// <summary>
  112. /// 创建销售出库单
  113. /// </summary>
  114. /// <param name="Bills"></param>
  115. /// <returns></returns>
  116. public string CerateSalesOrder(List<ICSSalesOrder> Bills)
  117. {
  118. string msg = "";
  119. DataTable dt = null;
  120. DataTable dtNew = null;
  121. string connS = "";
  122. //bool ResultFlag = false;
  123. SqlConnection conn = new SqlConnection();
  124. SqlCommand cmd = new SqlCommand();
  125. VouchKey key = new VouchKey();
  126. if (Bills.Count <= 0)
  127. {
  128. throw new Exception("传送数据为空!");
  129. }
  130. LogInfo(Bills);
  131. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  132. foreach (string WorkPoint in result)
  133. {
  134. try
  135. {
  136. connS = string.Format(connString, WorkPoint);
  137. conn = new System.Data.SqlClient.SqlConnection(connS);
  138. conn.Open();
  139. SqlTransaction sqlTran = conn.BeginTransaction();
  140. cmd = new SqlCommand();
  141. cmd.Transaction = sqlTran;
  142. cmd.Connection = conn;
  143. foreach (ICSSalesOrder head in Bills)
  144. {
  145. string sql = "";
  146. if (WorkPoint != head.WorkPoint)
  147. continue;
  148. ICSUserInfo userInfo = new ICSUserInfo();
  149. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  150. string[] ss = head.WorkPoint.Split('_');
  151. ERPDB = ss[1];
  152. #region 销售订单表头
  153. int num = 0;
  154. num = head.details.Count();
  155. string SO_SOMain_VT_ID = DBHelper.GetDefaultTemplate("17", cmd);//目标账套销售订单VT_ID模板
  156. DateTime time = DateTime.Now;
  157. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "SalesOrder", "" + num + "", head.WorkPoint, cmd);
  158. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  159. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  160. DateTime date = DateTime.Now;
  161. string iBaseCodeLen = DBHelper.GetAllRDCode("17", "" + time + "", "admin", "", head.WorkPoint, cmd);
  162. sql = string.Format(@"INSERT INTO dbo.SO_SOMain
  163. ( cSTCode ,dDate ,cSOCode ,cCusCode ,cDepCode ,
  164. cCusOAddress ,cexch_name ,iExchRate ,iTaxRate ,
  165. cMemo ,iStatus ,cMaker ,cVerifier ,bDisFlag ,
  166. cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,cDefine5 ,
  167. cDefine6 ,cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,
  168. bReturnFlag ,cCusName ,bOrder ,ID ,iVTid ,cBusType ,
  169. cDefine11 ,cDefine12 ,cDefine13 ,cDefine14 ,
  170. cDefine15 ,cDefine16 ,dPreMoDateBT ,dPreDateBT ,
  171. cgatheringplan ,caddcode ,iverifystate ,
  172. iswfcontrolled ,dverifydate ,ccusperson ,
  173. dcreatesystime ,dverifysystime ,bcashsale ,
  174. bmustbook ,cinvoicecompany ,cbcode)
  175. SELECT '10',CONVERT(NVARCHAR(50),GETDATE(),23),a.cPOID,@cCusCode,'999',
  176. (select cCusOAddress from {0}.dbo.Customer where cCusCode='@cCusCode'),a.cexch_name,a.nflat,a.iTaxRate,
  177. a.cMemo,1,@cMaker,a.cVerifier,0,
  178. a.cDefine1,a.cDefine2,a.cDefine3,a.cDefine4,a.cDefine5,
  179. a.cDefine6,a.cDefine7,a.cDefine8,a.cDefine9,a.cDefine10,
  180. 0,(select cCusName from {0}.dbo.Customer where cCusCode='@cCusCode'),0,@ID,@iVTid,'',
  181. a.cDefine11,'{1}',a.cDefine13,a.cDefine14,
  182. a.cDefine15,a.cDefine16,CONVERT(NVARCHAR(50),GETDATE(),23),CONVERT(NVARCHAR(50),GETDATE(),23),
  183. NULL,NULL,0,
  184. 0,a.cAuditDate,(select cCusPerson from {0}.dbo.Customer where cCusCode='@cCusCode'),
  185. GETDATE(),a.cAuditTime,0,
  186. 0,'@cCusCode','10'
  187. FROM dbo.PO_Pomain a
  188. where a.POID='" + head.POID + "'");
  189. cmd.Parameters.Clear();
  190. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  191. cmd.Parameters.Add(new SqlParameter("@iVTid", SO_SOMain_VT_ID));
  192. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  193. cmd.Parameters.Add(new SqlParameter("@cCusCode", head.CusCode));
  194. //cmd.Parameters.Add(new SqlParameter("@dDate", data.dMate));
  195. cmd.CommandText = sql;
  196. try
  197. {
  198. int count = cmd.ExecuteNonQuery();
  199. if (count <= 0)
  200. {
  201. log.Error("生成销售订单表头失败,受影响行数<=0;");
  202. throw new Exception("生成销售订单表头失败,受影响行数<=0;");
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. log.Error("生成销售订单表头失败!销售订单号:" + head.SOCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  208. throw new Exception("生成销售订单表头失败!销售订单号:" + head.SOCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  209. }
  210. #endregion
  211. #region 销售出库单表体
  212. foreach (ICSSalesOrders body in head.details)
  213. {
  214. #region 销售顶订单表体
  215. sql = string.Format(@"INSERT INTO {0}.dbo.SO_SODetails
  216. ( cSOCode ,cInvCode ,dPreDate ,iQuantity ,iNum,
  217. iQuotedPrice ,iUnitPrice ,
  218. iTaxUnitPrice ,iMoney ,
  219. iTax ,iSum ,
  220. iDisCount ,iNatUnitPrice ,
  221. iNatMoney ,iNatTax ,
  222. iNatSum ,iNatDisCount ,
  223. iFHNum ,iFHQuantity ,iFHMoney ,
  224. iSOsID ,KL ,KL2 ,
  225. cInvName ,iTaxRate ,cDefine22 ,cDefine23 ,
  226. cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  227. ID ,cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,
  228. cDefine32 ,cDefine33 ,cDefine34 ,cDefine35 ,
  229. cDefine36 ,cDefine37 ,fSalePrice ,
  230. dPreMoDate ,iRowNo ,fcusminprice ,foutquantity ,
  231. foutnum ,bOrderBOM ,bOrderBOMOver ,idemandtype ,
  232. busecusbom ,fVeriDispQty ,fVeriDispSum ,bsaleprice ,bgift )
  233. SELECT c.cPOID,a.cInvCode,CONVERT(NVARCHAR(50),GETDATE(),23),a.iQuantity,a.iNum,
  234. a.iTaxPrice,a.iUnitPrice,
  235. a.iTaxPrice,a.iMoney,
  236. a.iTax,a.iSum,
  237. 0,a.iNatUnitPrice,
  238. a.iNatMoney,a.iNatTax,
  239. a.iNatSum,0,
  240. 0,0,a.iMoney,
  241. @iSOsID,100,100,
  242. b.cInvName,a.iPerTaxRate,a.cDefine22,a.cDefine23,
  243. a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  244. @ID,a.ID,a.cDefine29,a.cDefine30,a.cDefine31,
  245. a.cDefine32,a.cDefine33,a.cDefine34,a.cDefine35,
  246. a.cDefine36,a.cDefine37,0,
  247. CONVERT(NVARCHAR(50),GETDATE(),23),@iRowNo,0,NULL,
  248. NULL,0,0,1,
  249. 0,0,a.iSum,1,0
  250. FROM dbo.PO_Podetails a
  251. LEFT JOIN dbo.Inventory b ON a.cInvCode=b.cInvCode
  252. LEFT JOIN dbo.PO_Pomain c ON a.POID=c.POID
  253. WHERE a.ID='" + body.PODetailID + "'");
  254. cmd.Parameters.Clear();
  255. cmd.Parameters.Add(new SqlParameter("@iSOsID", iChildId));
  256. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  257. cmd.Parameters.Add(new SqlParameter("@iRowNo", body.Sequence));
  258. //cmd.Parameters.Add(new SqlParameter("@dDate", data.dMate));
  259. cmd.CommandText = sql;
  260. try
  261. {
  262. int count = cmd.ExecuteNonQuery();
  263. if (count <= 0)
  264. {
  265. log.Error("生成销售订单表体失败,受影响行数<=0;");
  266. throw new Exception("生成销售订单表体失败,受影响行数<=0;");
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. log.Error("生成销售订单表体失败!销售订单号:" + head.SOCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  272. throw new Exception("生成销售订单表体失败!销售订单号:" + head.SOCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  273. }
  274. #endregion
  275. iChildId -= 1;
  276. }
  277. #endregion
  278. sql = @"
  279. select a.ID ,a.cSOCode ,a.cCusCode,c.cCusName,a.cDepCode ,
  280. d.cDepName,a.cMaker ,a.dcreatesystime ,a.cVerifier ,a.dverifydate ,
  281. b.AutoID,b.iRowNo ,b.cInvCode ,b.iQuantity ,b.iNum ,b.iFHQuantity ,
  282. b.dPreDate from SO_SOMain a
  283. inner join SO_SODetails b on a.ID =b.ID
  284. Left join Customer c on a.cCusCode=c.cCusCode
  285. left join Department d on a.cDepCode=d.cDepCode WHERE a.ID = '{0}' ";
  286. sql = string.Format(sql, iFatherId);
  287. dt = DBHelper.SQlReturnData(sql, cmd);
  288. if (dtNew == null)
  289. dtNew = dt;
  290. else
  291. dtNew.Merge(dt);
  292. cmd.CommandType = CommandType.Text;
  293. cmd.Parameters.Clear();
  294. }
  295. cmd.Transaction.Commit();
  296. }
  297. catch (Exception ex)
  298. {
  299. if (cmd.Transaction != null)
  300. cmd.Transaction.Rollback();
  301. log.Error(ex.Message);
  302. throw new Exception(ex.Message);
  303. }
  304. finally
  305. {
  306. if (conn.State == ConnectionState.Open)
  307. {
  308. conn.Close();
  309. }
  310. conn.Dispose();
  311. }
  312. }
  313. msg = JsonConvert.SerializeObject(dtNew);
  314. return msg;
  315. }
  316. /// <summary>
  317. /// 审核销售出库单
  318. /// </summary>
  319. /// <param name="infos"></param>
  320. /// <returns></returns>
  321. public string Approve(List<ICSSalesOrder> infos)
  322. {
  323. List<ICSSalesOrder> szJson = new List<ICSSalesOrder>();
  324. string connS = "";
  325. string json = "";
  326. if (infos.Count <= 0)
  327. {
  328. throw new Exception("传送数据为空!");
  329. }
  330. string res = string.Empty;
  331. SqlConnection conn = new SqlConnection();
  332. SqlCommand cmd = new SqlCommand();
  333. string sql = string.Empty;
  334. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  335. foreach (string WorkPoint in result)
  336. {
  337. try
  338. {
  339. connS = string.Format(connString, WorkPoint);
  340. conn = new System.Data.SqlClient.SqlConnection(connS);
  341. conn.Open();
  342. SqlTransaction sqlTran = conn.BeginTransaction();
  343. cmd = new SqlCommand();
  344. cmd.Transaction = sqlTran;
  345. cmd.Connection = conn;
  346. foreach (ICSSalesOrder info in infos)
  347. {
  348. if (WorkPoint != info.WorkPoint)
  349. continue;
  350. ICSUserInfo userInfo = new ICSUserInfo();
  351. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  352. if (info.MTime < new DateTime(2000, 01, 01))
  353. throw new Exception("请输入正确的操作时间:" + info.MTime);
  354. sql = @"UPDATE SO_SOMain SET cMaker ='" + userInfo.UserName + @"' ,
  355. dverifysystime =CONVERT(VARCHAR(50),GETDATE(),112),dverifydate =GETDATE() WHERE ID='{0}'";
  356. sql = string.Format(sql, info.ID);
  357. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  358. cmd.CommandType = CommandType.Text;
  359. cmd.Parameters.Clear();
  360. }
  361. cmd.Transaction.Commit();
  362. }
  363. catch (Exception ex)
  364. {
  365. if (cmd.Transaction != null)
  366. cmd.Transaction.Rollback();
  367. log.Error(ex.Message);
  368. throw new Exception(ex.Message);
  369. }
  370. finally
  371. {
  372. if (conn.State == ConnectionState.Open)
  373. {
  374. conn.Close();
  375. }
  376. conn.Dispose();
  377. }
  378. }
  379. return json;
  380. }
  381. /// <summary>
  382. /// 删除销售出库单
  383. /// </summary>
  384. /// <param name="infos"></param>
  385. /// <returns></returns>
  386. public string Delete(List<ICSSalesOrder> infos)
  387. {
  388. List<ICSSalesOrder> szJson = new List<ICSSalesOrder>();
  389. if (infos.Count <= 0)
  390. {
  391. throw new Exception("传送数据为空!");
  392. }
  393. string res = string.Empty;
  394. string connS = "";
  395. SqlConnection conn = new SqlConnection();
  396. SqlCommand cmd = new SqlCommand();
  397. string sql = string.Empty;
  398. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  399. foreach (string WorkPoint in result)
  400. {
  401. try
  402. {
  403. connS = string.Format(connString, WorkPoint);
  404. conn = new System.Data.SqlClient.SqlConnection(connS);
  405. conn.Open();
  406. SqlTransaction sqlTran = conn.BeginTransaction();
  407. cmd = new SqlCommand();
  408. cmd.Transaction = sqlTran;
  409. cmd.Connection = conn;
  410. foreach (ICSSalesOrder info in infos)
  411. {
  412. if (WorkPoint != info.WorkPoint)
  413. continue;
  414. if (info.MTime < new DateTime(2000, 01, 01))
  415. throw new Exception("请输入正确的操作时间:" + info.MTime);
  416. sql = @" DELETE SO_SOMain WHERE ID={0}";
  417. sql = string.Format(sql, info.ID);
  418. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  419. cmd.CommandType = CommandType.Text;
  420. cmd.Parameters.Clear();
  421. }
  422. cmd.Transaction.Commit();
  423. }
  424. catch (Exception ex)
  425. {
  426. if (cmd.Transaction != null)
  427. cmd.Transaction.Rollback();
  428. log.Error(ex.Message);
  429. throw new Exception(ex.Message);
  430. }
  431. finally
  432. {
  433. if (conn.State == ConnectionState.Open)
  434. {
  435. conn.Close();
  436. }
  437. conn.Dispose();
  438. }
  439. }
  440. return res;
  441. }
  442. /// <summary>
  443. ///
  444. /// 记录日志
  445. /// </summary>
  446. /// <param name="Bills"></param>
  447. private void LogInfo(List<ICSSalesOrder> Bills)
  448. {
  449. string HeadList = string.Empty;
  450. string BodyList = string.Empty;
  451. foreach (ICSSalesOrder head in Bills)
  452. {
  453. HeadList += "\r\n 表头主键ID:" + head.ID + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  454. foreach (ICSSalesOrders body in head.details)
  455. {
  456. BodyList += "\r\n 表体主键ID:数量:" + body.Quantity;
  457. }
  458. }
  459. log.Info(HeadList);
  460. log.Info(BodyList);
  461. }
  462. }
  463. }