strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL1USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL1RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) end) as ZL1TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL2USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL2RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL2TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL3USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL3RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE ,0),2) as numeric(20,2)) END) as ZL3TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL4USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL4RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL4TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL5USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL5RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL5TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL6USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL6RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL6TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL7USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL7RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL7TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL8USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL8RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL8TTL ");
}
strSql.Append(",sum(case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) as ZLTTLUSD ");
strSql.Append(",sum(case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) as ZLTTLRMB ");
strSql.Append(",sum(cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2))) as ZLTTLTTL ");
strSql.Append(" FROM CH_FEE F ");
strSql.Append("INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
strSql.Append("SELECT F.CUSTOMERNAME AS CUSTNAME ");
strSql.Append(",(CASE WHEN ISNULL(A.ACCTYPE,'')='' THEN '票结' ELSE CASE WHEN A.ACCTYPE='旬结' THEN '旬结' ELSE CASE WHEN A.ACCTYPE='月结' THEN '月结 间隔'+A.ACCMONTH+'月 账期日'+ACCDAY ELSE A.ACCTYPE+' 账期日'+A.ACCDAY END END END) ZLSTR ");
strSql.Append(",(CASE ISNULL(A.ACCTYPE,'') WHEN '月结' then A.accmonth*30 + A.accday WHEN '季结' then A.accmonth*90 + A.accday WHEN '旬结' then 10 else A.accday end) CONTRACTDAY ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL1USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL1RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) end) as ZL1TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL2USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL2RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL2TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL3USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL3RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE ,0),2) as numeric(20,2)) END) as ZL3TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL4USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL4RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL4TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL5USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL5RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL5TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL6USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL6RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL6TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL7USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL7RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL7TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL8USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL8RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL8TTL ");
}
strSql.Append(",sum(case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) as ZLTTLUSD ");
strSql.Append(",sum(case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) as ZLTTLRMB ");
strSql.Append(",sum(cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2))) as ZLTTLTTL ");
strSql.Append(" FROM CH_FEE F ");
strSql.Append("INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
// strSql.Append("LEFT JOIN (SELECT I.SHORTNAME,cast(MAX(ACCTYPE) as varchar) ACCTYPE,cast(MAX(ACCMONTH)as varchar) ACCMONTH,cast(MAX(ACCDAYS) as varchar) ACCDAY FROM Info_Client_ACCDATE A LEFT JOIN INFO_CLIENT I ON (I.GID=A.LINKGID) WHERE ISNULL(I.SHORTNAME, '') != '' GROUP BY I.SHORTNAME) A ON (A.SHORTNAME=F.CUSTOMERNAME) ");
strSql.Append("LEFT JOIN (SELECT I.SHORTNAME,cast(ACCTYPE as varchar) ACCTYPE,cast(ACCMONTH as varchar) ACCMONTH,cast(ACCDAYS as varchar) ACCDAY,row_number() over (partition by SHORTNAME order by isnull(BGNDATE,getdate()) DESC) as rowno FROM Info_Client_ACCDATE A LEFT JOIN INFO_CLIENT I ON (I.GID=A.LINKGID) WHERE ISNULL(I.SHORTNAME, '') != '') A ON (A.SHORTNAME=F.CUSTOMERNAME and A.rowno=1) ");
strSql.Append("SELECT B.SALE,F.CUSTOMERNAME AS CUSTNAME ");
strSql.Append(",(CASE ISNULL(A.ACCTYPE,'') WHEN '月结' then A.accmonth*30 + A.accday WHEN '季结' then A.accmonth*90 + A.accday WHEN '旬结' then 10 else A.accday end) CONTRACTDAY ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL1USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL1RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) end) as ZL1TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL2USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL2RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL2TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL3USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL3RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL3TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL4USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL4RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL4TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL5USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL5RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL5TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL6USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL6RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE ,0),2) as numeric(20,2)) END) as ZL6TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL7USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL7RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL7TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL8USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL8RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL8TTL ");
}
strSql.Append(",sum(case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) as ZLTTLUSD ");
strSql.Append(",sum(case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) as ZLTTLRMB ");
strSql.Append(",sum(cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2))) as ZLTTLTTL ");
strSql.Append(" FROM CH_FEE F ");
strSql.Append("INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
strSql.Append("LEFT JOIN (SELECT I.SHORTNAME,cast(ACCTYPE as varchar) ACCTYPE,cast(ACCMONTH as varchar) ACCMONTH,cast(ACCDAYS as varchar) ACCDAY,row_number() over (partition by SHORTNAME order by isnull(BGNDATE,getdate()) DESC) as rowno FROM Info_Client_ACCDATE A LEFT JOIN INFO_CLIENT I ON (I.GID=A.LINKGID) WHERE ISNULL(I.SHORTNAME, '') != '') A ON (A.SHORTNAME=F.CUSTOMERNAME and A.rowno=1) ");
if(!string.IsNullOrEmpty(condition))
{
strSql.Append(" Where "+condition);
}
strSql.Append(" Group by B.SALE,F.CUSTOMERNAME ");
/*strSql.AppendLine("SELECT (SELECT OPDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS OPDATE, ");
strSql.AppendLine("(SELECT CUSTOMERNAME AS CUSTNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTNAME, ");
strSql.AppendLine("(SELECT SALE FROM v_op_bill WHERE BSNO=F.BSNO) AS SALE, ");
strSql.AppendLine("(SELECT BSSOURCE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSSOURCE, ");
strSql.AppendLine("(SELECT OPLBNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS OPLBNAME, ");
strSql.AppendLine("(SELECT BSTYPE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSTYPE, ");
strSql.AppendLine("(SELECT PORTDISCHARGE FROM v_op_bill WHERE BSNO=F.BSNO) AS PORTDISCHARGE, ");
strSql.AppendLine("(SELECT CUSTOMDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTOMDATE, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("(SELECT MBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS MBLNO, ");
strSql.AppendLine("(SELECT HBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS HBLNO, ");
strSql.AppendLine("(SELECT TEU FROM v_op_bill WHERE BSNO=F.BSNO) AS TEU, ");
strSql.AppendLine("(SELECT KGS FROM v_op_bill WHERE BSNO=F.BSNO) AS KGS, ");
strSql.AppendLine("(SELECT GOODSNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS GOODSNAME, ");
strSql.AppendLine("(SELECT OP FROM v_op_bill WHERE BSNO=F.BSNO) AS OP, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE SALE='"+saler+"')");
strSql.AppendLine("ORDER BY ENTERDATE ASC ");*/
/*strSql.AppendLine("where "+condition);
strSql.AppendLine("order by B.SALE");*/
strSql.AppendLine("SELECT CONVERT(varchar(100), B.OPDATE, 23) AS OPDATE,B.CUSTOMERNAME AS CUSTNAME,B.SALE,B.BSSOURCE,B.OPLBNAME,B.BSTYPE,B.PORTDISCHARGE,CONVERT(varchar(100), B.CUSTOMDATE, 23) AS CUSTOMDATE,B.MBLNO,B.HBLNO,B.TEU,B.KGS,B.GOODSNAME,B.OP, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("left join v_op_bill as B on F.BSNO=B.BSNO ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
/*strSql.AppendLine("SELECT (SELECT OPDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS OPDATE, ");
strSql.AppendLine("(SELECT CUSTOMERNAME AS CUSTNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTNAME, ");
strSql.AppendLine("(SELECT SALE FROM v_op_bill WHERE BSNO=F.BSNO) AS SALE, ");
strSql.AppendLine("(SELECT BSSOURCE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSSOURCE, ");
strSql.AppendLine("(SELECT OPLBNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS OPLBNAME, ");
strSql.AppendLine("(SELECT BSTYPE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSTYPE, ");
strSql.AppendLine("(SELECT PORTDISCHARGE FROM v_op_bill WHERE BSNO=F.BSNO) AS PORTDISCHARGE, ");
strSql.AppendLine("(SELECT CUSTOMDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTOMDATE, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("(SELECT MBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS MBLNO, ");
strSql.AppendLine("(SELECT HBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS HBLNO, ");
strSql.AppendLine("(SELECT TEU FROM v_op_bill WHERE BSNO=F.BSNO) AS TEU, ");
strSql.AppendLine("(SELECT KGS FROM v_op_bill WHERE BSNO=F.BSNO) AS KGS, ");
strSql.AppendLine("(SELECT GOODSNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS GOODSNAME, ");
strSql.AppendLine("(SELECT OP FROM v_op_bill WHERE BSNO=F.BSNO) AS OP, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine("ORDER BY ENTERDATE ASC ");*/
/*strSql.AppendLine("where "+condition);
strSql.AppendLine("order by B.SALE");*/
strSql.AppendLine("SELECT B.OPDATE,B.CUSTOMERNAME AS CUSTNAME,B.SALE,B.BSSOURCE,B.OPLBNAME,B.BSTYPE,B.PORTDISCHARGE,B.CUSTOMDATE,B.MBLNO,B.HBLNO,B.TEU,B.KGS,B.GOODSNAME,B.OP, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("left join v_op_bill as B on F.BSNO=B.BSNO ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
/*strSql.AppendLine("SELECT (SELECT OPDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS OPDATE, ");
strSql.AppendLine("(SELECT CUSTOMERNAME AS CUSTNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTNAME, ");
strSql.AppendLine("(SELECT SALE FROM v_op_bill WHERE BSNO=F.BSNO) AS SALE, ");
strSql.AppendLine("(SELECT BSSOURCE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSSOURCE, ");
strSql.AppendLine("(SELECT OPLBNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS OPLBNAME, ");
strSql.AppendLine("(SELECT BSTYPE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSTYPE, ");
strSql.AppendLine("(SELECT PORTDISCHARGE FROM v_op_bill WHERE BSNO=F.BSNO) AS PORTDISCHARGE, ");
strSql.AppendLine("(SELECT CUSTOMDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTOMDATE, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("(SELECT MBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS MBLNO, ");
strSql.AppendLine("(SELECT HBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS HBLNO, ");
strSql.AppendLine("(SELECT TEU FROM v_op_bill WHERE BSNO=F.BSNO) AS TEU, ");
strSql.AppendLine("(SELECT KGS FROM v_op_bill WHERE BSNO=F.BSNO) AS KGS, ");
strSql.AppendLine("(SELECT GOODSNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS GOODSNAME, ");
strSql.AppendLine("(SELECT OP FROM v_op_bill WHERE BSNO=F.BSNO) AS OP, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine("ORDER BY ENTERDATE ASC ");*/
/*strSql.AppendLine("where "+condition);
strSql.AppendLine("order by B.SALE");*/
strSql.AppendLine("SELECT B.OPDATE,B.CUSTOMERNAME AS CUSTNAME,B.SALE,B.BSSOURCE,B.OPLBNAME,B.BSTYPE,B.PORTDISCHARGE,B.CUSTOMDATE,B.MBLNO,B.HBLNO,B.TEU,B.KGS,B.GOODSNAME,B.OP, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("left join v_op_bill as B on F.BSNO=B.BSNO ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
/*strSql.AppendLine("SELECT (SELECT OPDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS OPDATE, ");
strSql.AppendLine("(SELECT CUSTOMERNAME AS CUSTNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTNAME, ");
strSql.AppendLine("(SELECT SALE FROM v_op_bill WHERE BSNO=F.BSNO) AS SALE, ");
strSql.AppendLine("(SELECT BSSOURCE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSSOURCE, ");
strSql.AppendLine("(SELECT OPLBNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS OPLBNAME, ");
strSql.AppendLine("(SELECT BSTYPE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSTYPE, ");
strSql.AppendLine("(SELECT PORTDISCHARGE FROM v_op_bill WHERE BSNO=F.BSNO) AS PORTDISCHARGE, ");
strSql.AppendLine("(SELECT CUSTOMDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTOMDATE, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("(SELECT MBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS MBLNO, ");
strSql.AppendLine("(SELECT HBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS HBLNO, ");
strSql.AppendLine("(SELECT TEU FROM v_op_bill WHERE BSNO=F.BSNO) AS TEU, ");
strSql.AppendLine("(SELECT KGS FROM v_op_bill WHERE BSNO=F.BSNO) AS KGS, ");
strSql.AppendLine("(SELECT GOODSNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS GOODSNAME, ");
strSql.AppendLine("(SELECT OP FROM v_op_bill WHERE BSNO=F.BSNO) AS OP, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine("ORDER BY ENTERDATE ASC ");*/
/*strSql.AppendLine("where "+condition);
strSql.AppendLine("order by B.SALE");*/
strSql.AppendLine("SELECT CONVERT(varchar(100), B.OPDATE, 23) AS OPDATE,B.CUSTOMERNAME AS CUSTNAME,B.SALE,B.BSSOURCE,B.OPLBNAME,B.BSTYPE,B.PORTDISCHARGE,CONVERT(varchar(100), B.CUSTOMDATE, 23) AS CUSTOMDATE,B.MBLNO,B.HBLNO,B.TEU,B.KGS,B.GOODSNAME,B.OP, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("left join v_op_bill as B on F.BSNO=B.BSNO ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
/*strSql.AppendLine("SELECT (SELECT OPDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS OPDATE, ");
strSql.AppendLine("(SELECT CUSTOMERNAME AS CUSTNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTNAME, ");
strSql.AppendLine("(SELECT SALE FROM v_op_bill WHERE BSNO=F.BSNO) AS SALE, ");
strSql.AppendLine("(SELECT BSSOURCE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSSOURCE, ");
strSql.AppendLine("(SELECT OPLBNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS OPLBNAME, ");
strSql.AppendLine("(SELECT BSTYPE FROM v_op_bill WHERE BSNO=F.BSNO) AS BSTYPE, ");
strSql.AppendLine("(SELECT PORTDISCHARGE FROM v_op_bill WHERE BSNO=F.BSNO) AS PORTDISCHARGE, ");
strSql.AppendLine("(SELECT CUSTOMDATE FROM v_op_bill WHERE BSNO=F.BSNO) AS CUSTOMDATE, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("(SELECT MBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS MBLNO, ");
strSql.AppendLine("(SELECT HBLNO FROM v_op_bill WHERE BSNO=F.BSNO) AS HBLNO, ");
strSql.AppendLine("(SELECT TEU FROM v_op_bill WHERE BSNO=F.BSNO) AS TEU, ");
strSql.AppendLine("(SELECT KGS FROM v_op_bill WHERE BSNO=F.BSNO) AS KGS, ");
strSql.AppendLine("(SELECT GOODSNAME FROM v_op_bill WHERE BSNO=F.BSNO) AS GOODSNAME, ");
strSql.AppendLine("(SELECT OP FROM v_op_bill WHERE BSNO=F.BSNO) AS OP, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT "+param+"DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO IN (SELECT BSNO FROM v_op_bill WHERE CUSTOMERNAME='"+customer+"')");
strSql.AppendLine("ORDER BY ENTERDATE ASC ");*/
/*strSql.AppendLine("where "+condition);
strSql.AppendLine("order by B.SALE");*/
/*
strSql.AppendLine("SELECT CONVERT(varchar(100), B.OPDATE, 23) AS OPDATE,B.CUSTOMERNAME AS CUSTNAME,B.SALE,B.BSSOURCE,B.OPLBNAME,B.BSTYPE,B.PORTDISCHARGE,CONVERT(varchar(100), B.CUSTOMDATE, 23) AS CUSTOMDATE,B.MBLNO,B.HBLNO,B.TEU,B.KGS,B.GOODSNAME,B.OP, ");
strSql.AppendLine("(SELECT case when isnull(C1.AMOUNT,0)<>isnull(C1.SETTLEMENT,0) then (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, GETDATE()) end) ");
strSql.AppendLine("else (case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],ENTERDATE, S.SETTLETIME) end) end FROM ch_fee AS C1 LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C1.CUSTOMERNAME AND S.BSNO=C1.BSNO) WHERE C1.GID=F.GID) AS ACTUALDAY, ");
strSql.AppendLine("(SELECT case ENTERDATE when '1900-01-01 00:00:00.000' then 0 else case when isnull(C2.AMOUNT,0)<>isnull(C2.SETTLEMENT,0) then DATEDIFF([day],ENTERDATE,(CASE C.STLNAME WHEN '月结' THEN ");
strSql.AppendLine("DATEADD(month,1,DATEADD(day,ISNULL(CAST(C.STLDATE AS INT),1),DATEADD(day,-Day(ENTERDATE)+1,ENTERDATE))) ");
strSql.AppendLine("WHEN '半月结' THEN (CASE WHEN DATEPART(DAY, GETDATE())<=15 THEN DATEADD(day,ISNULL(CAST(C.STLFIRSTHALFDATE AS INT),16),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE)) ");
strSql.AppendLine("ELSE DATEADD(MONTH,1,DATEADD(day,ISNULL(CAST(C.STLMIDDLEDATE AS INT),1),DATEADD(Day,-Day(ENTERDATE)+1,ENTERDATE))) END) ");
strSql.AppendLine("ELSE GETDATE() END)) else (DATEDIFF([day],ENTERDATE, S.SETTLETIME)) end end FROM ch_fee AS C2 LEFT JOIN info_client C ON (C.SHORTNAME=C2.CUSTOMERNAME) ");
strSql.AppendLine("LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=C2.CUSTOMERNAME AND S.BSNO=C2.BSNO) WHERE C2.GID=F.GID) AS NODAY, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END,0) AS RMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS RMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS STLRMBDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS STLRMBDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END,0) AS BALRMBDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALRMBDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END,0) AS USDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS USDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS STLUSDDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS STLUSDDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLUSDDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END,0) AS BALUSDDR, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALUSDDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS OTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLOTDRAMOUNT, ");
strSql.AppendLine("isnull(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, ");
strSql.AppendLine("(SELECT 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 ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALOTDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS TTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS TTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS TTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS STLTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS STLTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS STLTTLDRAMOUNT, ");
strSql.AppendLine("isnull(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END,0) AS BALTTLDR, ");
strSql.AppendLine("(SELECT isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR ");
strSql.AppendLine("FROM CH_FEE F where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.AppendLine("AND F.BSNO = B.BSNO ");
strSql.AppendLine(") AS BALTTLDRAMOUNT ");
strSql.AppendLine("FROM CH_FEE F ");
strSql.AppendLine("left join v_op_bill as B on F.BSNO=B.BSNO ");
strSql.AppendLine("where datediff(day,ENTERDATE,getdate())<=(SELECT TOP 1 ZL1DAY FROM sys_zlset WHERE COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"') ");
strSql.AppendLine("AND F.FEETYPE='"+feeType+"' and F.AMOUNT<>F.SETTLEMENT and (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9) ");
strSql.Append(" sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL1USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL1RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL1DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) end) as ZL1TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL2USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL2RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL2DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL1DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL2TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL3USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL3RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL3DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL2DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL3TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL4USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL4RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL4DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL3DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL4TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL5USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL5RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL5DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL4DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL5TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL6USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL6RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL6DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL5DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL6TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL7USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL7RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())<="+zlset.ZL7DAY+" and datediff(day,B."+strdate+",getdate())>"+zlset.ZL6DAY+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL7TTL ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then (case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL8USD ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then (case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) else 0 end) as ZL8RMB ");
strSql.Append(",sum(case when datediff(day,B."+strdate+",getdate())>"+otherday.ToString()+" then cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2)) END) as ZL8TTL ");
}
strSql.Append(",sum(case F.CURRENCY when 'USD' then (F.AMOUNT-F.SETTLEMENT) else 0 end) as ZLTTLUSD ");
strSql.Append(",sum(case F.CURRENCY when 'RMB' then (F.AMOUNT-F.SETTLEMENT) else 0 end) as ZLTTLRMB ");
strSql.Append(",sum(cast(round(isnull((F.AMOUNT-F.SETTLEMENT)*F.EXCHANGERATE,0),2) as numeric(20,2))) as ZLTTLTTL ");
strSql.Append(" FROM CH_FEE F ");
strSql.Append("INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
str=" (B.OP='"+usercode+"' OR B.SALE='"+usercode+"')";
}
elseif(visiblerange=="2")
{
if(tb=="index")
{
varrangeDa=newRangeDA();
vardeptname=rangeDa.GetDEPTNAME(userid);
varuserstr=newStringBuilder();
userstr.Append(" select SHOWNAME from [user] where GID in (select USERID from user_company where COMPANYID='"+companyid+"') and GID in (select userid from user_baseinfo where DEPTNAME='"+deptname+"')");
str=" (B.Corpid='"+Convert.ToString(reader["COMPANYID"])+"' or B.SALECORPID='"+Convert.ToString(reader["COMPANYID"])+"' ";
}
else
{
str=str+" or B.Corpid='"+Convert.ToString(reader["COMPANYID"])+"' or B.SALECORPID='"+Convert.ToString(reader["COMPANYID"])+"'";
};
}
str=str+")";
reader.Close();
}
}
elseif(visiblerange=="6")
{
varuserstr=newStringBuilder();
userstr.Append(" select OPID,(select SHOWNAME from [user] where GID=user_authority_range_op.OPID) SHOWNAME from user_authority_range_op where userid='"+userid+"' and AUTHORITYID='"+AUTHORITYID+"' and VISIBLERANGE=1");
strSql.Append("SELECT GID,ZL1NAME,ZL1DAY,ZL2NAME,ZL2DAY,ZL3NAME,ZL3DAY,ZL4NAME,ZL4DAY,ZL5NAME,ZL5DAY,ZL6NAME,ZL6DAY,ZL7NAME,ZL7DAY,ZL8NAME,ZL8DAY from sys_zlset");
strSql.Append(" Where COMPANYID='"+companyid+"'");
@"insert into sys_zlset (GID,COMPANYID ,ZL1NAME,ZL1DAY,ZL2NAME,ZL2DAY,ZL3NAME,ZL3DAY,ZL4NAME,ZL4DAY,ZL5NAME,ZL5DAY,ZL6NAME,ZL6DAY,ZL7NAME,ZL7DAY,ZL8NAME,ZL8DAY)