stringsACCDATE01end=" and ACCDATE>='"+DateTime.Parse(strACCDATE+"-01").Year.ToString()+"-01'"+ACCDATEend.Replace("=","<=");
varstrSql=newStringBuilder();
if(strCURRENCY.Trim()=="综合本位币")
{
#region 综合本位币
strSql.Append("(");
//strSql.Append("SELECT *");
strSql.Append("SELECT newid() as GID,LINKCODE,DC,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC");
strSql.Append(",isnull(sum(YEARDR),0) as YEARDR,isnull(sum(YEARCR),0) as YEARCR,isnull(sum(YEARBLC),0) as YEARBLC,isnull(sum(DR),0) as DR,isnull(sum(CR),0) as CR,isnull(sum(BLC),0) as BLC,isnull(sum(LASTDR),0) as LASTDR,isnull(sum(LASTCR),0) as LASTCR,isnull(sum(LASTBLC),0) as LASTBLC,isnull(sum(AMTCPDR),0) as AMTCPDR,isnull(sum(AMTCPCR),0) as AMTCPCR,isnull(sum(QTYYEARDR),0) as QTYYEARDR,isnull(sum(QTYYEARCR),0) as QTYYEARCR,isnull(sum(QTYYEARBLC),0) as QTYYEARBLC,isnull(sum(QTYDR),0) as QTYDR,isnull(sum(QTYCR),0) as QTYCR,isnull(sum(QTYBLC),0) as QTYBLC,isnull(sum(QTYLASTDR),0) as QTYLASTDR,isnull(sum(QTYLASTCR),0) as QTYLASTCR,isnull(sum(QTYLASTBLC),0) as QTYLASTBLC,isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR");
strSql.Append(",QTYCPDR=isnull((case when DC='借' then (isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0))-(isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0)) else 0 end),0)");
strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0))-(isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0)) else 0 end),0)");
strSql.Append(" from (");
strSql.Append("SELECT newid() as GID,LINKCODE,DC");
if(strCbHSKM.IndexOf("CORPACC")>-1)
{
strSql.Append(",(LINKNAME+(case when (CORPACC='' or CORPACC is null) then '' else ('▁'+CORPACC) end)+(case when (DEPTACC='' or DEPTACC is null) then '' else ('▁'+DEPTACC) end)+(case when (EMPLACC='' or EMPLACC is null) then '' else ('▁'+EMPLACC) end)+(case when (ITEMACC='' or ITEMACC is null) then '' else ('▁'+ITEMACC) end)) as LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC");
}
else
{
strSql.Append(",LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC");
strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR+PFADR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 "+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR+PFACR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 "+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC+(PFADR-PFACR)) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 "+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 "+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 "+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 "+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 "+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 "+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 "+strCbHSKM+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR");
strSql.Append(" from (");
strSql.Append("select *,LINKCODE=(select top 1 ACCID from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID),LINKNAME=(select top 1 ACCNAME from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID),DC=(select top 1 DC from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems with(nolock) where [STARTGID]='"+strCwSTARTGID+"' and ISDELETE=0 and ISENABLE=1 "+strCbHSKM);
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(") as a");
if(strCbHSKM.IndexOf("CORPACC")>-1)
{
strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC,STARTGID");
}
else
{
//strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC");
strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID");
}
strSql.Append(") as b where 1=1");
if(!string.IsNullOrEmpty(strCbJZF))
{
strSql.Append(" and "+strCbJZF);
}
if(strCbHSKM.IndexOf("CORPACC")>-1)
{
strSql.Append(" group by LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC");
}
else
{
//strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC");
strSql.Append(" group by LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC");
}
strSql.Append(") union (");
strSql.Append("SELECT NEWID() as GID,'合计:' as LINKCODE,'' as DC,'' as LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC");
strSql.Append(",0 as YEARDR,0 as YEARCR,0 as YEARBLC,0 as DR,0 as CR,0 as BLC,0 as LASTDR,0 as LASTCR,0 as LASTBLC,0 as AMTCPDR,0 as AMTCPCR");
strSql.Append(",isnull(sum(QTYYEARDR),0) as QTYYEARDR");
strSql.Append(",isnull(sum(QTYYEARCR),0) as QTYYEARCR");
strSql.Append(",isnull(sum(QTYYEARBLC),0) as QTYYEARBLC");
strSql.Append(",isnull(sum(QTYDR),0) as QTYDR");
strSql.Append(",isnull(sum(QTYCR),0) as QTYCR");
strSql.Append(",isnull(sum(QTYBLC),0) as QTYBLC");
strSql.Append(",isnull(sum(QTYLASTDR),0) as QTYLASTDR");
strSql.Append(",isnull(sum(QTYLASTCR),0) as QTYLASTCR");
strSql.Append(",isnull(sum(QTYLASTBLC),0) as QTYLASTBLC");
strSql.Append(",isnull(sum(PFADR),0) as PFADR");
strSql.Append(",isnull(sum(PFACR),0) as PFACR");
strSql.Append(",isnull(sum(QTYCPDR),0) as QTYCPDR");
strSql.Append(",isnull(sum(QTYCPCR),0) as QTYCPCR");
strSql.Append(" from (");
strSql.Append("SELECT *");
strSql.Append(",QTYCPDR=isnull((case when DC='借' then (QTYYEARDR+QTYDR)-(QTYYEARCR+QTYCR) else 0 end),0)");
strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (QTYYEARCR+QTYCR)-(QTYYEARDR+QTYDR) else 0 end),0)");
strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR+PFADR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR+PFACR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC+(PFADR-PFACR)) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1"+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR");
strSql.Append(",DC=(select top 1 DC from [cw_accitems_gl] with(nolock) where gid=a.LINKGID)");
strSql.Append(" from cw_genleg_accitems as a");
strSql.Append(" where a.[STARTGID]='"+strCwSTARTGID+"' and ISDELETE=0 and ISENABLE=1");
strSql.Append(" and LINKGID in (select GID from [cw_accitems_gl] with(nolock) where [YEAR]=SUBSTRING('"+strACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and DETAILED=1)");//合计数不计算子科目,否则会造成重复合计
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(" group by LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID");
strSql.Append(") as b where 1=1");
if(!string.IsNullOrEmpty(strCbJZF))
{
strSql.Append(" and "+strCbJZF);
}
strSql.Append(") as c");
strSql.Append(")");
#endregion
}
elseif(strCURRENCY.Trim()=="RMB")
{
#region RMB
strSql.Append("(");
//strSql.Append("SELECT *");
strSql.Append("SELECT newid() as GID,LINKCODE,DC,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC");
strSql.Append(",isnull(sum(YEARDR),0) as YEARDR,isnull(sum(YEARCR),0) as YEARCR,isnull(sum(YEARBLC),0) as YEARBLC,isnull(sum(DR),0) as DR,isnull(sum(CR),0) as CR,isnull(sum(BLC),0) as BLC,isnull(sum(LASTDR),0) as LASTDR,isnull(sum(LASTCR),0) as LASTCR,isnull(sum(LASTBLC),0) as LASTBLC,isnull(sum(QTYYEARDR),0) as QTYYEARDR,isnull(sum(QTYYEARCR),0) as QTYYEARCR,isnull(sum(QTYYEARBLC),0) as QTYYEARBLC,isnull(sum(QTYDR),0) as QTYDR,isnull(sum(QTYCR),0) as QTYCR,isnull(sum(QTYBLC),0) as QTYBLC,isnull(sum(QTYLASTDR),0) as QTYLASTDR,isnull(sum(QTYLASTCR),0) as QTYLASTCR,isnull(sum(QTYLASTBLC),0) as QTYLASTBLC,isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR");
strSql.Append(",AMTCPDR=isnull((case when DC='借' then (isnull(sum(YEARDR),0)+isnull(sum(DR),0))-(isnull(sum(YEARCR),0)+isnull(sum(CR),0)) else 0 end),0)");
strSql.Append(",AMTCPCR=isnull((case when DC='贷' then (isnull(sum(YEARCR),0)+isnull(sum(CR),0))-(isnull(sum(YEARDR),0)+isnull(sum(DR),0)) else 0 end),0)");
strSql.Append(",QTYCPDR=isnull((case when DC='借' then (isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0))-(isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0)) else 0 end),0)");
strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0))-(isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0)) else 0 end),0)");
strSql.Append(" from (");
strSql.Append("SELECT newid() as GID,LINKCODE,DC");
if(strCbHSKM.IndexOf("CORPACC")>-1)
{
strSql.Append(",(LINKNAME+(case when (CORPACC='' or CORPACC is null) then '' else ('▁'+CORPACC) end)+(case when (DEPTACC='' or DEPTACC is null) then '' else ('▁'+DEPTACC) end)+(case when (EMPLACC='' or EMPLACC is null) then '' else ('▁'+EMPLACC) end)+(case when (ITEMACC='' or ITEMACC is null) then '' else ('▁'+ITEMACC) end)) as LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC");
}
else
{
strSql.Append(",LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC");
strSql.Append(",YEARDR=isnull((select sum(AMTYEARDR+PFADR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",YEARCR=isnull((select sum(AMTYEARCR+PFACR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",YEARBLC=isnull((select sum(AMTYEARBLC+(PFADR-PFACR)) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",DR=isnull((select sum(AMTDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",CR=isnull((select sum(AMTCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",BLC=isnull((select sum(AMTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTDR=isnull((select sum(AMTDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTCR=isnull((select sum(AMTCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTBLC=isnull((select sum(AMTLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR+PFADR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR-PFADR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC+(PFADR-PFACR)) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR+PFADR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR+PFACR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC+(PFADR-PFACR)) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR ");
strSql.Append(" from (");
strSql.Append("select *,LINKCODE=(select top 1 ACCID from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID),LINKNAME=(select top 1 ACCNAME from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID),DC=(select top 1 DC from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems with(nolock) where [STARTGID]='"+strCwSTARTGID+"' and ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM);
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(") as a");
if(strCbHSKM.IndexOf("CORPACC")>-1)
{
strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC,STARTGID");
}
else
{
//strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC");
strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID");
}
strSql.Append(") as b where 1=1");
if(!string.IsNullOrEmpty(strCbJZF))
{
strSql.Append(" and "+strCbJZF);
}
if(strCbHSKM.IndexOf("CORPACC")>-1)
{
strSql.Append(" group by LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC");
}
else
{
//strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC");
strSql.Append(" group by LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC");
}
strSql.Append(") union (");
strSql.Append("SELECT NEWID() as GID,'合计:' as LINKCODE,'' as DC,'' as LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC");
strSql.Append(",isnull(sum(YEARDR),0) as YEARDR");
strSql.Append(",isnull(sum(YEARCR),0) as YEARCR");
strSql.Append(",isnull(sum(YEARBLC),0) as YEARBLC");
strSql.Append(",isnull(sum(DR),0) as DR");
strSql.Append(",isnull(sum(CR),0) as CR");
strSql.Append(",isnull(sum(BLC),0) as BLC");
strSql.Append(",isnull(sum(LASTDR),0) as LASTDR");
strSql.Append(",isnull(sum(LASTCR),0) as LASTCR");
strSql.Append(",isnull(sum(LASTBLC),0) as LASTBLC");
strSql.Append(",isnull(sum(QTYYEARDR),0) as QTYYEARDR");
strSql.Append(",isnull(sum(QTYYEARCR),0) as QTYYEARCR");
strSql.Append(",isnull(sum(QTYYEARBLC),0) as QTYYEARBLC");
strSql.Append(",isnull(sum(QTYDR),0) as QTYDR");
strSql.Append(",isnull(sum(QTYCR),0) as QTYCR");
strSql.Append(",isnull(sum(QTYBLC),0) as QTYBLC");
strSql.Append(",isnull(sum(QTYLASTDR),0) as QTYLASTDR");
strSql.Append(",isnull(sum(QTYLASTCR),0) as QTYLASTCR");
strSql.Append(",isnull(sum(QTYLASTBLC),0) as QTYLASTBLC");
strSql.Append(",isnull(sum(PFADR),0) as PFADR");
strSql.Append(",isnull(sum(PFACR),0) as PFACR");
strSql.Append(",isnull(sum(AMTCPDR),0) as AMTCPDR");
strSql.Append(",isnull(sum(AMTCPCR),0) as AMTCPCR");
strSql.Append(",isnull(sum(QTYCPDR),0) as QTYCPDR");
strSql.Append(",isnull(sum(QTYCPCR),0) as QTYCPCR");
strSql.Append(" from (");
strSql.Append("SELECT *");
strSql.Append(",AMTCPDR=isnull((case when DC='借' then (YEARDR+DR)-(YEARCR+CR) else 0 end),0)");
strSql.Append(",AMTCPCR=isnull((case when DC='贷' then (YEARCR+CR)-(YEARDR+DR) else 0 end),0)");
strSql.Append(",QTYCPDR=isnull((case when DC='借' then (QTYYEARDR+QTYDR)-(QTYYEARCR+QTYCR) else 0 end),0)");
strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (QTYYEARCR+QTYCR)-(QTYYEARDR+QTYDR) else 0 end),0)");
strSql.Append(",YEARDR=isnull((select sum(AMTYEARDR+PFADR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",YEARCR=isnull((select sum(AMTYEARCR+PFACR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",YEARBLC=isnull((select sum(AMTYEARBLC+(PFADR-PFACR)) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",DR=isnull((select sum(AMTDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",CR=isnull((select sum(AMTCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",BLC=isnull((select sum(AMTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTDR=isnull((select sum(AMTDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTCR=isnull((select sum(AMTCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTBLC=isnull((select sum(AMTLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR+PFADR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR+PFACR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC+(PFADR-PFACR)) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTDR=isnull((select sum(QTYLASTDR+PFADR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTCR=isnull((select sum(QTYLASTCR+PFACR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC+(PFADR-PFACR)) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR");
strSql.Append(",DC=(select top 1 DC from [cw_accitems_gl] with(nolock) where gid=a.LINKGID)");
strSql.Append(" from cw_genleg_accitems as a");
strSql.Append(" where a.[STARTGID]='"+strCwSTARTGID+"' and ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'");
strSql.Append(" and LINKGID in (select GID from [cw_accitems_gl] with(nolock) where [YEAR]=SUBSTRING('"+strACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and DETAILED=1)");// + strCbHSKM//合计数不计算子科目,否则会造成重复合计
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(" group by LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID");
strSql.Append(") as b where 1=1");
if(!string.IsNullOrEmpty(strCbJZF))
{
strSql.Append(" and "+strCbJZF);
}
strSql.Append(") as c");
strSql.Append(")");
#endregion
}
else//外币
{
#region 外币
strSql.Append("(");
//strSql.Append("SELECT *");
strSql.Append("SELECT newid() as GID,LINKCODE,DC,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC");
strSql.Append(",isnull(sum(YEARDR),0) as YEARDR,isnull(sum(YEARCR),0) as YEARCR,isnull(sum(YEARBLC),0) as YEARBLC,isnull(sum(DR),0) as DR,isnull(sum(CR),0) as CR,isnull(sum(BLC),0) as BLC,isnull(sum(LASTDR),0) as LASTDR,isnull(sum(LASTCR),0) as LASTCR,isnull(sum(LASTBLC),0) as LASTBLC,isnull(sum(QTYYEARDR),0) as QTYYEARDR,isnull(sum(QTYYEARCR),0) as QTYYEARCR,isnull(sum(QTYYEARBLC),0) as QTYYEARBLC,isnull(sum(QTYDR),0) as QTYDR,isnull(sum(QTYCR),0) as QTYCR,isnull(sum(QTYBLC),0) as QTYBLC,isnull(sum(QTYLASTDR),0) as QTYLASTDR,isnull(sum(QTYLASTCR),0) as QTYLASTCR,isnull(sum(QTYLASTBLC),0) as QTYLASTBLC,isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR");
strSql.Append(",AMTCPDR=isnull((case when DC='借' then (isnull(sum(YEARDR),0)+isnull(sum(DR),0))-(isnull(sum(YEARCR),0)+isnull(sum(CR),0)) else 0 end),0)");
strSql.Append(",AMTCPCR=isnull((case when DC='贷' then (isnull(sum(YEARCR),0)+isnull(sum(CR),0))-(isnull(sum(YEARDR),0)+isnull(sum(DR),0)) else 0 end),0)");
strSql.Append(",QTYCPDR=isnull((case when DC='借' then (isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0))-(isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0)) else 0 end),0)");
strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0))-(isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0)) else 0 end),0)");
strSql.Append(" from (");
strSql.Append("SELECT newid() as GID,LINKCODE,DC");
if(strCbHSKM.IndexOf("CORPACC")>-1)
{
strSql.Append(",(LINKNAME+(case when (CORPACC='' or CORPACC is null) then '' else ('▁'+CORPACC) end)+(case when (DEPTACC='' or DEPTACC is null) then '' else ('▁'+DEPTACC) end)+(case when (EMPLACC='' or EMPLACC is null) then '' else ('▁'+EMPLACC) end)+(case when (ITEMACC='' or ITEMACC is null) then '' else ('▁'+ITEMACC) end)) as LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC");
}
else
{
strSql.Append(",LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC");
}
strSql.Append(",YEARDR=isnull((select sum(FCYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",YEARCR=isnull((select sum(FCYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",YEARBLC=isnull((select sum(FCYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",DR=isnull((select sum(FCYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",CR=isnull((select sum(FCYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",BLC=isnull((select sum(FCYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTDR=isnull((select sum(FCYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTCR=isnull((select sum(FCYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTBLC=isnull((select sum(FCYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR ");
strSql.Append(" from (");
strSql.Append("select *,LINKCODE=(select top 1 ACCID from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID),LINKNAME=(select top 1 ACCNAME from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID),DC=(select top 1 DC from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems with(nolock) where [STARTGID]='"+strCwSTARTGID+"' and ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+strCbHSKM);
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(") as a");
if(strCbHSKM.IndexOf("CORPACC")>-1)
{
strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC,STARTGID");
}
else
{
//strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC");
strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID");
}
strSql.Append(") as b where 1=1");
if(!string.IsNullOrEmpty(strCbJZF))
{
strSql.Append(" and "+strCbJZF);
}
if(strCbHSKM.IndexOf("CORPACC")>-1)
{
strSql.Append(" group by LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC");
}
else
{
//strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC");
strSql.Append(" group by LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC");
}
strSql.Append(") union (");
strSql.Append("SELECT NEWID() as GID,'合计:' as LINKCODE,'' as DC,'' as LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC");
strSql.Append(",isnull(sum(YEARDR),0) as YEARDR");
strSql.Append(",isnull(sum(YEARCR),0) as YEARCR");
strSql.Append(",isnull(sum(YEARBLC),0) as YEARBLC");
strSql.Append(",isnull(sum(DR),0) as DR");
strSql.Append(",isnull(sum(CR),0) as CR");
strSql.Append(",isnull(sum(BLC),0) as BLC");
strSql.Append(",isnull(sum(LASTDR),0) as LASTDR");
strSql.Append(",isnull(sum(LASTCR),0) as LASTCR");
strSql.Append(",isnull(sum(LASTBLC),0) as LASTBLC");
strSql.Append(",isnull(sum(QTYYEARDR),0) as QTYYEARDR");
strSql.Append(",isnull(sum(QTYYEARCR),0) as QTYYEARCR");
strSql.Append(",isnull(sum(QTYYEARBLC),0) as QTYYEARBLC");
strSql.Append(",isnull(sum(QTYDR),0) as QTYDR");
strSql.Append(",isnull(sum(QTYCR),0) as QTYCR");
strSql.Append(",isnull(sum(QTYBLC),0) as QTYBLC");
strSql.Append(",isnull(sum(QTYLASTDR),0) as QTYLASTDR");
strSql.Append(",isnull(sum(QTYLASTCR),0) as QTYLASTCR");
strSql.Append(",isnull(sum(QTYLASTBLC),0) as QTYLASTBLC");
strSql.Append(",isnull(sum(PFADR),0) as PFADR");
strSql.Append(",isnull(sum(PFACR),0) as PFACR");
strSql.Append(",isnull(sum(AMTCPDR),0) as AMTCPDR");
strSql.Append(",isnull(sum(AMTCPCR),0) as AMTCPCR");
strSql.Append(",isnull(sum(QTYCPDR),0) as QTYCPDR");
strSql.Append(",isnull(sum(QTYCPCR),0) as QTYCPCR");
strSql.Append(" from (");
strSql.Append("SELECT *");
strSql.Append(",AMTCPDR=isnull((case when DC='借' then (YEARDR+DR)-(YEARCR+CR) else 0 end),0)");
strSql.Append(",AMTCPCR=isnull((case when DC='贷' then (YEARCR+CR)-(YEARDR+DR) else 0 end),0)");
strSql.Append(",QTYCPDR=isnull((case when DC='借' then (QTYYEARDR+QTYDR)-(QTYYEARCR+QTYCR) else 0 end),0)");
strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (QTYYEARCR+QTYCR)-(QTYYEARDR+QTYDR) else 0 end),0)");
strSql.Append(" from (");
strSql.Append("SELECT STARTGID");
strSql.Append(",YEARDR=isnull((select sum(FCYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",YEARCR=isnull((select sum(FCYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",YEARBLC=isnull((select sum(FCYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",DR=isnull((select sum(FCYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",CR=isnull((select sum(FCYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",BLC=isnull((select sum(FCYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTDR=isnull((select sum(FCYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTCR=isnull((select sum(FCYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",LASTBLC=isnull((select sum(FCYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEbgn+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATEbgnend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+sACCDATE01end+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'"+ACCDATEend+" and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)");
strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR");
strSql.Append(",DC=(select top 1 DC from [cw_accitems_gl] with(nolock) where gid=a.LINKGID)");
strSql.Append(" from cw_genleg_accitems as a");
strSql.Append(" where a.[STARTGID]='"+strCwSTARTGID+"' and ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCURRENCY+"'");
strSql.Append(" and LINKGID in (select GID from [cw_accitems_gl] with(nolock) where [YEAR]=SUBSTRING('"+strACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"' and DETAILED=1)");// + strCbHSKM//合计数不计算子科目,否则会造成重复合计
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(" group by LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID");