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

425 lines
22 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. /// <summary>
  14. /// 销售发货
  15. /// </summary>
  16. public class SalesDeliveryNotice
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. private static string connString = System.Configuration.ConfigurationManager.AppSettings ["ERPConnStr"] ;
  20. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings ["ERPDB"] ;
  21. private static string Type = System.Configuration.ConfigurationManager.AppSettings ["Type"] ;
  22. string bustype = string.Empty;
  23. /// <summary>
  24. /// 获取销售发货
  25. /// </summary>
  26. /// <param name="infos"></param>
  27. /// <returns></returns>
  28. public string Get(List<ICSSalesDeliveryNotice> infos)
  29. {
  30. List<ICSSalesDeliveryNotice> szJson = new List<ICSSalesDeliveryNotice>();
  31. DataTable dt = null;
  32. string json = "";
  33. if (infos.Count <= 0)
  34. {
  35. throw new Exception("传送数据为空!");
  36. }
  37. string res = string.Empty;
  38. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  39. conn.Open();
  40. SqlTransaction sqlTran = conn.BeginTransaction();
  41. SqlCommand cmd = new SqlCommand();
  42. cmd.Transaction = sqlTran;
  43. cmd.Connection = conn;
  44. try
  45. {
  46. string sql = string.Empty;
  47. foreach (ICSSalesDeliveryNotice info in infos)
  48. {
  49. if (info.MTime < new DateTime(2000, 01, 01))
  50. throw new Exception("请输入正确的操作时间:" + info.MTime);
  51. sql = @" select a.DLID ,a.cDLCode ,a.cCusCode,c.cCusName,a.cDepCode ,
  52. d.cDepName,b.cordercode ,a.cMaker ,a.dcreatesystime ,
  53. b.AutoID ,b.irowno,b.cInvCode ,b.iQuantity ,b.iNum ,b.iSOsID
  54. FROM dbo.DispatchList a
  55. INNER JOIN dbo.DispatchLists b ON a.DLID = b.DLID
  56. LEFT JOIN dbo.Customer c ON a.cCusCode = c.cCusCode
  57. left join Department d on a.cDepCode=d.cDepCode ";
  58. if (!string.IsNullOrWhiteSpace(info.SDNCode))
  59. {
  60. sql += " and a.cDLCode='{0}'";
  61. }
  62. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  63. {
  64. sql += " and ISNULL(a.dnmodifytime ,ISNULL(a.dverifydate , ISNULL(a.dnmodifytime , a.dcreatesystime )))>='{1}'";
  65. }
  66. if (!string.IsNullOrWhiteSpace(info.User))
  67. {
  68. sql += "and a.CMAKER='{2}'";
  69. }
  70. sql = string.Format(sql, info.SOCode, info.MTime, info.User);
  71. dt = DBHelper.SQlReturnData(sql, cmd);
  72. if (dt.Rows.Count <= 0 || dt == null)
  73. throw new Exception("销售发货单号:" + info.SOCode + ",无信息!");
  74. json = JsonConvert.SerializeObject(dt);
  75. }
  76. cmd.Transaction.Commit();
  77. return json;
  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. /// <summary>
  95. /// 创建销售发货
  96. /// </summary>
  97. /// <param name="Bills"></param>
  98. /// <returns></returns>
  99. public string CerateSalesDeliveryNotice(List<ICSSalesDeliveryNotice> Bills)
  100. {
  101. string msg = "";
  102. DataTable dt = null;
  103. //bool ResultFlag = false;
  104. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  105. conn.Open();
  106. SqlTransaction sqlTran = conn.BeginTransaction();
  107. SqlCommand cmd = new SqlCommand();
  108. cmd.Transaction = sqlTran;
  109. cmd.Connection = conn;
  110. VouchKey key = new VouchKey();
  111. try
  112. {
  113. if (Bills.Count <= 0)
  114. {
  115. throw new Exception("传送数据为空!");
  116. }
  117. LogInfo(Bills);
  118. foreach (ICSSalesDeliveryNotice head in Bills)
  119. {
  120. string sql = "";
  121. #region 销售发货单表头
  122. int num = 0;
  123. foreach (ICSSalesDeliveryNotice heads in Bills)
  124. {
  125. num = heads.details.Count();
  126. }
  127. string SO_SOMain_VT_ID = DBHelper.GetDefaultTemplate("05", cmd);//目标账套销售订单VT_ID模板
  128. DateTime time = DateTime.Now;
  129. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "DelNote", "" + num + "");
  130. int iFatherId = Convert.ToInt32(dic ["iFatherId"].ToString());
  131. int iChildId = Convert.ToInt32(dic ["iChildId"] .ToString());
  132. DateTime date = DateTime.Now;
  133. string iBaseCodeLen = DBHelper.GetAllRDCode("05", "" + time + "", "admin");
  134. sql = string.Format(@"INSERT INTO dbo.DispatchList
  135. (@DLID , @cDLCode , @cVouchType , cSTCode ,CONVERT(NVARCHAR(50),GETDATE(),23)
  136. , @cDepCode ,SBVID , @cSOCode , @cCusCode , cPayCode
  137. , cSCCode, cCusOAddress , cexch_name , iExchRate , iTaxRate , 0 , bReturnFlag
  138. , bFirst , cMemo , cVerifier , cMaker , iSale, (select cCusName from {0}.dbo.Customer where cCusCode='@cCusCode')
  139. , iVTid , ufts , cBusType , cCloser , cAccounter , cCreChpName
  140. , bIAFirst , ioutgolden , cgatheringplan , dCreditStart , dGatheringDate , icreditdays , bCredit
  141. , caddcode , iverifystate , ireturncount , iswfcontrolled , icreditstate , bARFirst
  142. , cmodifier , dmoddate , dverifydate , ccusperson , dcreatesystime , dverifysystime , dmodifysystime
  143. , csvouchtype , iflowid , bsigncreate , bcashsale , cgathingcode , cChanger , cChangeMemo
  144. , outid , bmustbook , cBookDepcode , cBookType , bSaUsed , bneedbill , baccswitchflag
  145. , iPrintCount , ccuspersoncode , cSourceCode , bsaleoutcreatebill , cSysBarCode , cCurrentAuditor
  146. , csscode , cinvoicecompany , fEBweight , cEBweightUnit , cEBExpressCode , iEBExpressCoID
  147. , SeparateID , bNotToGoldTax , cEBTrnumber , cEBBuyer , cEBBuyerNote , ccontactname , cEBprovince
  148. , cEBcity , cEBdistrict , cmobilephone , cInvoiceCusName , cweighter , dweighttime , cPickVouchCode
  149. , cGCRouteCode)
  150. SELECT
  151. @DLID , @cDLCode , @cVouchType , cSTCode ,CONVERT(NVARCHAR(50),GETDATE(),23)
  152. , @cDepCode , 0 , @cSOCode , @cCusCode , cPayCode
  153. , cSCCode, cCusOAddress , cexch_name , iExchRate , iTaxRate , 0 , bReturnFlag
  154. , 0 , cMemo , cVerifier , cMaker , 0 , (select cCusName from dbo.Customer where cCusCode='@cCusCode')
  155. , @iVTid , ufts , cBusType , cCloser , NULL , NULL
  156. , 0 , null , cgatheringplan , CONVERT(NVARCHAR(50),GETDATE(),23) ,NULL , NULL , 0
  157. , caddcode , iverifystate , ireturncount , iswfcontrolled , icreditstate , 0
  158. , NULL , NULL , NULL , ccusperson , CONVERT(NVARCHAR(50),GETDATE(),23) , dverifysystime , dmodifysystime
  159. , csvouchtype , iflowid , 0 , 0 , cgathingcode , NULL , NULL
  160. , outid , bmustbook , NULL , NULL , NULL , 0 , 0
  161. , 0 , ccuspersoncode , NULL , 0 , cSysBarCode , cCurrentAuditor
  162. , csscode , cinvoicecompany , NULL , NULL , NULL , NULL
  163. , NULL , 0 , NULL , NULL , NULL , NULL , NULL
  164. , NULL , NULL , NULL , NULL , NULL , NULL , NULL
  165. , NULL FROM SO_SOMain a
  166. where a.cSOCode ='" + head.SOCode + "'");
  167. cmd.Parameters.Clear();
  168. cmd.Parameters.Add(new SqlParameter("@DLID", iFatherId));
  169. cmd.Parameters.Add(new SqlParameter("@cDLCode", iBaseCodeLen));
  170. cmd.Parameters.Add(new SqlParameter("@cVouchType","05"));
  171. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  172. cmd.Parameters.Add(new SqlParameter("@cSOCode", head.SOCode));
  173. cmd.Parameters.Add(new SqlParameter("@cCusCode", head.CusCode));
  174. cmd.Parameters.Add(new SqlParameter("@iVTid", SO_SOMain_VT_ID));
  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.SDNCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  189. throw new Exception("生成销售出库单表头失败!销售订单号:" + head.SDNCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  190. }
  191. #endregion
  192. #region 销售出库单表体
  193. foreach (ICSSalesDeliveryNotices body in head.details)
  194. {
  195. #region 销售顶订单表体
  196. sql = string.Format(@"INSERT INTO dbo.DispatchLists
  197. (AutoID , DLID , iCorID , cWhCode , cInvCode , iQuantity , iNum
  198. , iQuotedPrice , iUnitPrice , iTaxUnitPrice , iMoney , iTax , iSum , iDisCount , iNatUnitPrice
  199. , iNatMoney , iNatTax , iNatSum , iNatDisCount , iSettleNum , iSettleQuantity , iBatch , cBatch
  200. , bSettleAll , cMemo , iTB , dvDate , TBQuantity , TBNum , iSOsID , iDLsID
  201. , KL , KL2 , cInvName , iTaxRate
  202. , fOutQuantity , fOutNum , cItemCode , cItem_class , fSaleCost , fSalePrice , cVenAbbName
  203. , cItemName , cItem_CName
  204. , bIsSTQc , iInvExchRate , cUnitID , cCode , iRetQuantity , fEnSettleQuan , fEnSettleSum , iSettlePrice , dMDate , bGsp
  205. , cGspState , cSoCode , cCorCode , iPPartSeqID , iPPartID , iPPartQty , cContractID , cContractTagCode , cContractRowGuid
  206. , iMassDate , cMassUnit , bQANeedCheck , bQAUrgency , bQAChecking , bQAChecked , iQAQuantity , iQANum
  207. , cCusInvCode , cCusInvName , fsumsignquantity , fsumsignnum , cbaccounter , bcosting , cordercode
  208. , iorderrowno , fcusminprice , icostquantity , icostsum , ispecialtype , cvmivencode , iexchsum
  209. , imoneysum , irowno , frettbquantity , fretsum , iExpiratDateCalcu , dblPreExchMomey , dblPreMomey , idemandtype
  210. , cdemandcode , cdemandmemo , cdemandid , idemandseq , cvencode , cReasonCode , cInvSN , iInvSNCount , bneedsign
  211. , bsignover , bneedloss , flossrate , frlossqty , fulossqty , isettletype , crelacuscode , cLossMaker
  212. , dLossDate , dLossTime , icoridlsid , fretoutqty , body_outid , fVeriBillQty
  213. , fVeriBillSum , fVeriRetQty , fVeriRetSum , fLastSettleQty
  214. , fLastSettleSum , cBookWhcode , cInVouchType , cPosition , fretqtywkp , fretqtyykp , frettbqtyykp
  215. , fretsumykp , dkeepdate , cSCloser , isaleoutid , bsaleprice , bgift , bmpforderclosed
  216. , cbSysBarCode , fxjquantity , fxjnum
  217. , bIAcreatebill , cParentCode , cChildCode , fchildqty , fchildrate , iCalcType , fappretwkpqty , fappretwkpsum
  218. , fappretykpqty , fappretykpsum , fappretwkptbqty , fappretykptbqty , irtnappid , crtnappcode , fretailrealamount
  219. , fretailsettleamount , cFactoryCode , GCSourceId , GCSourceIds , cConfirmer , dConfirmDate )
  220. SELECT
  221. @AutoID , @DLID , @cWhCode , @cInvCode , @iQuantity , @iNum
  222. , iQuotedPrice , iUnitPrice , iTaxUnitPrice , iMoney , iTax , iSum , iDisCount , iNatUnitPrice
  223. , iNatMoney , iNatTax , iNatSum , iNatDisCount , NULL , NULL
  224. , 0 , cMemo , 0 , NULL , 0 , NULL , iSOsID , NULL
  225. , KL , KL2 , cInvName , iTaxRate
  226. , fOutQuantity , fOutNum , cItemCode , cItem_class , fSaleCost , fSalePrice ,NUll
  227. , cItemName , cItem_CName
  228. , 0 , iInvExchRate , cUnitID , NULL , 0 , 0 , 0 , 0 , NULL , 0
  229. , NULL , @cSoCode , NULL , iPPartSeqID , iPPartID , iPPartQty , cContractID , cContractTagCode , cContractRowGuid
  230. , NULL , NULL , 0 , 0 , 0 , 0 , 0 , 0
  231. , cCusInvCode , cCusInvName , 0 , 0 , NULL , 0 , @cordercode
  232. , NULL , fcusminprice , icostquantity , icostsum , NULL , NULL , iexchsum
  233. , imoneysum , @irowno , 0 , 0 , NULL , 0 , 0 , NULL
  234. , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL
  235. , NULL , NULL , NULL , 0 , 0 , NULL , NULL , NULL
  236. , NULL , NULL , NULL , NULL , NULL , NULL
  237. , NULL , NULL , NULL , NULL
  238. , NULL , NULL , NULL , NULL , NULL , NULL , NULL
  239. , NULL , NULL , NULL , isaleoutid , 1 , 0 , 0
  240. , cbSysBarCode , NULL , NULL
  241. , 1 , NULL , NULL , NULL , NULL , NULL , NULL , NULL
  242. , NULL , NULL , NULL , NULL , NULL , NULL , NULL
  243. , NULL , NULL , NULL , NULL , NULL , NULL FROM SO_SODetails a
  244. where a.AutoID ='" + body.SODetailID + "'");
  245. cmd.Parameters.Clear();
  246. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  247. cmd.Parameters.Add(new SqlParameter("@DLID", iFatherId));
  248. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  249. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  250. cmd.Parameters.Add(new SqlParameter("@iNum",body.Amount));
  251. cmd.Parameters.Add(new SqlParameter("@cSoCode", head.SOCode));
  252. cmd.Parameters.Add(new SqlParameter("@cordercode", head.SOCode));
  253. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  254. //cmd.Parameters.Add(new SqlParameter("@dDate", data.dMate));
  255. cmd.CommandText = sql;
  256. try
  257. {
  258. int count = cmd.ExecuteNonQuery();
  259. if (count <= 0)
  260. {
  261. log.Error("生成销售出库单表体失败,受影响行数<=0;");
  262. throw new Exception("生成销售出库单表体失败,受影响行数<=0;");
  263. }
  264. }
  265. catch (Exception ex)
  266. {
  267. log.Error("生成销售出库单表体失败!销售订单号:" + head.SOCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  268. throw new Exception("生成销售出库单表体失败!销售订单号:" + head.SOCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql, ex);
  269. }
  270. #endregion
  271. iChildId--;
  272. }
  273. #endregion
  274. sql = @"
  275. select a.DLID ,a.cDLCode ,a.cCusCode,c.cCusName,a.cDepCode ,
  276. d.cDepName,b.cordercode ,a.cMaker ,a.dcreatesystime ,
  277. b.AutoID ,b.irowno,b.cInvCode ,b.iQuantity ,b.iNum ,b.iSOsID
  278. FROM dbo.DispatchList a
  279. INNER JOIN dbo.DispatchLists b ON a.DLID = b.DLID
  280. LEFT JOIN dbo.Customer c ON a.cCusCode = c.cCusCode
  281. left join Department d on a.cDepCode=d.cDepCode WHERE a.DLID = '{0}' ";
  282. sql = string.Format(sql, iFatherId);
  283. dt = DBHelper.SQlReturnData(sql, cmd);
  284. }
  285. msg = JsonConvert.SerializeObject(dt);
  286. cmd.Transaction.Commit();
  287. }
  288. catch (Exception ex)
  289. {
  290. cmd.Transaction.Rollback();
  291. log.Error(ex.Message);
  292. throw new Exception(ex.Message);
  293. }
  294. return msg;
  295. }
  296. /// <summary>
  297. /// 审核销售发货
  298. /// </summary>
  299. /// <param name="infos"></param>
  300. /// <returns></returns>
  301. public string Approve(List<ICSSalesDeliveryNotice> infos)
  302. {
  303. List<ICSSalesDeliveryNotice> szJson = new List<ICSSalesDeliveryNotice>();
  304. DataTable dt = null;
  305. string json = "";
  306. if (infos.Count <= 0)
  307. {
  308. throw new Exception("传送数据为空!");
  309. }
  310. string res = string.Empty;
  311. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  312. conn.Open();
  313. SqlTransaction sqlTran = conn.BeginTransaction();
  314. SqlCommand cmd = new SqlCommand();
  315. cmd.Transaction = sqlTran;
  316. cmd.Connection = conn;
  317. try
  318. {
  319. string sql = string.Empty;
  320. foreach (ICSSalesDeliveryNotice info in infos)
  321. {
  322. if (info.MTime < new DateTime(2000, 01, 01))
  323. throw new Exception("请输入正确的操作时间:" + info.MTime);
  324. sql = @"UPDATE DispatchList SET cMaker ='" + info.User + @"' ,
  325. dverifysystime =CONVERT(VARCHAR(50),GETDATE(),112),dverifydate =GETDATE() WHERE DLID='{0}'";
  326. sql = string.Format(sql, info.ID);
  327. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  328. }
  329. cmd.Transaction.Commit();
  330. return json;
  331. }
  332. catch (Exception ex)
  333. {
  334. cmd.Transaction.Rollback();
  335. log.Error(ex.Message);
  336. throw new Exception(ex.Message);
  337. }
  338. finally
  339. {
  340. if (conn.State == ConnectionState.Open)
  341. {
  342. conn.Close();
  343. }
  344. conn.Dispose();
  345. }
  346. }
  347. /// <summary>
  348. /// 删除销售发货
  349. /// </summary>
  350. /// <param name="infos"></param>
  351. /// <returns></returns>
  352. public string Delete(List<ICSSalesDeliveryNotice> infos)
  353. {
  354. List<ICSSalesDeliveryNotice> szJson = new List<ICSSalesDeliveryNotice>();
  355. if (infos.Count <= 0)
  356. {
  357. throw new Exception("传送数据为空!");
  358. }
  359. string res = string.Empty;
  360. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  361. conn.Open();
  362. SqlTransaction sqlTran = conn.BeginTransaction();
  363. SqlCommand cmd = new SqlCommand();
  364. cmd.Transaction = sqlTran;
  365. cmd.Connection = conn;
  366. try
  367. {
  368. string sql = string.Empty;
  369. foreach (ICSSalesDeliveryNotice info in infos)
  370. {
  371. if (info.MTime < new DateTime(2000, 01, 01))
  372. throw new Exception("请输入正确的操作时间:" + info.MTime);
  373. sql = @" DELETE DispatchList WHERE DLID={0}";
  374. sql = string.Format(sql, info.ID);
  375. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  376. }
  377. cmd.Transaction.Commit();
  378. return res;
  379. ;
  380. }
  381. catch (Exception ex)
  382. {
  383. cmd.Transaction.Rollback();
  384. log.Error(ex.Message);
  385. throw new Exception(ex.Message);
  386. }
  387. finally
  388. {
  389. if (conn.State == ConnectionState.Open)
  390. {
  391. conn.Close();
  392. }
  393. conn.Dispose();
  394. }
  395. }
  396. /// <summary>
  397. ///
  398. /// 记录日志
  399. /// </summary>
  400. /// <param name="Bills"></param>
  401. private void LogInfo(List<ICSSalesDeliveryNotice> Bills)
  402. {
  403. string HeadList = string.Empty;
  404. string BodyList = string.Empty;
  405. foreach (ICSSalesDeliveryNotice head in Bills)
  406. {
  407. HeadList += "\r\n 表头主键ID:" + head.ID + ",用户:" + head.User;
  408. foreach (ICSSalesDeliveryNotices 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. }