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

495 lines
23 KiB

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