strDataIndex="WMSDATE,CUSTOMERNAME,BLNO,STORAGENAME,GOODSNAME,GOODSMODEL,hj_GOODSRKSL,hj_GOODSPFSL_OUT,hj_GOODSPFSL_MONTH,hj_GOODSSTOCK,hj_GOODSPACK,hj_GOODSPACKPFSL,hj_GOODSPACKPFSL_MONTH,hj_GOODSPACKSTOCK, (SELECT CONTRACTNO FROM op_seai WHERE BSNO = ASSOCIATEDNO) CONTRACTNO,CHARGEUNIT,CUSTOMNO,GOODSGRADE,GOODSSLICE,GOODSSTANDARD,REMARK,hj_GOODSPACKACTUAL,hj_GOODSRKSLACTUAL,AREANAME";//字段名称
+"select '总计:' as GID, '' as WMSDATE, '总计:' as CUSTOMERNAME, '' as BLNO, '' as STORAGENAME, '' as AREANAME, '' as GOODSNAME, '' as GOODSMODEL, '' as ASSOCIATEDNO, '' as CONTRACTNO, '' as CHARGEUNIT, '' as CUSTOMNO, '' as GOODSGRADE, 0 AS GOODSSLICE, '' as GOODSSTANDARD, '' AS REMARK"
+", ISNULL(sum(hj_GOODSRKSL), 0) AS hj_GOODSRKSL"//入库量
+", ISNULL(SUM(hj_GOODSPFSL_OUT), 0) AS hj_GOODSPFSL_OUT"//出库量
+", ISNULL(SUM(hj_GOODSPFSL_MONTH), 0) AS hj_GOODSPFSL_MONTH"//出库量_区间
+", ISNULL(sum(hj_GOODSSTOCK), 0) AS hj_GOODSSTOCK"//库存量
+", ISNULL(sum(hj_GOODSPACK), 0) AS hj_GOODSPACK"//入库件数
+", ISNULL(SUM(hj_GOODSPACKPFSL), 0) AS hj_GOODSPACKPFSL"//出库件数
+", ISNULL(SUM(hj_GOODSPACKPFSL_MONTH), 0) AS hj_GOODSPACKPFSL_MONTH"//出库件数_区间
+", ISNULL(sum(hj_GOODSPACKSTOCK), 0) AS hj_GOODSPACKSTOCK"//剩余件数
+", ISNULL(sum(hj_GOODSPACKACTUAL), 0) AS hj_GOODSPACKACTUAL"//实际件数
+", ISNULL(sum(hj_GOODSRKSLACTUAL), 0) AS hj_GOODSRKSLACTUAL"//实际数量
+",isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS BALRMBDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS BALUSDDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(AMOUNT,0) END) END)ELSE 0 END),0) AS OTDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(SETTLEMENT,0) END) END)ELSE 0 END),0) AS STLOTDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(AMOUNT-SETTLEMENT,0) END) END) ELSE 0 END),0) AS BALOTDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLRMBCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS BALRMBCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS BALUSDCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(AMOUNT,0) END) END)ELSE 0 END),0) AS OTCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(SETTLEMENT,0) END) END)ELSE 0 END),0) AS STLOTCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(AMOUNT-SETTLEMENT,0) END) END) ELSE 0 END),0) AS BALOTCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN ISNULL(AMOUNT,0) *ISNULL( EXCHANGERATE,0) ELSE 0 END),0) AS TTLCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN ISNULL(SETTLEMENT,0) *ISNULL( EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN ISNULL(AMOUNT-SETTLEMENT,0) *ISNULL( EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLCR"
+" FROM CH_FEE F "
+" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) "
+" Where 1=1 and "+strCondition
+" GROUP BY B.BSNO,F.CUSTOMERNAME,B.OPLBNAME, B.BSTYPE"
+",isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS BALRMBDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS BALUSDDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(AMOUNT,0) END) END)ELSE 0 END),0) AS OTDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(SETTLEMENT,0) END) END)ELSE 0 END),0) AS STLOTDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(AMOUNT-SETTLEMENT,0) END) END) ELSE 0 END),0) AS BALOTDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR,"
+"isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLRMBCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS BALRMBCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS BALUSDCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(AMOUNT,0) END) END)ELSE 0 END),0) AS OTCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(SETTLEMENT,0) END) END)ELSE 0 END),0) AS STLOTCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN 0 ELSE (CASE CURRENCY WHEN 'RMB' THEN 0 ELSE ISNULL(AMOUNT-SETTLEMENT,0) END) END) ELSE 0 END),0) AS BALOTCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN ISNULL(AMOUNT,0) *ISNULL( EXCHANGERATE,0) ELSE 0 END),0) AS TTLCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN ISNULL(SETTLEMENT,0) *ISNULL( EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLCR,"
+"isnull(SUM(CASE FEETYPE WHEN 2 THEN ISNULL(AMOUNT-SETTLEMENT,0) *ISNULL( EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLCR"
+" FROM CH_FEE F "
+" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) "
+" Where 1=1 and "+strCondition
+" GROUP BY B.BSNO,F.CUSTOMERNAME,B.OPLBNAME, B.BSTYPE"
strSql.Append("SELECT LINKCODE,LINKNAME,QTYYEARDR,QTYYEARCR,QTYDR,QTYCR,QTYLASTDR,QTYLASTCR,QTYCPDR,QTYCPCR from ((");
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(strCondition4.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 where ISDELETE=0 and ISENABLE=1 "+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 "+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 "+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 "+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 "+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 "+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 "+strCondition4+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 where ISDELETE=0 and ISENABLE=1 "+strCondition4+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 where ISDELETE=0 and ISENABLE=1 "+strCondition4+strCondition3+" 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] where gid=cw_genleg_accitems.LINKGID),LINKNAME=(select top 1 ACCNAME from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID),DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and ISDELETE=0 and ISENABLE=1 "+strCondition4);
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(") as a");
if(strCondition4.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(strCondition6))
{
strSql.Append(" and "+strCondition6);
}
if(strCondition4.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 where ISDELETE=0 and ISENABLE=1"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1"+sstrCondition2end+" 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 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 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 where ISDELETE=0 and ISENABLE=1"+strCondition3+" 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] 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] where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) 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(strCondition6))
{
strSql.Append(" and "+strCondition6);
}
strSql.Append(") as c");
strSql.Append(")) as d");
#endregion
}
elseif(strCondition5.Trim()=="RMB")
{
#region RMB
strSql.Append("select LINKCODE,LINKNAME,YEARDR,YEARCR,DR,CR,LASTDR,LASTCR,AMTCPDR,AMTCPCR,QTYYEARDR,QTYYEARCR,QTYDR,QTYCR,QTYLASTDR,QTYLASTCR,QTYCPDR,QTYCPCR from ((");
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(strCondition4.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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition3+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition3+" 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] where gid=cw_genleg_accitems.LINKGID),LINKNAME=(select top 1 ACCNAME from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID),DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4);
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(") as a");
if(strCondition4.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(strCondition6))
{
strSql.Append(" and "+strCondition6);
}
if(strCondition4.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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition3+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition3+" 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] 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='"+strCondition5+"'");
strSql.Append(" and LINKGID in (select GID from [cw_accitems_gl] where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and DETAILED=1)");// + strCondition4//合计数不计算子科目,否则会造成重复合计
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(strCondition6))
{
strSql.Append(" and "+strCondition6);
}
strSql.Append(") as c");
strSql.Append(")) as d");
#endregion
}
else//外币
{
#region 外币
strSql.Append("select LINKCODE,LINKNAME,YEARDR,YEARCR,DR,CR,LASTDR,LASTCR,AMTCPDR,AMTCPCR,QTYYEARDR,QTYYEARCR,QTYDR,QTYCR,QTYLASTDR,QTYLASTCR,QTYCPDR,QTYCPCR from ((");
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(strCondition4.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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition3+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4+strCondition3+" 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] where gid=cw_genleg_accitems.LINKGID),LINKNAME=(select top 1 ACCNAME from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID),DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='"+strCwSTARTGID+"' and ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition4);
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(") as a");
if(strCondition4.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(strCondition6))
{
strSql.Append(" and "+strCondition6);
}
if(strCondition4.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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition3+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition2+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+sstrCondition2end+" 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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+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 where ISDELETE=0 and ISENABLE=1 and CURRENCY='"+strCondition5+"'"+strCondition3+" 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] 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='"+strCondition5+"'");
strSql.Append(" and LINKGID in (select GID from [cw_accitems_gl] where [YEAR]=SUBSTRING('"+strCwACCDATE+"',1,4) and DETAILED=1)");// + strCondition4//合计数不计算子科目,否则会造成重复合计
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append(" group by LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID");
//string sSQL = "select * from IMPORT_TableCustomize where USERID='" + strUserID + "' and FORMNAME='" + formname + "' and hidden=0 order by DATAORDER";
//DataSet dsITC = T_ALL_DA.GetAllSQL(sSQL);
//if (dsITC != null)
//{
// if (dsITC.Tables[0].Rows.Count > 0)
// {
// for (int i = 0; i < dsITC.Tables[0].Rows.Count; i++)
stringstrDataIndex="VOUDATE,VKNO,VOUNO,SUM(case when FCYNO='RMB' THEN AMTDR ELSE 0 END) as AMTDR,SUM(case when FCYNO='RMB' THEN AMTCR ELSE 0 END) as AMTCR,SUM(FCYDR) as FCYDR,SUM(FCYCR) as FCYCR,SUM(AMTDR) as QTYDR,SUM(AMTCR) as QTYCR,MODIFIEDUSERNAME,CHECKED";//字段名称
stringstrSql="SELECT "+strDataIndex+" from VW_cw_vouchers_vouitems_gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and ISDELETE=0";
if(!string.IsNullOrEmpty(strCondition))
{
strSql+=" and "+strCondition;
}
strSql+=" GROUP BY VOUDATE,VKNO,VOUNO,PREPARED,MODIFIEDUSERNAME,CHECKED";
strSql+=" order by VKNO,convert(INT,VOUNO) desc,VOUDATE desc";
stringstrSql="SELECT "+strDataIndex+" from cw_vouitems_gl_rpt_temp WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and CREATEUSER='"+strUserID+"' and CORPID='"+strCompanyID+"' and (ACCID='合计:'"+strCondition2+") order by ACCID,ACCNAME";
sCondition+=" and ([YEAR]+'-'+[MONTH]+'-'+[DAY])='"+strCondition+"'";
sCondition2+=" and VOUDATE='"+strCondition+"'";
}
if(!string.IsNullOrEmpty(strCondition2))
{
if(strCondition2!="undefined")
{
sCondition+=" and LINKGID in (select GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+strCondition+"',1,4) and ACCID='"+strCondition2+"')";
}
}
if(!string.IsNullOrEmpty(strCondition3))
{
sCondition+=" and CURRENCY='"+strCondition3+"'";
sCondition2+=" and FCYNO='"+strCondition3+"'";
}
#endregion
varstrSql=newStringBuilder();
if(strCondition3=="RMB")
{
strSql.Append("select LINKCODE as 科目代码,LINKNAME as 科目名称,DC as 方向,QTYYEARBLC as 昨日余额,QTYDR as 今日借方,QTYCR as 今日贷方,DC2 as 方向,QTYLASTBLC as 今日余额,iDR as 借方笔数,iCR as 贷方笔数 from (");
}
else
{
strSql.Append("select LINKCODE as [科目代码],LINKNAME as [科目名称],DC as 方向,FCYYEARBLC as [昨日余额(原币)],QTYYEARBLC as [昨日余额(本位币)],FCYDR as [今日借方(原币)],QTYDR as [今日借方(本位币)],FCYCR as [今日贷方(原币)],QTYCR as [今日贷方(本位币)],DC2 as [方向],FCYLASTBLC as [今日余额(原币)],QTYLASTBLC as [今日余额(本位币)],iDR as [借方笔数],iCR as [贷方笔数] from (");
strSql.Append(",[QTYLASTBLC]=(case when QTYLASTBLC<0 then (0-QTYLASTBLC) else QTYLASTBLC end)");
strSql.Append(",[IsInitialEntry],[CORPID],[CREATEUSER],[CREATETIME],[MODIFIEDUSER],[MODIFIEDTIME],[ACCDATE],[STARTGID],LINKCODE=(select top 1 ACCID from [cw_accitems_gl] WITH(NOLOCK) WHERE STARTGID='"+strCwSTARTGID+"' AND gid=ct_genleg_accitems.LINKGID),LINKNAME=(select top 1 ACCNAME from [cw_accitems_gl] WITH(NOLOCK) WHERE STARTGID='"+strCwSTARTGID+"' AND gid=ct_genleg_accitems.LINKGID),DC=(CASE WHEN QTYYEARBLC<0 THEN '贷' ELSE '借' END),DC2=(CASE WHEN QTYLASTBLC<0 THEN '贷' ELSE '借' END),iDR=(SELECT COUNT(*) AS inum from ct_bank_journal WHERE STARTGID='"+strCtSTARTGID+"' and DR<>0 AND ACCGID=ct_genleg_accitems.LINKGID"+sCondition2+"),iCR=(SELECT COUNT(*) AS inum from ct_bank_journal WHERE STARTGID='"+strCtSTARTGID+"' and CR<>0 AND ACCGID=ct_genleg_accitems.LINKGID"+sCondition2+") from ct_genleg_accitems where STARTGID='"+strCtSTARTGID+"'"+sCondition);
sCondition+=" and ([YEAR]+'-'+[MONTH])>='"+strCondition+"'";
}
if(!string.IsNullOrEmpty(strCondition2))
{
sCondition+=" and ([YEAR]+'-'+[MONTH])<='"+strCondition2+"'";
}
if(!string.IsNullOrEmpty(strCondition3))
{
if(strCondition3!="undefined")
{
sCondition+=" and LINKGID in (select GID from cw_accitems_gl where [YEAR]=SUBSTRING('"+strCondition+"',1,4) and ACCID='"+strCondition3+"')";
}
}
if(!string.IsNullOrEmpty(strCondition4))
{
sCondition+=" and CURRENCY='"+strCondition4+"'";
}
#endregion
#region 查询数据
varstrSql=newStringBuilder();
if(strCondition3=="RMB")
{
strSql.Append("select LINKCODE as 科目代码,LINKNAME as 科目名称,DC as 方向,QTYYEARBLC as 期初余额,QTYDR as 本期借方,QTYCR as 本期贷方,DC2 as 方向,QTYLASTBLC as 期末余额,iDR as 借方笔数,iCR as 贷方笔数 from (");
}
else
{
strSql.Append("select LINKCODE as [科目代码],LINKNAME as [科目名称],DC as 方向,FCYYEARBLC as [期初余额(原币)],QTYYEARBLC as [期初余额(本位币)],FCYDR as [本期借方(原币)],QTYDR as [本期借方(本位币)],FCYCR as [本期贷方(原币)],QTYCR as [本期贷方(本位币)],DC2 as [方向],FCYLASTBLC as [期末余额(原币)],QTYLASTBLC as [期末余额(本位币)],iDR as [借方笔数],iCR as [贷方笔数] from (");
}
strSql.Append("SELECT newid() as GID,[LINKGID],[CURRENCY]");
strSql.Append(",LINKCODE=(select top 1 ACCID from [cw_accitems_gl] where gid=b.LINKGID)");
strSql.Append(",LINKNAME=(select top 1 ACCNAME from [cw_accitems_gl] where gid=b.LINKGID)");
strSql.Append(",[QTYLASTBLC]=(case when QTYLASTBLC<0 then (0-QTYLASTBLC) else QTYLASTBLC end)");
strSql.Append(",DC=(CASE WHEN QTYYEARBLC<0 THEN '贷' ELSE '借' END)");
strSql.Append(",DC2=(CASE WHEN QTYLASTBLC<0 THEN '贷' ELSE '借' END)");
strSql.Append(",iDR=(SELECT COUNT(*) AS inum from ct_bank_journal WHERE STARTGID='"+strCtSTARTGID+"' and DR<>0 AND ACCGID=b.LINKGID and ACCDATE>='"+strCondition+"' and ACCDATE<='"+strCondition+"' and FCYNO='"+strCondition4+"')");
strSql.Append(",iCR=(SELECT COUNT(*) AS inum from ct_bank_journal WHERE STARTGID='"+strCtSTARTGID+"' and CR<>0 AND ACCGID=b.LINKGID and ACCDATE>='"+strCondition+"' and ACCDATE<='"+strCondition+"' and FCYNO='"+strCondition4+"') ");
strSql.Append(",[AMTYEARDR]=(select top 1 [AMTYEARDR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])='"+strCondition+"' and CURRENCY='"+strCondition4+"' ORDER BY [DAY])");
strSql.Append(",[AMTYEARCR]=(select top 1 [AMTYEARCR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])='"+strCondition+"' and CURRENCY='"+strCondition4+"' ORDER BY [DAY])");
strSql.Append(",[AMTYEARBLC]=(select top 1 [AMTYEARBLC] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])='"+strCondition+"' and CURRENCY='"+strCondition4+"' ORDER BY [DAY])");
strSql.Append(",ISNULL(sum([AMTDR]),0) as [AMTDR]");
strSql.Append(",ISNULL(sum([AMTCR]),0) as [AMTCR]");
strSql.Append(",ISNULL(sum([AMTBLC]),0) as [AMTBLC]");
strSql.Append(",[AMTLASTDR]=(select top 1 [AMTLASTDR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])<='"+strCondition2+"' and CURRENCY='"+strCondition4+"' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC)");
strSql.Append(",[AMTLASTCR]=(select top 1 [AMTLASTCR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])<='"+strCondition2+"' and CURRENCY='"+strCondition4+"' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC)");
strSql.Append(",[AMTLASTBLC]=(select top 1 [AMTLASTBLC] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])<='"+strCondition2+"' and CURRENCY='"+strCondition4+"' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC)");
strSql.Append(",[FCYYEARDR]=(select top 1 [FCYYEARDR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])='"+strCondition+"' and CURRENCY='"+strCondition4+"' ORDER BY [DAY])");
strSql.Append(",[FCYYEARCR]=(select top 1 [FCYYEARCR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])='"+strCondition+"' and CURRENCY='"+strCondition4+"' ORDER BY [DAY])");
strSql.Append(",[FCYYEARBLC]=(select top 1 [FCYYEARBLC] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])='"+strCondition+"' and CURRENCY='"+strCondition4+"' ORDER BY [DAY])");
strSql.Append(",ISNULL(sum([FCYDR]),0) as [FCYDR]");
strSql.Append(",ISNULL(sum([FCYCR]),0) as [FCYCR]");
strSql.Append(",ISNULL(sum([FCYBLC]),0) as [FCYBLC]");
strSql.Append(",[FCYLASTDR]=(select top 1 [FCYLASTDR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])<='"+strCondition2+"' and CURRENCY='"+strCondition4+"' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC)");
strSql.Append(",[FCYLASTCR]=(select top 1 [FCYLASTCR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])<='"+strCondition2+"' and CURRENCY='"+strCondition4+"' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC)");
strSql.Append(",[FCYLASTBLC]=(select top 1 [FCYLASTBLC] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])<='"+strCondition2+"' and CURRENCY='"+strCondition4+"' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC)");
strSql.Append(",[QTYYEARDR]=(select top 1 [QTYYEARDR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])='"+strCondition+"' and CURRENCY='"+strCondition4+"' ORDER BY [DAY])");
strSql.Append(",[QTYYEARCR]=(select top 1 [QTYYEARCR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])='"+strCondition+"' and CURRENCY='"+strCondition4+"' ORDER BY [DAY])");
strSql.Append(",[QTYYEARBLC]=(select top 1 [QTYYEARBLC] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])='"+strCondition+"' and CURRENCY='"+strCondition4+"' ORDER BY [DAY])");
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(",[QTYLASTDR]=(select top 1 [QTYLASTDR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])<='"+strCondition2+"' and CURRENCY='"+strCondition4+"' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC)");
strSql.Append(",[QTYLASTCR]=(select top 1 [QTYLASTCR] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])<='"+strCondition2+"' and CURRENCY='"+strCondition4+"' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC)");
strSql.Append(",[QTYLASTBLC]=(select top 1 [QTYLASTBLC] from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='"+strCtSTARTGID+"' AND LINKGID=a.LINKGID AND ([YEAR]+'-'+[MONTH])<='"+strCondition2+"' and CURRENCY='"+strCondition4+"' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC)");
strSql.Append(" from ct_genleg_accitems as a");
strSql.Append(" WHERE a.STARTGID='"+strCtSTARTGID+"' AND"+sCondition);
strSql.Append(" GROUP BY [LINKGID],[CURRENCY],[STARTGID]");
strSql.Append(") as b");
strSql.Append(") as ls order by LINKCODE");
//strSql.Append(" and CORPID='" + companyid + "'");
strSql.Append("select VOUDATE as 凭证日期,ITEMNO as 当日序号,ACCDATE as 会计期间,EXPLAN as 摘要,VKNO as 凭证字,VOUNO as 凭证号,FENTRYID as 分录号,SETTLETYPENAME as 结算方式,BILLNO as 结算号,DR as 借方,CR as 贷方,DC as 方向,BLC as 余额,ENTERED as 经手人,CREATEUSERNAME as 制单人 from (");
}
else
{
strSql.Append("select VOUDATE as 凭证日期,ITEMNO as 当日序号,ACCDATE as 会计期间,EXPLAN as 摘要,VKNO as 凭证字,VOUNO as 凭证号,FENTRYID as 分录号,SETTLETYPENAME as 结算方式,BILLNO as 结算号,DR as 借方原币,QTYDR as 借方本位币,CR as 贷方原币,QTYCR as 贷方本位币,DC as 方向,BLC as 余额原币,QTYBLC as 余额本位币,ENTERED as 经手人,CREATEUSERNAME as 制单人 from (");
}
strSql.Append("SELECT *,CREATEUSERNAME=(select top 1 SHOWNAME from [user] where gid=ct_bank_journal.CREATEUSER),MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=ct_bank_journal.MODIFIEDUSER),ACCNAME=(select top 1 ACCNAME from [cw_accitems_gl] where gid=ct_bank_journal.ACCGID),SETTLETYPENAME=isnull((select top 1 STLNAME from [code_stlmode] where STLCODE=ct_bank_journal.SETTLETYPE),''),ISCHECKING=(case when CHECKINGGID='' then '' else (case when CHECKINGGID like 'auto%' then '*' else '**' end) end),QTYBLC=CAST((BLC*FCYEXRATE) as decimal) from ct_bank_journal WITH(NOLOCK) where STARTGID='"+strCtSTARTGID+"'");
}
else
{
strSql.Append("select VOUDATE as 凭证日期,ITEMNO as 当日序号,ACCDATE as 会计期间,EXPLAN as 摘要,SETTLETYPENAME as 结算方式,BILLNO as 结算号,DR as 银行借方,CR as 银行贷方,DC as 方向,BLC as 余额,ENTERED as 经手人,CREATEUSERNAME as 制单人 from (");
strSql.Append("SELECT *,CREATEUSERNAME=(select top 1 SHOWNAME from [user] where gid=ct_bank_statement.CREATEUSER),MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=ct_bank_statement.MODIFIEDUSER),ACCNAME=(select top 1 ACCNAME from [cw_accitems_gl] where STARTGID='"+strCwSTARTGID+"' and gid=ct_bank_statement.ACCGID),SETTLETYPENAME=(select top 1 STLNAME from [code_stlmode] where STLCODE=ct_bank_statement.SETTLETYPE),ISCHECKING=(case when CHECKINGGID='' then '' else (case when CHECKINGGID like 'auto%' then '*' else '**' end) end),ISIMPORTNAME=(case when ISIMPORT='True' then '是' else '' end) from ct_bank_statement WITH(NOLOCK) where STARTGID='"+strCtSTARTGID+"'");
}
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
strSql.Append(") as ls");
strSql.Append(" order by VOUDATE,convert(decimal(10),ITEMNO)");
sbl=T_ALL_DA.GetStrSQL("gid","select gid from cw_design where AR='"+myRow["ACCID"].ToString().Trim()+"' or ARFC='"+myRow["ACCID"].ToString().Trim()+"' or AP='"+myRow["ACCID"].ToString().Trim()+"' or APFC='"+myRow["ACCID"].ToString().Trim()+"'");
stringstrDataIndex="VOUALLNO as 凭证号,OPLBNAME as 业务类型,CUSTOMERNAME as 委托单位,CUSTNO as 委托单号,MBLNO as 主提单号,ACCDATE as 会计期间,ETD as 业务日期,FEETYPE as 费用类型,FEESTATUS as 费用状态,FEENAME as 费用名称,SETTLEMENTNAME as 结算单位,UNIT as 单位,UNITPRICE as 不含税单价,TAXRATE as 税率,TAXUNITPRICE as 单价,QUANTITY as 数量,NOTAXAMOUNT as 不含税金额,AMOUNT as 金额,CURRENCY as 币别,EXCHANGERATE as 汇率,ACCTAXRATE as 销项税率,SETTLEMENT as 结算金额,INVOICE as 开票金额,INVOICENUM as 发票号码,REMARK as 费用备注,ENTEROPERATOR as 录入人,ENTERDATE as 录入时间";//字段名称
//
strCondition="select "+strDataIndex+" from ("+strCondition+") as a order by LINENUM";
stringstrDataIndex="CUSTOMERNAME_i as 委托方名称,INVOICENO as 发票号,MBLNO as [(运)提单号],VESSEL as 船名,VOYNO as 航次,PORTLOAD as 起运港,PORTDISCHARGE as 卸货港,REMARK as 备注,gjUSD as [国际运费USD],gjRMB as [国际运费RMB],gkRMB as 港口码头费,qtRMB as 其他收费项";//字段名称
//
strCondition="select "+strDataIndex+" from ("+strCondition+") as a order by INVOICENO,CUSTOMERNAME_i,MBLNO";
sbl=T_ALL_DA.GetStrSQL("gid","select gid from cw_design_gl where AR='"+myRow["ACCID"].ToString().Trim()+"' or ARFC='"+myRow["ACCID"].ToString().Trim()+"' or AP='"+myRow["ACCID"].ToString().Trim()+"' or APFC='"+myRow["ACCID"].ToString().Trim()+"'");
+",BLNO,FCYNO,FCYEXRATE,DC,(case FCYNO when 'RMB' then AMTDR+AMTCR else FCYDR+FCYCR end) as FFCYAMT,null,0,AMTDR,AMTCR,FSETTLCODE,FSETTLENO,FSETTLDATE"