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(" 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) ");
str=" (OP='"+username/*+ "' OR INPUTBY='" + username*/+"')";
}
elseif(visiblerange=="3")
{
str=" (OP='"+username/*+ "' OR INPUTBY='" + username*/+"')";
}
elseif(visiblerange=="2")
{
varrangeDa=newRangeDA();
vardeptname=rangeDa.GetDEPTNAME(userid);
str=" (OP in (select showname from [user] where GID in (select USERID from user_company where COMPANYID=(select GID from [company] where codename='"+companyid+"')) and GID in (select userid from user_baseinfo where DEPTNAME='"+deptname+"')))";
//+ " OR INPUTBY in (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 + "')))";
}
elseif(visiblerange=="1")
{
str=" (OP in (select showname from [user] where GID in (select USERID from user_company where COMPANYID=(select GID from [company] where codename='"+companyid+"'))))";
//+ " OR INPUTBY in (select showname from [user] where GID in (select USERID from user_company where COMPANYID='" + companyid + "')))";
}
elseif(visiblerange=="5")
{
str=" (OP in (select showname from [user] where GID in (select USERID from user_company 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");
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 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(" from CH_FEE_AutoDuiDetail D left join V_op_bill B on REPLACE(REPLACE(B.MBLNO,'-',''),' ','')=REPLACE(REPLACE(D.MBLNO,'-',''),' ','') ");
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(" 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 + "') ");
strSql.Append(" where F.AMOUNT<>F.SETTLEMENT and F.CUSTOMERNAME='"+CUSTOMERNAME+"' and EXISTS (select 1 from CH_FEE_AutoDuiDetail fd where (REPLACE(REPLACE(fd.MBLNO,'-',''),' ','')=REPLACE(REPLACE(B.MBLNO,'-',''),' ','') OR fd.MBLNO=B.HBLNO) and fd.BSNO='"+AutoDui_GID+"') ");
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(" where F.AMOUNT<>F.SETTLEMENT and F.CUSTOMERNAME='"+CUSTOMERNAME+"' and EXISTS (select 1 from CH_FEE_AutoDuiDetail fd where (REPLACE(REPLACE(fd.MBLNO,'-',''),' ','')=REPLACE(REPLACE(B.MBLNO,'-',''),' ','') OR fd.MBLNO=B.HBLNO) and fd.BSNO='"+AutoDui_GID+"') ");
//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 (REPLACE(REPLACE(MBLNO,'-',''),' ','')=REPLACE(REPLACE(D.MBLNO,'-',''),' ','') or HBLNO=D.MBLNO) ) FROM CH_FEE_AutoDuiDetail d where bsno='"+duihead.GID+"'";
var_L_RMBAMOUNT=db.ExecuteScalar(CommandType.Text,"select MBLNO FROM v_op_bill where REPLACE(REPLACE(MBLNO,'-',''),' ','')=REPLACE(REPLACE('"+MBLNO+"','-',''),' ','') ");
// 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);