IcsFromERPJob
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.

261 lines
14 KiB

3 months ago
  1. using Quartz;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace ICSSoft.FromERP
  13. {
  14. /// <summary>
  15. /// 销售费用明细账
  16. /// </summary>
  17. public class YERP_004 : IJob
  18. {
  19. private static object key = new object();
  20. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  21. public void Execute(IJobExecutionContext context)
  22. {
  23. try
  24. {
  25. lock (key)
  26. {
  27. log.Info("开始……………………………………………………………………");
  28. Execute();
  29. log.Info("结束……………………………………………………………………");
  30. }
  31. }
  32. catch (Exception ex)
  33. {
  34. log.Error(ex.ToString());
  35. }
  36. }
  37. public async void Execute()
  38. {
  39. try
  40. {
  41. Configuration config = GetConfig();
  42. //string url = config.ConnectionStrings.ConnectionStrings["APIAddStdWorkHour"].ConnectionString.ToString();
  43. var Dates = DateTime.Now;
  44. string conStr = ICSHelper.GetConnectString();
  45. // 202404
  46. // 过滤时间为上个月
  47. string Date = Dates.ToString("yyyy");
  48. string Muoth = Dates.AddMonths(-1).ToString("MM");
  49. string Day = Dates.ToString("dd");
  50. //<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:yerp="http://skch.com/YERP_004_SALES_COST_STMT">
  51. // <soapenv:Header/>
  52. // <soapenv:Body>
  53. // <yerp:YERP_004_OA1_MT>
  54. // <!--1 or more repetitions:-->
  55. // <SL_CST_SPTDOC>
  56. // <YYYY>?</YYYY>
  57. // <MM>?</MM>
  58. // <DD>?</DD>
  59. // <SPTDOC>?</SPTDOC>
  60. // <SMR>?</SMR>
  61. // <!--Optional:-->
  62. // <DBTO>?</DBTO>
  63. // <!--Optional:-->
  64. // <CRDTO>?</CRDTO>
  65. // <INVT>?</INVT>
  66. // <BLNC>?</BLNC>
  67. // <!--Optional:-->
  68. // <WH_CST>?</WH_CST>
  69. // <!--Optional:-->
  70. // <TRNSF_CST>?</TRNSF_CST>
  71. // <!--Optional:-->
  72. // <XBT_CST>?</XBT_CST>
  73. // <!--Optional:-->
  74. // <NLD_CST>?</NLD_CST>
  75. // <!--Optional:-->
  76. // <SMPL_CST>?</SMPL_CST>
  77. // <!--Optional:-->
  78. // <TRNSF_AGNC_CST>?</TRNSF_AGNC_CST>
  79. // <!--Optional:-->
  80. // <ADTV>?</ADTV>
  81. // <!--Optional:-->
  82. // <NCLSN_CST>?</NCLSN_CST>
  83. // <!--Optional:-->
  84. // <SVC_CST>?</SVC_CST>
  85. // </SL_CST_SPTDOC>
  86. // </yerp:YERP_004_OA1_MT>
  87. // </soapenv:Body>
  88. // </soapenv:Envelope>
  89. var beginPeriod = "202301";//Date + Muoth;
  90. var endPeriod = "202303";//Date + Muoth;
  91. string sqls = @"
  92. begin transaction
  93. exec GL_Ledger @tblname=N'zyywsrmxz',@KmCode=N'6601',@beginPeriod='{0}',@endPeriod='{1}',@bVouch=0,@bequal=1,@sum=0,@bMJ=1,@swhere=N'',@sAuth=N'',@ReportID=N'GL13',@ReportType=0,@iUnite=0
  94. select autoid,iyear YYYY,imonth MM,iday DD,csign_no SPTDOC,cDigest SMR,cDCode ODR_ACC,md DBTO_AMT,mc CRDTO_AMT,tmpme tmpme
  95. into #test from tempdb..zyywsrmxz a
  96. select YYYY,SPTDOC,MM,DD,sum(DBTO_AMT) DBTO_AMT,sum(CRDTO_AMT) CRDTO_AMT,sum(isnull(tmpme,0)) tmpme
  97. into #test2
  98. from #test a
  99. where SPTDOC is not null and DD IS NOT NULL
  100. group by SPTDOC,YYYY,MM,DD
  101. order by SPTDOC
  102. update a set DBTO_AMT=b.DBTO_AMT,a.CRDTO_AMT=b.CRDTO_AMT,a.tmpme=b.tmpme from #test a
  103. left join #test2 b on a.SPTDOC=b.SPTDOC and a.MM=b.MM and a.DD=b.DD
  104. where a.sptdoc is not null
  105. select autoid,sptdoc,YYYY,MM,DD into #test3 from #test where sptdoc is not null
  106. select s.* into #test4 from (select *,ROW_NUMBER() over (partition by sptdoc+CAST(MM AS VARCHAR(255))+CAST(DD AS VARCHAR(255)) order by autoid) as group_idx from #test3)s where s.group_idx>1
  107. delete a from #test a
  108. left join #test4 b on a.autoid=b.autoid
  109. where b.autoid is not null
  110. select *,(case when (Select Sum(-tmpme) from #test where autoid<=a.autoid) =0 then '平' else (case when (Select Sum(-tmpme) from #test where autoid<=a.autoid) >0 then '贷' else '借' end) end) INVT,
  111. (case when (Select Sum(-tmpme) from #test where autoid<=a.autoid)<0 then -(Select Sum(-tmpme) from #test where autoid<=a.autoid) else (Select Sum(-tmpme) from #test where autoid<=a.autoid) end) BLNC into #test5 from #test a
  112. select cCode SBJ_CD,ccode_name SBJ_NM,imonth,iday,csign_no SPTDOC,sum(md) DBTO_AMT
  113. into #test6
  114. from tempdb..zyywsrmxz a
  115. where csign_no is not null
  116. group by ccode_name,csign_no,cCode,imonth,iday
  117. order by csign_no,imonth,iday
  118. select DISTINCT sptdoc,imonth,iday into #test7 from #test6
  119. select *,ROW_NUMBER() over (order by sptdoc,imonth,iday) rownumber into #test8 from #test7
  120. declare @k int
  121. set @k=1
  122. while @k<=(select COUNT(*) from #test7)
  123. begin
  124. declare @i int
  125. set @i=0
  126. while @i<1
  127. begin
  128. insert into #test6 (SBJ_CD,SBJ_NM,imonth,iday,SPTDOC,DBTO_AMT)
  129. select a.ccode,a.ccode_name,(select imonth from #test8 where rownumber=@k),(select iday from #test8 where rownumber=@k),(select SPTDOC from #test8 where rownumber=@k),0
  130. from code a left join #test6 b on b.SPTDOC=(select SPTDOC from #test8 where rownumber=@k) and imonth=(select imonth from #test8 where rownumber=@k) and iday=(select iday from #test8 where rownumber=@k) and SBJ_NM=ccode_name
  131. where a.iYear=2024 and ( bclose=0 and ( ccode like N'6601%' or ccode_name like N'6601%' or ccode_engl like N'6601%' or chelp like N'6601%' )) and ccode <>'6601' and b.SPTDOC is null
  132. set @i=@i +1
  133. end
  134. set @k=@k +1
  135. end
  136. SELECT
  137. SPTDOC,imonth,iday,
  138. SUM(CASE WHEN SBJ_CD = '660101' THEN DBTO_AMT ELSE 0 END) WH_CST,
  139. SUM(CASE WHEN SBJ_CD = '660102' THEN DBTO_AMT ELSE 0 END) TRNSF_CST,
  140. SUM(CASE WHEN SBJ_CD = '660103' THEN DBTO_AMT ELSE 0 END) XBT_CST,
  141. SUM(CASE WHEN SBJ_CD = '660104' THEN DBTO_AMT ELSE 0 END) NLD_CST,
  142. SUM(CASE WHEN SBJ_CD = '660105' THEN DBTO_AMT ELSE 0 END) SMPL_CST,
  143. SUM(CASE WHEN SBJ_CD = '660106' THEN DBTO_AMT ELSE 0 END) TRNSF_AGNC_CST,
  144. SUM(CASE WHEN SBJ_CD = '660107' THEN DBTO_AMT ELSE 0 END) ADTV,
  145. SUM(CASE WHEN SBJ_CD = '660108' THEN DBTO_AMT ELSE 0 END) NCLSN_CST,
  146. SUM(CASE WHEN SBJ_CD = '660109' THEN DBTO_AMT ELSE 0 END) SVC_CST
  147. into #test9
  148. FROM
  149. #test6
  150. GROUP BY
  151. SPTDOC,imonth,iday
  152. select YYYY,MM,DD,A.SPTDOC,SMR,DBTO_AMT DBTO,CRDTO_AMT CRDTO,INVT,BLNC,WH_CST,TRNSF_CST,XBT_CST,NLD_CST,SMPL_CST,TRNSF_AGNC_CST,ADTV,NCLSN_CST,SVC_CST from #test5 a left join #test9 b on a.SPTDOC=b.SPTDOC and a.MM=b.imonth and a.DD=b.iday
  153. rollback transaction
  154. ";
  155. sqls = string.Format(sqls, beginPeriod, endPeriod);
  156. log.Info("YERP_004 sql:" + sqls);
  157. DataTable vbsdt = ICSHelper.ExecuteTable(conStr, sqls);
  158. log.Info("sql结果数量" + vbsdt.Rows.Count);
  159. if (vbsdt.Rows.Count > 0)
  160. {
  161. StringBuilder soapRequestData = new StringBuilder();
  162. soapRequestData.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:yerp=\"http://skch.com/YERP_004_SALES_COST_STMT\">");
  163. soapRequestData.Append("<soapenv:Header/>");
  164. soapRequestData.Append("<soapenv:Body>");
  165. soapRequestData.Append("<yerp:YERP_004_OA1_MT>");
  166. //循环数据
  167. foreach (DataRow itemRow in vbsdt.Rows)
  168. {
  169. //原循环部分 begin
  170. soapRequestData.Append("<SL_CST_SPTDOC>");
  171. soapRequestData.Append(itemRow["YYYY"] != null ? "<YYYY>" + itemRow["YYYY"].ToString() + "</YYYY>" : "<YYYY>" + "</YYYY>");
  172. soapRequestData.Append(itemRow["MM"] != null ? "<MM>" + itemRow["MM"].ToString() + "</MM>" : "<MM>" + "</MM>");
  173. soapRequestData.Append(itemRow["DD"] != null ? "<DD>" + itemRow["DD"].ToString() + "</DD>" : "<DD>" + "</DD>");
  174. soapRequestData.Append(itemRow["SPTDOC"] != null ? "<SPTDOC>" + itemRow["SPTDOC"].ToString() + "</SPTDOC>" : "<SPTDOC>" + "</SPTDOC>");
  175. soapRequestData.Append(itemRow["SMR"] != null ? "<SMR>" + itemRow["SMR"].ToString() + "</SMR>" : "<SMR>" + "</SMR>");
  176. soapRequestData.Append(itemRow["DBTO"] != null ? "<DBTO>" + itemRow["DBTO"].ToString() + "</DBTO>" : "<DBTO>" + "</DBTO>");
  177. soapRequestData.Append(itemRow["CRDTO"] != null ? "<CRDTO>" + itemRow["CRDTO"].ToString() + "</CRDTO>" : "<CRDTO>" + "</CRDTO>");
  178. soapRequestData.Append(itemRow["INVT"] != null ? "<INVT>" + itemRow["INVT"].ToString() + "</INVT>" : "<INVT>" + "</INVT>");
  179. soapRequestData.Append(itemRow["BLNC"] != null ? "<BLNC>" + itemRow["BLNC"].ToString() + "</BLNC>" : "<BLNC>" + "</BLNC>");
  180. soapRequestData.Append(itemRow["WH_CST"] != null ? "<WH_CST>" + itemRow["WH_CST"].ToString() + "</WH_CST>" : "<WH_CST>" + "</WH_CST>");
  181. soapRequestData.Append(itemRow["TRNSF_CST"] != null ? "<TRNSF_CST>" + itemRow["TRNSF_CST"].ToString() + "</TRNSF_CST>" : "<TRNSF_CST>" + "</TRNSF_CST>");
  182. soapRequestData.Append(itemRow["XBT_CST"] != null ? "<XBT_CST>" + itemRow["XBT_CST"].ToString() + "</XBT_CST>" : "<XBT_CST>" + "</XBT_CST>");
  183. soapRequestData.Append(itemRow["NLD_CST"] != null ? "<NLD_CST>" + itemRow["NLD_CST"].ToString() + "</NLD_CST>" : "<NLD_CST>" + "</NLD_CST>");
  184. soapRequestData.Append(itemRow["SMPL_CST"] != null ? "<SMPL_CST>" + itemRow["SMPL_CST"].ToString() + "</SMPL_CST>" : "<SMPL_CST>" + "</SMPL_CST>");
  185. soapRequestData.Append(itemRow["TRNSF_AGNC_CST"] != null ? "<TRNSF_AGNC_CST>" + itemRow["TRNSF_AGNC_CST"].ToString() + "</TRNSF_AGNC_CST>" : "<TRNSF_AGNC_CST>" + "</TRNSF_AGNC_CST>");
  186. soapRequestData.Append(itemRow["ADTV"] != null ? "<ADTV>" + itemRow["ADTV"].ToString() + "</ADTV>" : "<ADTV>" + "</ADTV>");
  187. soapRequestData.Append(itemRow["NCLSN_CST"] != null ? "<NCLSN_CST>" + itemRow["NCLSN_CST"].ToString() + "</NCLSN_CST>" : "<NCLSN_CST>" + "</NCLSN_CST>");
  188. soapRequestData.Append(itemRow["SVC_CST"] != null ? "<SVC_CST>" + itemRow["SVC_CST"].ToString() + "</SVC_CST>" : "<SVC_CST>" + "</SVC_CST>");
  189. soapRequestData.Append("</SL_CST_SPTDOC>");
  190. //原循环部分 end
  191. }
  192. soapRequestData.Append("</yerp:YERP_004_OA1_MT>");
  193. soapRequestData.Append("</soapenv:Body>");
  194. soapRequestData.Append("</soapenv:Envelope>");
  195. string postData = soapRequestData.ToString();
  196. log.Info("YERP_004 xmlRequest:" + postData);
  197. string statusCode;
  198. string resultContent;
  199. ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
  200. RestOpHelper rop = new RestOpHelper();
  201. rop.Clientp12path = @"C:\Users\Administrator\Desktop\yonyou-cert\wildcard.pfx";
  202. rop.Clientp12PassWord = "Yonyou2024!";
  203. rop.Url = @"https://yerp-proxy-sap-dev.skchemicals.com/XISOAPAdapter/MessageServlet?senderParty=&senderService=YERP_D&receiverParty=&receiverService=&interface=YERP_004_OA1_SI&interfaceNamespace=http://skch.com/YERP_004_SALES_COST_STMT";
  204. rop.RequestSAP(rop.Url, 10000, postData, "IF_YERP", "Xtxc355860", out statusCode, out resultContent);
  205. log.Info("YERP_004 statusCode:" + statusCode + "\r\n" + "resultContent:" + resultContent);
  206. }
  207. }
  208. catch (Exception ex)
  209. {
  210. log.Error(ex.ToString());
  211. }
  212. }
  213. public static Configuration GetConfig()
  214. {
  215. Assembly assembly = Assembly.GetCallingAssembly();
  216. string path = string.Format("{0}.config", assembly.Location);
  217. if (!File.Exists(path))
  218. {
  219. throw new FileNotFoundException(path + "路径下的文件未找到!");
  220. }
  221. try
  222. {
  223. ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
  224. configFile.ExeConfigFilename = path;
  225. Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
  226. return config;
  227. }
  228. catch (Exception)
  229. {
  230. throw;
  231. }
  232. }
  233. }
  234. }