varaddinvlist=ChsettlementDAL.GetPayDCDataList("BILLNO in (select BILLNO FROM ch_fee_bankdata_fitdetail WHERE LINKGID='"+Bankdata.GID+"')",userid,username,companyid,"","RMB");
varaddinvlist=ChsettlementDAL.GetPayDCDataAllList("BILLNO in (select BILLNO FROM ch_fee_bankdata_fitdetail WHERE LINKGID='"+Bankdata.GID+"')","","RMB");
strSql.Append("SELECT *,AMOUNT-DOAMOUNT AS BALAMOUNT,USDAMOUNT-USDDOAMOUNT AS USDBALAMOUNT FROM (SELECT GID,BILLNO,BILLSTATUS,CUSTOMERNAME,CURR,CHEQUEPAYABLE,SETTLETYPE ");
strSql.Append("SELECT *,AMOUNT-DOAMOUNT AS BALAMOUNT,USDAMOUNT-USDDOAMOUNT AS USDBALAMOUNT,OTAMOUNT-OTDOAMOUNT AS OTBALAMOUNT FROM (SELECT GID,cm.BILLNO,BILLSTATUS,CUSTOMERNAME,CURR,CHEQUEPAYABLE,SETTLETYPE ");
strSql.Append(",(select STLNAME from [code_stlmode] where STLCODE=cm.SETTLETYPE) as SETTLETYPEREF");
strSql.Append(" ,REMARK,COMPANYID,SALECORPID,(select [NAME] from [company] where GID=cm.SALECORPID) as SALECORP ");
strSql.Append(",(select SUM(CASE FEETYPE WHEN 1 THEN -DOAMOUNT ELSE DOAMOUNT END) from ch_fee_do where CURRENCY='RMB' AND BILLNO=cm.BILLNO) as DOAMOUNT ");
strSql.Append(",(select SUM(CASE FEETYPE WHEN 1 THEN -DOAMOUNT ELSE DOAMOUNT END) from ch_fee_do where CURRENCY!='RMB' AND BILLNO=cm.BILLNO) as USDDOAMOUNT ");
strSql.Append(",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=97005 and EnumValueID=cm.BILLSTATUS) as BILLSTATUSREF");
strSql.Append(",(select ShowName from [user] where GID=cm.APPLICANT) as APPLICANTNAME");
strSql.Append(",CUSTACCOUNTGID");
strSql.Append(",(select CODENAME+'_'+bankname from info_client_bank where gid=cm.CUSTACCOUNTGID) CUSTBANKNAME");
strSql.Append(",(select ACCOUNT from info_client_bank where gid=cm.CUSTACCOUNTGID) CUSTACCOUNT,cm.STLAMOUNT,cm.STLRATE,cm.STLCURR,cm.INVNO");
strSql.Append(",AUDITTIME,(select ShowName from [user] where GID=cm.AUDITUSER) as AUDITUSERREF");
strSql.Append(" FROM ch_fee_payapplication cm where BILLSTATUS IN (0,4,5) ");
strSql.Append(",AUDITTIME,(select ShowName from [user] where GID=cm.AUDITUSER) as AUDITUSERREF,ISREVINV");
strSql.Append(" FROM ch_fee_payapplication cm ");
strSql.Append(" LEFT JOIN (SELECT BILLNO,ISNULL(SUM(CASE WHEN CURRENCY='"+localcurr+"' THEN CASE WHEN FEETYPE=1 THEN -AMOUNT ELSE AMOUNT END ELSE 0 END),0) AMOUNT"+
" ,ISNULL(SUM(CASE WHEN CURRENCY='USD' THEN CASE WHEN FEETYPE=1 THEN -AMOUNT ELSE AMOUNT END ELSE 0 END),0) USDAMOUNT,"+
"ISNULL(SUM(CASE WHEN CURRENCY<>'"+localcurr+"' AND CURRENCY<>'USD' THEN CASE WHEN FEETYPE=1 THEN -AMOUNT ELSE AMOUNT END ELSE 0 END),0) OTAMOUNT"+
",ISNULL(SUM(CASE WHEN CURRENCY='"+localcurr+"' THEN CASE WHEN FEETYPE=1 THEN -DOAMOUNT ELSE DOAMOUNT END ELSE 0 END),0) DOAMOUNT"+
",ISNULL(SUM(CASE WHEN CURRENCY='USD' THEN CASE WHEN FEETYPE=1 THEN -DOAMOUNT ELSE DOAMOUNT END ELSE 0 END),0) USDDOAMOUNT"+
" ,ISNULL(SUM(CASE WHEN CURRENCY<>'"+localcurr+"' AND CURRENCY<>'USD' THEN CASE WHEN FEETYPE=1 THEN -DOAMOUNT ELSE DOAMOUNT END ELSE 0 END),0) OTDOAMOUNT"+
" from ch_fee_do where CATEGORY=4 GROUP BY BILLNO ) V ON (V.BILLNO=cm.BILLNO)");
strSql.Append("SELECT *,AMOUNT-DOAMOUNT AS BALAMOUNT,USDAMOUNT-USDDOAMOUNT AS USDBALAMOUNT,OTAMOUNT-OTDOAMOUNT AS OTBALAMOUNT FROM (SELECT GID,cm.BILLNO,BILLSTATUS,CUSTOMERNAME,CURR,CHEQUEPAYABLE,SETTLETYPE ");
strSql.Append(",(select STLNAME from [code_stlmode] where STLCODE=cm.SETTLETYPE) as SETTLETYPEREF");
strSql.Append(" ,REMARK,COMPANYID,SALECORPID,(select [NAME] from [company] where GID=cm.SALECORPID) as SALECORP ");
strSql.Append(",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=97005 and EnumValueID=cm.BILLSTATUS) as BILLSTATUSREF");
strSql.Append(",(select ShowName from [user] where GID=cm.APPLICANT) as APPLICANTNAME");
strSql.Append(",CUSTACCOUNTGID");
strSql.Append(",(select CODENAME+'_'+bankname from info_client_bank where gid=cm.CUSTACCOUNTGID) CUSTBANKNAME");
strSql.Append(",(select ACCOUNT from info_client_bank where gid=cm.CUSTACCOUNTGID) CUSTACCOUNT,cm.STLAMOUNT,cm.STLRATE,cm.STLCURR,cm.INVNO");
strSql.Append(",AUDITTIME,(select ShowName from [user] where GID=cm.AUDITUSER) as AUDITUSERREF,ISREVINV");
strSql.Append(" FROM ch_fee_payapplication cm ");
strSql.Append(" LEFT JOIN (SELECT BILLNO,ISNULL(SUM(CASE WHEN CURRENCY='"+localcurr+"' THEN CASE WHEN FEETYPE=1 THEN -AMOUNT ELSE AMOUNT END ELSE 0 END),0) AMOUNT"+
" ,ISNULL(SUM(CASE WHEN CURRENCY='USD' THEN CASE WHEN FEETYPE=1 THEN -AMOUNT ELSE AMOUNT END ELSE 0 END),0) USDAMOUNT,"+
"ISNULL(SUM(CASE WHEN CURRENCY<>'"+localcurr+"' AND CURRENCY<>'USD' THEN CASE WHEN FEETYPE=1 THEN -AMOUNT ELSE AMOUNT END ELSE 0 END),0) OTAMOUNT"+
",ISNULL(SUM(CASE WHEN CURRENCY='"+localcurr+"' THEN CASE WHEN FEETYPE=1 THEN -DOAMOUNT ELSE DOAMOUNT END ELSE 0 END),0) DOAMOUNT"+
",ISNULL(SUM(CASE WHEN CURRENCY='USD' THEN CASE WHEN FEETYPE=1 THEN -DOAMOUNT ELSE DOAMOUNT END ELSE 0 END),0) USDDOAMOUNT"+
" ,ISNULL(SUM(CASE WHEN CURRENCY<>'"+localcurr+"' AND CURRENCY<>'USD' THEN CASE WHEN FEETYPE=1 THEN -DOAMOUNT ELSE DOAMOUNT END ELSE 0 END),0) OTDOAMOUNT"+
" from ch_fee_do where CATEGORY=4 GROUP BY BILLNO ) V ON (V.BILLNO=cm.BILLNO)");
strSql.Append(" and NOT EXISTS(select 1 from ch_fee_do where BILLNO=cm.BILLNO and EXISTS (select 1 from v_op_bill where OPLBNAME='海运出口' and bsno=ch_fee_do.BSNO and FEESTATUS<>1 )) ");
//strCondition = strCondition + " and B.FEESTATUS=1 ";
strSql.Append(",(select [NAME] from [company] where GID=cm.SALECORPID) as SALECORP");
strSql.Append(",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=97008 and EnumValueID=cm.INVOICECATEGORY) as INVOICECATEGORYREF");
strSql.Append(",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=97009 and EnumValueID=cm.INVOICETYPE) as INVOICETYPEREF");
strSql.Append(",CASE WHEN cm.CURRENCY=cm.RECVCURR then (select SUM(CASE FEETYPE WHEN 1 THEN AMOUNT-DOAMOUNT ELSE DOAMOUNT-AMOUNT END) from ch_fee_do where CURRENCY=cm.RECVCURR AND BILLNO=cm.BILLNO) ELSE ");
strSql.Append(" (select SUM(CASE FEETYPE WHEN 1 THEN ORIGAMOUNT-ISNULL(ORIGSTLAMOUNT,0) ELSE ISNULL(ORIGSTLAMOUNT,0)-ORIGAMOUNT END) from ch_fee_do where ORIGCURRENCY=cm.RECVCURR AND BILLNO=cm.BILLNO) END as DOAMOUNT ");
strSql.Append(",(case cm.billstatus when 1 then '已锁定' else '未锁定' end) as BILLSTATUSREF");
strSql.Append(",(select ShowName from [user] where GID=cm.OPERATOR) as OPERATORNAME");
strSql.Append(",(select ShowName from [user] where GID=cm.DELETEOPERATOR) as DELOPERATORNAME");
strSql.Append(", INVSHENNO=stuff ((SELECT distinct BILLNO+',' FROM ch_fee_invoiceapplication p WHERE p.INVBILLNO=cm.BILLNO FOR xml path('')), 1, 0, '')");
strSql.Append(" FROM ch_fee_invoice cm where EXISTS (select 1 from ch_fee_do d left join ch_fee f on (f.gid=d.feeid) where d.BILLNO=cm.billno and d.CATEGORY=7 ");
strSql.Append(" and f.FEESTATUS IN (0,8) and (d.ORIGAMOUNT-ISNULL(d.ORIGSTLAMOUNT,0))<>0 and (( f.AMOUNT>0 AND (d.ORIGAMOUNT-ISNULL(d.ORIGSTLAMOUNT,0))<=(f.AMOUNT-isnull(f.SETTLEMENT,0)-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0))) ");
strSql.Append(" or ( f.AMOUNT<0 AND (d.ORIGAMOUNT-ISNULL(d.ORIGSTLAMOUNT,0))>=(f.AMOUNT-isnull(f.SETTLEMENT,0)-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0)))) ) ");
sql=sql+getAndConSql(sql,customNo," BILLNO like '%" +customNo+"%'");
sql=sql+getAndConSql(sql,customNo," cm.BILLNO like '%" +customNo+"%'");
varBH=form.findField('BH').getValue();
sql=sql+getAndConSql(sql,BH," EXISTS(select 1 from ch_fee_do where BILLNO=cm.BILLNO and EXISTS(select 1 from v_op_bill where bsno=ch_fee_do.BSNO and (CUSTNO like '%"+BH+"%' or MBLNO like '%"+BH+"%' or HBLNO like '%"+BH+"%') ))");