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

401 lines
19 KiB

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