IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_35_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_35_data]
GO
CREATE PROCEDURE report_35_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@qry_branch 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)
declare @qrydate varchar(10)
SELECT @qrydate = CONVERT(varchar(10), DATEADD(DAY,-1,GETDATE()) , 120)

create table #temp1 (
    ord_branch varchar(100),
    ord_ysl decimal(18,2) default 0,
    ord_wcl decimal(18,2) default 0,
    ord_wcv decimal(18,2) default 0,
    ord_xlys int default 0,
    ord_xlwc int default 0,
    ord_xlwcv decimal(18,2) default 0,
    ord_dm decimal(18,2) default 0,
    ord_zdmsrwcv decimal(18,2) default 0,
    ord_gjv decimal(18,2) default 0,
    ord_pjpb decimal(18,2) default 0,
    ord_pjmsrwcv decimal(18,2) default 0
)

create table #temp2 (
    ord_branch varchar(100),
    ord_zmsr decimal(10,2) default 0,
    ord_zttb decimal(10,2) default 0,
    ord_tdtb decimal(10,2) default 0,
    ord_dm decimal(10,2) default 0,
    ord_zdmsr decimal(10,2) default 0,
    ord_pb decimal(10,2) default 0,
    ord_pjmsr decimal(10,2) default 0
)

if @qry_branch = '佳海总公司'
begin

   insert into #temp1(ord_branch,
                   ord_ysl,ord_wcl ,ord_wcv ,
                   ord_xlys ,ord_xlwc ,ord_xlwcv,ord_dm ,ord_zdmsrwcv,
                   ord_gjv ,ord_pjpb ,ord_pjmsrwcv )
    select [分公司名称]
      ,[总毛收入预算量],[总毛收入完成量],[总毛收入完成率]*100
      ,[终端预算量],[终端完成量],[终端销量完成率]*100,[终端单毛],[终端毛收入完成率]*100
      ,[增值产品跟机率]*100,[配件配比],[配件毛收入完成率]*100
    from [DBTC].[datas].[dbo].[dd_date] 
    where [数据日期] = @qrydate and [分公司名称] is not null 
      and [区域] is null and [门店号] is null and [工号] is null
      
    insert into #temp2(ord_branch,
                   ord_zmsr,
                   ord_zttb,ord_tdtb,ord_dm,ord_zdmsr,
                   ord_pb ,ord_pjmsr )
    select [分公司名称]
      ,[总毛收入同期]*100
      ,[终端整体同期]*100,[终端同店同比]*100,[单毛同期]*100,[终端毛收入同期]*100
      ,[配件配比同期]*100,[配件毛收入同期]*100
    FROM [DBTC].[datas].[dbo].[dd_datetb]
    where [数据日期] = @qrydate and [分公司名称] is not null 
      and [区域] is null and [门店号] is null 
   
end
else 
begin 
    insert into #temp1(ord_branch,
                   ord_ysl,ord_wcl ,ord_wcv ,
                   ord_xlys ,ord_xlwc ,ord_xlwcv,ord_dm ,ord_zdmsrwcv,
                   ord_gjv ,ord_pjpb ,ord_pjmsrwcv )
    select [分公司名称]
      ,[总毛收入预算量],[总毛收入完成量],[总毛收入完成率]*100
      ,[终端预算量],[终端完成量],[终端销量完成率]*100.00,[终端单毛],[终端毛收入完成率]*100
      ,[增值产品跟机率]*100,[配件配比],[配件毛收入完成率]*100
    from [DBTC].[datas].[dbo].[dd_date] 
    where [数据日期] = @qrydate and [分公司名称] = @qry_branch
      and [区域] is null and [门店号] is null and [工号] is null
      
    insert into #temp2(ord_branch,
                   ord_zmsr,
                   ord_zttb,ord_tdtb,ord_dm,ord_zdmsr,
                   ord_pb ,ord_pjmsr )
    select [分公司名称]
      ,[总毛收入同期]*100
      ,[终端整体同期]*100,[终端同店同比]*100,[单毛同期]*100,[终端毛收入同期]*100
      ,[配件配比同期]*100,[配件毛收入同期]*100
    FROM [DBTC].[datas].[dbo].[dd_datetb]
    where [数据日期] = @qrydate and [分公司名称] = @qry_branch
      and [区域] is null and [门店号] is null 
end

select 'tb_report1' tableid ,* from #temp1

select 'tb_report2' tableid ,* from #temp2
END