varCompanyName=T_ALL_DA.GetStrSQL("FULLNAME","select FULLNAME from [company] where GID='"+Convert.ToString(Session["COMPANYID"]).ToString().Trim()+"'");
varCompanyName=T_ALL_DA.GetStrSQL("FULLNAME","select FULLNAME from [company] where GID='"+Convert.ToString(Session["COMPANYID"]).ToString().Trim()+"'");
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(",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("((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=B.CUSTOMERNAME) AS CUSTOMERNAMEREF");
strSql.Append(" ,(SELECT DESCRIPTION FROM info_client WHERE SHORTNAME=P.TRUCKER) AS TRUCKERREF");
strSql.Append(" ,(SELECT DESCRIPTION FROM info_client WHERE SHORTNAME=P.TRUCKER) AS TRUCKERREF,ISTEMPLATENAME");
strSql.Append(" FROM op_truck_bulk B LEFT JOIN v_op_gain_dr_INV_truck G ON (G.BSNO=B.BSNO)");
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(" 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(" 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(",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(",P.SALE,P.CUSTSERVICE,P.KGS TTLPCKGS,CASE WHEN ISNULL(P.TEMPLATENAME,'')='' THEN '否' ELSE CASE WHEN (SELECT isnull(COUNT(1),0) FROM Cust_Truck_feetemplate WHERE CONTRACTNO<>'' AND TEMPLATENAME=P.TEMPLATENAME)>0 THEN '是' ELSE '否' END END ISTEMPLATENAME ");
strSql.Append(" 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(" 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(",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(",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("-((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=B.CUSTOMERNAME) AS CUSTOMERNAMEREF");
strSql.Append(" ,(SELECT DESCRIPTION FROM info_client WHERE SHORTNAME=P.TRUCKER) AS TRUCKERREF");
strSql.Append(" ,(SELECT DESCRIPTION FROM info_client WHERE SHORTNAME=P.TRUCKER) AS TRUCKERREF,ISTEMPLATENAME");
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(" 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(" 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 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(",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(",CASE WHEN ISNULL(P.TEMPLATENAME,'')='' THEN '否' ELSE CASE WHEN (SELECT isnull(COUNT(1),0) FROM Cust_Truck_feetemplate WHERE CONTRACTNO<>'' AND TEMPLATENAME=P.TEMPLATENAME)>0 THEN '是' ELSE '否' END END ISTEMPLATENAME ");
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(",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(" 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)");