strSql.Append("isnull(SUM(CASE FEETYPE WHEN 1 THEN cast(round(ISNULL(ORDERAMOUNT,0) * ISNULL(EXCHANGERATE,0),2) as numeric(20,2)) ELSE 0 END),0) AS ORDERTTLDR,");
strSql.Append("isnull(SUM(CASE FEETYPE WHEN 2 THEN cast(round(ISNULL(ORDERAMOUNT,0) *ISNULL( EXCHANGERATE,0),2) as numeric(20,2)) ELSE 0 END),0) AS ORDERTTLCR");
strSql.Append(",case when isnull(SUM(F.AMOUNT),0)<>isnull(SUM(F.SETTLEMENT),0) then (case A.OPDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],A.OPDATE, GETDATE()) end) ");
strSql.Append(" else (case A.OPDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],A.OPDATE, S.SETTLETIME) end) end AS ACTUALDAY,S.SETTLETIME");
//strSql.Append(",case when isnull(SUM(F.AMOUNT),0)<>isnull(SUM(F.SETTLEMENT),0) then (case A.OPDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],A.OPDATE, GETDATE()) end) ");
//strSql.Append(" else (case A.OPDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],A.OPDATE, S.SETTLETIME) end) end AS ACTUALDAY,S.SETTLETIME");
if(NORPTSTLDATETYPE.PARAMVALUE=="1")
{
strSql.Append(",[dbo].[GetCustStlDay](A.OPDATE,F.CUSTOMERNAME,A.SALE) AS STLDATE");
strSql.Append(",isnull((select top 1 case ACCTYPE WHEN '月结' then accmonth*30 + accdays WHEN '季结' then accmonth*90 + accdays else accdays end from Info_Client_ACCDATE WITH (NOLOCK) where LINKGID = (select top 1 GID from info_client WITH (NOLOCK) where shortname =F.CUSTOMERNAME ) AND ((ISNULL(SALE,'')='') or (ISNULL(SALE,'')=B.SALE)) and BGNDATE<=B.OPDATE AND ENDDATE>=B.OPDATE),0) CONTRACTDAY ");
//if (NORPTSTLDATETYPE.PARAMVALUE == "1")
//{
// strSql.Append(",[dbo].[GetCustStlDay](A.OPDATE,F.CUSTOMERNAME,A.SALE) AS STLDATE");
// strSql.Append(",isnull((select top 1 case ACCTYPE WHEN '月结' then accmonth*30 + accdays WHEN '季结' then accmonth*90 + accdays else accdays end from Info_Client_ACCDATE WITH (NOLOCK) where LINKGID = (select top 1 GID from info_client WITH (NOLOCK) where shortname =F.CUSTOMERNAME ) AND ((ISNULL(SALE,'')='') or (ISNULL(SALE,'')=B.SALE)) and BGNDATE<=B.OPDATE AND ENDDATE>=B.OPDATE),0) CONTRACTDAY ");
}
else{
strSql.Append(",CASE WHEN A.STLDATE IS NULL THEN A.OPDATE ELSE A.STLDATE END STLDATE");
strSql.Append(",isnull((select top 1 case ACCTYPE WHEN '月结' then accmonth*30 + accdays WHEN '季结' then accmonth*90 + accdays else accdays end from Info_Client_ACCDATE WITH (NOLOCK) where LINKGID = (select top 1 GID from info_client WITH (NOLOCK) where shortname =A.CUSTOMERNAME ) AND ((ISNULL(SALE,'')='') or (ISNULL(SALE,'')=B.SALE)) and BGNDATE<=B.OPDATE AND ENDDATE>=B.OPDATE),0) CONTRACTDAY ");
//}
//else {
// strSql.Append(",CASE WHEN A.STLDATE IS NULL THEN A.OPDATE ELSE A.STLDATE END STLDATE");
// strSql.Append(",isnull((select top 1 case ACCTYPE WHEN '月结' then accmonth*30 + accdays WHEN '季结' then accmonth*90 + accdays else accdays end from Info_Client_ACCDATE WITH (NOLOCK) where LINKGID = (select top 1 GID from info_client WITH (NOLOCK) where shortname =A.CUSTOMERNAME ) AND ((ISNULL(SALE,'')='') or (ISNULL(SALE,'')=B.SALE)) and BGNDATE<=B.OPDATE AND ENDDATE>=B.OPDATE),0) CONTRACTDAY ");
strSql.Append("isnull(SUM(CASE FEETYPE WHEN 1 THEN cast(round(ISNULL(ORDERAMOUNT,0) * ISNULL(EXCHANGERATE,0),2) as numeric(20,2)) ELSE 0 END),0) AS ORDERTTLDR,");
strSql.Append("isnull(SUM(CASE FEETYPE WHEN 2 THEN cast(round(ISNULL(ORDERAMOUNT,0) *ISNULL( EXCHANGERATE,0),2) as numeric(20,2)) ELSE 0 END),0) AS ORDERTTLCR");
strSql.Append(",case when isnull(SUM(F.AMOUNT),0)<>isnull(SUM(F.SETTLEMENT),0) then (case B.OPDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],B.OPDATE, GETDATE()) end) ");
strSql.Append(" else (case B.OPDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],B.OPDATE, S.SETTLETIME) end) end AS ACTUALDAY,S.SETTLETIME");
//strSql.Append(",case when isnull(SUM(F.AMOUNT),0)<>isnull(SUM(F.SETTLEMENT),0) then (case B.OPDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],B.OPDATE, GETDATE()) end) ");
//strSql.Append(" else (case B.OPDATE when '1900-01-01 00:00:00.000' then 0 else DATEDIFF([day],B.OPDATE, S.SETTLETIME) end) end AS ACTUALDAY,S.SETTLETIME");
if(NORPTSTLDATETYPE.PARAMVALUE=="1")
{
strSql.Append(",[dbo].[GetCustStlDay](B.OPDATE,F.CUSTOMERNAME,B.SALE) AS STLDATE");
strSql.Append(",isnull((select top 1 case ACCTYPE WHEN '月结' then accmonth*30 + accdays WHEN '季结' then accmonth*90 + accdays else accdays end from Info_Client_ACCDATE where LINKGID = (select top 1 GID from info_client where shortname =F.CUSTOMERNAME ) AND ((ISNULL(SALE,'')='') or (ISNULL(SALE,'')=B.SALE)) and BGNDATE<=B.OPDATE AND ENDDATE>=B.OPDATE),0) CONTRACTDAY ");
//if (NORPTSTLDATETYPE.PARAMVALUE == "1")
//{
// strSql.Append(",[dbo].[GetCustStlDay](B.OPDATE,F.CUSTOMERNAME,B.SALE) AS STLDATE");
// strSql.Append(",isnull((select top 1 case ACCTYPE WHEN '月结' then accmonth*30 + accdays WHEN '季结' then accmonth*90 + accdays else accdays end from Info_Client_ACCDATE where LINKGID = (select top 1 GID from info_client where shortname =F.CUSTOMERNAME ) AND ((ISNULL(SALE,'')='') or (ISNULL(SALE,'')=B.SALE)) and BGNDATE<=B.OPDATE AND ENDDATE>=B.OPDATE),0) CONTRACTDAY ");
}
else
{
strSql.Append(",CASE WHEN B.STLDATE IS NULL THEN B.OPDATE ELSE B.STLDATE END STLDATE");
strSql.Append(",isnull((select top 1 case ACCTYPE WHEN '月结' then accmonth*30 + accdays WHEN '季结' then accmonth*90 + accdays else accdays end from Info_Client_ACCDATE where LINKGID = (select top 1 GID from info_client where shortname =B.CUSTOMERNAME ) AND ((ISNULL(SALE,'')='') or (ISNULL(SALE,'')=B.SALE)) and BGNDATE<=B.OPDATE AND ENDDATE>=B.OPDATE),0) CONTRACTDAY ");
//}
//else
//{
// strSql.Append(",CASE WHEN B.STLDATE IS NULL THEN B.OPDATE ELSE B.STLDATE END STLDATE");
// strSql.Append(",isnull((select top 1 case ACCTYPE WHEN '月结' then accmonth*30 + accdays WHEN '季结' then accmonth*90 + accdays else accdays end from Info_Client_ACCDATE where LINKGID = (select top 1 GID from info_client where shortname =B.CUSTOMERNAME ) AND ((ISNULL(SALE,'')='') or (ISNULL(SALE,'')=B.SALE)) and BGNDATE<=B.OPDATE AND ENDDATE>=B.OPDATE),0) CONTRACTDAY ");
strSql.Append(" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
strSql.Append("INNER JOIN v_op_gain_sum G ON (G.BSNO=B.BSNO) ");
strSql.Append(" INNER JOIN info_client C WITH (NOLOCK) ON (C.SHORTNAME=F.CUSTOMERNAME) ");
strSql.Append(" LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=F.CUSTOMERNAME AND S.BSNO=B.BSNO) ");
//strSql.Append(" LEFT JOIN V_MAX_SETTLEMENTTIME S ON (S.CUSTOMERNAME=F.CUSTOMERNAME AND S.BSNO=B.BSNO) ");
strSql.Append(" LEFT JOIN ( select d.CUSTOMERNAME,d.BSNO,SUM(CASE WHEN d.CURRENCY='RMB' THEN d.AMOUNT ELSE 0 END) INVRMB , SUM(CASE WHEN d.CURRENCY = 'USD' THEN d.AMOUNT ELSE 0 END) INVUSD");
strSql.Append(",MAX(d.EXCHANGERATE) INVEXCHANGERATE FROM ch_fee_do d WHERE d.CATEGORY = 7 GROUP BY d.BSNO,d.CUSTOMERNAME) INV ON(INV.BSNO= B.BSNO and INV.CUSTOMERNAME=F.CUSTOMERNAME) ");