strSql.Append("select FCYNO as GID,FCYNO,FCYNAME=(select top 1 DESCRIPTION from code_currency where CODENAME=FCYACC.FCYNO),FCYEXRATE=(SELECT top 1 DEFAULTRATE=isnull((select top 1 BEGRATE from cw_currency_rate where LINKGID=code_currency.GID and ISDELETE=0 and ACCDATE= '"+strCwACCDATE+"' and STARTGID='"+strCwSTARTGID+"'),DEFAULTRATE) from code_currency WITH(NOLOCK) where CODENAME=FCYACC.FCYNO)"// and CORPID='" + companyid + "'
+",FCYEXRATE2=(SELECT top 1 DEFAULTRATE=isnull((select top 1 ENDRATE=ISNULL((CASE WHEN ENDRATE=1 THEN BEGRATE else ENDRATE END),BEGRATE) from cw_currency_rate where LINKGID=code_currency.GID and ISDELETE=0"// and CORPID='" + companyid + "'
+"),DEFAULTRATE) from code_currency WITH(NOLOCK) where CODENAME=FCYACC.FCYNO) from (select distinct CURRENCY FCYNO FROM cw_genleg_accitems where STARTGID='"+strCwSTARTGID+"' AND CURRENCY<>'RMB') FCYACC WHERE 1=1 ");
// strSql.Append(" and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl where STARTGID='" + strCwSTARTGID + "' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=isnull((SELECT top 1 SUBSTRING(convert(varchar,dateadd(month,1,(ACCDATE+'-01')),23),1,7) as ACCDATE from cw_carry_over WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "' ORDER by ACCDATE DESC),(select top 1 STARTMONTH from cw_design_startusing where (ISDELETE=0 or ISDELETE is null) and ISENABLE=1 and GID=(select top 1 STARTGID from [cw_design_startusing_user] WITH(NOLOCK) where USERGID='" + strUSERID + "') order by STARTMONTH desc)))");
strSql.Append("SELECT *,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=cw_carry_accitems.MODIFIEDUSER) from cw_carry_accitems WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"'");
if(!string.IsNullOrEmpty(condition))
{
strSql.Append(" and "+condition);
}
//会计期间
varstrSql4=newStringBuilder();
strSql4.Append("select ACCDATE");
strSql4.Append(",convert(varchar,dateadd(day,-day(ACCDATE+'-01'),dateadd(month,1,(ACCDATE+'-01'))),23) as VOUDATE");
strSql4.Append(",year(ACCDATE+'-01') as [YEAR]");
strSql4.Append(",month(ACCDATE+'-01') as [MONTH2]");
strSql4.Append(",SUBSTRING(convert(varchar,(ACCDATE+'-01'),23),6,2) as [MONTH]");
strSql4.Append(",VOUNO=isnull((select top 1 (VOUNO+1) from Cw_Vouchers_Gl WHERE STARTGID='"+strCwSTARTGID+"' and VKNO='记' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=a.ACCDATE AND (CarryOverType='' or CarryOverType is NULL) ORDER by convert(int,VOUNO) desc),'1')");//1001
strSql4.Append(",VOUNO2=isnull((select top 1 (VOUNO+2) as VOUNO from Cw_Vouchers_Gl WHERE STARTGID='"+strCwSTARTGID+"' and VKNO='记' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=a.ACCDATE AND (CarryOverType='' or CarryOverType is NULL) ORDER by convert(int,VOUNO) desc),'2')");//1002
strSql4.Append(",VOUNUM=(select COUNT(*) as VOUNUM from Cw_Vouchers_Gl WHERE STARTGID='"+strCwSTARTGID+"' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=a.ACCDATE AND (CarryOverType='' or CarryOverType is NULL))");
strSql4.Append(",VOUNUM_ISCHECKED=(select COUNT(*) as VOUNUM from Cw_Vouchers_Gl WHERE STARTGID='"+strCwSTARTGID+"' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=a.ACCDATE and (ISCHECKED=0 or ISCHECKED is null))");// AND (CarryOverType='' or CarryOverType is NULL)
strSql4.Append(",isCarryOverType=(select (case VOUNUM when 0 then 'false' else 'true' end) as isCarryOverType from (select COUNT(*) as VOUNUM from Cw_Vouchers_Gl WHERE STARTGID='"+strCwSTARTGID+"' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=a.ACCDATE AND (CarryOverType<>'' and CarryOverType is not NULL)) as a)");// and ISCHECKED=1
strSql4.Append(",VOUNUM_ISCARRY=(select COUNT(*) as VOUNUM from Cw_Vouchers_Gl WHERE STARTGID='"+strCwSTARTGID+"' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=a.ACCDATE and (ISCARRY=0 or ISCARRY is null) AND (CarryOverType='' or CarryOverType is NULL))");
strSql4.Append(" from (SELECT isnull((SELECT top 1 SUBSTRING(convert(varchar,dateadd(month,1,(ACCDATE+'-01')),23),1,7) as ACCDATE from cw_carry_over WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' ORDER by ACCDATE DESC),(select top 1 STARTMONTH from cw_design_startusing where (ISDELETE=0 or ISDELETE is null) and ISENABLE=1 and GID=(select top 1 STARTGID from [cw_design_startusing_user] WITH(NOLOCK) where USERGID='"+strUSERID+"') order by STARTMONTH desc)) as ACCDATE) as a");
stringsSQL="select top 1 gid from cw_carry_accitems WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"'";// and CORPID='" + companyid + "'
stringsGID=T_ALL_DA.GetStrSQL("gid",sSQL);//科目
if(sGID=="")//插入0
{
sSQL="INSERT INTO [cw_carry_accitems]([GID],[ExchangeGains],[ExchangeLoss],[CurrentYearProfits],[UndistributedProfit],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME],STARTGID) VALUES(newid(),'"+headData.ExchangeGains.ToString().Trim()+"','"+headData.ExchangeLoss.ToString().Trim()+"','"+headData.CurrentYearProfits.ToString().Trim()+"','"+headData.UndistributedProfit.ToString().Trim()+"','"+companyid+"','"+strUserID+"',GETDATE(),'"+strCwSTARTGID+"')";
stringstrSqlDel="delete from Cw_Vouitems_Gl where ORDNO in (select ORDNO from Cw_Vouchers_Gl where STARTGID='"+strCwSTARTGID+"' and (CarryOverType='期末调汇' or CarryOverType='结转损益' or CarryOverType='年末结转') and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7))";
strSqlDel="delete from Cw_Vouchers_Gl where STARTGID='"+strCwSTARTGID+"' and (CarryOverType='期末调汇' or CarryOverType='结转损益' or CarryOverType='年末结转') and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7)";
stringFCYGID=T_ALL_DA.GetStrSQL("GID","select top 1 GID from code_currency where CODENAME='"+enumValue.FCYNO.ToString()+"'");
stringsls="update cw_currency_rate set ENDRATE="+enumValue.FCYEXRATE2.ToString()+",MODIFIEDUSER='"+strUserID+"',MODIFIEDTIME=getdate() where (ISDELETE=0 or ISDELETE is null) and ACCDATE='"+strCwACCDATE+"' and STARTGID='"+strCwSTARTGID+"' and LINKGID='"+FCYGID+"'";// and CORPID='" + companyid + "'
#endregion
T_ALL_DA.GetExecuteSqlCommand(sls);
#region 更新汇率表的下期期初汇率
DateTimedt=DateTime.Parse(strCwACCDATE+"-01");
DateTimedt2=dt.AddMonths(1);
stringsYear=dt2.Year.ToString();
stringsMonth=dt2.Month.ToString();
if(sMonth.Length==1)
{
sMonth="0"+sMonth;
}
stringstrACCDATE_New=sYear+"-"+sMonth;
sls="update cw_currency_rate set BEGRATE="+enumValue.FCYEXRATE2.ToString()+",MODIFIEDUSER='"+strUserID+"',MODIFIEDTIME=getdate() where (ISDELETE=0 or ISDELETE is null) and ACCDATE='"+strACCDATE_New+"' and STARTGID='"+strCwSTARTGID+"' and LINKGID='"+FCYGID+"' and (BEGRATE=0 or BEGRATE=1)";// and CORPID='" + companyid + "'
+",(case when (sum(AMTDR)-sum(AMTCR))>0 THEN (sum(AMTDR)-sum(AMTCR)) else 0 END) as AMTDR"
+",(case when (sum(AMTCR)-sum(AMTDR))>0 THEN (sum(AMTCR)-sum(AMTDR)) else 0 END) as AMTCR"
+",(case when (sum(FCYDR)-sum(FCYCR))>0 THEN (sum(FCYDR)-sum(FCYCR)) else 0 END) as FCYDR"
+",(case when (sum(FCYCR)-sum(FCYDR))>0 THEN (sum(FCYCR)-sum(FCYDR)) else 0 END) as FCYCR"
+",FCYNO"//,DC
+",(case when (sum(AMTDR2)-sum(AMTCR2))>0 THEN (sum(AMTDR2)-sum(AMTCR2)) else 0 END) as AMTDR2"
+",(case when (sum(AMTCR2)-sum(AMTDR2))>0 THEN (sum(AMTCR2)-sum(AMTDR2)) else 0 END) as AMTCR2"
+",DC2"
+",AMTDR3=ISNULL((select top 1 QTYYEARDR from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID=a.ACCID)),0)"
+",AMTCR3=ISNULL((select top 1 QTYYEARCR from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID=a.ACCID)),0)"
+",FCYDR3=ISNULL((select top 1 FCYYEARDR from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID=a.ACCID)),0)"
+",FCYCR3=ISNULL((select top 1 FCYYEARCR from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID=a.ACCID)),0)"
+",DC2=(select top 1 DC from cw_accitems_gl where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,4))"
+",ITEMNAME=ISNULL((select top 1 ITEMNAME from cw_item where GID=Cw_Vouitems_Gl.ITEMGID),'')"
+",DEPTNAME=ISNULL((select top 1 DEPTNAME from sys_dept where GID=Cw_Vouitems_Gl.DEPTGID),'')"
+" from Cw_Vouitems_Gl where 1=1"
+" and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl where STARTGID='"+strCwSTARTGID+"' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and CarryOverType<>'期末调汇' and CarryOverType<>'结转损益')"
+" and FCYNO='"+enumValue.FCYNO.ToString()+"' ) as a"
+" group BY ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,FCYNO,DC2)"
+" union "
+"(select AG.ACCID,AG.ACCNAME,AG.ACCNAME as ACCALL,G.CORPACC as CUSTOMERNAME,G.EMPLACC as SALE,G.ITEMACC as ITEMNAME,G.DEPTACC as DEPTNAME,0 as AMTDR,0 as AMTCR,0 as FCYDR,0 as FCYCR,G.CURRENCY as FCYNO,0 as AMTDR2,0 as AMTCR2,AG.DC as DC2"
+",isnull(sum(G.QTYYEARDR),0) as AMTDR3"
+",isnull(sum(G.QTYYEARCR),0) as AMTCR3"
+",isnull(sum(G.FCYYEARDR),0) as FCYDR3"
+",isnull(sum(G.FCYYEARCR),0) as FCYCR3"
+" from cw_genleg_accitems as G INNER JOIN cw_accitems_gl as AG ON AG.GID = G.LINKGID"
+" where G.[STARTGID]='"+strCwSTARTGID+"' and AG.[YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and AG.[STARTGID]='"+strCwSTARTGID+"' and AG.DETAILED=1 and AG.ISENABLE=1 and CURRENCY='"+enumValue.FCYNO.ToString()+"'and ACCDATE=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and (G.QTYYEARDR<>0 or G.QTYYEARCR<>0 or G.FCYYEARDR<>0 or G.FCYYEARCR<>0)"
+"and LINKGID not in (select LINKGID=(select GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID=Cw_Vouitems_Gl.ACCID) from Cw_Vouitems_Gl where FCYNO='USD' and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl where STARTGID='"+strCwSTARTGID+"' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and CarryOverType<>'期末调汇' and CarryOverType<>'结转损益'))"
+" group BY AG.ACCID,AG.ACCNAME,G.CORPACC,G.EMPLACC,G.ITEMACC,G.DEPTACC,G.CURRENCY,AG.DC)"
+") as b"//,DC
+" where ((AMTDR+AMTDR3)-(AMTCR+AMTCR3))<>0"
+" order by ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,FCYNO";
*/
#endregion
#region 插入明细表_合计数据集_按总账表合计
strSql="select *"
+",AMTDRCE=(case when DC='借' then cast((FCYLASTBLC*"+enumValue.FCYEXRATE2.ToString()+"-(QTYLASTBLC-AMTLASTBLC)) as decimal(18,2)) else 0 end)"
+",AMTCRCE=(case when DC='借' then 0 else cast((FCYLASTBLC*"+enumValue.FCYEXRATE2.ToString()+"-(QTYLASTBLC-AMTLASTBLC)) as decimal(18,2)) end)"
+",FCYDRCE=(case when DC='借' then cast(((FCYLASTBLC*"+enumValue.FCYEXRATE2.ToString()+"-(QTYLASTBLC-AMTLASTBLC))/"+enumValue.FCYEXRATE2.ToString()+") as decimal(18,2)) else 0 end)"
+",FCYCRCE=(case when DC='借' then 0 else cast(((FCYLASTBLC*"+enumValue.FCYEXRATE2.ToString()+"-(QTYLASTBLC-AMTLASTBLC))/"+enumValue.FCYEXRATE2.ToString()+") as decimal(18,2)) end)"
+" from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
+" where DETAILED=1 and ACCATTRIBUTE<>'损益'"
+" and STARTGID='"+strCwSTARTGID+"'"
+" and currency<>'RMB' and currency='"+enumValue.FCYNO.ToString()+"' and (FCYLASTBLC<>0 or QTYLASTBLC<>0)"
+" and ([YEAR]+'-'+[MONTH])=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7)";
stringlsSQL="select top 1 GID from cw_item WITH(NOLOCK) where [STARTGID]='"+strCwSTARTGID+"' and ITEMNAME='"+dsCwVouitemsGl.Tables[0].Rows[i]["ITEMACC"].ToString().Trim()+"'";
stringsDC="select top 1 DC from cw_accitems_gl With(NOLOCK) where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID='"+headData.ExchangeGains.ToString().Trim()+"'";
sDC=T_ALL_DA.GetStrSQL("DC",sDC);//科目
if(sDC=="借")
{
#region 借
MsCwVouitemsGl2.AMTDR=-dlAMTCR;
MsCwVouitemsGl2.FCYDR=0;
MsCwVouitemsGl2.AMTCR=0;
MsCwVouitemsGl2.FCYCR=0;
MsCwVouitemsGl2.DC="D";//借方向
#endregion
}
else
{
#region 贷
MsCwVouitemsGl2.AMTDR=0;
MsCwVouitemsGl2.FCYDR=0;
MsCwVouitemsGl2.AMTCR=dlAMTCR;
MsCwVouitemsGl2.FCYCR=0;
MsCwVouitemsGl2.DC="C";//借方向
#endregion
}
MsCwVouitemsGl2.ISFCY=false;//是否外币
MsCwVouitemsGl2.FCYNO="RMB";//外币编号usd
MsCwVouitemsGl2.SALE="";//strSALE;//人员名称
MsCwVouitemsGl2.CUSTOMERNAME="";//客户(系统)
MsCwVouitemsGl2.ITEMGID="";//项目名称//varchar
MsCwVouitemsGl2.DEPTGID="";
//
MsCwVouitemsGl2.ACCID="";//科目
MsCwVouitemsGl2.ACCALL="";
stringsSQL="select top 1 * from cw_accitems_gl With(NOLOCK) where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID='"+headData.ExchangeGains.ToString().Trim()+"'";
stringsDC="select top 1 DC from cw_accitems_gl With(NOLOCK) where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID='"+headData.ExchangeLoss.ToString().Trim()+"'";
stringsSQL="select top 1 * from cw_accitems_gl With(NOLOCK) where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID='"+headData.ExchangeLoss.ToString().Trim()+"'";
+",AMTDR3=ISNULL((select top 1 AMTYEARDR from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+headData.VOUDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID=a.ACCID)),0)"
+",AMTCR3=ISNULL((select top 1 AMTYEARCR from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+headData.VOUDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID=a.ACCID)),0)"
+",FCYDR3=ISNULL((select top 1 FCYYEARDR from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+headData.VOUDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID=a.ACCID)),0)"
+",FCYCR3=ISNULL((select top 1 FCYYEARCR from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+headData.VOUDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID=a.ACCID)),0)"
+" from ("
+" select *"
+",DC2=ISNULL((select top 1 DC from cw_accitems_gl where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,4)),'')"
+",ITEMNAME=ISNULL((select top 1 ITEMNAME from cw_item where GID=Cw_Vouitems_Gl.ITEMGID),'')"
+",DEPTNAME=ISNULL((select top 1 DEPTNAME from sys_dept where GID=Cw_Vouitems_Gl.DEPTGID),'')"
+" from Cw_Vouitems_Gl where 1=1"
+" and ACCID in (select ACCID from [cw_accitems_gl] where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCATTRIBUTE='损益') "
+" and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl where STARTGID='"+strCwSTARTGID+"' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7) and CarryOverType<>'结转损益')"
//+ " and FCYNO='RMB'"
+") as a"
+" group BY ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,DC,DC2,FCYNO,FCYEXRATE) as b"
+" order by ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,FCYNO,FCYEXRATE";
stringsSQL="select top 1 GID from cw_item WITH(NOLOCK) where [STARTGID]='"+strCwSTARTGID+"' and ITEMNAME='"+dsCwVouitemsGl2.Tables[0].Rows[i]["ITEMNAME"].ToString().Trim()+"'";
stringsls="update cw_vouchers_gl set ISCARRY=1 where ORDNO in (select ORDNO FROM Cw_Vouchers_Gl where STARTGID='"+strCwSTARTGID+"' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7))";
#endregion
T_ALL_DA.GetExecuteSqlCommand(sls);
}
}
else{
#region 更新月末结转标志 ISCARRY
stringsls="update cw_vouchers_gl set ISCARRY=1 where ORDNO in (select ORDNO FROM Cw_Vouchers_Gl where STARTGID='"+strCwSTARTGID+"' and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+headData.VOUDATE+"',23),1,7))";
stringsSQL="select top 1 * from cw_accitems_gl With(NOLOCK) where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID='"+headData.CurrentYearProfits.ToString().Trim()+"'";
stringsSQL="select top 1 gid from cw_carry_accitems With(NOLOCK) where STARTGID='"+strCwSTARTGID+"'";// and CORPID='" + companyid + "'
stringsGID=T_ALL_DA.GetStrSQL("gid",sSQL);//科目
if(sGID=="")//插入0
{
sSQL="INSERT INTO [cw_carry_accitems]([GID],[ExchangeGains],[ExchangeLoss],[CurrentYearProfits],[UndistributedProfit],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME],STARTGID) VALUES(newid(),'"+headData.ExchangeGains.ToString().Trim()+"','"+headData.ExchangeLoss.ToString().Trim()+"','"+headData.CurrentYearProfits.ToString().Trim()+"','"+headData.UndistributedProfit.ToString().Trim()+"','"+companyid+"','"+strUserID+"',GETDATE(),'"+strCwSTARTGID+"')";
strSql="SELECT ACCID=ISNULL((SELECT TOP 1 ACCID from cw_accitems_gl WHERE GID=[cw_genleg_accitems].LINKGID),''),ACCNAME=ISNULL((SELECT TOP 1 ACCNAME from cw_accitems_gl WHERE GID=[cw_genleg_accitems].LINKGID),''),DC2=ISNULL((select top 1 DC from cw_accitems_gl WHERE GID=[cw_genleg_accitems].LINKGID),''),ITEMGID=ISNULL((select top 1 GID from cw_item WHERE ITEMNAME=[cw_genleg_accitems].ITEMACC),''),DEPTGID=ISNULL((select top 1 GID from sys_dept WHERE DEPTNAME=[cw_genleg_accitems].DEPTACC),''),DEPTID=ISNULL((select top 1 FINANCESOFTCODE from sys_dept WHERE DEPTNAME=[cw_genleg_accitems].DEPTACC),''),BLC=(QTYLASTBLC+"+dlBLC+"),* FROM [cw_genleg_accitems] where [STARTGID]='"+strCwSTARTGID+"' and ACCDATE='"+strCwACCDATE+"' and LINKGID=(select GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and ACCID='"+headData.CurrentYearProfits.ToString().Trim()+"' and [STARTGID]='"+strCwSTARTGID+"' )";// and AMTLASTBLC<>0
stringsSQL="select top 1 * from cw_accitems_gl With(NOLOCK) where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and ACCID='"+headData.UndistributedProfit.ToString().Trim()+"'";
stringsSQL="select top 1 gid from cw_carry_accitems WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"'";// and CORPID='" + companyid + "'
stringsGID=T_ALL_DA.GetStrSQL("gid",sSQL);//科目
if(sGID=="")//插入0
{
sSQL="INSERT INTO [cw_carry_accitems]([GID],[ExchangeGains],[ExchangeLoss],[CurrentYearProfits],[UndistributedProfit],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME],STARTGID) VALUES(newid(),'"+headData.ExchangeGains.ToString().Trim()+"','"+headData.ExchangeLoss.ToString().Trim()+"','"+headData.CurrentYearProfits.ToString().Trim()+"','"+headData.UndistributedProfit.ToString().Trim()+"','"+companyid+"','"+strUserID+"',GETDATE(),'"+strCwSTARTGID+"')";
stringsSQL="select VKNO from VW_cw_vouchers_vouitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCYEAR='"+strYEAR+"' and ACCMONTH='"+strMONTH+"' and ISCHECKED=1 group by VKNO";
DataSetds=T_ALL_DA.GetAllSQL(sSQL);
if(ds!=null)
{
if(ds.Tables[0].Rows.Count>0)
{
for(inti=0;i<ds.Tables[0].Rows.Count;i++)
{
sSQL="select VOUNO from VW_cw_vouchers_vouitems_gl where STARTGID='"+strCwSTARTGID+"' and ISCHECKED=1 and ACCYEAR='"+strYEAR+"' and ACCMONTH='"+strMONTH+"' and VKNO='"+ds.Tables[0].Rows[i]["VKNO"].ToString()+"' group by VOUNO order by convert(int,VOUNO)";
// //+ ",AMTYEARDR=isnull((SELECT (case when cw_accitems_gl.DC='借' then AMTLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//RMB期初借方
// //+ ",AMTYEARCR=isnull((SELECT (case when cw_accitems_gl.DC='贷' then AMTLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//RMB期初贷方
// //+ ",AMTYEARBLC=isnull((SELECT AMTLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//RMB期初余额
// //+ ",AMTDR"//RMB本月借方
// //+ ",AMTCR"//RMB本月贷方
// //+ ",AMTBLC"//RMB本月余额
// //+ ",AMTLASTDR=ISNULL((AMTDR+isnull((SELECT (case when cw_accitems_gl.DC='借' then AMTLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//RMB累计借方
// //+ ",AMTLASTCR=ISNULL((AMTCR+isnull((SELECT (case when cw_accitems_gl.DC='贷' then AMTLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//RMB累计贷方
// //+ ",AMTLASTBLC=ISNULL((AMTBLC+isnull((SELECT AMTLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//RMB累计余额
// //+ ",FCYYEARDR=isnull((SELECT (case when cw_accitems_gl.DC='借' then FCYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//外币期初借方
// //+ ",FCYYEARCR=isnull((SELECT (case when cw_accitems_gl.DC='贷' then FCYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//外币期初贷方
// //+ ",FCYYEARBLC=isnull((SELECT FCYLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//外币期初余额
// //+ ",FCYDR"//外币本月借方
// //+ ",FCYCR"//外币本月贷方
// //+ ",FCYBLC"//外币本月余额
// //+ ",FCYLASTDR=ISNULL((FCYDR+isnull((SELECT (case when cw_accitems_gl.DC='借' then FCYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//外币累计借方
// //+ ",FCYLASTCR=ISNULL((FCYCR+isnull((SELECT (case when cw_accitems_gl.DC='贷' then FCYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//外币累计贷方
// //+ ",FCYLASTBLC=ISNULL((FCYBLC+isnull((SELECT FCYLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//外币累计余额
// //+ ",QTYYEARDR=isnull((SELECT (case when cw_accitems_gl.DC='借' then QTYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//期初借方
// //+ ",QTYYEARCR=isnull((SELECT (case when cw_accitems_gl.DC='贷' then QTYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//期初贷方
// //+ ",QTYYEARBLC=isnull((SELECT QTYLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//期初余额
// //+ ",QTYDR"//本月借方
// //+ ",QTYCR"//本月贷方
// //+ ",QTYBLC"//本月余额
// //+ ",QTYLASTDR=ISNULL((QTYDR+isnull((SELECT (case when cw_accitems_gl.DC='借' then QTYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//累计借方
// //+ ",QTYLASTCR=ISNULL((QTYCR+isnull((SELECT (case when cw_accitems_gl.DC='贷' then QTYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//累计贷方
// //+ ",QTYLASTBLC=ISNULL((QTYBLC+isnull((SELECT QTYLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//累计余额
// // + "(case when a.DC='借' then AMTLASTBLC ELSE 0 end) as AMTYEARDR,"//RMB期初借方
// // + "(case when a.DC='贷' then AMTLASTBLC ELSE 0 end) as AMTYEARCR,"//RMB期初贷方
// // + "AMTLASTBLC as AMTYEARBLC,"//RMB期初余额
// // + "0 as AMTDR,"//RMB本月借方
// // + "0 as AMTCR,"//RMB本月贷方
// // + "0 as AMTBLC,"//RMB本月余额
// // + "(case when a.DC='借' then AMTLASTBLC ELSE 0 end) as AMTLASTDR,"//RMB累计借方
// // + "(case when a.DC='贷' then AMTLASTBLC ELSE 0 end) as AMTLASTCR,"//RMB累计贷方
// // + "AMTLASTBLC as AMTLASTBLC,"//RMB累计余额
// // + "(case when a.DC='借' then FCYLASTBLC ELSE 0 end) as FCYYEARDR,"//外币期初借方
// // + "(case when a.DC='贷' then FCYLASTBLC ELSE 0 end) as FCYYEARCR,"//外币期初贷方
// // + "FCYLASTBLC as FCYYEARBLC,"//外币期初余额
// // + "0 as FCYDR,"//外币本月借方
// // + "0 as FCYCR,"//外币本月贷方
// // + "0 as FCYBLC,"//外币本月余额
// // + "(case when a.DC='借' then FCYLASTBLC ELSE 0 end) as FCYLASTDR,"//外币累计借方
// // + "(case when a.DC='贷' then FCYLASTBLC ELSE 0 end) as FCYLASTCR,"//外币累计贷方
// // + "FCYLASTBLC as FCYLASTBLC,"//外币累计余额
// // + "(case when a.DC='借' then QTYLASTBLC ELSE 0 end) as QTYYEARDR,"//期初借方
// // + "(case when a.DC='贷' then QTYLASTBLC ELSE 0 end) as QTYYEARCR,"//期初贷方
// // + "QTYLASTBLC as QTYYEARBLC,"//期初余额
// // + "0 as QTYDR,"//本月借方
// // + "0 as QTYCR,"//本月贷方
// // + "0 as QTYBLC,"//本月余额
// // + "(case when a.DC='借' then QTYLASTBLC ELSE 0 end) as QTYLASTDR,"//累计借方
// // + "(case when a.DC='贷' then QTYLASTBLC ELSE 0 end) as QTYLASTCR,"//累计贷方
// // + "QTYLASTBLC as QTYLASTBLC,"//累计余额
// // + "'" + companyid + "' as CORPID,"//分公司代码
// // + "'False' as ISDELETE,'' as DELETEUSER,getdate() as DELETETIME,'" + strUserID + "' as CREATEUSER,getdate() as CREATETIME,'" + strUserID + "' as MODIFIEDUSER,getdate() as MODIFIEDTIME,'True' as ISENABLE,PFADR,PFACR,STARTGID,"//账套启用GID
// // + "SUBSTRING(convert(varchar,dateadd(month,1,(ACCDATE+'-01')),23),1,7) as ACCDATE"//会计期间
// // + " from VW_cw_genleg_accitems_gl as a where a.STARTGID='" + strCwSTARTGID + "' and [ACCYEAR]='" + strYEAR + "' and ACCDATE='" + strYEAR + "-" + strMONTH + "' and ACCATTRIBUTE<>'损益' and a.GID NOT in (SELECT b.gid from (select * from VW_cw_genleg_accitems_gl where STARTGID='" + strCwSTARTGID + "' and [ACCYEAR]='" + strYEAR + "' and ACCDATE='" + strYEAR + "-" + strMONTH + "' and ACCATTRIBUTE<>'损益') as b, (select * from VW_cw_genleg_accitems_gl where STARTGID='" + strCwSTARTGID + "' and [ACCYEAR]='" + strYEAR + "' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('" + rq2 + "')),23),1,7) and ACCATTRIBUTE<>'损益') as c where b.STARTGID=c.STARTGID and b.LINKGID=c.LINKGID and b.DEPTACC=c.DEPTACC and b.EMPLACC=c.EMPLACC and b.CORPACC=c.CORPACC and b.ITEMACC=c.ITEMACC and b.CURRENCY=c.CURRENCY)";
// // + "(case when cw_accitems_gl.DC='借' then AMTLASTBLC ELSE 0 end) as AMTYEARDR,"//RMB期初借方
// // + "(case when cw_accitems_gl.DC='贷' then AMTLASTBLC ELSE 0 end) as AMTYEARCR,"//RMB期初贷方
// // + "AMTLASTBLC as AMTYEARBLC,"//RMB期初余额
// // + "0 as AMTDR,"//RMB本月借方
// // + "0 as AMTCR,"//RMB本月贷方
// // + "0 as AMTBLC,"//RMB本月余额
// // + "(case when cw_accitems_gl.DC='借' then AMTLASTBLC ELSE 0 end) as AMTLASTDR,"//RMB累计借方
// // + "(case when cw_accitems_gl.DC='贷' then AMTLASTBLC ELSE 0 end) as AMTLASTCR,"//RMB累计贷方
// // + "AMTLASTBLC as AMTLASTBLC,"//RMB累计余额
// // + "(case when cw_accitems_gl.DC='借' then FCYLASTBLC ELSE 0 end) as FCYYEARDR,"//外币期初借方
// // + "(case when cw_accitems_gl.DC='贷' then FCYLASTBLC ELSE 0 end) as FCYYEARCR,"//外币期初贷方
// // + "FCYLASTBLC as FCYYEARBLC,"//外币期初余额
// // + "0 as FCYDR,"//外币本月借方
// // + "0 as FCYCR,"//外币本月贷方
// // + "0 as FCYBLC,"//外币本月余额
// // + "(case when cw_accitems_gl.DC='借' then FCYLASTBLC ELSE 0 end) as FCYLASTDR,"//外币累计借方
// // + "(case when cw_accitems_gl.DC='贷' then FCYLASTBLC ELSE 0 end) as FCYLASTCR,"//外币累计贷方
// // + "FCYLASTBLC as FCYLASTBLC,"//外币累计余额
// // + "(case when cw_accitems_gl.DC='借' then QTYLASTBLC ELSE 0 end) as QTYYEARDR,"//期初借方
// // + "(case when cw_accitems_gl.DC='贷' then QTYLASTBLC ELSE 0 end) as QTYYEARCR,"//期初贷方
// // + "QTYLASTBLC as QTYYEARBLC,"//期初余额
// // + "0 as QTYDR,"//本月借方
// // + "0 as QTYCR,"//本月贷方
// // + "0 as QTYBLC,"//本月余额
// // + "(case when cw_accitems_gl.DC='借' then QTYLASTBLC ELSE 0 end) as QTYLASTDR,"//累计借方
// // + "(case when cw_accitems_gl.DC='贷' then QTYLASTBLC ELSE 0 end) as QTYLASTCR,"//累计贷方
// // + "QTYLASTBLC as QTYLASTBLC,"//累计余额
// // + "'" + companyid + "' as CORPID,"//分公司代码
// // + "'False' as ISDELETE,'' as DELETEUSER,getdate() as DELETETIME,'" + strUserID + "' as CREATEUSER,getdate() as CREATETIME,'" + strUserID + "' as MODIFIEDUSER,getdate() as MODIFIEDTIME,'True' as ISENABLE,PFADR,PFACR,cw_genleg_accitems.STARTGID,"//账套启用GID
// // + "SUBSTRING(convert(varchar,dateadd(month,1,(ACCDATE+'-01')),23),1,7) as ACCDATE"//会计期间
// // + " from cw_genleg_accitems INNER JOIN cw_accitems_gl ON cw_genleg_accitems.LINKGID = cw_accitems_gl.GID where cw_accitems_gl.[YEAR]='" + strYEAR + "' and ACCDATE='" + strYEAR + "-" + strMONTH + "' and cw_accitems_gl.ACCATTRIBUTE<>'损益' and cw_accitems_gl.[STARTGID]='" + strCwSTARTGID + "' and cw_genleg_accitems.[STARTGID]='" + strCwSTARTGID + "'";
// // + "(case when cw_accitems_gl.DC='借' then AMTLASTBLC ELSE 0 end) as AMTYEARDR,"//RMB期初借方
// // + "(case when cw_accitems_gl.DC='贷' then AMTLASTBLC ELSE 0 end) as AMTYEARCR,"//RMB期初贷方
// // + "AMTLASTBLC as AMTYEARBLC,"//RMB期初余额
// // + "0 as AMTDR,"//RMB本月借方
// // + "0 as AMTCR,"//RMB本月贷方
// // + "0 as AMTBLC,"//RMB本月余额
// // + "(case when cw_accitems_gl.DC='借' then AMTLASTBLC ELSE 0 end) as AMTLASTDR,"//RMB累计借方
// // + "(case when cw_accitems_gl.DC='贷' then AMTLASTBLC ELSE 0 end) as AMTLASTCR,"//RMB累计贷方
// // + "AMTLASTBLC as AMTLASTBLC,"//RMB累计余额
// // + "(case when cw_accitems_gl.DC='借' then FCYLASTBLC ELSE 0 end) as FCYYEARDR,"//外币期初借方
// // + "(case when cw_accitems_gl.DC='贷' then FCYLASTBLC ELSE 0 end) as FCYYEARCR,"//外币期初贷方
// // + "FCYLASTBLC as FCYYEARBLC,"//外币期初余额
// // + "0 as FCYDR,"//外币本月借方
// // + "0 as FCYCR,"//外币本月贷方
// // + "0 as FCYBLC,"//外币本月余额
// // + "(case when cw_accitems_gl.DC='借' then FCYLASTBLC ELSE 0 end) as FCYLASTDR,"//外币累计借方
// // + "(case when cw_accitems_gl.DC='贷' then FCYLASTBLC ELSE 0 end) as FCYLASTCR,"//外币累计贷方
// // + "FCYLASTBLC as FCYLASTBLC,"//外币累计余额
// // + "(case when cw_accitems_gl.DC='借' then QTYLASTBLC ELSE 0 end) as QTYYEARDR,"//期初借方
// // + "(case when cw_accitems_gl.DC='贷' then QTYLASTBLC ELSE 0 end) as QTYYEARCR,"//期初贷方
// // + "QTYLASTBLC as QTYYEARBLC,"//期初余额
// // + "0 as QTYDR,"//本月借方
// // + "0 as QTYCR,"//本月贷方
// // + "0 as QTYBLC,"//本月余额
// // + "(case when cw_accitems_gl.DC='借' then QTYLASTBLC ELSE 0 end) as QTYLASTDR,"//累计借方
// // + "(case when cw_accitems_gl.DC='贷' then QTYLASTBLC ELSE 0 end) as QTYLASTCR,"//累计贷方
// // + "QTYLASTBLC as QTYLASTBLC,"//累计余额
// // + "'" + companyid + "' as CORPID,"//分公司代码
// // + "'False' as ISDELETE,'' as DELETEUSER,getdate() as DELETETIME,'" + strUserID + "' as CREATEUSER,getdate() as CREATETIME,'" + strUserID + "' as MODIFIEDUSER,getdate() as MODIFIEDTIME,'True' as ISENABLE,PFADR,PFACR,cw_genleg_accitems.STARTGID,"//账套启用GID
// // + "SUBSTRING(convert(varchar,dateadd(month,1,(ACCDATE+'-01')),23),1,7) as ACCDATE"//会计期间
// // + " from cw_genleg_accitems INNER JOIN cw_accitems_gl ON cw_genleg_accitems.LINKGID = cw_accitems_gl.GID where cw_accitems_gl.[YEAR]='" + strYEAR + "' and ACCDATE='" + strYEAR + "-" + strMONTH + "' and cw_accitems_gl.ACCATTRIBUTE<>'损益' and cw_accitems_gl.[STARTGID]='" + strCwSTARTGID + "' and cw_genleg_accitems.[STARTGID]='" + strCwSTARTGID + "'";
// //+ ",AMTYEARDR=isnull((SELECT (case when cw_accitems_gl.DC='借' then AMTLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//RMB期初借方
// //+ ",AMTYEARCR=isnull((SELECT (case when cw_accitems_gl.DC='贷' then AMTLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//RMB期初贷方
// //+ ",AMTYEARBLC=isnull((SELECT AMTLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//RMB期初余额
// //+ ",AMTDR"//RMB本月借方
// //+ ",AMTCR"//RMB本月贷方
// //+ ",AMTBLC"//RMB本月余额
// //+ ",AMTLASTDR=ISNULL((AMTDR+isnull((SELECT AMTLASTDR from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//RMB累计借方
// //+ ",AMTLASTCR=ISNULL((AMTCR+isnull((SELECT AMTLASTCR from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//RMB累计贷方
// //+ ",AMTLASTBLC=ISNULL((AMTBLC+isnull((SELECT AMTLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//RMB累计余额
// //+ ",FCYYEARDR=isnull((SELECT (case when cw_accitems_gl.DC='借' then FCYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//外币期初借方
// //+ ",FCYYEARCR=isnull((SELECT (case when cw_accitems_gl.DC='贷' then FCYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//外币期初贷方
// //+ ",FCYYEARBLC=isnull((SELECT FCYLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//外币期初余额
// //+ ",FCYDR"//外币本月借方
// //+ ",FCYCR"//外币本月贷方
// //+ ",FCYBLC"//外币本月余额
// //+ ",FCYLASTDR=ISNULL((FCYDR+isnull((SELECT FCYLASTDR from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//外币累计借方
// //+ ",FCYLASTCR=ISNULL((FCYCR+isnull((SELECT FCYLASTCR from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//外币累计贷方
// //+ ",FCYLASTBLC=ISNULL((FCYBLC+isnull((SELECT FCYLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//外币累计余额
// //+ ",QTYYEARDR=isnull((SELECT (case when cw_accitems_gl.DC='借' then QTYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//期初借方
// //+ ",QTYYEARCR=isnull((SELECT (case when cw_accitems_gl.DC='贷' then QTYLASTBLC ELSE 0 end) from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//期初贷方
// //+ ",QTYYEARBLC=isnull((SELECT QTYLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)"//期初余额
// //+ ",QTYDR"//本月借方
// //+ ",QTYCR"//本月贷方
// //+ ",QTYBLC"//本月余额
// //+ ",QTYLASTDR=ISNULL((QTYDR+isnull((SELECT QTYLASTDR from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//累计借方
// //+ ",QTYLASTCR=ISNULL((QTYCR+isnull((SELECT QTYLASTCR from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//累计贷方
// //+ ",QTYLASTBLC=ISNULL((QTYBLC+isnull((SELECT QTYLASTBLC from cw_genleg_accitems where a.STARTGID=STARTGID and a.LINKGID=LINKGID and a.DEPTACC=DEPTACC and a.EMPLACC=EMPLACC and a.CORPACC=CORPACC and a.ITEMACC=ITEMACC and a.CURRENCY=CURRENCY and ACCDATE='" + strYEAR + "-" + strMONTH + "'),0)),0)"//累计余额
// // + "(case when a.DC='借' then AMTLASTBLC ELSE 0 end) as AMTYEARDR,"//RMB期初借方
// // + "(case when a.DC='贷' then AMTLASTBLC ELSE 0 end) as AMTYEARCR,"//RMB期初贷方
// // + "AMTLASTBLC as AMTYEARBLC,"//RMB期初余额
// // + "0 as AMTDR,"//RMB本月借方
// // + "0 as AMTCR,"//RMB本月贷方
// // + "0 as AMTBLC,"//RMB本月余额
// // + "AMTLASTDR,"//RMB累计借方
// // + "AMTLASTCR,"//RMB累计贷方
// // + "AMTLASTBLC,"//RMB累计余额
// // + "(case when a.DC='借' then FCYLASTBLC ELSE 0 end) as FCYYEARDR,"//外币期初借方
// // + "(case when a.DC='贷' then FCYLASTBLC ELSE 0 end) as FCYYEARCR,"//外币期初贷方
// // + "FCYLASTBLC as FCYYEARBLC,"//外币期初余额
// // + "0 as FCYDR,"//外币本月借方
// // + "0 as FCYCR,"//外币本月贷方
// // + "0 as FCYBLC,"//外币本月余额
// // + "FCYLASTDR,"//外币累计借方
// // + "FCYLASTCR,"//外币累计贷方
// // + "FCYLASTBLC,"//外币累计余额
// // + "(case when a.DC='借' then QTYLASTBLC ELSE 0 end) as QTYYEARDR,"//期初借方
// // + "(case when a.DC='贷' then QTYLASTBLC ELSE 0 end) as QTYYEARCR,"//期初贷方
// // + "QTYLASTBLC as QTYYEARBLC,"//期初余额
// // + "0 as QTYDR,"//本月借方
// // + "0 as QTYCR,"//本月贷方
// // + "0 as QTYBLC,"//本月余额
// // + "QTYLASTDR,"//累计借方
// // + "QTYLASTCR,"//累计贷方
// // + "QTYLASTBLC,"//累计余额
// // + "'" + companyid + "' as CORPID,"//分公司代码
// // + "'False' as ISDELETE,'' as DELETEUSER,getdate() as DELETETIME,'" + strUserID + "' as CREATEUSER,getdate() as CREATETIME,'" + strUserID + "' as MODIFIEDUSER,getdate() as MODIFIEDTIME,'True' as ISENABLE,PFADR,PFACR,STARTGID,"//账套启用GID
// // + "SUBSTRING(convert(varchar,dateadd(month,1,(ACCDATE+'-01')),23),1,7) as ACCDATE"//会计期间
// // + " from VW_cw_genleg_accitems_gl as a where a.STARTGID='" + strCwSTARTGID + "' and [ACCYEAR]=SUBSTRING('" + headData.VOUDATE + "',1,4) and ACCDATE=SUBSTRING(convert(varchar,'" + headData.VOUDATE + "',23),1,7) and ACCATTRIBUTE='损益' and a.GID NOT in (SELECT b.gid from (select * from VW_cw_genleg_accitems_gl where STARTGID='" + strCwSTARTGID + "' and [ACCYEAR]='" + strYEAR + "' and ACCDATE='" + strYEAR + "-" + strMONTH + "' and ACCATTRIBUTE='损益') as b, (select * from VW_cw_genleg_accitems_gl where STARTGID='" + strCwSTARTGID + "' and [ACCYEAR]='" + strYEAR + "' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('" + rq2 + "')),23),1,7) and ACCATTRIBUTE='损益') as c where b.STARTGID=c.STARTGID and b.LINKGID=c.LINKGID and b.DEPTACC=c.DEPTACC and b.EMPLACC=c.EMPLACC and b.CORPACC=c.CORPACC and b.ITEMACC=c.ITEMACC and b.CURRENCY=c.CURRENCY)";
// // + "(case when cw_accitems_gl.DC='借' then AMTLASTBLC ELSE 0 end) as AMTYEARDR,"//RMB期初借方
// // + "(case when cw_accitems_gl.DC='贷' then AMTLASTBLC ELSE 0 end) as AMTYEARCR,"//RMB期初贷方
// // + "AMTLASTBLC as AMTYEARBLC,"//RMB期初余额
// // + "0 as AMTDR,"//RMB本月借方
// // + "0 as AMTCR,"//RMB本月贷方
// // + "0 as AMTBLC,"//RMB本月余额
// // + "AMTLASTDR,"//RMB累计借方
// // + "AMTLASTCR,"//RMB累计贷方
// // + "AMTLASTBLC,"//RMB累计余额
// // + "(case when cw_accitems_gl.DC='借' then FCYLASTBLC ELSE 0 end) as FCYYEARDR,"//外币期初借方
// // + "(case when cw_accitems_gl.DC='贷' then FCYLASTBLC ELSE 0 end) as FCYYEARCR,"//外币期初贷方
// // + "FCYLASTBLC as FCYYEARBLC,"//外币期初余额
// // + "0 as FCYDR,"//外币本月借方
// // + "0 as FCYCR,"//外币本月贷方
// // + "0 as FCYBLC,"//外币本月余额
// // + "FCYLASTDR,"//外币累计借方
// // + "FCYLASTCR,"//外币累计贷方
// // + "FCYLASTBLC,"//外币累计余额
// // + "(case when cw_accitems_gl.DC='借' then QTYLASTBLC ELSE 0 end) as QTYYEARDR,"//期初借方
// // + "(case when cw_accitems_gl.DC='贷' then QTYLASTBLC ELSE 0 end) as QTYYEARCR,"//期初贷方
// // + "QTYLASTBLC as QTYYEARBLC,"//期初余额
// // + "0 as QTYDR,"//本月借方
// // + "0 as QTYCR,"//本月贷方
// // + "0 as QTYBLC,"//本月余额
// // + "QTYLASTDR,"//累计借方
// // + "QTYLASTCR,"//累计贷方
// // + "QTYLASTBLC,"//累计余额
// // + "'" + companyid + "' as CORPID,"//分公司代码
// // + "'False' as ISDELETE,'' as DELETEUSER,getdate() as DELETETIME,'" + strUserID + "' as CREATEUSER,getdate() as CREATETIME,'" + strUserID + "' as MODIFIEDUSER,getdate() as MODIFIEDTIME,'True' as ISENABLE,PFADR,PFACR,cw_genleg_accitems.STARTGID,"//账套启用GID
// // + "SUBSTRING(convert(varchar,dateadd(month,1,(ACCDATE+'-01')),23),1,7) as ACCDATE"//会计期间
// // + " from cw_genleg_accitems INNER JOIN cw_accitems_gl ON cw_genleg_accitems.LINKGID = cw_accitems_gl.GID where cw_accitems_gl.[YEAR]=SUBSTRING('" + headData.VOUDATE + "',1,4) and ACCDATE=SUBSTRING(convert(varchar,'" + headData.VOUDATE + "',23),1,7) and cw_accitems_gl.ACCATTRIBUTE='损益' and cw_accitems_gl.[STARTGID]='" + strCwSTARTGID + "' and cw_genleg_accitems.[STARTGID]='" + strCwSTARTGID + "'";
// // + "(case when cw_accitems_gl.DC='借' then AMTLASTBLC ELSE 0 end) as AMTYEARDR,"//RMB期初借方
// // + "(case when cw_accitems_gl.DC='贷' then AMTLASTBLC ELSE 0 end) as AMTYEARCR,"//RMB期初贷方
// // + "AMTLASTBLC as AMTYEARBLC,"//RMB期初余额
// // + "0 as AMTDR,"//RMB本月借方
// // + "0 as AMTCR,"//RMB本月贷方
// // + "0 as AMTBLC,"//RMB本月余额
// // + "AMTLASTDR,"//RMB累计借方
// // + "AMTLASTCR,"//RMB累计贷方
// // + "AMTLASTBLC,"//RMB累计余额
// // + "(case when cw_accitems_gl.DC='借' then FCYLASTBLC ELSE 0 end) as FCYYEARDR,"//外币期初借方
// // + "(case when cw_accitems_gl.DC='贷' then FCYLASTBLC ELSE 0 end) as FCYYEARCR,"//外币期初贷方
// // + "FCYLASTBLC as FCYYEARBLC,"//外币期初余额
// // + "0 as FCYDR,"//外币本月借方
// // + "0 as FCYCR,"//外币本月贷方
// // + "0 as FCYBLC,"//外币本月余额
// // + "FCYLASTDR,"//外币累计借方
// // + "FCYLASTCR,"//外币累计贷方
// // + "FCYLASTBLC,"//外币累计余额
// // + "(case when cw_accitems_gl.DC='借' then QTYLASTBLC ELSE 0 end) as QTYYEARDR,"//期初借方
// // + "(case when cw_accitems_gl.DC='贷' then QTYLASTBLC ELSE 0 end) as QTYYEARCR,"//期初贷方
// // + "QTYLASTBLC as QTYYEARBLC,"//期初余额
// // + "0 as QTYDR,"//本月借方
// // + "0 as QTYCR,"//本月贷方
// // + "0 as QTYBLC,"//本月余额
// // + "QTYLASTDR,"//累计借方
// // + "QTYLASTCR,"//累计贷方
// // + "QTYLASTBLC,"//累计余额
// // + "'" + companyid + "' as CORPID,"//分公司代码
// // + "'False' as ISDELETE,'' as DELETEUSER,getdate() as DELETETIME,'" + strUserID + "' as CREATEUSER,getdate() as CREATETIME,'" + strUserID + "' as MODIFIEDUSER,getdate() as MODIFIEDTIME,'True' as ISENABLE,PFADR,PFACR,cw_genleg_accitems.STARTGID,"//账套启用GID
// // + "SUBSTRING(convert(varchar,dateadd(month,1,(ACCDATE+'-01')),23),1,7) as ACCDATE"//会计期间
// // + " from cw_genleg_accitems INNER JOIN cw_accitems_gl ON cw_genleg_accitems.LINKGID = cw_accitems_gl.GID where cw_accitems_gl.[YEAR]=SUBSTRING('" + headData.VOUDATE + "',1,4) and ACCDATE=SUBSTRING(convert(varchar,'" + headData.VOUDATE + "',23),1,7) and cw_accitems_gl.ACCATTRIBUTE='损益' and cw_accitems_gl.[STARTGID]='" + strCwSTARTGID + "' and cw_genleg_accitems.[STARTGID]='" + strCwSTARTGID + "'";
stringsSQL1=" UPDATE B SET B.AMTYEARDR = (CASE WHEN A.DC = '借' THEN A.AMTLASTBLC ELSE 0 END),"
+"B.AMTYEARCR = (CASE WHEN A.DC = '贷' THEN A.AMTLASTBLC ELSE 0 END),"
+"B.AMTYEARBLC = A.AMTLASTBLC,"
+"B.AMTLASTDR = (CASE WHEN A.DC = '借' THEN B.AMTDR + A.AMTLASTBLC ELSE B.AMTDR END),"
+"B.AMTLASTcR = (CASE WHEN A.DC = '贷' THEN B.AMTCR + A.AMTLASTBLC ELSE B.AMTCR END),"
+"B.AMTLASTBLC = B.AMTBLC+A.AMTLASTBLC,"
+"B.FCYYEARDR = (CASE WHEN A.DC = '借' THEN A.FCYLASTBLC ELSE 0 END),"
+"B.FCYYEARCR = (CASE WHEN A.DC = '贷' THEN A.FCYLASTBLC ELSE 0 END),"
+"B.FCYYEARBLC = A.FCYLASTBLC,"
+"B.FCYLASTDR = (CASE WHEN A.DC = '借' THEN B.FCYDR + A.FCYLASTBLC ELSE 0 END),"
+"B.FCYLASTcR = (CASE WHEN A.DC = '贷' THEN B.FCYCR + A.FCYLASTBLC ELSE 0 END),"
+"B.FCYLASTBLC = B.FCYBLC+A.FCYLASTBLC,"
+"B.QTYYEARDR = (CASE WHEN A.DC = '借' THEN A.QTYLASTBLC ELSE 0 END),"
+"B.QTYYEARCR = (CASE WHEN A.DC = '贷' THEN A.QTYLASTBLC ELSE 0 END),"
+"B.QTYYEARBLC = A.QTYLASTBLC,"
+"B.QTYLASTDR = (CASE WHEN A.DC = '借' THEN B.QTYDR + A.QTYLASTBLC ELSE 0 END),"
+"B.QTYLASTcR = (CASE WHEN A.DC = '贷' THEN B.QTYCR + A.QTYLASTBLC ELSE 0 END),"
+"B.QTYLASTBLC = B.QTYBLC+A.QTYLASTBLC "
+" from VW_cw_genleg_accitems_gl A,VW_cw_genleg_accitems_gl B"
+" where a.[STARTGID]='"+strCwSTARTGID+"' and a.ACCDATE='"+strYEAR+"-"+strMONTH+"' "
+" and B.[STARTGID]= '"+strCwSTARTGID+"' and B.ACCDATE= '"+strYEAR_new+"-"+strMONTH_new+"'"
+" AND A.LINKGID= B.LINKGID AND A.DEPTACC= B.DEPTACC AND A.EMPLACC= B.EMPLACC AND A.CORPACC= B.CORPACC AND A.ITEMACC= B.ITEMACC AND A.CURRENCY= B.CURRENCY";// and ACCATTRIBUTE<>'损益' and CORPID='" + companyid + "'
boolbl=T_ALL_DA.GetExecuteSqlCommand(sSQL1);
if(!bl)
{
result.Success=false;
result.Message="操作出现错误,请重试!"+sSQL1;
returnresult;
}
stringsSQL11=" INSERT INTO cw_genleg_accitems (GID,[YEAR],[MONTH],LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,CURRENCY,AMTYEARDR,AMTYEARCR,AMTYEARBLC,AMTDR,AMTCR,AMTBLC,AMTLASTDR,AMTLASTCR,AMTLASTBLC,FCYYEARDR,FCYYEARCR,FCYYEARBLC,FCYDR,FCYCR,FCYBLC,FCYLASTDR,FCYLASTCR,FCYLASTBLC,"
+" select newid() as GID,'"+strYEAR_new+"' as [YEAR],'"+strMONTH_new+"' as [MONTH],"
+" LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,CURRENCY,(case when DC = '借' then AMTLASTBLC ELSE 0 end) as AMTYEARDR,(case when DC = '贷' then AMTLASTBLC ELSE 0 end) as AMTYEARCR,AMTLASTBLC as AMTYEARBLC,0 as AMTDR,0 as AMTCR,0 as AMTBLC,"
+" (case when DC = '借' then AMTLASTBLC ELSE 0 end) as AMTLASTDR,(case when DC = '贷' then AMTLASTBLC ELSE 0 end) as AMTLASTCR,AMTLASTBLC as AMTLASTBLC,(case when DC = '借' then FCYLASTBLC ELSE 0 end) as FCYYEARDR,"
+" (case when DC = '贷' then FCYLASTBLC ELSE 0 end) as FCYYEARCR,FCYLASTBLC as FCYYEARBLC,0 as FCYDR,0 as FCYCR,0 as FCYBLC,(case when DC = '借' then FCYLASTBLC ELSE 0 end) as FCYLASTDR,(case when DC = '贷' then FCYLASTBLC ELSE 0 end) as FCYLASTCR,"
+" FCYLASTBLC as FCYLASTBLC,(case when DC = '借' then QTYLASTBLC ELSE 0 end) as QTYYEARDR,(case when DC = '贷' then QTYLASTBLC ELSE 0 end) as QTYYEARCR,QTYLASTBLC as QTYYEARBLC,0 as QTYDR,0 as QTYCR,0 as QTYBLC,(case when DC = '借' then QTYLASTBLC ELSE 0 end) as QTYLASTDR,"
+" (case when DC = '贷' then QTYLASTBLC ELSE 0 end) as QTYLASTCR,QTYLASTBLC as QTYLASTBLC,'"+companyid+"' as CORPID,'False' as ISDELETE,'' as DELETEUSER,getdate() as DELETETIME,'"+strUserID+"' as CREATEUSER,getdate() as CREATETIME,"
+" '"+strUserID+"' as MODIFIEDUSER,getdate() as MODIFIEDTIME,'True' as ISENABLE,PFADR,PFACR,STARTGID,SUBSTRING(convert(varchar, dateadd(month, 1, (ACCDATE + '-01')), 23), 1, 7) as ACCDATE"
+" from VW_cw_genleg_accitems_gl A where [STARTGID] = '"+strCwSTARTGID+"' and ACCDATE = '"+strYEAR+"-"+strMONTH+"'"
+" AND NOT EXISTS"
+" (SELECT 1 FROM VW_cw_genleg_accitems_gl B WHERE B.[STARTGID] = '"+strCwSTARTGID+"' and B.ACCDATE = '"+strYEAR_new+"-"+strMONTH_new+"'"
+" AND A.LINKGID = B.LINKGID AND A.DEPTACC = B.DEPTACC AND A.EMPLACC = B.EMPLACC AND A.CORPACC = B.CORPACC AND A.ITEMACC = B.ITEMACC AND A.CURRENCY = B.CURRENCY)";// and ACCATTRIBUTE<>'损益' and CORPID='" + companyid + "'
bl=T_ALL_DA.GetExecuteSqlCommand(sSQL11);
if(!bl)
{
result.Success=false;
result.Message="操作出现错误,请重试!"+sSQL11;
returnresult;
}
//取下期账期数据
sSQL1=" select * from vw_cw_genleg_accitems_gl WITH(NOLOCK) where [STARTGID]='"+strCwSTARTGID+"' and ACCDATE='"+strYEAR_new+"-"+strMONTH_new+"' and [YEAR]='"+strYEAR_new+"'";// and ACCATTRIBUTE<>'损益' and CORPID='" + companyid + "'
#endregion
#endregion
#region 更新cw_carry_over语句
sSQL1="INSERT INTO [cw_carry_over]([GID],[ACCDATE],[YEAR],[MONTH],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME],STARTGID) VALUES(newid(),'"+strYEAR+"-"+strMONTH+"','"+strYEAR+"','"+strMONTH+"','"+companyid+"','"+strUserID+"',GETDATE(),'"+strCwSTARTGID+"')";
+",AMTDR=isnull((SELECT AMTDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",AMTCR=isnull((SELECT AMTCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",AMTBLC=isnull((SELECT AMTBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",AMTLASTDR=isnull((AMTYEARDR+isnull((SELECT AMTDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)),0)"
+",AMTLASTCR=isnull((AMTYEARCR+isnull((SELECT AMTCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)),0)"
+",AMTLASTBLC=isnull((AMTYEARBLC+isnull((SELECT AMTBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)),0)"
+",FCYYEARDR,FCYYEARCR,FCYYEARBLC"
+",FCYDR=isnull((SELECT FCYDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",FCYCR=isnull((SELECT FCYCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",FCYBLC=isnull((SELECT FCYBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",FCYLASTDR=isnull((FCYYEARDR+isnull((SELECT FCYDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)),0)"
+",FCYLASTCR=isnull((FCYYEARCR+isnull((SELECT FCYCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)),0)"
+",FCYLASTBLC=isnull((FCYYEARBLC+isnull((SELECT FCYBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)),0)"
+",QTYYEARDR,QTYYEARCR,QTYYEARBLC"
+",QTYDR=isnull((SELECT QTYDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",QTYCR=isnull((SELECT QTYCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",QTYBLC=isnull((SELECT QTYBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",QTYLASTDR=isnull((QTYYEARDR+isnull((SELECT QTYDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)),0)"
+",QTYLASTCR=isnull((QTYYEARCR+isnull((SELECT QTYCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)),0)"
+",QTYLASTBLC=isnull((QTYYEARBLC+isnull((SELECT QTYBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)),0)"
+",ISENABLE=isnull((SELECT ISENABLE from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+" where a.STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and ACCATTRIBUTE<>'损益' and [ACCYEAR]='"+strYEAR+"' and CORPID='"+companyid+"'"
+",AMTDR=isnull((SELECT AMTDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",AMTCR=isnull((SELECT AMTCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",AMTBLC=isnull((SELECT AMTBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",AMTLASTDR=isnull((SELECT AMTDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",AMTLASTCR=isnull((SELECT AMTCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",AMTLASTBLC=isnull((SELECT AMTBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",0 as FCYYEARDR,0 as FCYYEARCR,0 as FCYYEARBLC"
+",FCYDR=isnull((SELECT FCYDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",FCYCR=isnull((SELECT FCYCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",FCYBLC=isnull((SELECT FCYBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",FCYLASTDR=isnull((SELECT FCYDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",FCYLASTCR=isnull((SELECT FCYCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",FCYLASTBLC=isnull((SELECT FCYBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",0 as QTYYEARDR,0 as QTYYEARCR,0 as QTYYEARBLC"
+",QTYDR=isnull((SELECT QTYDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",QTYCR=isnull((SELECT QTYCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",QTYBLC=isnull((SELECT QTYBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",QTYLASTDR=isnull((SELECT QTYDR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",QTYLASTCR=isnull((SELECT QTYCR from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",QTYLASTBLC=isnull((SELECT QTYBLC from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+",ISENABLE=isnull((SELECT ISENABLE from VW_cw_genleg_accitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCYEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=a.ACCID AND DEPTACC=a.DEPTACC AND EMPLACC=a.EMPLACC AND CORPACC=a.CORPACC AND ITEMACC=a.ITEMACC AND CURRENCY=a.CURRENCY),0)"
+" where a.STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and ACCATTRIBUTE='损益' and [ACCYEAR]='"+strYEAR+"' and CORPID='"+companyid+"'"
stringsSQL6="delete from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and LINKGID in (SELECT gid from cw_accitems_gl WHERE [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID in (SELECT ACCID from cw_accitems_gl WHERE [YEAR]='"+strYEAR+"' and [STARTGID]='"+strCwSTARTGID+"'))";// and CORPID='" + companyid + "'
stringsSQL4="INSERT INTO [cw_accitems_gl]([GID],[ACCID],[ACCNAME],[DETAILED],[DC],[ISFCY],[ISDEPTACC],[ISEMPLACC],[ISCORPACC],[ISITEMACC],[REMARKS],[YEAR],[MONTH],[PACCGID],[ACCATTRIBUTE],[ISENABLE],[ACCTYPE],[STARTGID]) "
+" SELECT NEWID() as GID,[ACCID],[ACCNAME],[DETAILED],[DC],[ISFCY],[ISDEPTACC],[ISEMPLACC],[ISCORPACC],[ISITEMACC],[REMARKS],SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) as [YEAR],'01' as [MONTH],[PACCGID]=ISNULL((SELECT top 1 gid from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and ACCID=SUBSTRING(convert(varchar,a.ACCID,23),1,(len(a.ACCID)-2))),[PACCGID]),[ACCATTRIBUTE],0 as [ISENABLE],[ACCTYPE],[STARTGID] from cw_accitems_gl as a WHERE [YEAR]='"+strYEAR+"' and [STARTGID]='"+strCwSTARTGID+"' and ACCID NOT in (SELECT ACCID from cw_accitems_gl WHERE [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4))";
stringsSQL4="update cw_genleg_accitems SET LINKGID=("
+"select top 1 bGID from ("
+"select a.GID as aGID,b.GID as bGID"
+" from (select gid,ACCID from cw_accitems_gl where [year]='"+strYEAR+"' and [STARTGID]='"+strCwSTARTGID+"' GROUP by gid,ACCID) as a"
+" INNER JOIN "
+"(select gid,ACCID from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and [year]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) GROUP by gid,ACCID) as b "
+" where ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and LINKGID in (SELECT gid from cw_accitems_gl WHERE [YEAR]='"+strYEAR+"' and [STARTGID]='"+strCwSTARTGID+"')";
+" WHERE [STARTGID]='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and LINKGID in (select gid from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and year=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4)) and (QTYYEARDR<>0 or QTYYEARCR<>0 or QTYYEARBLC<>0)";
sSQL5="select count(*) as inum from VW_cw_vouchers_vouitems_gl WHERE STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,7) and [ACCGID] in (select gid from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and year=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4))";
sSQL5="insert into cw_genleg_accitems(GID,YEAR,MONTH,LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,CURRENCY,AMTYEARDR,AMTYEARCR,AMTYEARBLC,AMTDR,AMTCR,AMTBLC,AMTLASTDR,AMTLASTCR,AMTLASTBLC,FCYYEARDR,FCYYEARCR,FCYYEARBLC,FCYDR,FCYCR,FCYBLC,FCYLASTDR,FCYLASTCR,FCYLASTBLC,QTYYEARDR,QTYYEARCR,QTYYEARBLC,QTYDR,QTYCR,QTYBLC,QTYLASTDR,QTYLASTCR,QTYLASTBLC,CORPID,ISDELETE,DELETEUSER,DELETETIME,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME,ISENABLE,STARTGID,ACCDATE,IsInitialEntry) "
+"(select newid() as GID, SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) as [YEAR],'01' as [MONTH],GID as LINKGID,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC,'RMB' as CURRENCY,0 as AMTYEARDR,0 as AMTYEARCR,0 as AMTYEARBLC,0 as AMTDR,0 as AMTCR,0 as AMTBLC,0 as AMTLASTDR,0 as AMTLASTCR,0 as AMTLASTBLC,0 as FCYYEARDR,0 as FCYYEARCR,0 as FCYYEARBLC,0 as FCYDR,0 as FCYCR,0 as FCYBLC,0 as FCYLASTDR,0 as FCYLASTCR,0 as FCYLASTBLC,0 as QTYYEARDR,0 as QTYYEARCR,0 as QTYYEARBLC,0 as QTYDR,0 as QTYCR,0 as QTYBLC,0 as QTYLASTDR,0 as QTYLASTCR,0 as QTYLASTBLC,'"+companyid+"' as CORPID,0 as ISDELETE,'"+strUserID+"' as DELETEUSER,getdate() as DELETETIME,'"+strUserID+"' as CREATEUSER,getdate() as CREATETIME,'"+strUserID+"' as MODIFIEDUSER,getdate() as MODIFIEDTIME,1 as ISENABLE,'',(SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4)+'-01'),0 from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) and gid not in (select LINKGID from cw_genleg_accitems where [YEAR]<=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4)))";
sSQL5="UPDATE cw_genleg_accitems set ISENABLE="+iNum+",CORPID='"+companyid+"',MODIFIEDUSER='"+strUserID+"',MODIFIEDTIME=GETDATE() WHERE [STARTGID]='"+strCwSTARTGID+"' and LINKGID in (select gid from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and year=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4))";//ACCDATE=SUBSTRING(convert(varchar,dateadd(month,1,('" + rq2 + "')),23),1,7) and
stringsSQL51="UPDATE cw_accitems_gl set ISENABLE="+iNum+" WHERE [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4)";
stringsSQL7="select count(*) as inum from cw_sofp_template_detail WHERE [LINKGID] in (select gid from cw_sofp_template where [STARTGID]='"+strCwSTARTGID+"') and [ACCGID] in (select gid from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and year=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4))";
stringsSQL8="select count(*) as inum from cw_pl_template_detail WHERE [LINKGID] in (select gid from cw_pl_template where [STARTGID]='"+strCwSTARTGID+"') and [ACCGID] in (select gid from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and year=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4))";
stringsSQL9="select count(*) as inum from cw_accitems_currency WHERE [LINKGID] in (select gid from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and year=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4))";
sSQL9="INSERT INTO [cw_accitems_currency]([GID],[LINKGID],[CURRENCY],[CORPID],[CREATEUSER],[CREATETIME])"
+" SELECT TOP 1 NEWID() as GID"
+",[LINKGID]="
+"("
+"select top 1 bGID from ("
+" select a.GID as aGID,b.GID as bGID"
+" from (select gid,ACCID from cw_accitems_gl where [year]='"+strYEAR+"' and [STARTGID]='"+strCwSTARTGID+"' GROUP by gid,ACCID) as a"
+" ,(select gid,ACCID from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and [year]=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4) GROUP by gid,ACCID) as b"
+" where a.ACCID=b.ACCID"
+") as c "
+" where c.aGID=[cw_accitems_currency].LINKGID"
+")"
+",[CURRENCY],[CORPID],[CREATEUSER],GETDATE() as [CREATETIME]"
+" from [cw_accitems_currency]"
+" where [LINKGID] in (select gid from cw_accitems_gl where [year]='"+strYEAR+"' and [STARTGID]='"+strCwSTARTGID+"') ORDER BY CREATETIME DESC ";
stringsSQL10="select count(*) as inum from cw_cfs_template_detail WHERE LINKGID in (select gid from cw_cfs_template where [STARTGID]='"+strCwSTARTGID+"') and [ACCGID] in (select gid from cw_accitems_gl where [STARTGID]='"+strCwSTARTGID+"' and year=SUBSTRING(convert(varchar,dateadd(month,1,('"+rq2+"')),23),1,4))";