varct=MsOpTruckBulkDAL.GetRdCount("BSNO<>'"+headData.BSNO+"' AND CUSTNO='"+headData.CUSTNO+"' and CORPID='"+Convert.ToString(Session["COMPANYID"])+"'");
varct=MsOpTruckBulkDAL.GetRdCount("BSNO<>'"+headData.BSNO+"' AND CUSTNO='"+headData.CUSTNO+"' and CORPID='"+Convert.ToString(Session["COMPANYID"])+"'");
varct=MsOpTruckBulkDAL.GetRdCount("BSNO<>'"+headData.BSNO+"' AND CUSTNO='"+headData.CUSTNO+"' and CORPID='"+Convert.ToString(Session["COMPANYID"])+"'");
varisfee=MsOpTruckBulkDAL.GetFeeCount(headData.BSNO," CARGO_GID='"+headData.BSNO+"' and (SETTLEMENT<>0 or INVOICE<>0 and ORDERAMOUNT<>0 and ORDERINVOICE<>0) ");
varisfee=MsOpTruckBulkDAL.GetFeeCount(headData.BSNO," CARGO_GID='"+headData.BSNO+"' and (SETTLEMENT<>0 or INVOICE<>0 and ORDERAMOUNT<>0 and ORDERINVOICE<>0) ");
varisfee=MsOpTruckBulkDAL.GetFeeCount(headData.BSNO," CARGO_GID='"+headData.BSNO+"' and (SETTLEMENT<>0 or INVOICE<>0 and ORDERAMOUNT<>0 and ORDERINVOICE<>0) ");
varisfee=MsOpTruckBulkDAL.GetFeeCount(headData.BSNO," CARGO_GID='"+headData.BSNO+"' and (SETTLEMENT<>0 or INVOICE<>0 and ORDERAMOUNT<>0 and ORDERINVOICE<>0) ");
strSql.Append(",P.FREIGHT*P.PCKGS PCFREIGHT,P.TAXRATE,P.TTLFREIGHT*P.PCKGS PCTTLFREIGHT,P.TTLOTCR*P.PCKGS TTLOTCR,P.TTLCR*P.PCKGS+(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.KGS*P.PCKGS,2) as numeric(20,2)) END) TTLCR,");
strSql.Append("(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.KGS*P.PCKGS+(P.TTLDR*P.PCKGS),2) as numeric(20,2)) END)-((P.TTLCR*P.PCKGS)+(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.KGS*P.PCKGS,2) as numeric(20,2)) END)) AS PROFIT");
strSql.Append(",CASE WHEN (case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.KGS*P.PCKGS,2) as numeric(20,2)) END)=0 THEN 0 ELSE cast(round((((case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.KGS*P.PCKGS+(P.TTLDR*P.PCKGS),2) as numeric(20,2)) END)-");
strSql.Append("((P.TTLCR*P.PCKGS)+(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.KGS*P.PCKGS,2) as numeric(20,2)) END)))/(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.KGS*P.PCKGS+(P.TTLDR*P.PCKGS),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 op_truck_bulk 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,P.REMARK,CASE WHEN P.KGS=0 THEN 0 ELSE P.FREIGHT/P.KGS END FREIGHT,");
strSql.Append(" P.TAXRATE,CASE WHEN P.KGS=0 THEN 0 ELSE P.TTLFREIGHT/P.KGS END TTLFREIGHT,CASE WHEN P.KGS=0 THEN 0 ELSE G.TTLOTCR/P.KGS END TTLOTCR,CASE WHEN P.KGS=0 THEN 0 ELSE G.TTKHFCR/P.KGS END TTKHFCR");
strSql.Append(",CASE WHEN P.KGS=0 THEN 0 ELSE G.TTLCR/P.KGS END TTLCR,CASE WHEN P.KGS=0 THEN 0 ELSE G.TTLDR/P.KGS END TTLDR,PD.LINKBSNO,PD.PCKGS,PD.PCCTNNUM,PD.PCPKGS,PD.PCCBM,PD.PCNETWEIGHT ");
strSql.Append(",P.SALE,P.CUSTSERVICE,P.KGS TTLPCKGS from (select BSNO,LINKBSNO,SUM(KGS)PCKGS,SUM(CTNNUM) PCCTNNUM,SUM(PKGS) PCPKGS,SUM(CBM) PCCBM,SUM(NETWEIGHT) PCNETWEIGHT 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(" where B.transtype='普运' ");
if(!string.IsNullOrEmpty(condition))
{
strSql.Append(" and "+condition);
}
strSql.Append(") B WHERE TRANSSTATUS IN ('审核通过','已到货','已回单','已回单','在途') ");
strSql.Append("SELECT sum(PCKGS) PCKGS,SUM(PCPKGS) PCPKGS,SUM(PCCBM) PCCBM,SUM(PCNETWEIGHT) PCNETWEIGHT,sum(FREIGHT) FREIGHT,sum(TTLDR) TTLDR,sum(TTLCR) TTLCR,SUM(PROFIT) PROFIT,SUM(PCFREIGHT) PCFREIGHT,SUM(PCTTLFREIGHT) PCTTLFREIGHT,SUM(TTLOTCR) TTLOTCR,SUM(TTLOTDR) TTLOTDR,SUM(TTKHFDR) TTKHFDR,SUM(TTKHFCR) TTKHFCR,CASE WHEN SUM(TTLDR)=0 THEN 0 ELSE cast(round(sum(PROFIT)/sum(TTLDR)*100,2) AS numeric(20,2)) END PROFITRATE,sum(DELAYDAYS) DELAYDAYS");
strSql.Append(" FROM (SELECT B.BSNO,B.CUSTOMERNAME,B.ORDERNO,B.ORDERNO1,B.GOODSNAME,B.PORTLOAD,B.PORTDISCHARGE,B.ETD,B.ETA,B.TRANSTYPE,B.PKGS,B.KGS,B.NETWEIGHT,B.CNTRTOTAL ");
strSql.Append(",case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(B.FREIGHT/B.KGS*P.PCKGS,2) as numeric(20,2)) END FREIGHT");
strSql.Append(",case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.KGS*P.PCKGS+(P.TTLDR*P.PCKGS),2) as numeric(20,2)) END TTLDR");
strSql.Append(",case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLOTDR/B.KGS*P.PCKGS,2) as numeric(20,2)) END TTLOTDR");
strSql.Append(",(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTKHFDR/B.KGS*P.PCKGS,2) as numeric(20,2)) END) TTKHFDR");
strSql.Append(",(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.KGS*P.PCKGS,2) as numeric(20,2)) END)+(P.TTKHFCR*P.PCKGS) TTKHFCR");
strSql.Append(",P.FREIGHT*P.PCKGS PCFREIGHT,P.TAXRATE,P.TTLFREIGHT*P.PCKGS PCTTLFREIGHT,P.TTLOTCR*P.PCKGS TTLOTCR, P.TTLCR*P.PCKGS+(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.KGS*P.PCKGS,2) as numeric(20,2)) END) TTLCR,");
strSql.Append("(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.KGS*P.PCKGS+(P.TTLDR*P.PCKGS),2) as numeric(20,2)) END)-((P.TTLCR*P.PCKGS)+(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.KGS*P.PCKGS,2) as numeric(20,2)) END)) AS PROFIT");
strSql.Append(",CASE WHEN (case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.KGS*P.PCKGS,2) as numeric(20,2)) END)=0 THEN 0 ELSE cast(round((((case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.KGS*P.PCKGS+(P.TTLDR*P.PCKGS),2) as numeric(20,2)) END)");
strSql.Append("-((P.TTLCR*P.PCKGS)+(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTKHFCR/B.KGS*P.PCKGS,2) as numeric(20,2)) END)))/(case when ISNULL(B.KGS,0)=0 THEN 0 ELSE cast(round(G.TTLDR/B.KGS*P.PCKGS+(P.TTLDR*P.PCKGS),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 op_truck_bulk 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,P.REMARK,CASE WHEN P.KGS=0 THEN 0 ELSE P.FREIGHT/P.KGS END FREIGHT,");
strSql.Append(" P.TAXRATE,CASE WHEN P.KGS=0 THEN 0 ELSE P.TTLFREIGHT/P.KGS END TTLFREIGHT,CASE WHEN P.KGS=0 THEN 0 ELSE G.TTLOTCR/P.KGS END TTLOTCR,CASE WHEN P.KGS=0 THEN 0 ELSE G.TTKHFCR/P.KGS END TTKHFCR");
strSql.Append(",CASE WHEN P.KGS=0 THEN 0 ELSE G.TTLCR/P.KGS END TTLCR,CASE WHEN P.KGS=0 THEN 0 ELSE G.TTLDR/P.KGS END TTLDR,PD.LINKBSNO,PD.PCKGS,PD.PCCTNNUM,PD.PCPKGS,PD.PCCBM,PD.PCNETWEIGHT");
strSql.Append(",P.SALE,P.CUSTSERVICE,P.KGS TTLPCKGS from (select BSNO,LINKBSNO,SUM(KGS)PCKGS,SUM(CTNNUM) PCCTNNUM,SUM(PKGS) PCPKGS,SUM(CBM) PCCBM,SUM(NETWEIGHT) PCNETWEIGHT 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(" where B.transtype='普运' ");
if(!string.IsNullOrEmpty(condition))
{
strSql.Append(" and "+condition);
}
strSql.Append(") B WHERE TRANSSTATUS IN ('审核通过','已到货','已回单','已回单','在途') ");
strSql.Append(" FROM (SELECT B.BSNO,B.CUSTOMERNAME,B.ORDERNO,B.ORDERNO1,B.GOODSNAME,B.PORTLOAD,B.PORTDISCHARGE,B.ETD,B.ETA,B.TRANSTYPE,B.PKGS,B.KGS,B.NETWEIGHT,B.CNTRTOTAL ");
strSql.Append(",case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(B.FREIGHT/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END FREIGHT");
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 TTLDR");
strSql.Append(",case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTLOTDR/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END TTLOTDR");
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.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)");
strSql.Append(" where B.transtype='集装箱' ");
if(!string.IsNullOrEmpty(condition))
{
strSql.Append(" and "+condition);
}
strSql.Append(") B WHERE TRANSSTATUS IN ('审核通过','已到货','已回单','已回单','在途') ");
strSql.Append("SELECT sum(FREIGHT) FREIGHT,sum(TTLDR) TTLDR,sum(TTLCR) TTLCR,SUM(PROFIT) PROFIT,SUM(PCFREIGHT) PCFREIGHT,SUM(PCTTLFREIGHT) PCTTLFREIGHT,SUM(TTLOTCR) TTLOTCR,SUM(TTLOTDR) TTLOTDR,SUM(TTKHFDR) TTKHFDR,SUM(TTKHFCR) TTKHFCR,CASE WHEN SUM(TTLDR)=0 THEN 0 ELSE cast(round(sum(PROFIT)/sum(TTLDR)*100,2) AS numeric(20,2)) END PROFITRATE,sum(DELAYDAYS) DELAYDAYS ");
strSql.Append(" FROM (SELECT B.BSNO,B.CUSTOMERNAME,B.ORDERNO,B.ORDERNO1,B.GOODSNAME,B.PORTLOAD,B.PORTDISCHARGE,B.ETD,B.ETA,B.TRANSTYPE,B.PKGS,B.KGS,B.NETWEIGHT,B.CNTRTOTAL ");
strSql.Append(",case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(B.FREIGHT/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END FREIGHT");
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 TTLDR");
strSql.Append(",case when ISNULL(B.CTNNUM,0)=0 THEN 0 ELSE cast(round(G.TTLOTDR/B.CTNNUM*P.PCCTNNUM,2) as numeric(20,2)) END TTLOTDR");
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(",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.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)");