using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ICSSoft.FromERP
{
///
/// 财务费用 明细账
///
public class YERP_007 : IJob
{
private static object key = new object();
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void Execute(IJobExecutionContext context)
{
try
{
lock (key)
{
log.Info("开始……………………………………………………………………");
Execute();
log.Info("结束……………………………………………………………………");
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
public async void Execute()
{
try
{
Configuration config = GetConfig();
var Dates = DateTime.Now;
string conStr = ICSHelper.GetConnectString();
//
//
//
//
//
//
// ?
// 1
// ?
// ?
// ?
// ?
// ?
// ?
// ?
//
// ?
//
// ?
//
// ?
//
//
//
//
//
//
//
//
//
//
// ?
// ?
// ?
// ?
// ?
// ?
// ?
// ?
// ?
//
//
//
//
// 过滤时间为上个月
string Date = Dates.ToString("yyyy");
string beginMuoth = Dates.AddMonths(-1).ToString("MM");
string endMuoth = Dates.AddMonths(-1).ToString("MM");
var beginPeriod = Date + beginMuoth; //"202409";
var endPeriod = Date + endMuoth; //"202409";
string sqls = @"
begin transaction
exec GL_Ledger @tblname=N'cwfymxz',@KmCode=N'6603',@beginPeriod='{0}',@endPeriod='{1}',@bVouch=0,@bequal=1,@sum=0,@bMJ=1,@swhere=N'',@sAuth=N'',@ReportID=N'GL13',@ReportType=0,@iUnite=0
--select * from tempdb..cwfymxz
select autoid,a.iyear YYYY,imonth MM,iday DD,csign_no SPTDOC,cDigest SMR,cDCode ODR_ACC,a.md DBTO_AMT,a.mc CRDTO_AMT,tmpme tmpme,b.cbegind_c
into #test
from tempdb..cwfymxz a
join GL_accsum b on a.cCode = b.ccode and a.iyear = b.iyear and b.iperiod=1
select SPTDOC,YYYY,MM,DD,sum(DBTO_AMT) DBTO_AMT,sum(CRDTO_AMT) CRDTO_AMT,sum(isnull(tmpme,0)) tmpme,cbegind_c
into #test2
from #test a
where SPTDOC is not null and DD IS NOT NULL
group by SPTDOC,YYYY,MM,DD,cbegind_c
order by SPTDOC
update a set DBTO_AMT=b.DBTO_AMT,a.CRDTO_AMT=b.CRDTO_AMT,a.tmpme=b.tmpme from #test a
left join #test2 b on a.SPTDOC=b.SPTDOC and a.MM=b.MM and a.DD=b.DD
where a.sptdoc is not null
select autoid,sptdoc,YYYY,MM,DD into #test3 from #test where sptdoc is not null
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
delete a from #test a
left join #test4 b on a.autoid=b.autoid
where b.autoid is not null
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,
(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
select cCode SBJ_CD,ccode_name SBJ_NM,imonth,iday,csign_no SPTDOC,sum(md) DBTO_AMT
into #test6
from tempdb..cwfymxz a
where csign_no is not null
group by ccode_name,csign_no,cCode,imonth,iday
order by csign_no,imonth,iday
select DISTINCT sptdoc,imonth,iday into #test7 from #test6
select *,ROW_NUMBER() over (order by sptdoc,imonth,iday) rownumber into #test8 from #test7
declare @k int
set @k=1
while @k<=(select COUNT(*) from #test7)
begin
declare @i int
set @i=0
while @i<1
begin
insert into #test6 (SBJ_CD,SBJ_NM,imonth,iday,SPTDOC,DBTO_AMT)
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
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
where a.iYear=2024 and ( bclose=0 and ( ccode like N'6603%' or ccode_name like N'6603%' or ccode_engl like N'6603%' or chelp like N'6603%' )) and ccode <>'6603' and len(ccode)=6
and b.SPTDOC is null
set @i=@i +1
end
set @k=@k +1
end
SELECT
SPTDOC,imonth,iday,
SUM(CASE WHEN SBJ_CD = '660301' THEN DBTO_AMT ELSE 0 END) DBTO_CMSN,
SUM(CASE WHEN SBJ_CD = '660302' THEN DBTO_AMT ELSE 0 END) DBTO_IENS,
SUM(CASE WHEN SBJ_CD = '660303' THEN DBTO_AMT ELSE 0 END) DBTO_XCH_PC
into #test9
FROM
#test6
GROUP BY
SPTDOC,imonth,iday
--select YYYY,MM,DD,A.SPTDOC,SMR,DBTO_AMT DBTO,CRDTO_AMT CRDTO,INVT,BLNC,DBTO_CMSN,DBTO_IENS,DBTO_XCH_PC,'' DBTO_CRDTO_DVSN,cbegind_c
--from #test5 a left join #test9 b on a.SPTDOC=b.SPTDOC and a.MM=b.imonth and a.DD=b.iday
--where DD is null and SPTDOC is null
--group by YYYY,MM,DD,A.SPTDOC,SMR,DBTO_CMSN,DBTO_IENS,DBTO_XCH_PC,'' DBTO_CRDTO_DVSN,cbegind_c
--order by MM
select YYYY Created_Year,MM Created_Month,
sum(DBTO_AMT) DBTO,SUM(DBTO_CMSN) DBTO_CMSN,SUM(DBTO_IENS) DBTO_IENS,SUM(DBTO_XCH_PC) DBTO_XCH_PC
from #test5 a left join #test9 b on a.SPTDOC=b.SPTDOC and a.MM=b.imonth and a.DD=b.iday
where ISNULL(DD,'') != ''
group by YYYY,MM
order by YYYY,MM
rollback transaction";//and dInvCreateDatetime > '{0}' and dInvCreateDatetime < '{1}'
sqls = string.Format(sqls, beginPeriod, endPeriod);
log.Info("YERP_007 sql:" + sqls);
DataTable vbsdt = ICSHelper.ExecuteTable(conStr, sqls);
log.Info("YERP_007 sql结果数量" + vbsdt.Rows.Count);
string sqlyywsr = @"
begin transaction
exec GL_Ledger @tblname=N'cwfymxz',@KmCode=N'6301',@beginPeriod='{0}',@endPeriod='{1}',@bVouch=0,@bequal=1,@sum=0,@bMJ=1,@swhere=N'',@sAuth=N'',@ReportID=N'GL13',@ReportType=0,@iUnite=0
--select * from tempdb..cwfymxz
select autoid,a.iyear YYYY,imonth MM,iday DD,csign_no SPTDOC,cDigest SMR,cDCode ODR_ACC,a.md DBTO_AMT,a.mc CRDTO_AMT,tmpme tmpme,b.cbegind_c
into #test
from tempdb..cwfymxz a
join GL_accsum b on a.cCode = b.ccode and a.iyear = b.iyear and b.iperiod=1
select SPTDOC,YYYY,MM,DD,sum(DBTO_AMT) DBTO_AMT,sum(CRDTO_AMT) CRDTO_AMT,sum(isnull(tmpme,0)) tmpme,cbegind_c
into #test2
from #test a
where SPTDOC is not null and DD IS NOT NULL
group by SPTDOC,YYYY,MM,DD,cbegind_c
order by SPTDOC
update a set DBTO_AMT=b.DBTO_AMT,a.CRDTO_AMT=b.CRDTO_AMT,a.tmpme=b.tmpme from #test a
left join #test2 b on a.SPTDOC=b.SPTDOC and a.MM=b.MM and a.DD=b.DD
where a.sptdoc is not null
select autoid,sptdoc,YYYY,MM,DD into #test3 from #test where sptdoc is not null
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
delete a from #test a
left join #test4 b on a.autoid=b.autoid
where b.autoid is not null
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,
(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
select cCode SBJ_CD,ccode_name SBJ_NM,imonth,iday,csign_no SPTDOC,sum(md) DBTO_AMT
into #test6
from tempdb..cwfymxz a
where csign_no is not null
group by ccode_name,csign_no,cCode,imonth,iday
order by csign_no,imonth,iday
select DISTINCT sptdoc,imonth,iday into #test7 from #test6
select *,ROW_NUMBER() over (order by sptdoc,imonth,iday) rownumber into #test8 from #test7
declare @k int
set @k=1
while @k<=(select COUNT(*) from #test7)
begin
declare @i int
set @i=0
while @i<1
begin
insert into #test6 (SBJ_CD,SBJ_NM,imonth,iday,SPTDOC,DBTO_AMT)
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
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
where a.iYear=2024 and ( bclose=0 and ( ccode like N'6301%' or ccode_name like N'6301%' or ccode_engl like N'6301%' or chelp like N'6301%' )) and ccode <>'6301' and len(ccode)=6
and b.SPTDOC is null
set @i=@i +1
end
set @k=@k +1
end
--SELECT
--SPTDOC,imonth,iday,
-- sum(DBTO_AMT)
-- -- into #test9
-- FROM
-- #test6
-- GROUP BY
-- SPTDOC,imonth,iday
--GROUP BY
-- SPTDOC,imonth,iday
SELECT
SPTDOC,imonth,iday,sum(DBTO_AMT) as sumss
into #test9
FROM
#test6
GROUP BY
SPTDOC,imonth,iday
select YYYY Created_Year,MM Created_Month,SUM(ISNULL(sumss,0)) NOSLS_INCM
from #test5 a left join #test9 b on a.SPTDOC=b.SPTDOC and a.MM=b.imonth and a.DD=b.iday
group by YYYY,MM
order by MM
rollback transaction";
sqlyywsr = string.Format(sqlyywsr, beginPeriod, endPeriod);
log.Info("YERP_007 营业外收入 sqlyywsr:" + sqlyywsr);
DataTable vbsyywsrdt = ICSHelper.ExecuteTable(conStr, sqlyywsr);
log.Info("YERP_007 营业外收入sql结果数量" + vbsyywsrdt.Rows.Count);
string sqlyywzc = @"begin transaction
exec GL_Ledger @tblname=N'cwfymxz',@KmCode=N'6711',@beginPeriod='{0}',@endPeriod='{1}',@bVouch=0,@bequal=1,@sum=0,@bMJ=1,@swhere=N'',@sAuth=N'',@ReportID=N'GL13',@ReportType=0,@iUnite=0
--select * from tempdb..cwfymxz
select autoid,a.iyear YYYY,imonth MM,iday DD,csign_no SPTDOC,cDigest SMR,cDCode ODR_ACC,a.md DBTO_AMT,a.mc CRDTO_AMT,tmpme tmpme,b.cbegind_c
into #test
from tempdb..cwfymxz a
join GL_accsum b on a.cCode = b.ccode and a.iyear = b.iyear and b.iperiod=1
select SPTDOC,YYYY,MM,DD,sum(DBTO_AMT) DBTO_AMT,sum(CRDTO_AMT) CRDTO_AMT,sum(isnull(tmpme,0)) tmpme,cbegind_c
into #test2
from #test a
where SPTDOC is not null and DD IS NOT NULL
group by SPTDOC,YYYY,MM,DD,cbegind_c
order by SPTDOC
update a set DBTO_AMT=b.DBTO_AMT,a.CRDTO_AMT=b.CRDTO_AMT,a.tmpme=b.tmpme from #test a
left join #test2 b on a.SPTDOC=b.SPTDOC and a.MM=b.MM and a.DD=b.DD
where a.sptdoc is not null
select autoid,sptdoc,YYYY,MM,DD into #test3 from #test where sptdoc is not null
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
delete a from #test a
left join #test4 b on a.autoid=b.autoid
where b.autoid is not null
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,
(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
select cCode SBJ_CD,ccode_name SBJ_NM,imonth,iday,csign_no SPTDOC,sum(md) DBTO_AMT
into #test6
from tempdb..cwfymxz a
where csign_no is not null
group by ccode_name,csign_no,cCode,imonth,iday
order by csign_no,imonth,iday
select DISTINCT sptdoc,imonth,iday into #test7 from #test6
select *,ROW_NUMBER() over (order by sptdoc,imonth,iday) rownumber into #test8 from #test7
declare @k int
set @k=1
while @k<=(select COUNT(*) from #test7)
begin
declare @i int
set @i=0
while @i<1
begin
insert into #test6 (SBJ_CD,SBJ_NM,imonth,iday,SPTDOC,DBTO_AMT)
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
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
where a.iYear=2024 and ( bclose=0 and ( ccode like N'6711%' or ccode_name like N'6711%' or ccode_engl like N'6711%' or chelp like N'6711%' )) and ccode <>'6711' and len(ccode)=6
and b.SPTDOC is null
set @i=@i +1
end
set @k=@k +1
end
--SELECT
--SPTDOC,imonth,iday,
-- sum(DBTO_AMT)
-- -- into #test9
-- FROM
-- #test6
-- GROUP BY
-- SPTDOC,imonth,iday
--GROUP BY
-- SPTDOC,imonth,iday
SELECT
SPTDOC,imonth,iday,sum(DBTO_AMT) as sumss
into #test9
FROM
#test6
GROUP BY
SPTDOC,imonth,iday
select YYYY Created_Year,MM Created_Month,SUM(ISNULL(sumss,0)) NOSLS_SPND
from #test5 a left join #test9 b on a.SPTDOC=b.SPTDOC and a.MM=b.imonth and a.DD=b.iday
group by YYYY,MM
order by MM
rollback transaction";
sqlyywzc = string.Format(sqlyywzc, beginPeriod, endPeriod);
log.Info("YERP_007 营业外支出 sqlyywzc:" + sqlyywzc);
DataTable vbsyywzcdt = ICSHelper.ExecuteTable(conStr, sqlyywzc);
log.Info("YERP_007 营业外支出sql结果数量" + vbsyywzcdt.Rows.Count);
//广州服务器
string conStrgz = ICSHelper.GetConfigString("SysConnectionString_gz");
if (string.IsNullOrEmpty(conStrgz))
{
log.Info("YERP_006_gz conStr未找到。");
return;
}
string sqlsgz = @"
begin transaction
exec GL_Ledger @tblname=N'cwfymxz',@KmCode=N'5603',@beginPeriod='{0}',@endPeriod='{1}',@bVouch=0,@bequal=1,@sum=0,@bMJ=1,@swhere=N'',@sAuth=N'',@ReportID=N'GL13',@ReportType=0,@iUnite=0
--select * from tempdb..cwfymxz
select autoid,a.iyear YYYY,imonth MM,iday DD,csign_no SPTDOC,cDigest SMR,cDCode ODR_ACC,a.md DBTO_AMT,a.mc CRDTO_AMT,tmpme tmpme,b.cbegind_c
into #test
from tempdb..cwfymxz a
join GL_accsum b on a.cCode = b.ccode and a.iyear = b.iyear and b.iperiod=1
select SPTDOC,YYYY,MM,DD,sum(DBTO_AMT) DBTO_AMT,sum(CRDTO_AMT) CRDTO_AMT,sum(isnull(tmpme,0)) tmpme,cbegind_c
into #test2
from #test a
where SPTDOC is not null and DD IS NOT NULL
group by SPTDOC,YYYY,MM,DD,cbegind_c
order by SPTDOC
update a set DBTO_AMT=b.DBTO_AMT,a.CRDTO_AMT=b.CRDTO_AMT,a.tmpme=b.tmpme from #test a
left join #test2 b on a.SPTDOC=b.SPTDOC and a.MM=b.MM and a.DD=b.DD
where a.sptdoc is not null
select autoid,sptdoc,YYYY,MM,DD into #test3 from #test where sptdoc is not null
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
delete a from #test a
left join #test4 b on a.autoid=b.autoid
where b.autoid is not null
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,
(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
select cCode SBJ_CD,ccode_name SBJ_NM,imonth,iday,csign_no SPTDOC,sum(md) DBTO_AMT
into #test6
from tempdb..cwfymxz a
where csign_no is not null
group by ccode_name,csign_no,cCode,imonth,iday
order by csign_no,imonth,iday
select DISTINCT sptdoc,imonth,iday into #test7 from #test6
select *,ROW_NUMBER() over (order by sptdoc,imonth,iday) rownumber into #test8 from #test7
declare @k int
set @k=1
while @k<=(select COUNT(*) from #test7)
begin
declare @i int
set @i=0
while @i<1
begin
insert into #test6 (SBJ_CD,SBJ_NM,imonth,iday,SPTDOC,DBTO_AMT)
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
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
where a.iYear=2024 and ( bclose=0 and ( ccode like N'6603%' or ccode_name like N'6603%' or ccode_engl like N'6603%' or chelp like N'6603%' )) and ccode <>'6603' and len(ccode)=6
and b.SPTDOC is null
set @i=@i +1
end
set @k=@k +1
end
SELECT
SPTDOC,imonth,iday,
SUM(CASE WHEN SBJ_CD = '660301' THEN DBTO_AMT ELSE 0 END) DBTO_CMSN,
SUM(CASE WHEN SBJ_CD = '660302' THEN DBTO_AMT ELSE 0 END) DBTO_IENS,
SUM(CASE WHEN SBJ_CD = '660303' THEN DBTO_AMT ELSE 0 END) DBTO_XCH_PC
into #test9
FROM
#test6
GROUP BY
SPTDOC,imonth,iday
--select YYYY,MM,DD,A.SPTDOC,SMR,DBTO_AMT DBTO,CRDTO_AMT CRDTO,INVT,BLNC,DBTO_CMSN,DBTO_IENS,DBTO_XCH_PC,'' DBTO_CRDTO_DVSN,cbegind_c
--from #test5 a left join #test9 b on a.SPTDOC=b.SPTDOC and a.MM=b.imonth and a.DD=b.iday
--where DD is null and SPTDOC is null
--group by YYYY,MM,DD,A.SPTDOC,SMR,DBTO_CMSN,DBTO_IENS,DBTO_XCH_PC,'' DBTO_CRDTO_DVSN,cbegind_c
--order by MM
select YYYY Created_Year,MM Created_Month,
sum(DBTO_AMT) DBTO,SUM(DBTO_CMSN) DBTO_CMSN,SUM(DBTO_IENS) DBTO_IENS,SUM(DBTO_XCH_PC) DBTO_XCH_PC
from #test5 a left join #test9 b on a.SPTDOC=b.SPTDOC and a.MM=b.imonth and a.DD=b.iday
where ISNULL(DD,'') != ''
group by YYYY,MM
order by YYYY,MM
rollback transaction";//and dInvCreateDatetime > '{0}' and dInvCreateDatetime < '{1}'
sqlsgz = string.Format(sqlsgz, beginPeriod, endPeriod);
log.Info("YERP_007_gz sql:" + sqlsgz);
DataTable vbsgzdt = ICSHelper.ExecuteTable(conStrgz, sqlsgz);
log.Info("YERP_007_gz sql结果数量" + vbsgzdt.Rows.Count);
string sqlyywsr_gz = @"
begin transaction
exec GL_Ledger @tblname=N'cwfymxz',@KmCode=N'5301',@beginPeriod='{0}',@endPeriod='{1}',@bVouch=0,@bequal=1,@sum=0,@bMJ=1,@swhere=N'',@sAuth=N'',@ReportID=N'GL13',@ReportType=0,@iUnite=0
--select * from tempdb..cwfymxz
select autoid,a.iyear YYYY,imonth MM,iday DD,csign_no SPTDOC,cDigest SMR,cDCode ODR_ACC,a.md DBTO_AMT,a.mc CRDTO_AMT,tmpme tmpme,b.cbegind_c
into #test
from tempdb..cwfymxz a
join GL_accsum b on a.cCode = b.ccode and a.iyear = b.iyear and b.iperiod=1
select SPTDOC,YYYY,MM,DD,sum(DBTO_AMT) DBTO_AMT,sum(CRDTO_AMT) CRDTO_AMT,sum(isnull(tmpme,0)) tmpme,cbegind_c
into #test2
from #test a
where SPTDOC is not null and DD IS NOT NULL
group by SPTDOC,YYYY,MM,DD,cbegind_c
order by SPTDOC
update a set DBTO_AMT=b.DBTO_AMT,a.CRDTO_AMT=b.CRDTO_AMT,a.tmpme=b.tmpme from #test a
left join #test2 b on a.SPTDOC=b.SPTDOC and a.MM=b.MM and a.DD=b.DD
where a.sptdoc is not null
select autoid,sptdoc,YYYY,MM,DD into #test3 from #test where sptdoc is not null
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
delete a from #test a
left join #test4 b on a.autoid=b.autoid
where b.autoid is not null
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,
(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
select cCode SBJ_CD,ccode_name SBJ_NM,imonth,iday,csign_no SPTDOC,sum(md) DBTO_AMT
into #test6
from tempdb..cwfymxz a
where csign_no is not null
group by ccode_name,csign_no,cCode,imonth,iday
order by csign_no,imonth,iday
select DISTINCT sptdoc,imonth,iday into #test7 from #test6
select *,ROW_NUMBER() over (order by sptdoc,imonth,iday) rownumber into #test8 from #test7
declare @k int
set @k=1
while @k<=(select COUNT(*) from #test7)
begin
declare @i int
set @i=0
while @i<1
begin
insert into #test6 (SBJ_CD,SBJ_NM,imonth,iday,SPTDOC,DBTO_AMT)
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
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
where a.iYear=2024 and ( bclose=0 and ( ccode like N'6301%' or ccode_name like N'6301%' or ccode_engl like N'6301%' or chelp like N'6301%' )) and ccode <>'6301' and len(ccode)=6
and b.SPTDOC is null
set @i=@i +1
end
set @k=@k +1
end
--SELECT
--SPTDOC,imonth,iday,
-- sum(DBTO_AMT)
-- -- into #test9
-- FROM
-- #test6
-- GROUP BY
-- SPTDOC,imonth,iday
--GROUP BY
-- SPTDOC,imonth,iday
SELECT
SPTDOC,imonth,iday,sum(DBTO_AMT) as sumss
into #test9
FROM
#test6
GROUP BY
SPTDOC,imonth,iday
select YYYY Created_Year,MM Created_Month,SUM(ISNULL(sumss,0)) NOSLS_INCM
from #test5 a left join #test9 b on a.SPTDOC=b.SPTDOC and a.MM=b.imonth and a.DD=b.iday
group by YYYY,MM
order by MM
rollback transaction";
sqlyywsr_gz = string.Format(sqlyywsr_gz, beginPeriod, endPeriod);
log.Info("YERP_007_gz 营业外收入 sqlyywsr_gz:" + sqlyywsr_gz);
DataTable vbsyywsrdt_gz = ICSHelper.ExecuteTable(conStrgz, sqlyywsr_gz);
log.Info("YERP_007_gz 营业外收入sql结果数量" + vbsyywsrdt_gz.Rows.Count);
string sqlyywzc_gz = @"begin transaction
exec GL_Ledger @tblname=N'cwfymxz',@KmCode=N'5711',@beginPeriod='{0}',@endPeriod='{1}',@bVouch=0,@bequal=1,@sum=0,@bMJ=1,@swhere=N'',@sAuth=N'',@ReportID=N'GL13',@ReportType=0,@iUnite=0
--select * from tempdb..cwfymxz
select autoid,a.iyear YYYY,imonth MM,iday DD,csign_no SPTDOC,cDigest SMR,cDCode ODR_ACC,a.md DBTO_AMT,a.mc CRDTO_AMT,tmpme tmpme,b.cbegind_c
into #test
from tempdb..cwfymxz a
join GL_accsum b on a.cCode = b.ccode and a.iyear = b.iyear and b.iperiod=1
select SPTDOC,YYYY,MM,DD,sum(DBTO_AMT) DBTO_AMT,sum(CRDTO_AMT) CRDTO_AMT,sum(isnull(tmpme,0)) tmpme,cbegind_c
into #test2
from #test a
where SPTDOC is not null and DD IS NOT NULL
group by SPTDOC,YYYY,MM,DD,cbegind_c
order by SPTDOC
update a set DBTO_AMT=b.DBTO_AMT,a.CRDTO_AMT=b.CRDTO_AMT,a.tmpme=b.tmpme from #test a
left join #test2 b on a.SPTDOC=b.SPTDOC and a.MM=b.MM and a.DD=b.DD
where a.sptdoc is not null
select autoid,sptdoc,YYYY,MM,DD into #test3 from #test where sptdoc is not null
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
delete a from #test a
left join #test4 b on a.autoid=b.autoid
where b.autoid is not null
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,
(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
select cCode SBJ_CD,ccode_name SBJ_NM,imonth,iday,csign_no SPTDOC,sum(md) DBTO_AMT
into #test6
from tempdb..cwfymxz a
where csign_no is not null
group by ccode_name,csign_no,cCode,imonth,iday
order by csign_no,imonth,iday
select DISTINCT sptdoc,imonth,iday into #test7 from #test6
select *,ROW_NUMBER() over (order by sptdoc,imonth,iday) rownumber into #test8 from #test7
declare @k int
set @k=1
while @k<=(select COUNT(*) from #test7)
begin
declare @i int
set @i=0
while @i<1
begin
insert into #test6 (SBJ_CD,SBJ_NM,imonth,iday,SPTDOC,DBTO_AMT)
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
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
where a.iYear=2024 and ( bclose=0 and ( ccode like N'6711%' or ccode_name like N'6711%' or ccode_engl like N'6711%' or chelp like N'6711%' )) and ccode <>'6711' and len(ccode)=6
and b.SPTDOC is null
set @i=@i +1
end
set @k=@k +1
end
--SELECT
--SPTDOC,imonth,iday,
-- sum(DBTO_AMT)
-- -- into #test9
-- FROM
-- #test6
-- GROUP BY
-- SPTDOC,imonth,iday
--GROUP BY
-- SPTDOC,imonth,iday
SELECT
SPTDOC,imonth,iday,sum(DBTO_AMT) as sumss
into #test9
FROM
#test6
GROUP BY
SPTDOC,imonth,iday
select YYYY Created_Year,MM Created_Month,SUM(ISNULL(sumss,0)) NOSLS_SPND
from #test5 a left join #test9 b on a.SPTDOC=b.SPTDOC and a.MM=b.imonth and a.DD=b.iday
group by YYYY,MM
order by MM
rollback transaction";
sqlyywzc_gz = string.Format(sqlyywzc_gz, beginPeriod, endPeriod);
log.Info("YERP_007_gz 营业外支出 sqlyywzc_gz:" + sqlyywzc_gz);
DataTable vbsyywzcdt_gz = ICSHelper.ExecuteTable(conStrgz, sqlyywzc_gz);
log.Info("YERP_007_gz 营业外支出sql结果数量" + vbsyywzcdt_gz.Rows.Count);
if (vbsdt.Rows.Count + vbsgzdt.Rows.Count > 0)
{
var datasrrows = new List();
var datazcrows = new List();
var datasrrows_gz = new List();
var datazcrows_gz = new List();
StringBuilder soapRequestData = new StringBuilder();
soapRequestData.Append("");
soapRequestData.Append("");
soapRequestData.Append("");
soapRequestData.Append("");
//循环数据 DBTO_CRDTO_DVSN
if(vbsdt.Rows.Count > 0)
{
foreach (DataRow itemRow in vbsdt.Rows)
{
soapRequestData.Append("");
soapRequestData.Append(itemRow["Created_Year"] != null ? "" + itemRow["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["Created_Month"] != null ? "" + itemRow["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "100" + "");
soapRequestData.Append(itemRow["DBTO"] != null ? "" + itemRow["DBTO"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["DBTO_CMSN"] != null ? "" + itemRow["DBTO_CMSN"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["DBTO_IENS"] != null ? "" + itemRow["DBTO_IENS"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["DBTO_XCH_PC"] != null ? "" + itemRow["DBTO_XCH_PC"].ToString() + "" : "" + "");
// 根据年月关联NOSLS_INCM 营业外收入
if (vbsyywsrdt.Rows.Count > 0)
{
var NOSLS_INCM = vbsyywsrdt.Select("Created_Year='" + itemRow["Created_Year"].ToString() + "' and Created_Month='" + itemRow["Created_Month"].ToString() + "'");
if (NOSLS_INCM.Length > 0)
{
var NOSLS_INCMrow = NOSLS_INCM.FirstOrDefault();
soapRequestData.Append(NOSLS_INCMrow["NOSLS_INCM"] != null ? "" + NOSLS_INCMrow["NOSLS_INCM"].ToString() + "" : "" + "");
var datatemp = new TempDatas
{
YYYY = itemRow["Created_Year"].ToString(),
MM = itemRow["Created_Month"].ToString(),
};
datasrrows.Add(datatemp);
}
}
// 根据年月关联NOSLS_INCM 营业外支出
if (vbsyywzcdt.Rows.Count > 0)
{
var NOSLS_SPND = vbsyywzcdt.Select("Created_Year='" + itemRow["Created_Year"].ToString() + "' and Created_Month='" + itemRow["Created_Month"].ToString() + "'");
if (NOSLS_SPND.Length > 0)
{
var NOSLS_SPNDrow = NOSLS_SPND.FirstOrDefault();
soapRequestData.Append(NOSLS_SPNDrow["NOSLS_SPND"] != null ? "" + NOSLS_SPNDrow["NOSLS_SPND"].ToString() + "" : "" + "");
var datatemp = new TempDatas
{
YYYY = itemRow["Created_Year"].ToString(),
MM = itemRow["Created_Month"].ToString(),
};
datazcrows.Add(datatemp);
}
}
soapRequestData.Append("");
}
}
// 若有未关联的数据,拼接在数据后面 营业外收入
if (vbsyywsrdt.Rows.Count > 0)
{
foreach (DataRow item in vbsyywsrdt.Rows)
{
if (datasrrows.Count > 0)
{
var exists = datasrrows.Any(q => q.YYYY == item["Created_Year"].ToString() && q.MM == item["Created_Month"].ToString());
log.Info("有关联数据" + datasrrows.Count + "条,过滤后exists结果为" + exists + "。");
if (!exists)
{
soapRequestData.Append("");
soapRequestData.Append(item["Created_Year"] != null ? "" + item["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(item["Created_Month"] != null ? "" + item["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "100" + "");
soapRequestData.Append(item["NOSLS_INCM"] != null ? "" + item["NOSLS_INCM"].ToString() + "" : "" + "");
soapRequestData.Append("");
}
}
else
{
soapRequestData.Append("");
soapRequestData.Append(item["Created_Year"] != null ? "" + item["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(item["Created_Month"] != null ? "" + item["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "100" + "");
soapRequestData.Append(item["NOSLS_INCM"] != null ? "" + item["NOSLS_INCM"].ToString() + "" : "" + "");
soapRequestData.Append("");
}
}
}
// 若有未关联的数据,拼接在数据后面 营业外支出
if (vbsyywzcdt.Rows.Count > 0)
{
foreach (DataRow item in vbsyywzcdt.Rows)
{
if (datazcrows.Count > 0)
{
var exists = datazcrows.Any(q => q.YYYY == item["Created_Year"].ToString() && q.MM == item["Created_Month"].ToString());
log.Info("有关联数据" + datazcrows.Count + "条,过滤后exists结果为" + exists + "。");
if (!exists)
{
soapRequestData.Append("");
soapRequestData.Append(item["Created_Year"] != null ? "" + item["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(item["Created_Month"] != null ? "" + item["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "100" + "");
soapRequestData.Append(item["NOSLS_SPND"] != null ? "" + item["NOSLS_SPND"].ToString() + "" : "" + "");
soapRequestData.Append("");
}
}
else
{
soapRequestData.Append("");
soapRequestData.Append(item["Created_Year"] != null ? "" + item["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(item["Created_Month"] != null ? "" + item["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "100" + "");
soapRequestData.Append(item["NOSLS_SPND"] != null ? "" + item["NOSLS_SPND"].ToString() + "" : "" + "");
soapRequestData.Append("");
}
}
}
//广州部分
if (vbsgzdt.Rows.Count > 0)
{
foreach (DataRow itemRow in vbsgzdt.Rows)
{
soapRequestData.Append("");
soapRequestData.Append(itemRow["Created_Year"] != null ? "" + itemRow["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["Created_Month"] != null ? "" + itemRow["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "200" + "");
soapRequestData.Append(itemRow["DBTO"] != null ? "" + itemRow["DBTO"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["DBTO_CMSN"] != null ? "" + itemRow["DBTO_CMSN"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["DBTO_IENS"] != null ? "" + itemRow["DBTO_IENS"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["DBTO_XCH_PC"] != null ? "" + itemRow["DBTO_XCH_PC"].ToString() + "" : "" + "");
// 根据年月关联NOSLS_INCM 营业外收入
if (vbsyywsrdt_gz.Rows.Count > 0)
{
var NOSLS_INCM = vbsyywsrdt_gz.Select("Created_Year='" + itemRow["Created_Year"].ToString() + "' and Created_Month='" + itemRow["Created_Month"].ToString() + "'");
if (NOSLS_INCM.Length > 0)
{
var NOSLS_INCMrow = NOSLS_INCM.FirstOrDefault();
soapRequestData.Append(NOSLS_INCMrow["NOSLS_INCM"] != null ? "" + NOSLS_INCMrow["NOSLS_INCM"].ToString() + "" : "" + "");
var datatemp = new TempDatas
{
YYYY = itemRow["Created_Year"].ToString(),
MM = itemRow["Created_Month"].ToString(),
};
datasrrows_gz.Add(datatemp);
}
}
// 根据年月关联NOSLS_INCM 营业外支出
if (vbsyywzcdt_gz.Rows.Count > 0)
{
var NOSLS_SPND = vbsyywzcdt_gz.Select("Created_Year='" + itemRow["Created_Year"].ToString() + "' and Created_Month='" + itemRow["Created_Month"].ToString() + "'");
if (NOSLS_SPND.Length > 0)
{
var NOSLS_SPNDrow = NOSLS_SPND.FirstOrDefault();
soapRequestData.Append(NOSLS_SPNDrow["NOSLS_SPND"] != null ? "" + NOSLS_SPNDrow["NOSLS_SPND"].ToString() + "" : "" + "");
var datatemp = new TempDatas
{
YYYY = itemRow["Created_Year"].ToString(),
MM = itemRow["Created_Month"].ToString(),
};
datazcrows_gz.Add(datatemp);
}
}
soapRequestData.Append("");
}
}
// 若有未关联的数据,拼接在数据后面 营业外收入
if (vbsyywsrdt_gz.Rows.Count > 0)
{
foreach (DataRow item in vbsyywsrdt_gz.Rows)
{
if (datasrrows_gz.Count > 0)
{
var exists = datasrrows_gz.Any(q => q.YYYY == item["Created_Year"].ToString() && q.MM == item["Created_Month"].ToString());
log.Info("有关联数据" + datasrrows_gz.Count + "条,过滤后exists结果为" + exists + "。");
if (!exists)
{
soapRequestData.Append("");
soapRequestData.Append(item["Created_Year"] != null ? "" + item["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(item["Created_Month"] != null ? "" + item["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "200" + "");
soapRequestData.Append(item["NOSLS_INCM"] != null ? "" + item["NOSLS_INCM"].ToString() + "" : "" + "");
soapRequestData.Append("");
}
}
else
{
soapRequestData.Append("");
soapRequestData.Append(item["Created_Year"] != null ? "" + item["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(item["Created_Month"] != null ? "" + item["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "200" + "");
soapRequestData.Append(item["NOSLS_INCM"] != null ? "" + item["NOSLS_INCM"].ToString() + "" : "" + "");
soapRequestData.Append("");
}
}
}
// 若有未关联的数据,拼接在数据后面 营业外支出
if (vbsyywzcdt_gz.Rows.Count > 0)
{
foreach (DataRow item in vbsyywzcdt_gz.Rows)
{
if (datazcrows_gz.Count > 0)
{
var exists = datazcrows_gz.Any(q => q.YYYY == item["Created_Year"].ToString() && q.MM == item["Created_Month"].ToString());
log.Info("有关联数据" + datazcrows_gz.Count + "条,过滤后exists结果为" + exists + "。");
if (!exists)
{
soapRequestData.Append("");
soapRequestData.Append(item["Created_Year"] != null ? "" + item["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(item["Created_Month"] != null ? "" + item["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "200" + "");
soapRequestData.Append(item["NOSLS_SPND"] != null ? "" + item["NOSLS_SPND"].ToString() + "" : "" + "");
soapRequestData.Append("");
}
}
else
{
soapRequestData.Append("");
soapRequestData.Append(item["Created_Year"] != null ? "" + item["Created_Year"].ToString() + "" : "" + "");
soapRequestData.Append(item["Created_Month"] != null ? "" + item["Created_Month"].ToString() + "" : "" + "");
soapRequestData.Append("" + "200" + "");
soapRequestData.Append(item["NOSLS_SPND"] != null ? "" + item["NOSLS_SPND"].ToString() + "" : "" + "");
soapRequestData.Append("");
}
}
}
soapRequestData.Append("");
soapRequestData.Append("");
soapRequestData.Append("");
string postData = soapRequestData.ToString();
log.Info("YERP_007 xmlRequest:" + postData);
string statusCode;
string resultContent;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
RestOpHelper rop = new RestOpHelper();
rop.Clientp12path = @"C:\Users\Administrator\Desktop\yonyou-cert\wildcard.pfx";
rop.Clientp12PassWord = "Yonyou2024!";
rop.Url = @"https://yerp-proxy-sap-prod.skchemicals.com/XISOAPAdapter/MessageServlet?senderParty=&senderService=YERP_P&receiverParty=&receiverService=&interface=YERP_007_OA1_SI&interfaceNamespace=http://skch.com/YERP_007_TAX_FOR_FIN_EXPENSE";
rop.RequestSAP(rop.Url, 10000, postData, "IF_YERP", "Kfiw282082", out statusCode, out resultContent);
log.Info("YERP_007 statusCode:" + statusCode + "\r\n" + "resultContent:" + resultContent);
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
public static Configuration GetConfig()
{
Assembly assembly = Assembly.GetCallingAssembly();
string path = string.Format("{0}.config", assembly.Location);
if (!File.Exists(path))
{
throw new FileNotFoundException(path + "路径下的文件未找到!");
}
try
{
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = path;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
return config;
}
catch (Exception)
{
throw;
}
}
}
}