strSql.Append(" left join (select BSNO,SUM(RMBAMOUNT) RMBAMOUNT,SUM(USDAMOUNT) USDAMOUNT,SUM(L_RMBAMOUNT) L_RMBAMOUNT,SUM(L_USDAMOUNT) L_USDAMOUNT FROM CH_FEE_AutoDuiDetail GROUP BY BSNO) D ON (D.BSNO=GID) ");
strSql.Append("select dl.*,b.ETD,B.CUSTOMERNAME,B.OP,B.CARRIER,B.TEU,B.SALE,B.CUSTNO from ");
// strSql.Append("(select GID,BSNO,(select top 1 BSNO from v_op_bill where (MBLNO=D.b_mblno or HBLNO=D.MBLNO) and OPLBNAME in('海运进口','海运出口','空运进口','空运出口')) B_BSNO,MBLNO,B_MBLNO,RMBAMOUNT,USDAMOUNT,L_RMBAMOUNT,L_USDAMOUNT,ISOK,REMARK,REMARK2 ");
strSql.Append("select dl.*,b.ETD,B.CUSTOMERNAME,B.OP,B.CARRIER,B.TEU,B.SALE,B.CUSTNO from ");
// strSql.Append("(select GID,BSNO,(select top 1 BSNO from v_op_bill where (MBLNO=D.b_mblno or HBLNO=D.MBLNO) and OPLBNAME in('海运进口','海运出口','空运进口','空运出口')) B_BSNO,MBLNO,B_MBLNO,RMBAMOUNT,USDAMOUNT,L_RMBAMOUNT,L_USDAMOUNT,ISOK,REMARK,REMARK2 ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END) ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END) ELSE 0 END),0) AS L_RMBAMOUNT, ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END)ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END)ELSE 0 END),0) AS L_USDAMOUNT ");
}
else
{
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS L_RMBAMOUNT, ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS L_USDAMOUNT ");
strSql.Append(",isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE WHEN F.FEESTATUS=0 OR F.FEESTATUS=8 OR F.FEESTATUS=9 THEN 0 ELSE ISNULL(AMOUNT,0) END)ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE WHEN F.FEESTATUS=0 OR F.FEESTATUS=8 OR F.FEESTATUS=9 THEN 0 ELSE ISNULL(AMOUNT,0) END)ELSE 0 END),0) AS B_AMOUNT ");
strSql.Append(" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
strSql.Append(" where F.AMOUNT<>F.SETTLEMENT and F.CUSTOMERNAME='"+CUSTOMERNAME+"' and EXISTS (select 1 from CH_FEE_AutoDuiDetail fd where (fd.MBLNO=B.MBLNO OR fd.MBLNO=B.HBLNO) and fd.BSNO='"+AutoDui_GID+"') ");
if(HAVEDEBIT=="0"){
strSql.Append(" and isnull(f.ISDEBIT,0)=0 ");
}
if((FEEDC!="0")&&(FEEDC.Trim()!="")){
strSql.Append(" and f.FEETYPE="+FEEDC);
}
if(CURR!="")
{
strSql.Append(" and f.CURRENCY='"+CURR+"'");
}
if(FSTATUS=="1"||FSTATUS=="3"){
strSql.Append(" AND (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9)");
}elseif(FSTATUS=="2"){
strSql.Append(" AND (F.FEESTATUS!=0 and F.FEESTATUS!=8 and F.FEESTATUS!=9) ");
}
if(FSTATUS=="3"||FSTATUS=="4")
{
strSql.Append(" AND (f.AMOUNT-isnull(f.SETTLEMENT,0)-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0))<>0");
}
if(SALECORP!=""){
strSql.Append(" and f.SALECORP='"+SALECORP+"'");
}
if(FEEDUITYPE!="")
{
if(FEEDUITYPECONDIT=="不等于")
{
strSql.Append(" and not EXISTS (select 1 from code_fee c where c.[NAME]=f.FEENAME and c.DUITYPE='"+FEEDUITYPE+"') ");
}
else
{
strSql.Append(" and EXISTS (select 1 from code_fee c where c.[NAME]=f.FEENAME and c.DUITYPE='"+FEEDUITYPE+"') ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END) ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END) ELSE 0 END),0) AS L_RMBAMOUNT, ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END)ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END)ELSE 0 END),0) AS L_USDAMOUNT ");
}
else
{
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS L_RMBAMOUNT, ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS L_USDAMOUNT ");
strSql.Append(",isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE WHEN F.FEESTATUS=0 OR F.FEESTATUS=8 OR F.FEESTATUS=9 THEN 0 ELSE ISNULL(AMOUNT,0) END)ELSE 0 END),0)- ");
strSql.Append(" isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE WHEN F.FEESTATUS=0 OR F.FEESTATUS=8 OR F.FEESTATUS=9 THEN 0 ELSE ISNULL(AMOUNT,0) END)ELSE 0 END),0) AS B_AMOUNT ");
strSql.Append(" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
strSql.Append(" where F.AMOUNT<>F.SETTLEMENT and F.CUSTOMERNAME='"+CUSTOMERNAME+"' and EXISTS (select 1 from CH_FEE_AutoDuiDetail fd where (fd.MBLNO=B.MBLNO OR fd.MBLNO=B.HBLNO) and fd.BSNO='"+AutoDui_GID+"') ");
if(HAVEDEBIT=="0")
{
strSql.Append(" and isnull(f.ISDEBIT,0)=0 ");
}
if((FEEDC!="0")&&(FEEDC.Trim()!=""))
{
strSql.Append(" and f.FEETYPE="+FEEDC);
}
if(CURR!="")
{
strSql.Append(" and f.CURRENCY='"+CURR+"'");
}
if(SALECORP!="")
{
strSql.Append(" and f.SALECORP='"+SALECORP+"'");
}
if(FSTATUS=="1")
{
strSql.Append(" AND (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9)");
}
elseif(FSTATUS=="2")
{
strSql.Append(" AND (F.FEESTATUS!=0 and F.FEESTATUS!=8 and F.FEESTATUS!=9) ");
}
if(FEEDUITYPE!="")
{
if(FEEDUITYPECONDIT=="不等于")
{
strSql.Append(" and not EXISTS (select 1 from code_fee c where c.[NAME]=f.FEENAME and c.DUITYPE='"+FEEDUITYPE+"') ");
}
else
{
strSql.Append(" and EXISTS (select 1 from code_fee c where c.[NAME]=f.FEENAME and c.DUITYPE='"+FEEDUITYPE+"') ");
//var blUpSQL = " update CH_FEE_AutoDuiDetail set B_BSNO=(select top 1 BSNO from v_op_bS where (MBLNO=D.b_mblno or HBLNO=D.MBLNO) and OPLBNAME in('海运进口','海运出口','空运进口','空运出口') ) FROM CH_FEE_AutoDuiDetail d where bsno='" + duihead.GID+"'";
varblUpSQL=" update CH_FEE_AutoDuiDetail set B_BSNO=(select top 1 BSNO from v_op_bS where (MBLNO=D.mblno or HBLNO=D.MBLNO) ) FROM CH_FEE_AutoDuiDetail d where bsno='"+duihead.GID+"'";
// BillList = Chfee_AuditDAL.Chfee_AuditDAL.GetBillDataList(" OPLBNAME='海运出口' and ETD between '" + head.STARTDATE + "' and '" + head.ENDDATE + "' and exists(select 1 from ch_fee where customername='" + head.CUSTOMERNAME + "' and bsno=V_op_Bill.BSNO and Amount<>settlement and isnull(ISDEBIT,0)=0) ", userid, username, companyid);
BillList=Chfee_AuditDAL.Chfee_AuditDAL.GetBillDataList(" ETD between '"+head.STARTDATE+"' and '"+head.ENDDATE+"' and exists(select 1 from ch_fee where customername='"+head.CUSTOMERNAME+"' and bsno=V_op_Bill.BSNO and Amount<>settlement and isnull(ISDEBIT,0)=0) ",userid,username,companyid);
}
else
{
BillList=Chfee_AuditDAL.Chfee_AuditDAL.GetBillDataList(" ETD between '"+head.STARTDATE+"' and '"+head.ENDDATE+"' and exists(select 1 from ch_fee where customername='"+head.CUSTOMERNAME+"' and bsno=V_op_Bill.BSNO and Amount<>settlement) ",userid,username,companyid);