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

445 lines
20 KiB

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