sqlBank=sqlBank+",SXF=ISNULL((SELECT (SXF+DHF) FROM ch_fee_settlement_PAYRMB WHERE BILLNO=b.BILLNO),0)";
sqlBank=sqlBank+" from ch_fee_settlement as b WHERE 1=1 and (ACCOUNTRMB='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"' or ACCOUNTUSD='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"')";
sqlBank=sqlBank+" and GID in ("+sGids.Trim()+") ";
sqlBank=sqlBank+" GROUP BY billno,SETTLENO,SETTLETIME,CUSTOMERNAME,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,AHSR_CURRENCY,AHSR_RATE,AHSR_MONEY,Billtype,CURR";//,FEETYPE,CURRENCY,EXCHANGERATE
sqlBank=sqlBank+" GROUP BY GID,billno,SETTLENO,SETTLETIME,CUSTOMERNAME,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,AHSR_CURRENCY,AHSR_RATE,AHSR_MONEY,Billtype,CURR";//,FEETYPE,CURRENCY,EXCHANGERATE
sqlBank=sqlBank+" ) as a ";
sqlBank=sqlBank+" GROUP BY BILLNO,SETTLENO,Billtype,SETTLETIME,FEETYPE,SETTLETYPE,CURRENCY,CUSTOMERNAME,AHSR_CURRENCY,AHSR_RATE,ACCOUNTRMB,ACCOUNTUSD,BILLNO_ACAD";//,EXCHANGERATE
sqlBank=sqlBank+" GROUP BY GID,BILLNO,SETTLENO,Billtype,SETTLETIME,FEETYPE,SETTLETYPE,CURRENCY,CUSTOMERNAME,AHSR_CURRENCY,AHSR_RATE,ACCOUNTRMB,ACCOUNTUSD,BILLNO_ACAD";//,EXCHANGERATE
sqlBank=sqlBank+" ORDER BY FEETYPE desc,CURRENCY";//
//+ ",(case when CURRENCY='RMB' then 1 else convert(numeric(8,5),round(SUM(hj)/SUM(je),5)) END) as EXCHANGERATE"
+",(case when CURRENCY='RMB' then 1 else (case when rate<>1 and rate<>0 then rate else convert(numeric(8,5),round(SUM(hj)/SUM(je),5)) end) END) as EXCHANGERATE"
+",ACCOUNTRMB,ACCOUNTUSD,SUM(je) AS je,SUM(hj) AS hj"+sISEXPLAN//,SETTLETYPE+ strISEXPLAN
+" from ("
+"SELECT BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,(CASE WHEN FEETYPE = 1 THEN '应收账款' WHEN FEETYPE=2 THEN '应付账款' ELSE '' END) as FEETYPE"
+"SELECT gid_s GID,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,(CASE WHEN FEETYPE = 1 THEN '应收账款' WHEN FEETYPE=2 THEN '应付账款' ELSE '' END) as FEETYPE"
+",CURRENCY,1 as EXCHANGERATE,rate,ACCOUNTRMB,ACCOUNTUSD"
+",SUM(AMOUNT) AS je"
+",SUM(convert(decimal(18,2),round(ORIGAMOUNT*rate,2))) AS hj"
+" From VW_ch_fee_settlement_do WHERE 1=1"+strCondition+" and (ACCOUNTRMB='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"' or ACCOUNTUSD='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"')"
+" and gid_s in ("+sGids.Trim()+") "
+" GROUP BY BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ACCOUNTRMB,ACCOUNTUSD,FEETYPE,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+" GROUP BY gid_s,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ACCOUNTRMB,ACCOUNTUSD,FEETYPE,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+") as a"
+" GROUP BY BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ACCOUNTRMB,ACCOUNTUSD,FEETYPE,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+" GROUP BY GID,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ACCOUNTRMB,ACCOUNTUSD,FEETYPE,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+" ORDER BY CURRENCY,FEETYPE"+sISEXPLAN;// + strISEXPLAN
//+ ",(case when CURRENCY='RMB' then 1 else convert(numeric(8,5),round(SUM(hj)/SUM(je),5)) END) as EXCHANGERATE"
+",(case when CURRENCY='RMB' then 1 else (case when rate<>1 and rate<>0 then rate else convert(numeric(8,5),round(SUM(hj)/SUM(je),5)) end) END) as EXCHANGERATE"
+",ACCOUNTRMB,ACCOUNTUSD,SUM(je) AS je,SUM(hj) AS hj"+sISEXPLAN//,SETTLETYPE+ strISEXPLAN
+" from ("
+"SELECT BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,(CASE WHEN FEETYPE = 1 THEN '应收账款' WHEN FEETYPE=2 THEN '应付账款' ELSE '' END) as FEETYPE"
+"SELECT gid_s GID,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,(CASE WHEN FEETYPE = 1 THEN '应收账款' WHEN FEETYPE=2 THEN '应付账款' ELSE '' END) as FEETYPE"
+",CURRENCY,1 as EXCHANGERATE,rate,ACCOUNTRMB,ACCOUNTUSD"
+",SUM(AMOUNT) AS je"
+",SUM(convert(decimal(18,2),round(ORIGAMOUNT*"+tbRATE_CASH+",2))) AS hj"
+" From VW_ch_fee_settlement_do WHERE 1=1"+strCondition+" and (ACCOUNTRMB='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"' or ACCOUNTUSD='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"')"
+" and gid_s in ("+sGids.Trim()+") "
+" GROUP BY BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ACCOUNTRMB,ACCOUNTUSD,FEETYPE,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+" GROUP BY gid_s,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ACCOUNTRMB,ACCOUNTUSD,FEETYPE,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+") as a"
+" GROUP BY BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ACCOUNTRMB,ACCOUNTUSD,FEETYPE,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+" GROUP BY GID,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ACCOUNTRMB,ACCOUNTUSD,FEETYPE,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+" ORDER BY CURRENCY,FEETYPE"+sISEXPLAN;// + strISEXPLAN
}
}
else
{
sqlBank="SELECT BILLNO,SETTLENO,BILLTYPE,SETTLETYPE SETTLETYPEREF,(CASE WHEN FEETYPE = 1 THEN '应收账款' WHEN FEETYPE=2 THEN '应付账款' ELSE '' END) as FEETYPE,'RMB' as CURRENCY,1 as EXCHANGERATE,ACCOUNTRMB,ACCOUNTUSD"
sqlBank="SELECT gid_s GID,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE SETTLETYPEREF,(CASE WHEN FEETYPE = 1 THEN '应收账款' WHEN FEETYPE=2 THEN '应付账款' ELSE '' END) as FEETYPE,'RMB' as CURRENCY,1 as EXCHANGERATE,ACCOUNTRMB,ACCOUNTUSD"
+",SUM(ORIGAMOUNT) AS je"
+",SUM(convert(decimal(18,2),round(ORIGAMOUNT*"+tbRATE_CASH+",2))) AS hj"
+sISEXPLAN//,SETTLETYPE+ strISEXPLAN
+" From VW_ch_fee_settlement_do WHERE 1=1"+strCondition+" and (ACCOUNTRMB='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"' or ACCOUNTUSD='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"')"
+" and gid_s in ("+sGids.Trim()+") "
+" GROUP BY BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,FEETYPE,ACCOUNTRMB,ACCOUNTUSD,RATE" +sISEXPLAN//BILLNO,SETTLETYPE, + strISEXPLAN
+" GROUP BY gid_s,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,FEETYPE,ACCOUNTRMB,ACCOUNTUSD,RATE" +sISEXPLAN//BILLNO,SETTLETYPE, + strISEXPLAN
+" ORDER BY FEETYPE,ACCOUNTRMB,ACCOUNTUSD,RATE"+sISEXPLAN;// + strISEXPLAN
//+ ",(case when CURRENCY='RMB' then 1 else convert(numeric(8,5),round(SUM(hj)/SUM(je),5)) END) as EXCHANGERATE"
+",(case when CURRENCY='RMB' then 1 else (case when rate<>1 and rate<>0 then rate else convert(numeric(8,5),round(SUM(hj)/SUM(je),5)) end) END) as EXCHANGERATE"
+",SUM(je) AS je,SUM(hj) AS hj"+sISEXPLAN//,SETTLETYPE+ strISEXPLAN
+" from ("
+"SELECT BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,CURRENCY,1 as EXCHANGERATE,rate"
+"SELECT gid_s GID,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,CURRENCY,1 as EXCHANGERATE,rate"
+",SUM((case when FEETYPE=1 then AMOUNT else (-AMOUNT) end)) AS je"
+",SUM(convert(decimal(18,2),round((case when FEETYPE=1 then ORIGAMOUNT else (-ORIGAMOUNT) end)*"+tbRATE_CASH+",2))) AS hj"
+sISEXPLAN//,SETTLETYPE+ strISEXPLAN
+" From VW_ch_fee_settlement_do WHERE 1=1"+strCondition+" and (ACCOUNTRMB='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"' or ACCOUNTUSD='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"')"
+" and gid_s in ("+sGids.Trim()+") "
+" GROUP BY BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,EXCHANGERATE,CURRENCY,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+" GROUP BY gid_s,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,EXCHANGERATE,CURRENCY,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+") as a"
+" GROUP BY BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,EXCHANGERATE,CURRENCY,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+" GROUP BY GID,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,EXCHANGERATE,CURRENCY,RATE" +sISEXPLAN//,BILLNO,SETTLETYPE + strISEXPLAN
+" ORDER BY CURRENCY"+sISEXPLAN;// + strISEXPLAN
}
else
{
sqlBankCE="SELECT BILLNO,SETTLENO,BILLTYPE,SETTLETYPE SETTLETYPEREF,ACCOUNTRMB,ACCOUNTUSD,'RMB' as CURRENCY,1 as EXCHANGERATE"
sqlBankCE="SELECT gid_s GID,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE SETTLETYPEREF,ACCOUNTRMB,ACCOUNTUSD,'RMB' as CURRENCY,1 as EXCHANGERATE"
+",SUM((case when FEETYPE=1 then AMOUNT else (-AMOUNT) end)) AS je"
+",SUM(convert(decimal(18,2),round((case when FEETYPE=1 then ORIGAMOUNT else (-ORIGAMOUNT) end)*"+tbRATE_CASH+",2))) AS hj"
+sISEXPLAN//,SETTLETYPE+ strISEXPLAN
+" From VW_ch_fee_settlement_do WHERE 1=1"+strCondition+" and (ACCOUNTRMB='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"' or ACCOUNTUSD='"+dsBank.Tables[0].Rows[i]["BankGid"].ToString().Trim()+"')"
+" and gid_s in ("+sGids.Trim()+") ";
sqlBankCE+=" GROUP BY BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,RATE" +sISEXPLAN.Trim();//strISEXPLAN.Trim() +
sqlBankCE+=" GROUP BY gid_s,BILLNO,SETTLENO,BILLTYPE,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,RATE" +sISEXPLAN.Trim();//strISEXPLAN.Trim() +
sqlBankCE+=" ORDER BY ACCOUNTRMB,ACCOUNTUSD,RATE"+sISEXPLAN.Trim();//strISEXPLAN.Trim() +
+"left join (select top 1 ISNULL(f.EXCHANGERATE,1) EXCHANGERATE,d.BILLNO from ch_fee f left join ch_fee_do d on (d.FEEID=f.GID) "
+"WHERE F.CURRENCY<>'RMB' ) as f on (f.BILLNO=s.BILLNO)"
+" WHERE 1=1 and s.GID in ("+sGids.Trim()+") "// + strISEXPLAN//,FEETYPE
+" GROUP BY s.billno,s.SETTLENO,s.SETTLETIME,CUSTOMERNAME,EXCHANGERATE,CURR,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,ACCOUNTS_CURRENCY,PREPAY_CURRENCY,AHSR_CURRENCY,FINANCIAL_CURRENCY,ADVANCE_CURRENCY,ACCOUNTS_RATE,PREPAY_RATE,AHSR_RATE,FINANCIAL_RATE,ADVANCE_RATE,ACCOUNTS_MONEY,PREPAY_MONEY,AHSR_MONEY,FINANCIAL_MONEY,ADVANCE_MONEY,Billtype,CURR"// + strISEXPLAN//,FEETYPE
+" GROUP BY s.GID,s.billno,s.SETTLENO,s.SETTLETIME,CUSTOMERNAME,EXCHANGERATE,CURR,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD,ACCOUNTS_CURRENCY,PREPAY_CURRENCY,AHSR_CURRENCY,FINANCIAL_CURRENCY,ADVANCE_CURRENCY,ACCOUNTS_RATE,PREPAY_RATE,AHSR_RATE,FINANCIAL_RATE,ADVANCE_RATE,ACCOUNTS_MONEY,PREPAY_MONEY,AHSR_MONEY,FINANCIAL_MONEY,ADVANCE_MONEY,Billtype,CURR"// + strISEXPLAN//,FEETYPE
+" ORDER BY FEETYPE desc,CURR,CUSTOMERNAME";// +strISEXPLAN;
+" From VW_ch_fee_settlement_do WHERE 1=1"+strCondition
+" and gid_s in ("+sGids.Trim()+") "
+" GROUP BY BILLNO,SETTLENO,BILLTYPE,DRACCID,CRACCID,FEETYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ORIGCURRENCY,ORIGEXCHANGERATE,ACCOUNTRMB,ACCOUNTUSD,RATE" +strISEXPLAN+sISEXPLAN
+" GROUP BY gid_s,BILLNO,SETTLENO,BILLTYPE,DRACCID,CRACCID,FEETYPE,SETTLETYPE,EXCHANGERATE,CURRENCY,ORIGCURRENCY,ORIGEXCHANGERATE,ACCOUNTRMB,ACCOUNTUSD,RATE" +strISEXPLAN+sISEXPLAN
+" ORDER BY DRACCID,CRACCID,FEETYPE desc,ORIGCURRENCY"+strISEXPLAN+sISEXPLAN;
tempstr+=GetSpaceStr(bill.MBLNO,20);//16 //Original_reference//C//X(20)//98 //117 //webCSM Alternate Booking Reference, only if alt booking ref is used
tempstr+=GetSpaceStr(bill.ORDERNO,20);//16 //Original_reference//C//X(20)//98 //117 //webCSM Alternate Booking Reference, only if alt booking ref is used
}
tempstr+=GetSpaceStr("",20);//17 //Bkg Pty Reference//O//X(20)//118 //137 //webCSM Bkg Party Reference
str=" BX.CREATEUSER in (select showname from vw_user where COMPANYID='"+companyid+"') ";
}
elseif(visiblerange=="1")
{
str=" BX.CREATEUSER in (select showname from vw_user where COMPANYID='"+companyid+"') ";
}
elseif(visiblerange=="5")
{
str=" BX.CREATEUSER in (select showname from vw_user where COMPANYID in (select COMPANYID from user_authority_range_company where userid='"+userid+"' and AUTHORITYID='"+AUTHORITYID+"' and VISIBLERANGE=1)) ";
//var userstr = new StringBuilder();
//userstr.Append(" select COMPANYID from user_authority_range_company where userid='" + userid + "' and AUTHORITYID='" + AUTHORITYID + "' and VISIBLERANGE=1");
str=" EXISTS (select 1 from user_authority_range_op P left join [user] u on (u.GID=P.OPID) where (BX.CREATEUSER=U.SHOWNAME ) AND P.userid='"+userid+"' and P.AUTHORITYID='"+AUTHORITYID+"' and P.VISIBLERANGE=1) ";