IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_42_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_42_data]
GO
CREATE PROCEDURE report_42_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@sf_tjrq varchar(512)
,@sf_jsrq varchar(512)

AS
BEGIN
 set nocount on 

declare @dataIds table(objid int)
if @objids!='_NONE' and @objids!='_none_' and @objids!='' 
insert into @dataIds(objid)
select value from dbo.fn_split_int(@objids)
create table #temp (
tableid varchar(10),
ord_branch varchar(50),--公司名称
ord_success int DEFAULT 0,--成功数
ord_lose int DEFAULT 0,--失败数
ord_sbzb varchar(50),--百分比失败占比
ord_sbzbd decimal(18,0),--小数失败占比
ord_dsh int DEFAULT 0,--待审核
ord_mdzs int DEFAULT 0,--门店总数
ord_tdmds int DEFAULT 0,--提单门店数
ord_tdmdzb varchar(50),--提单门店百分比
ord_tdmdzbd decimal(18,0) DEFAULT 0.0,--提单门店小数占比
ord_companyid int DEFAULT 0--公司id
)

insert into #temp(tableid, ord_branch,ord_companyid)
select DISTINCT 'tb_report1' tableid,ord_branch,ord_bcid
from order_270
where ord_status != '已作废' 
and charindex('临',ord_no) = 0
and ord_date >= @sf_tjrq + ' 00:00:00' 
and ord_date <= @sf_jsrq + ' 23:59:59'
group by ord_branch,ord_bcid

update #temp set ord_success = (select count(*) from order_270 
where ord_bcid = #temp.ord_companyid 
and ord_status = '已送' 
and charindex('临',ord_no) = 0
and ord_date >= @sf_tjrq + ' 00:00:00' 
and ord_date <= @sf_jsrq + ' 23:59:59')



update #temp set ord_lose = (select count(*) from order_270 
where ord_bcid = #temp.ord_companyid 
and ord_status = '不能送' 
and charindex('临',ord_no) = 0
and ord_date >= @sf_tjrq + ' 00:00:00' 
and ord_date <= @sf_jsrq + ' 23:59:59')



update #temp set ord_sbzbd =  CONVERT(DECIMAL(18,0),round((1.0*ord_lose)/(1.0*ord_success),5,5)*100)


update #temp set ord_sbzb = convert(varchar(20),ord_sbzbd)+'%'




update #temp set ord_dsh = (select count(*) from order_270 
where ord_bcid = #temp.ord_companyid 
and ord_status = '待审核' 
and charindex('临',ord_no) = 0
and ord_date >= @sf_tjrq + ' 00:00:00' 
and ord_date <= @sf_jsrq + ' 23:59:59')


update #temp set ord_mdzs =(
select count(dp_id) from mps_depart a
    where dp_isdepart = 1 and  #temp.ord_companyid=a.dp_bcid and a.dp_status != '关店'
)



update #temp set ord_tdmds = (select count(distinct ord_createdepart)  from order_270 
where ord_bcid = #temp.ord_companyid 
and charindex('临',ord_no) = 0
and ord_date >= @sf_tjrq + ' 00:00:00' 
and ord_date <= @sf_jsrq + ' 23:59:59')



update #temp set ord_tdmdzbd =  CONVERT(DECIMAL(18,0),round((1.0*ord_tdmds)/(1.0*ord_mdzs),5,5)*100)


update #temp set ord_tdmdzb = convert(varchar(20),ord_tdmdzbd)+'%'



select * from #temp

END