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

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