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

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