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 RMBDOAMOUNT ");
strSql.Append(",(select SUM(CASE FEETYPE WHEN 1 THEN -DOAMOUNT ELSE DOAMOUNT END) from ch_fee_do where CURRENCY='USD' AND BILLNO=cm.BILLNO) as USDDOAMOUNT ");
strSql.Append(",(select SUM(CASE FEETYPE WHEN 1 THEN -DOAMOUNT ELSE DOAMOUNT END) from ch_fee_do where CURRENCY!='RMB' AND CURRENCY!='USD' AND BILLNO=cm.BILLNO) as OTDOAMOUNT ");
strSql.Append(",(select SUM(CASE FEETYPE WHEN 1 THEN -DOAMOUNT ELSE DOAMOUNT END) from ch_fee_do WITH (NOLOCK) where CURRENCY='RMB' AND BILLNO=cm.BILLNO AND CATEGORY=4) as RMBDOAMOUNT ");
strSql.Append(",(select SUM(CASE FEETYPE WHEN 1 THEN -DOAMOUNT ELSE DOAMOUNT END) from ch_fee_do WITH (NOLOCK) where CURRENCY='USD' AND BILLNO=cm.BILLNO AND CATEGORY=4) as USDDOAMOUNT ");
strSql.Append(",(select SUM(CASE FEETYPE WHEN 1 THEN -DOAMOUNT ELSE DOAMOUNT END) from ch_fee_do WITH (NOLOCK) where CURRENCY!='RMB' AND CURRENCY!='USD' AND BILLNO=cm.BILLNO AND CATEGORY=4) as OTDOAMOUNT ");
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");
strSql.Append(",ISNULL((select sum(isnull(amount,0)-isnull(invoice,0)) as noinvoice from ch_fee WITH (NOLOCK) where isnull(ISINVOICE,0)<>1 and gid in (select feeid from ch_fee_do WITH (NOLOCK) where BILLNO=cm.BILLNO)),0) as NOINVOICE");
strSql.Append(",ISNULL((select sum(isnull(amount,0)-isnull(invoice,0)) as noinvoice from ch_fee WITH (NOLOCK) where isnull(ISINVOICE,0)<>1 and EXISTS (select 1 from ch_fee_do WITH (NOLOCK) where ch_fee_do.FEEID=ch_fee.gid and ch_fee_do.BILLNO=cm.BILLNO)),0) as NOINVOICE");
strSql.Append(",(select STLNAME from [code_stlmode] where STLCODE=i.SETTLETYPE) as SETTLETYPEREF");
strSql.Append(",(select CODENAME+'_'+bankname from info_client_bank where gid=i.CUSTACCOUNTGID) CUSTBANKNAME");
strSql.Append(",(select ACCOUNT from info_client_bank where gid=i.CUSTACCOUNTGID) CUSTACCOUNT");
strSql.Append(",ISNULL((select sum(isnull(f.amount,0)-isnull(f.invoice,0)) as noinvoice from ch_fee f left join ch_fee_do d on (d.FEEID=f.GID) where ISINVOICE<>1 and d.BILLNO=i.BILLNO),0) as NOINVOICE");
strSql.Append(",ISNULL((select sum(isnull(amount,0)-isnull(invoice,0)) as noinvoice from ch_fee WITH (NOLOCK) where isnull(ISINVOICE,0)<>1 and EXISTS (select 1 from ch_fee_do WITH (NOLOCK) where ch_fee_do.FEEID=ch_fee.gid and ch_fee_do.BILLNO=cm.BILLNO)),0) as NOINVOICE");
strSql.Append(",i.BS_CUSTOMERNAME,dbo.[GetUsingStep](wu.WORKFLOWID,wu.currentid,'"+userid+"',wu.stepno) as IsAudit ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where BILLNO IN (SELECT BILLNO FROM ch_fee_do_invoice WHERE FEEID=c.FEEID)) INVOICENO ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where EXISTS (SELECT 1 FROM ch_fee_do_invoice WHERE ch_fee_do_invoice.BILLNO=ch_fee_invoicehexiao.BILLNO AND FEEID=c.FEEID)) INVOICENO ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where BILLNO IN (SELECT BILLNO FROM ch_fee_do_invoice WHERE FEEID=c.FEEID)) INVOICENO ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where EXISTS (SELECT 1 FROM ch_fee_do_invoice WHERE ch_fee_do_invoice.BILLNO=ch_fee_invoicehexiao.BILLNO AND FEEID=c.FEEID)) INVOICENO ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where BILLNO IN (SELECT BILLNO FROM ch_fee_do_invoice WHERE FEEID=c.FEEID)) INVOICENO ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where EXISTS (SELECT 1 FROM ch_fee_do_invoice WHERE ch_fee_do_invoice.BILLNO=ch_fee_invoicehexiao.BILLNO AND FEEID=c.FEEID)) INVOICENO ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where BILLNO IN (SELECT BILLNO FROM ch_fee_do_invoice WHERE FEEID=c.FEEID)) INVOICENO ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where EXISTS (SELECT 1 FROM ch_fee_do_invoice WHERE ch_fee_do_invoice.BILLNO=ch_fee_invoicehexiao.BILLNO AND FEEID=c.FEEID)) INVOICENO ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where BILLNO IN (SELECT BILLNO FROM ch_fee_do_invoice WHERE FEEID=c.FEEID)) INVOICENO ");
strSql.Append(",b.DOC,b.SALE,(select top 1 INVNO from ch_fee_invoicehexiao where EXISTS (SELECT 1 FROM ch_fee_do_invoice WHERE ch_fee_do_invoice.BILLNO=ch_fee_invoicehexiao.BILLNO AND FEEID=c.FEEID)) INVOICENO ");
varAiriCondition=" OP_AIRI.CUSTOMERNAME=(select shortname from info_client where gid= '"+headData.LINKGID+"') and OP_AIRI.ETA between Convert(datetime,'"+headData.BGNDATE+"') and convert(datetime,'"+headData.ENDDATE+"') ";
strSql.Append(",(SELECT COUNT(*) FROM OP_APPLY WHERE MASTERNO=B.BSNO) as APPLYCOUNT,B.BillFeeStatus ");
strSql.Append(",SALECORPID,(select [NAME] from [company] where GID=B.SALECORPID) as SALECORP,CONTRACTNO,LANE,DELIVERYDATE,DOC,HSCODE,ISCANCEL");
strSql.Append(",(select TOP 1 COLOR FROM code_op_status_detail WITH (NOLOCK) WHERE OPSTATUS = (CASE WHEN (SELECT TOP 1 [STATUS] FROM V_OP_STATUS WHERE BSNO=B.BSNO ORDER BY COMPTIME DESC,INPUTTIME DESC) is null THEN '接受委托' else (SELECT TOP 1[STATUS] FROM V_OP_STATUS WHERE BSNO = B.BSNO ORDER BY COMPTIME DESC, INPUTTIME DESC) end))AS COLOR");
strSql.Append(" from OP_AIRE B");
strSql.Append(" LEFT JOIN v_op_gain_dr_INV I ON (I.BSNO=B.BSNO) ");
strSql.Append(",SALECORPID,(select [NAME] from [company] where GID=B.SALECORPID) as SALECORP,CONTRACTNO,LANE,DELIVERYDATE,DOC,HSCODE,ISCANCEL");
strSql.Append(",(select TOP 1 COLOR FROM code_op_status_detail WITH (NOLOCK) WHERE OPSTATUS = (CASE WHEN (SELECT TOP 1 [STATUS] FROM V_OP_STATUS WHERE BSNO=B.BSNO ORDER BY COMPTIME DESC,INPUTTIME DESC) is null THEN '接受委托' else (SELECT TOP 1[STATUS] FROM V_OP_STATUS WHERE BSNO = B.BSNO ORDER BY COMPTIME DESC, INPUTTIME DESC) end))AS COLOR");
strSql.Append(" from OP_AIRE B");
strSql.Append(" LEFT JOIN v_op_gain_dr_INV I ON (I.BSNO=B.BSNO) ");
strSql.Append(",SALECORPID,(select [NAME] from [company] where GID=B.SALECORPID) as SALECORP,CONTRACTNO,LANE,DELIVERYDATE,DOC,HSCODE,ISCANCEL");
strSql.Append(",(select TOP 1 COLOR FROM code_op_status_detail WITH (NOLOCK) WHERE OPSTATUS = (CASE WHEN (SELECT TOP 1 [STATUS] FROM V_OP_STATUS WHERE BSNO=B.BSNO ORDER BY COMPTIME DESC,INPUTTIME DESC) is null THEN '接受委托' else (SELECT TOP 1[STATUS] FROM V_OP_STATUS WHERE BSNO = B.BSNO ORDER BY COMPTIME DESC, INPUTTIME DESC) end))AS COLOR");
strSql.Append(",(SELECT COUNT(*) FROM OP_APPLY WHERE MASTERNO=OP_AIRI.BSNO) as APPLYCOUNT ");
strSql.Append(",(SELECT TOP 1 BLSTATUS FROM op_blissue WHERE BSNO=OP_AIRI.BSNO) BLISSUESTATUS,DOC,OP_AIRI.BillFeeStatus ");
strSql.Append(",SALECORPID,(select [NAME] from [company] where GID=OP_AIRI.SALECORPID) as SALECORP,LANE,DELIVERYDATE,HSCODE");
strSql.Append(",(select TOP 1 COLOR FROM code_op_status_detail WITH (NOLOCK) WHERE OPSTATUS = (CASE WHEN (SELECT TOP 1 [STATUS] FROM V_OP_STATUS WHERE BSNO=OP_AIRI.BSNO ORDER BY COMPTIME DESC,INPUTTIME DESC) is null THEN '接受委托' else (SELECT TOP 1[STATUS] FROM V_OP_STATUS WHERE BSNO = OP_AIRI.BSNO ORDER BY COMPTIME DESC, INPUTTIME DESC) end))AS COLOR");
strSql.Append(",(SELECT COUNT(*) FROM OP_APPLY WHERE MASTERNO=B.BSNO) as APPLYCOUNT ");
strSql.Append(",(SELECT TOP 1 BLSTATUS FROM op_blissue WHERE BSNO=B.BSNO) BLISSUESTATUS,DOC,B.BillFeeStatus ");
strSql.Append(",SALECORPID,(select [NAME] from [company] where GID=B.SALECORPID) as SALECORP,LANE,DELIVERYDATE,HSCODE");
strSql.Append(",(select TOP 1 COLOR FROM code_op_status_detail WITH (NOLOCK) WHERE OPSTATUS = (CASE WHEN (SELECT TOP 1 [STATUS] FROM V_OP_STATUS WHERE BSNO=B.BSNO ORDER BY COMPTIME DESC,INPUTTIME DESC) is null THEN '接受委托' else (SELECT TOP 1[STATUS] FROM V_OP_STATUS WHERE BSNO = B.BSNO ORDER BY COMPTIME DESC, INPUTTIME DESC) end))AS COLOR");
strSql.Append(",SALECORPID,(select [NAME] from [company] where GID=B.SALECORPID) as SALECORP,LANE,CURRENCY,CARGOVALUE,DELIVERYDATE,HSCODE");
strSql.Append(",(select TOP 1 COLOR FROM code_op_status_detail WITH (NOLOCK) WHERE OPSTATUS = (CASE WHEN (SELECT TOP 1 [STATUS] FROM V_OP_STATUS WHERE BSNO=B.BSNO ORDER BY COMPTIME DESC,INPUTTIME DESC) is null THEN '接受委托' else (SELECT TOP 1[STATUS] FROM V_OP_STATUS WHERE BSNO = B.BSNO ORDER BY COMPTIME DESC, INPUTTIME DESC) end))AS COLOR");
strSql.Append(",case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTKHFDR/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END TTKHFDR");
strSql.Append(",(case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END)+(P.TTKHFCR*P.PCCTNNUM) TTKHFCR");
//strSql.Append(",P.FREIGHT*P.PCCTNNUM PCFREIGHT,P.TAXRATE,P.TTLFREIGHT*P.PCCTNNUM PCTTLFREIGHT,P.TTLOTCR*P.PCCTNNUM TTLOTCR,P.TTLCR*P.PCCTNNUM+(case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END) TTLCR");
//strSql.Append(",(case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.CTNNUM*P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM),2) as numeric(20,2)) END)-((P.TTLCR*P.PCCTNNUM)+(case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END)) AS PROFIT");
//// strSql.Append(",cast(round(CASE WHEN (case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.CTNNUM*P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM),2) as numeric(20,2)) END)=0 THEN 0 ELSE(case when ISNULL(B.CTNNUM, 0) = 0 THEN 0 ELSE cast(round(G.TTLDR / B.CTNNUM * P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM), 2) as numeric(20, 2)) END) - (P.TTLCR * P.PCCTNNUM)/(case when ISNULL(B.CTNNUM, 0)= 0 THEN 0 ELSE cast(round(G.TTLDR / B.CTNNUM * P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM), 2) as numeric(20, 2)) END) END,2) AS numeric(20, 2)) PROFITRATE ");
//strSql.Append(",CASE WHEN (case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.CTNNUM*P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM),2) as numeric(20,2)) END)=0 THEN 0 ELSE cast(round((((case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.CTNNUM*P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM),2) as numeric(20,2)) END)");
//strSql.Append("-((P.TTLCR*P.PCCTNNUM)+(case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END)))/(case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.CTNNUM*P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM),2) as numeric(20,2)) END)*100),2) AS numeric(20,2)) END PROFITRATE ");
//strSql.Append(" ,(SELECT DESCRIPTION FROM info_client WHERE SHORTNAME=B.CUSTOMERNAME) AS CUSTOMERNAMEREF");
//strSql.Append(" ,(SELECT DESCRIPTION FROM info_client WHERE SHORTNAME=P.TRUCKER) AS TRUCKERREF");
//strSql.Append(" FROM (select *,(select sum(CTNNUM) from op_truck_bulk_detail where BSNO=op_truck_bulk.BSNO) CTNNUM from op_truck_bulk where transtype='集装箱') B LEFT JOIN v_op_gain_dr_INV_truck G ON (G.BSNO=B.BSNO)");
//strSql.Append(" LEFT JOIN (select P.BSNO,P.TRANSSTATUS,P.ETD,P.CREATETIME,datediff(day,P.ETD,P.CREATETIME) DELAYDAYS,P.TRUCKER,P.DRIVER,P.TRUCKNO,P.DRIVERTEL,CASE WHEN P.CTNNUM=0 THEN 0 ELSE P.FREIGHT/P.CTNNUM END FREIGHT,P.TAXRATE");
//strSql.Append(",CASE WHEN P.CTNNUM=0 THEN 0 ELSE P.TTLFREIGHT/P.CTNNUM END TTLFREIGHT,CASE WHEN P.CTNNUM=0 THEN 0 ELSE G.TTLOTCR/P.CTNNUM END TTLOTCR,CASE WHEN P.CTNNUM=0 THEN 0 ELSE G.TTKHFCR/P.CTNNUM END TTKHFCR");
//strSql.Append(",CASE WHEN P.CTNNUM=0 THEN 0 ELSE G.TTLCR/P.CTNNUM END TTLCR,CASE WHEN P.CTNNUM=0 THEN 0 ELSE G.TTLDR/P.CTNNUM END TTLDR,PD.LINKBSNO,PD.PCKGS,PD.PCCTNNUM");
//strSql.Append(",P.SALE,P.CUSTSERVICE from (select BSNO,LINKBSNO,SUM(KGS)PCKGS,SUM(CTNNUM) PCCTNNUM from op_truck_bulk_pc_detail pd GROUP BY BSNO,LINKBSNO) PD");
//strSql.Append(" LEFT JOIN op_truck_bulk_pc p ON (P.BSNO=pD.BSNO) LEFT JOIN v_op_gain_dr_INV_truck G ON (G.BSNO=p.BSNO)) P ON (P.LINKBSNO=B.BSNO)");
strSql.Append(",P.FREIGHT*P.PCCTNNUM PCFREIGHT,P.TAXRATE,P.TTLFREIGHT*P.PCCTNNUM PCTTLFREIGHT,P.TTLOTCR*P.PCCTNNUM TTLOTCR,P.TTLCR*P.PCCTNNUM+(case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END) TTLCR");
strSql.Append(",(case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.CTNNUM*P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM),2) as numeric(20,2)) END)-((P.TTLCR*P.PCCTNNUM)+(case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END)) AS PROFIT");
// strSql.Append(",cast(round(CASE WHEN (case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.CTNNUM*P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM),2) as numeric(20,2)) END)=0 THEN 0 ELSE(case when ISNULL(B.CTNNUM, 0) = 0 THEN 0 ELSE cast(round(G.TTLDR / B.CTNNUM * P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM), 2) as numeric(20, 2)) END) - (P.TTLCR * P.PCCTNNUM)/(case when ISNULL(B.CTNNUM, 0)= 0 THEN 0 ELSE cast(round(G.TTLDR / B.CTNNUM * P.PCCTNNUM+(P.TTLDR*P.PCCTNNUM), 2) as numeric(20, 2)) END) END,2) AS numeric(20, 2)) PROFITRATE ");
strSql.Append(" ,(SELECT DESCRIPTION FROM info_client WHERE SHORTNAME=B.CUSTOMERNAME) AS CUSTOMERNAMEREF");
strSql.Append(" ,(SELECT DESCRIPTION FROM info_client WHERE SHORTNAME=P.TRUCKER) AS TRUCKERREF");
strSql.Append(" FROM (select *,(select sum(CTNNUM) from op_truck_bulk_detail where BSNO=op_truck_bulk.BSNO) CTNNUM from op_truck_bulk where transtype='集装箱') B LEFT JOIN v_op_gain_dr_INV_truck G ON (G.BSNO=B.BSNO)");
strSql.Append(" LEFT JOIN (select P.BSNO,P.TRANSSTATUS,P.ETD,P.CREATETIME,datediff(day,P.ETD,P.CREATETIME) DELAYDAYS,P.TRUCKER,P.DRIVER,P.TRUCKNO,P.DRIVERTEL,CASE WHEN P.CTNNUM=0 THEN 0 ELSE P.FREIGHT/P.CTNNUM END FREIGHT,P.TAXRATE");
strSql.Append(" LEFT JOIN (select P.BSNO,P.TRANSSTATUS,P.ETD,P.ETA,P.CREATETIME,datediff(day,P.ETA,P.CREATETIME) DELAYDAYS,P.TRUCKER,P.DRIVER,P.TRUCKNO,P.DRIVERTEL,P.REMARK,CASE WHEN P.CTNNUM=0 THEN 0 ELSE P.FREIGHT/P.CTNNUM END FREIGHT,P.TAXRATE");
strSql.Append(",CASE WHEN P.CTNNUM=0 THEN 0 ELSE P.TTLFREIGHT/P.CTNNUM END TTLFREIGHT,CASE WHEN P.CTNNUM=0 THEN 0 ELSE G.TTLOTCR/P.CTNNUM END TTLOTCR,CASE WHEN P.CTNNUM=0 THEN 0 ELSE G.TTKHFCR/P.CTNNUM END TTKHFCR");
strSql.Append(",CASE WHEN P.CTNNUM=0 THEN 0 ELSE G.TTLCR/P.CTNNUM END TTLCR,CASE WHEN P.CTNNUM=0 THEN 0 ELSE G.TTLDR/P.CTNNUM END TTLDR,PD.LINKBSNO,PD.PCKGS,PD.PCCTNNUM");
strSql.Append(",P.SALE,P.CUSTSERVICE from (select BSNO,LINKBSNO,SUM(KGS)PCKGS,SUM(CTNNUM) PCCTNNUM from op_truck_bulk_pc_detail pd GROUP BY BSNO,LINKBSNO) PD");
strSql.Append(" LEFT JOIN op_truck_bulk_pc p ON (P.BSNO=pD.BSNO) LEFT JOIN v_op_gain_dr_INV_truck G ON (G.BSNO=p.BSNO)) P ON (P.LINKBSNO=B.BSNO)");