/*非油销售报表*/
if exists(select * from sysobjects where name='p_rpt_salerpt' and xtype='p')
  drop procedure p_rpt_salerpt
go

create procedure p_rpt_salerpt
(
  @pfinacode varchar(1000),
  @porgcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pclscode varchar(1000),
  @pplucode varchar(2000),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @stmpdate varchar(10)
  declare @sfinacode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @sclscode varchar(1000)
  declare @splucode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
  set @sfinacode=replace(@pfinacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @sclscode=replace(@pclscode,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'

  if exists(select name from tempdb..sysobjects where name='##tmpgclass' and xtype='u') 
    drop table ##tmpgclass
  create table ##tmpgclass (
    clscode varchar(10),clsname varchar(20),uppercode1 varchar(10),uppername1 varchar(20),uppercode2 varchar(10),uppername2 varchar(20))
  
  if exists(select name from tempdb..sysobjects where name='##tmpplusale' and xtype='u') 
    drop table ##tmpplusale
  create table ##tmpplusale (
    accdate varchar(10),orgcode varchar(10),orgname varchar(30),plucode varchar(20),pluname varchar(40),
    supcode varchar(15),supname varchar(100),counts numeric(19,4),cost numeric(19,4),netcost numeric(19,4),total numeric(19,4),
    amount numeric(19,4),netamount numeric(19,4),normaldsc numeric(19,4),vipdsc numeric(19,4),pricedsc numeric(19,4),
    grossprofit numeric(19,4),netprofit numeric(19,4),adjustprofit numeric(19,4),netadjustprofit numeric(19,4),
    jtaxrate int,staxrate int,prctr varchar(10),finacode varchar(10),kccount numeric(19,4),price numeric(19,4),
    clscode varchar(10),clsname varchar(20),uppercode1 varchar(10),uppername1 varchar(20),uppercode2 varchar(10),uppername2 varchar(20))

  insert into ##tmpgclass (clscode,clsname,uppercode1,uppercode2)
    select clscode,clsname,uppercode1,uppercode2 from t_gclass where clslevel=3
  update a set uppername1=b.clsname
    from ##tmpgclass a,t_gclass b where a.uppercode1=b.clscode
  update a set uppername2=b.clsname
    from ##tmpgclass a,t_gclass b where a.uppercode2=b.clscode

  set @stmpdate=@sbgndate
  while substring(@stmpdate,1,7)<=substring(@senddate,1,7)
  begin
    if exists(select name from sysobjects where name='t_plusale_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2) and xtype='u')
    begin
      set @ssql='insert into ##tmpplusale(accdate,orgcode,orgname,plucode,pluname,supcode,supname,counts,cost,netcost,total,amount,netamount,normaldsc,vipdsc,pricedsc,grossprofit,netprofit,
                                          adjustprofit,netadjustprofit,jtaxrate,staxrate,finacode,kccount,price,clscode,clsname,uppercode1,uppername1,uppercode2,uppername2,prctr)
                   select a.accdate,a.orgcode,b.orgname,a.plucode,d.pluname,a.supcode,c.supname,a.counts,a.cost,a.netcost,a.total,a.amount,a.netamount,a.normaldsc,a.vipdsc,a.pricedsc,a.grossprofit,a.netprofit,
                          a.adjustprofit,a.netadjustprofit,a.jtaxrate,a.staxrate,b.finacode,e.gcount+e.ccount,e.price,d.clscode,f.clsname,f.uppercode1,f.uppername1,f.uppercode2,f.uppername2,
                          ''P''+b.finacode+''1''+case when b.orgtype=''2'' then b.param1 else b.orgcode end
                     from t_plusale_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' a
                          left join t_shop b on a.orgcode=b.orgcode
                          left join t_supplier c on a.supcode=c.supcode
                          left join t_plu d on a.plucode=d.plucode
                          left join t_plu_org e on a.plucode=e.plucode and a.orgcode=e.orgcode
                          left join ##tmpgclass f on d.clscode=f.clscode
                    where accdate between '''+@sbgndate+''' and '''+@senddate+'''
                      and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
      if @sfinacode<>''
        set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.orgcode and finacode in ('''+@sfinacode+'''))'
      if @sorgcode<>''
        set @ssql=@ssql+' and a.orgcode in ('''+@sorgcode+''')'
      if @sclscode<>''
        set @ssql=@ssql+' and exists(select * from t_gclass where clscode=d.clscode and uppercode1 in ('''+@sclscode+'''))'
      if @splucode<>''
        set @ssql=@ssql+' and a.plucode in ('''+@splucode+''')'
      exec(@ssql)
    end    
    set @stmpdate=convert(varchar(10),dateadd(month,1,@stmpdate),120)
  end

  set @ssql='select finacode,orgcode,orgname,accdate,plucode,pluname,
             sum(counts) counts,sum(total) total,sum(amount) as amount,
             sum(normaldsc+vipdsc+pricedsc) as dsc,sum(netamount) as netamount,staxrate,
             sum(amount-netamount) as taxamount,sum(normaldsc+vipdsc+pricedsc)/(1+jtaxrate/100.0) as netdsc,
             sum(normaldsc+vipdsc+pricedsc)-sum(normaldsc+vipdsc+pricedsc)/(1+jtaxrate/100.0) as taxdsc,
             sum(cost+adjustprofit) as cost,sum(netcost+netadjustprofit) as netcost,sum(grossprofit-adjustprofit) as grossprofit,
             case when sum(amount)=0 then 0 else sum(grossprofit-adjustprofit)/sum(amount)*100 end as profitrate,
             ''FP'' as fptype,uppercode2,uppername2,uppercode1,uppername1,clscode,clsname,supcode,supname,prctr,kccount,price
             from ##tmpplusale where 0=0
             group by finacode,orgcode,orgname,accdate,plucode,pluname,jtaxrate,staxrate,uppercode2,uppername2,
                      uppercode1,uppername1,clscode,clsname,supcode,supname,prctr,kccount,price
                      order by finacode,orgcode,accdate,plucode'
  exec(@ssql)

  if exists(select name from tempdb..sysobjects where name='##tmpgclass' and xtype='u')
    drop table ##tmpgclass
  if exists(select name from tempdb..sysobjects where name='##tmpplusale' and xtype='u')
    drop table ##tmpplusale
end
go

/*非油支付报表*/
if exists(select * from sysobjects where name='p_rpt_tendrpt' and xtype='p')
  drop procedure p_rpt_tendrpt
go

create procedure p_rpt_tendrpt
(
  @porgcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pclscode varchar(1000),
  @pqryall varchar(1),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @stmpdate varchar(10)
  declare @sorgcode varchar(1000)
  declare @sclscode varchar(1000)
  declare @sadd varchar(4000)
  declare @tendcode varchar(4)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
  set @sorgcode=replace(@porgcode,',',''',''')
  set @sclscode=replace(@pclscode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
  
  if exists(select name from tempdb..sysobjects where name='##tmppaydeilysum' and xtype='u') 
    drop table ##tmppaydeilysum
  create table ##tmppaydeilysum (
    orgcode varchar(10),accdate varchar(10),clscode varchar(10),tendcode varchar(4),amount numeric(19,4))
  if exists(select name from tempdb..sysobjects where name='##tmppaydeily' and xtype='u') 
    drop table ##tmppaydeily
  create table ##tmppaydeily (
    finacode varchar(10),finaname varchar(40),orgcode varchar(10),orgname varchar(30),accdate varchar(10),clscode varchar(10),clsname varchar(20),
    amount numeric(19,4))
  
  set @stmpdate=@sbgndate
  while substring(@stmpdate,1,7)<=substring(@senddate,1,7)
  begin
    if exists(select name from sysobjects where name='t_int_flow_pay_deily_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2) and xtype='u')
    begin
	  set @ssql='update a set amount=b.amount
                   from ##tmppaydeilysum a,(select business_date,werks,clscode,mop_id,sum(amount) as amount
                   from t_int_flow_pay_deily_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                  where business_date between '''+@sbgndate+''' and '''+@senddate+'''
                    and clscode not in (''1001'',''1002'') and left(werks,2)<>''2D''
                  group by business_date,werks,clscode,mop_id) b where a.accdate=b.business_date and a.orgcode=b.werks and a.clscode=b.clscode and a.tendcode=b.mop_id'
      exec(@ssql)
	  
      set @ssql='insert into ##tmppaydeilysum(orgcode,accdate,clscode,tendcode,amount)
                   select werks,business_date,clscode,mop_id,sum(amount)
                     from t_int_flow_pay_deily_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where business_date between '''+@sbgndate+''' and '''+@senddate+'''
                      and clscode not in (''1001'',''1002'') and left(werks,2)<>''2D''
                      and not exists(select * from ##tmppaydeilysum where accdate=t.business_date and orgcode=t.werks and clscode=t.clscode and tendcode=t.mop_id)
                      and exists(select * from t_user_org where orgcode=t.werks and usercode='''+@ps_SysUserCode+''')'
      if @sorgcode<>''
        set @ssql=@ssql+' and werks in ('''+@sorgcode+''')'
      if @sclscode<>''
        set @ssql=@ssql+' and clscode in ('''+@sclscode+''')'
      if isnull(@pqryall,'')='0'
        set @ssql=@ssql+' and exists(select * from t_shop where orgcode=t.werks and overdate>=t.business_date)'
      set @ssql=@ssql+' group by werks,business_date,clscode,mop_id'
      exec(@ssql)
      
      set @ssql='update a set amount=b.amount
                   from ##tmppaydeily a,(select business_date,werks,clscode,sum(amount) as amount
                   from t_int_flow_pay_deily_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                  where business_date between '''+@sbgndate+''' and '''+@senddate+'''
                    and clscode not in (''1001'',''1002'') and left(werks,2)<>''2D''
                  group by business_date,werks,clscode) b where a.accdate=b.business_date and a.orgcode=b.werks and a.clscode=b.clscode'
      exec(@ssql)
      
      set @ssql='insert into ##tmppaydeily(finacode,finaname,orgcode,orgname,accdate,clscode,clsname,amount)
                   select b.finacode,c.finaname,a.werks,b.orgname,a.business_date,a.clscode,d.clsname,sum(a.amount)
                     from t_int_flow_pay_deily_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' a
                     left join t_shop b on a.werks=b.orgcode
                     left join t_financial_body c on b.finacode=c.finacode
                     left join t_gclass d on a.clscode=d.clscode
                    where a.business_date between '''+@sbgndate+''' and '''+@senddate+'''
                      and a.clscode not in (''1001'',''1002'') and left(werks,2)<>''2D''
                      and not exists(select * from ##tmppaydeily where orgcode=a.werks and accdate=a.business_date and clscode=a.clscode)
                      and exists(select * from t_user_org where orgcode=a.werks and usercode='''+@ps_SysUserCode+''')'
      if @sorgcode<>''
        set @ssql=@ssql+' and b.orgcode in ('''+@sorgcode+''')'
      if @sclscode<>''
        set @ssql=@ssql+' and a.clscode in ('''+@sclscode+''')'
      if isnull(@pqryall,'')='0'
        set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.werks and overdate>=a.business_date)'
      set @ssql=@ssql+' group by b.finacode,c.finaname,a.werks,b.orgname,a.business_date,a.clscode,d.clsname'
      exec(@ssql)
    end    
    set @stmpdate=convert(varchar(10),dateadd(month,1,@stmpdate),120)
  end
  
  set @sadd=''
  set @ssql=''
  declare cur cursor for select tendcode from t_tend order by tendcode
  open cur
  fetch next from cur into @tendcode
  while @@fetch_status=0
  begin
    set @sadd=@sadd+'alter table ##tmppaydeily add tend'+@tendcode+' numeric(19,4)'+char(10)
    set @ssql=@ssql+'update a set tend'+@tendcode+'=b.amount from ##tmppaydeily a,##tmppaydeilysum b where a.orgcode=b.orgcode and a.accdate=b.accdate and a.clscode=b.clscode and b.tendcode='+@tendcode+char(10)
    fetch next from cur into @tendcode
  end
  close cur
  deallocate cur
  exec(@sadd)
  exec(@ssql)

  select * from ##tmppaydeily order by finacode,orgcode,accdate,clscode

  if exists(select name from tempdb..sysobjects where name='##tmppaydeilysum' and xtype='u')
    drop table ##tmppaydeilysum
  if exists(select name from tempdb..sysobjects where name='##tmppaydeily' and xtype='u')
    drop table ##tmppaydeily
end
go

/*油站销售日报*/
if exists(select * from sysobjects where name='p_rpt_dayrpt' and xtype='p')
  drop procedure p_rpt_dayrpt
go

create procedure p_rpt_dayrpt
(
  @pfinacode varchar(1000),
  @porgcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pisdiff varchar(1),
  @pqryall varchar(1),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @stmpdate varchar(10)
  declare @sfinacode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
  set @sfinacode=replace(@pfinacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
	
  if exists(select name from tempdb..sysobjects where name='##tmpdayrpt' and xtype='u') 
    drop table ##tmpdayrpt
  create table ##tmpdayrpt (
    accdate varchar(10),orgcode varchar(10),orgname varchar(30),finacode varchar(10),finaname varchar(40),
    amount numeric(19,4),tendamount numeric(19,4))

  set @stmpdate=@sbgndate
  while substring(@stmpdate,1,7)<=substring(@senddate,1,7)
  begin
    if exists(select name from sysobjects where name='t_flow_total_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2) and xtype='u')
    begin
	  set @ssql='update a set amount=amount+b.plutotal
                   from ##tmpdayrpt a,(select accdate,orgcode,sum(plutotal) as plutotal
                   from t_flow_item_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+'
                  where accdate between '''+@sbgndate+''' and '''+@senddate+'''
                    and clscode not in (''1001'',''1002'') and left(orgcode,2)<>''2D''
                  group by accdate,orgcode) b where a.accdate=b.accdate and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='insert into ##tmpdayrpt(accdate,orgcode,amount)
                   select accdate,orgcode,sum(plutotal)
                     from t_flow_item_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' a
                    where accdate between '''+@sbgndate+''' and '''+@senddate+'''
                      and clscode not in (''1001'',''1002'') and left(orgcode,2)<>''2D''
                      and not exists(select * from ##tmpdayrpt where accdate=a.accdate and orgcode=a.orgcode)
                      and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
      if @sorgcode<>''
        set @ssql=@ssql+' and a.orgcode in ('''+@sorgcode+''')'
      if isnull(@pqryall,'')='0'
        set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.orgcode and overdate>=a.accdate)'
      set @ssql=@ssql+' group by accdate,orgcode'
      exec(@ssql)
    end
    if exists(select name from sysobjects where name='t_int_flow_pay_deily_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2) and xtype='u')
    begin
      set @ssql='update a set tendamount=b.amount
                   from ##tmpdayrpt a,(select business_date,werks,sum(amount) as amount
                   from t_int_flow_pay_deily_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                  where business_date between '''+@sbgndate+''' and '''+@senddate+'''
                    and clscode not in (''1001'',''1002'') and left(werks,2)<>''2D''
                  group by business_date,werks) b where a.accdate=b.business_date and a.orgcode=b.werks'
      exec(@ssql)
      
      set @ssql='insert into ##tmpdayrpt(accdate,orgcode,amount,tendamount)
                   select business_date,werks,0,sum(amount)
                     from t_int_flow_pay_deily_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where business_date between '''+@sbgndate+''' and '''+@senddate+'''
                      and clscode not in (''1001'',''1002'') and left(werks,2)<>''2D''
                      and not exists(select * from ##tmpdayrpt where accdate=t.business_date and orgcode=t.werks)
                      and exists(select * from t_user_org where orgcode=t.werks and usercode='''+@ps_SysUserCode+''')'
      if @sorgcode<>''
        set @ssql=@ssql+' and werks in ('''+@sorgcode+''')'
      if isnull(@pqryall,'')='0'
        set @ssql=@ssql+' and exists(select * from t_shop where orgcode=t.werks and overdate>=t.business_date)'
      set @ssql=@ssql+' group by business_date,werks'
      exec(@ssql)
    end    
    set @stmpdate=convert(varchar(10),dateadd(month,1,@stmpdate),120)
  end
  
  if @pqryall='1'
  begin
    set @ssql='update a set amount=amount+b.plutotal
                 from ##tmpdayrpt a,(select accdate,orgcode,sum(plutotal) as plutotal
                 from t_flow_item
                where accdate between '''+@sbgndate+''' and '''+@senddate+'''
                  and clscode not in (''1001'',''1002'') and left(orgcode,2)<>''2D''
                group by accdate,orgcode) b where a.accdate=b.accdate and a.orgcode=b.orgcode'
    exec(@ssql)
  
    set @ssql='insert into ##tmpdayrpt(accdate,orgcode,amount)
                 select accdate,orgcode,sum(plutotal)
                   from t_flow_item a
                  where accdate between '''+@sbgndate+''' and '''+@senddate+'''
                    and clscode not in (''1001'',''1002'') and left(orgcode,2)<>''2D''
                    and not exists(select * from ##tmpdayrpt where accdate=a.accdate and orgcode=a.orgcode)
                    and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
    if @sorgcode<>''
      set @ssql=@ssql+' and a.orgcode in ('''+@sorgcode+''')'
    set @ssql=@ssql+' group by accdate,orgcode'
    exec(@ssql)
  end
  
  update a set finacode=b.finacode,orgname=b.orgname
    from ##tmpdayrpt a,t_shop b where a.orgcode=b.orgcode
  update a set finaname=b.finaname
    from ##tmpdayrpt a,t_financial_body b where a.finacode=b.finacode
  update ##tmpdayrpt set tendamount=isnull(tendamount,0)
    
  set @ssql='select accdate,orgcode,orgname,amount,tendamount,amount-tendamount as diffamount,finacode,finaname
             from ##tmpdayrpt where 0=0'
  if @sfinacode<>''
    set @ssql=@ssql+' and finacode in ('''+@sfinacode+''')'
  if isnull(@pisdiff,'')='1'
    set @ssql=@ssql+' and amount-tendamount<>0'
  
  set @ssql=@ssql+' order by accdate,orgcode'
  exec(@ssql)
  
  if exists(select name from tempdb..sysobjects where name='##tmpdayrpt' and xtype='u')
    drop table ##tmpdayrpt
end
go

/*全网络所有SKU移动明细*/
if exists(select * from sysobjects where name='p_rpt_plubook' and xtype='p')
  drop procedure p_rpt_plubook
go

create procedure p_rpt_plubook
(
  @porgcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pplucode varchar(2000),
  @pbilltype varchar(2),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @stmpdate varchar(10)
  declare @sorgcode varchar(1000)
  declare @splucode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
  set @sorgcode=replace(@porgcode,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
  
  if exists(select name from tempdb..sysobjects where name='##tmpplubook' and xtype='u') 
    drop table ##tmpplubook
  create table ##tmpplubook (
    accdate varchar(10),orgcode varchar(10),plucode varchar(20),billno varchar(20),billtype varchar(1),unitcode varchar(10),
    ywbilltype varchar(1),inbilltype varchar(2),r_billtype varchar(20),tblname varchar(6),serialno int,fscount numeric(19,4),fsnetcost numeric(19,4))

  set @stmpdate=@sbgndate
  while substring(@stmpdate,1,7)<=substring(@senddate,1,7)
  begin
    if exists(select name from sysobjects where name='t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2) and xtype='u')
    begin
      set @ssql='insert into ##tmpplubook(accdate,plucode,orgcode,billno,billtype,ywbilltype,tblname,serialno,fscount,fsnetcost)
                   select distinct accdate,plucode,orgcode,billno,billtype,'''','''+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+''',serialno,
                     case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fscount else fscount end,
                     case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') and serialno <> 21151 then -fsnetcost else fsnetcost end
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' a
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and (billtype=''E'' and fscount<>0 or billtype<>''E'')
                      and billtype not in (''4'',''8'',''Z'')
                      and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
      if @sorgcode='C009'
        set @ssql=@ssql+' and orgcode in (select orgcode from t_shop where finacode<>''2000'')'
      else if @sorgcode<>''
        set @ssql=@ssql+' and orgcode in ('''+@sorgcode+''')'
      if @splucode<>''
        set @ssql=@ssql+' and plucode in ('''+@splucode+''')'
      exec(@ssql)
    end
    set @stmpdate=convert(varchar(10),dateadd(month,1,@stmpdate),120)
  end
  
  set @ssql='insert into ##tmpplubook(accdate,plucode,orgcode,billno,billtype,ywbilltype,tblname,serialno,fscount,fsnetcost)
               select distinct accdate,plucode,orgcode,billno,billtype,'''',''yyyymm'',serialno,
                 case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fscount else fscount end,
                 case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fsnetcost else fsnetcost end
                 from t_gbk_yyyymm a
                where accdate between '''+@sbgndate+''' and '''+@senddate+''' and (billtype=''E'' and fscount<>0 or billtype<>''E'')
                  and billtype not in (''4'',''8'',''Z'')
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sorgcode='C009'
    set @ssql=@ssql+' and orgcode in (select orgcode from t_shop where finacode<>''2000'')'
  else if @sorgcode<>''
    set @ssql=@ssql+' and orgcode in ('''+@sorgcode+''')'
  if @splucode<>''
    set @ssql=@ssql+' and plucode in ('''+@splucode+''')'
  exec(@ssql)
  
  set @ssql='update a set ywbilltype=case when c.finacode<>''2000'' and b.billtype = ''2'' then ''9'' when c.finacode<>''2000'' and b.billtype <> ''2'' then ''8'' else b.billtype end
               from ##tmpplubook a,t_accept_head b,t_shop c
              where a.billno=b.billno and b.orgcode=c.orgcode and a.billtype=''0'''
  exec(@ssql)
  
  set @ssql='update a set ywbilltype=case when c.finacode<>''2000'' and b.billtype = ''2'' then ''9'' when c.finacode<>''2000'' and b.billtype <> ''2'' then ''8'' else b.billtype end
               from ##tmpplubook a,t_return_head b,t_shop c
              where a.billno=b.billno and b.orgcode=c.orgcode and a.billtype=''5'''
  exec(@ssql)

  --跨财务主体的直送验收不产生虚拟配送单台账，动态生成台账，体现在内部销售
  set @ssql='insert into ##tmpplubook(accdate,plucode,orgcode,billno,billtype,ywbilltype,tblname,serialno,fscount,fsnetcost)
               select a.accdate,a.plucode,b.orgcode,b.billno,''E'',a.ywbilltype,a.tblname,a.serialno,a.fscount,a.fsnetcost
                 from ##tmpplubook a,t_dist_head b
                where a.billno=b.remark and a.billtype=''0'' and a.ywbilltype=''9'' and a.orgcode=b.shopcode
                  and not exists(select * from ##tmpplubook where accdate=a.accdate and plucode=a.plucode and orgcode=b.orgcode and billno=b.billno and billtype=''E'')'
  exec(@ssql)
  --跨财务主体的直送退货不产生虚拟配退单台账，动态生成台账，体现在内部销售
  set @ssql='insert into ##tmpplubook(accdate,plucode,orgcode,billno,billtype,ywbilltype,tblname,serialno,fscount,fsnetcost)
               select a.accdate,a.plucode,b.orgcode,b.billno,''E'',a.ywbilltype,a.tblname,a.serialno,a.fscount,a.fsnetcost
                 from ##tmpplubook a,t_dist_head b
                where a.billno=b.billno and a.billtype=''5'' and a.ywbilltype=''9'' and a.orgcode=b.shopcode
                  and not exists(select * from ##tmpplubook where accdate=a.accdate and plucode=a.plucode and orgcode=b.orgcode and billno=b.billno and billtype=''E'')'
  exec(@ssql)
  --跨财务主体的配送单，体现在内部销售
  set @ssql='update a set ywbilltype=''9''
               from ##tmpplubook a,t_dist_head b,t_shop c
              where a.billno=b.billno and b.shopcode=c.orgcode and a.billtype=''E'' and c.finacode<>''2000'''
  exec(@ssql)

/* *
 * 采购验收：中央仓采购（C001）、油站直采、子公司油站向中央仓采购（配送验收 ywbilltype='8'）、子公司油站直采（ywbilltype='9'）、中央仓向供应商的虚拟采购（ywbilltype='9'）
 * 采购退货：中央仓采购退货（C002)、油站直采退货、子公司油站向中央仓退货（配送退货 ywbilltype='8'）、子公司油站直退（ywbilltype='9'）、中央仓向供应商的虚拟退货（ywbilltype='9'）
 * 配送入库：中央仓收到油站退配(C002)、油站收到中央仓配送（集团内销售生成的配送单不在此处）
 * 配送出库：中央仓向油站配送(C001/C002)、油站向中央仓退配（集团内销售生成的配送单不在此处）
 * 调拨：调入站（正数）、调出站（负数）
 * 销售：油站销售（销售-负数/销售退货-正数）、销售补录（正向数据-负数/反向数据-正数）、中央仓批发销售（负数）、中央仓批发销售退货（正数）
 * 内部销售：跨财务主体的配送单（ywbilltype='9'）
 * 盘点：临时盘点、月度盘点（盘盈-正数/盘亏-负数）
 * 报损：报损（负数）
 * 领用：领用（负数）
 * 加工用料：原料成本（负数）
 * 加工产出：产成品成本（正数）
 * 移库：中央仓入库方（正数）、中央仓出库方（负数）
 * */
  set @ssql='update ##tmpplubook set
               inbilltype=case when billtype=''0'' and ywbilltype in (''0'',''1'',''2'') then ''01''
                               when billtype=''0'' and ywbilltype =''8'' then ''01''
                               when billtype=''0'' and ywbilltype=''9'' then ''15''
                               when billtype=''5'' and ywbilltype in (''0'',''2'') then ''02''
                               when billtype=''5'' and ywbilltype =''8'' then ''02''
                               when billtype=''5'' and ywbilltype=''9'' then ''16''
                               when billtype=''E'' and fscount>0 and ywbilltype<>''9'' or billtype=''0'' and ywbilltype=''3'' then ''03''
                               when billtype=''E'' and fscount<0 and ywbilltype<>''9'' or billtype=''5'' and ywbilltype=''3'' then ''04''
                               when billtype=''F'' then ''05'' when billtype in (''6'',''A'',''B'') then ''06''
                               when billtype=''E'' and ywbilltype=''9'' then ''07'' when billtype=''2'' then ''08''
                               when billtype=''S'' then ''09'' when billtype=''7'' then ''10''
                               when billtype=''C'' then ''11'' when billtype=''D'' then ''12''
                               when billtype=''G'' then ''13'' when billtype=''4'' then ''14''
                          else ''99'' end,
               r_billtype=case when billtype=''0'' and ywbilltype in (''0'',''1'',''2'',''8'') then ''采购验收''
                               when billtype=''0'' and ywbilltype=''9'' then ''直送采购验收''
                               when billtype=''5'' and ywbilltype in (''0'',''2'',''8'') then ''采购退货''
                               when billtype=''5'' and ywbilltype=''9'' then ''直送采购退货''
                               when billtype=''E'' and fscount>0 and ywbilltype<>''9'' or billtype=''0'' and ywbilltype=''3'' then ''配送入库''
                               when billtype=''E'' and fscount<0 and ywbilltype<>''9'' or billtype=''5'' and ywbilltype=''3'' then ''配送出库''
                               when billtype=''F'' then ''调拨'' when billtype in (''6'',''A'',''B'') then ''销售''
                               when billtype=''E'' and ywbilltype=''9'' then ''内部销售'' when billtype=''2'' then ''盘点''
                               when billtype=''S'' then ''报损'' when billtype=''7'' then ''领用''
                               when billtype=''C'' then ''加工用料'' when billtype=''D'' then ''加工产出''
                               when billtype=''G'' then ''移库'' when billtype=''4'' then ''调现存''
                          else ''其他'' end'
  exec(@ssql)
  
  set @ssql='update a set unitcode=(select top 1 unitcode from t_unit where unitname=b.unit)
               from ##tmpplubook a,t_plu b
              where a.plucode=b.plucode'
  exec(@ssql)
  
  set @ssql='select a.plucode,b.pluname,a.orgcode,c.orgname,e.clscode,a.inbilltype,a.r_billtype,a.billno,a.accdate,a.fscount,a.unitcode,a.fsnetcost
               from ##tmpplubook a
               left join t_plu b on a.plucode=b.plucode
               left join t_shop c on a.orgcode=c.orgcode
               left join t_gclass d on b.clscode=d.clscode
               left join t_gclass e on d.uppercode1=e.clscode
              where 1=1'
  if @sorgcode<>''
    set @ssql=@ssql+' and a.orgcode in ('''+@sorgcode+''')'
  if @pbilltype<>''
    set @ssql=@ssql+' and a.inbilltype='''+@pbilltype+''''
  set @ssql=@ssql+' order by a.tblname,a.serialno,a.plucode,a.orgcode'
  exec(@ssql)
  
  if exists(select name from tempdb..sysobjects where name='##tmpplubook' and xtype='u')
    drop table ##tmpplubook
end
go

/*全网络所有SKU的期末库存*/
if exists(select * from sysobjects where name='p_rpt_plustock' and xtype='p')
  drop procedure p_rpt_plustock
go

create procedure p_rpt_plustock
(
  @porgcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pplucode varchar(2000),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @stmpdate varchar(10)
  declare @sorgcode varchar(1000)
  declare @splucode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
  set @sorgcode=replace(@porgcode,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
  
  if exists(select name from tempdb..sysobjects where name='##tmpplustock' and xtype='u') 
    drop table ##tmpplustock
  create table ##tmpplustock (
    plucode varchar(20),orgcode varchar(10),qccount numeric(19,4),incount numeric(19,4),outcount numeric(19,4),jccount numeric(19,4),
    qcnetcost numeric(19,4),innetcost numeric(19,4),outnetcost numeric(19,4),jcnetcost numeric(19,4))

  set @ssql='insert into ##tmpplustock(plucode,orgcode,qccount,incount,outcount,jccount,qcnetcost,innetcost,outnetcost,jcnetcost)
               select plucode,orgcode,0,0,0,0,0,0,0,0 from t_plu_org t
                where mngstock=''1'' and exists(select * from t_shop where orgcode=t.orgcode and status=''1'')
                  and exists(select * from t_user_org where orgcode=t.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sorgcode<>''
    set @ssql=@ssql+' and orgcode in ('''+@sorgcode+''')'
  if @splucode<>''
    set @ssql=@ssql+' and plucode in ('''+@splucode+''')'
  exec(@ssql)
  
  if exists(select name from sysobjects where name='t_gbk_'+substring(@sbgndate,1,4)+substring(@sbgndate,6,2) and xtype='u')
  begin
	set @ssql='update a set qccount=b.jccount,qcnetcost=b.jcnetcost
                 from ##tmpplustock a,t_gbk_'+substring(@sbgndate,1,4)+substring(@sbgndate,6,2)+' b
                where a.plucode=b.plucode and a.orgcode=b.orgcode and b.billtype=''Z''
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
    exec(@ssql)
  end
  
  if exists(select name from sysobjects where name='t_gbk_'+substring(@senddate,1,4)+substring(@senddate,6,2) and xtype='u')
  begin
	set @ssql='update a set jccount=b.jccount,jcnetcost=b.jcnetcost
                 from ##tmpplustock a,t_gbk_'+substring(@senddate,1,4)+substring(@senddate,6,2)+' b
                where a.plucode=b.plucode and a.orgcode=b.orgcode and b.billtype=''Z''
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
    exec(@ssql)
  end
  
  set @stmpdate=@sbgndate
  while substring(@stmpdate,1,7)<=substring(@senddate,1,7)
  begin
    if exists(select name from sysobjects where name='t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2) and xtype='u')
    begin
	  if substring(@stmpdate,1,7)=substring(@sbgndate,1,7)
	  begin
		set @ssql='update a set qccount=a.qccount+b.fscount,qcnetcost=a.qcnetcost+b.fsnetcost
                     from ##tmpplustock a,(select plucode,orgcode,
                       sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fscount else fscount end) as fscount,
                       sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fsnetcost else fsnetcost end) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                      where accdate<'''+@sbgndate+''' and billtype not in (''4'',''8'',''Z'')
                        and exists(select * from t_user_org where orgcode=t.orgcode and usercode='''+@ps_SysUserCode+''')'
        if @sorgcode<>''
          set @ssql=@ssql+' and orgcode in ('''+@sorgcode+''')'
        if @splucode<>''
          set @ssql=@ssql+' and plucode in ('''+@splucode+''')'
        set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
        exec(@ssql)
	  end
	  
	  if substring(@stmpdate,1,7)=substring(@senddate,1,7)
	  begin
		set @ssql='update a set jccount=a.jccount+b.fscount,jcnetcost=a.jcnetcost+b.fsnetcost
                     from ##tmpplustock a,(select plucode,orgcode,
                       sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fscount else fscount end) as fscount,
                       sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fsnetcost else fsnetcost end) as fsnetcost
                       from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                      where accdate<='''+@senddate+''' and billtype not in (''4'',''8'',''Z'')
                        and exists(select * from t_user_org where orgcode=t.orgcode and usercode='''+@ps_SysUserCode+''')'
        if @sorgcode<>''
          set @ssql=@ssql+' and orgcode in ('''+@sorgcode+''')'
        if @splucode<>''
          set @ssql=@ssql+' and plucode in ('''+@splucode+''')'
        set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
        exec(@ssql)
	  end
	  
      set @ssql='update a set incount=a.incount+b.incount,outcount=a.outcount+b.outcount,
                              innetcost=a.innetcost+b.innetcost,outnetcost=a.outnetcost+b.outnetcost
                   from ##tmpplustock a,(select plucode,orgcode,
                     sum(case when billtype in (''0'',''B'',''D'') then fscount when billtype=''2'' and fscount>0 then fscount when billtype in (''E'',''F'',''G'') and fscount<0 then -fscount else 0 end) as incount,
                     sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''S'') then -fscount when billtype=''2'' and fscount<0 then fscount when billtype=''C'' then fscount when billtype in (''E'',''F'',''G'') and fscount>0 then -fscount else 0 end) as outcount,
                     sum(case when billtype in (''0'',''B'',''D'') then fsnetcost when billtype=''2'' and fsnetcost>0 then fsnetcost when billtype in (''E'',''F'',''G'') and fsnetcost<0 then -fsnetcost else 0 end) as innetcost,
                     sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''S'') then -fsnetcost when billtype=''2'' and fsnetcost<0 then fsnetcost when billtype=''C'' then fsnetcost when billtype in (''E'',''F'',''G'') and fsnetcost>0 then -fsnetcost else 0 end) as outnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype not in (''4'',''8'',''Z'')
                      and exists(select * from t_user_org where orgcode=t.orgcode and usercode='''+@ps_SysUserCode+''')'
      if @sorgcode<>''
        set @ssql=@ssql+' and orgcode in ('''+@sorgcode+''')'
      if @splucode<>''
        set @ssql=@ssql+' and plucode in ('''+@splucode+''')'
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
    end
    set @stmpdate=convert(varchar(10),dateadd(month,1,@stmpdate),120)
  end
  
  set @ssql='select distinct a.orgcode,a.plucode,'''+@pbgndate+''' as bgndate,'''+@penddate+''' as enddate,
               a.qccount,a.incount,a.outcount,a.jccount,
               (select top 1 unitcode from t_unit where unitname=b.unit order by unitcode) as unitcode,
               a.qcnetcost,a.innetcost,a.outnetcost,a.jcnetcost,''CNY'' as currunit
               from ##tmpplustock a
               left join t_plu b on a.plucode=b.plucode
              where 0=0 order by orgcode,plucode'
  exec(@ssql)
  
  if exists(select name from tempdb..sysobjects where name='##tmpplustock' and xtype='u')
    drop table ##tmpplustock
end
go

/*商品进销存报表*/
if exists(select * from sysobjects where name='p_rpt_plujxc' and xtype='p')
  drop procedure p_rpt_plujxc
go

create procedure p_rpt_plujxc
(
  @pfinacode varchar(1000),
  @pregncode varchar(1000),
  @pareacode varchar(1000),
  @porgcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pclscode varchar(1000),
  @pplucode varchar(2000),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @stmpdate varchar(10)
  declare @sfinacode varchar(1000)
  declare @sregncode varchar(1000)
  declare @sareacode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @sclscode varchar(1000)
  declare @splucode varchar(1000)
  declare @sfilt varchar(8000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
--  if isnull(@porgcode,'')=''
--  begin
--    raiserror('必须选择油站',16,1)
--    return(1)
--  end  
  set @sfinacode=replace(@pfinacode,',',''',''')
  set @sregncode=replace(@sregncode,',',''',''')
  set @sareacode=replace(@sareacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @sclscode=replace(@pclscode,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
  
  set @sfilt=' and exists(select * from t_user_org where orgcode=t.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sfinacode<>''
    set @sfilt=@sfilt+' and exists(select * from t_shop where orgcode=t.orgcode and finacode in ('''+@sfinacode+'''))'
  if @sregncode<>''
    set @sfilt=@sfilt+' and exists(select * from t_shop m,t_area n where m.areacode=n.areacode and m.orgcode=t.orgcode and n.uppercode1 in ('''+@sregncode+'''))'
  if @sareacode<>''
    set @sfilt=@sfilt+' and exists(select * from t_shop where orgcode=t.orgcode and areacode in ('''+@sareacode+'''))'
  if @sorgcode<>''
    set @sfilt=@sfilt+' and orgcode in ('''+@sorgcode+''')'
  if @sclscode<>''
    set @sfilt=@sfilt+' and exists(select * from t_plu m,t_gclass n where m.clscode=n.clscode and m.plucode=t.plucode and n.uppercode1 in ('''+@sclscode+'''))'
  if @splucode<>''
    set @sfilt=@sfilt+' and plucode in ('''+@splucode+''')'
  
  if exists(select name from tempdb..sysobjects where name='##tmpplujxc' and xtype='u') 
    drop table ##tmpplujxc
  create table ##tmpplujxc (
    plucode varchar(20),orgcode varchar(10),qccount numeric(19,4),qcnetcost numeric(19,4),
    jhcount numeric(19,4),jhnetcost numeric(19,4),thcount numeric(19,4),thnetcost numeric(19,4),
    xscount numeric(19,4),xsnetcost numeric(19,4),drcount numeric(19,4),drnetcost numeric(19,4),
    dccount numeric(19,4),dcnetcost numeric(19,4),pjhcount numeric(19,4),pjhnetcost numeric(19,4),
    pscount numeric(19,4),psnetcost numeric(19,4),tccount numeric(19,4),tcnetcost numeric(19,4),
    pthcount numeric(19,4),pthnetcost numeric(19,4),pycount numeric(19,4),pynetcost numeric(19,4),
    pkcount numeric(19,4),pknetcost numeric(19,4),bscount numeric(19,4),bsnetcost numeric(19,4),
    lycount numeric(19,4),lynetcost numeric(19,4),jgcount numeric(19,4),jgnetcost numeric(19,4),
    cjgcount numeric(19,4),cjgnetcost numeric(19,4),hccount numeric(19,4),hcnetcost numeric(19,4),
    zpscount numeric(19,4),zpsnetcost numeric(19,4),zthcount numeric(19,4),zthnetcost numeric(19,4),
    jccount numeric(19,4),jcnetcost numeric(19,4))

  set @ssql='insert into ##tmpplujxc(plucode,orgcode,qccount,qcnetcost,jhcount,jhnetcost,thcount,thnetcost,
               xscount,xsnetcost,drcount,drnetcost,dccount,dcnetcost,pjhcount,pjhnetcost,pscount,psnetcost,
               tccount,tcnetcost,pthcount,pthnetcost,pycount,pynetcost,pkcount,pknetcost,bscount,bsnetcost,
               lycount,lynetcost,jgcount,jgnetcost,cjgcount,cjgnetcost,hccount,hcnetcost,zpscount,zpsnetcost,
               zthcount,zthnetcost,jccount,jcnetcost)
               select plucode,orgcode,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
               from t_plu_org t where mngstock=''1'' and exists(select * from t_shop where orgcode=t.orgcode and status=''1'')'
  set @ssql=@ssql+@sfilt
  exec(@ssql)
  
  if exists(select name from sysobjects where name='t_gbk_'+substring(@sbgndate,1,4)+substring(@sbgndate,6,2) and xtype='u')
  begin
	set @ssql='update a set qccount=b.jccount,qcnetcost=b.jcnetcost
                 from ##tmpplujxc a,t_gbk_'+substring(@sbgndate,1,4)+substring(@sbgndate,6,2)+' b
                where a.plucode=b.plucode and a.orgcode=b.orgcode and b.billtype=''Z''
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
    exec(@ssql)
  end
  
  if exists(select name from sysobjects where name='t_gbk_'+substring(@senddate,1,4)+substring(@senddate,6,2) and xtype='u')
  begin
	set @ssql='update a set jccount=b.jccount,jcnetcost=b.jcnetcost
                 from ##tmpplujxc a,t_gbk_'+substring(@senddate,1,4)+substring(@senddate,6,2)+' b
                where a.plucode=b.plucode and a.orgcode=b.orgcode and b.billtype=''Z''
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
    exec(@ssql)
  end
  
  set @stmpdate=@sbgndate
  while substring(@stmpdate,1,7)<=substring(@senddate,1,7)
  begin
    if exists(select name from sysobjects where name='t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2) and xtype='u')
    begin
	  if substring(@stmpdate,1,7)=substring(@sbgndate,1,7)
	  begin
		set @ssql='update a set qccount=a.qccount+b.fscount,qcnetcost=a.qcnetcost+b.fsnetcost
                     from ##tmpplujxc a,(select plucode,orgcode,
                     sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fscount else fscount end) as fscount,
                     sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fsnetcost else fsnetcost end) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate<'''+@sbgndate+''' and billtype not in (''4'',''8'',''Z'')'
        set @ssql=@ssql+@sfilt
        set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
        exec(@ssql)
	  end
	  
	  if substring(@stmpdate,1,7)=substring(@senddate,1,7)
	  begin
		set @ssql='update a set jccount=a.jccount+b.fscount,jcnetcost=a.jcnetcost+b.fsnetcost
                     from ##tmpplujxc a,(select plucode,orgcode,
                     sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fscount else fscount end) as fscount,
                     sum(case when billtype in (''3'',''5'',''6'',''7'',''A'',''E'',''F'',''G'',''S'') then -fsnetcost else fsnetcost end) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate<='''+@senddate+''' and billtype not in (''4'',''8'',''Z'')'
        set @ssql=@ssql+@sfilt
        set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
        exec(@ssql)
	  end
	  
      set @ssql='update a set jhcount=a.jhcount+b.fscount,jhnetcost=a.jhnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''0''
                      and exists(select * from t_accept_head where billno=t.billno and billtype in (''0'',''1'',''2''))'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set thcount=a.thcount+b.fscount,thnetcost=a.thnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''5''
                      and exists(select * from t_return_head where billno=t.billno and billtype in (''0'',''2''))'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set xscount=a.xscount+b.fscount,xsnetcost=a.xsnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,
                     sum(case when billtype=''B'' then -fscount else fscount end) as fscount,
                     sum(case when billtype=''B'' then -fsnetcost else fsnetcost end) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype in (''6'',''A'',''B'')'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set drcount=a.drcount+b.fscount,drnetcost=a.drnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,-sum(fscount) as fscount,-sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''F'' and fscount<0'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set drcount=a.drcount+b.fscount,drnetcost=a.drnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''F'' and fscount>0'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set pjhcount=a.pjhcount+b.fscount,pjhnetcost=a.pjhnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''0''
                      and exists(select * from t_accept_head p,t_shop q where p.orgcode=q.orgcode and billno=t.billno and p.billtype=''3'' and q.finacode=''2000'')'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set pscount=a.pscount+b.fscount,psnetcost=a.psnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''E''
                      and (exists(select * from t_dist_head p,t_shop q where p.shopcode=q.orgcode and billno=t.billno and p.zpcount>0 and q.finacode=''2000'')
                       or exists(select * from t_dist_head where billno=t.billno and zpcount=0 and zpamount>0))'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set tccount=a.tccount+b.fscount,tcnetcost=a.tcnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''5''
                      and exists(select * from t_return_head p,t_shop q where p.orgcode=q.orgcode and billno=t.billno and p.billtype=''3'' and q.finacode=''2000'')'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set pthcount=a.pthcount+b.fscount,pthnetcost=a.pthnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''E''
                      and (exists(select * from t_dist_head p,t_shop q where p.shopcode=q.orgcode and billno=t.billno and p.zpcount<0 and p.orgcode=''C002'' and q.finacode=''2000'')
                       or exists(select * from t_dist_head where billno=t.billno and zpcount=0 and zpamount<0))'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set pycount=a.pycount+b.fscount,pynetcost=a.pynetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''2'' and fscount>0'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set pkcount=a.pkcount+b.fscount,pknetcost=a.pknetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''2'' and fscount<0'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set bscount=a.bscount+b.fscount,bsnetcost=a.bsnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''S'''
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set lycount=a.lycount+b.fscount,lynetcost=a.lynetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''7'''
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set jgcount=a.jgcount+b.fscount,jgnetcost=a.jgnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''C'''
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set cjgcount=a.cjgcount+b.fscount,cjgnetcost=a.cjgnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''D'''
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set hccount=a.hccount+b.fscount,hcnetcost=a.hcnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''E''
                      and exists(select * from t_dist_head p,t_shop q where p.shopcode=q.orgcode and billno=t.billno and p.zpcount<0 and p.orgcode=''C001'' and q.finacode=''2000'')'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set zpscount=a.zpscount+b.fscount,zpsnetcost=a.zpsnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''E''
                      and exists(select * from t_dist_head p,t_shop q where p.shopcode=q.orgcode and billno=t.billno and p.zpcount>0 and q.finacode<>''2000'')'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
      
      set @ssql='update a set zthcount=a.zthcount+b.fscount,zthnetcost=a.zthnetcost+b.fsnetcost
                   from ##tmpplujxc a,(select plucode,orgcode,sum(fscount) as fscount,sum(fsnetcost) as fsnetcost
                     from t_gbk_'+substring(@stmpdate,1,4)+substring(@stmpdate,6,2)+' t
                    where accdate between '''+@sbgndate+''' and '''+@senddate+''' and billtype=''E''
                      and exists(select * from t_dist_head p,t_shop q where p.shopcode=q.orgcode and billno=t.billno and p.zpcount<0 and q.finacode<>''2000'')'
      set @ssql=@ssql+@sfilt
      set @ssql=@ssql+' group by plucode,orgcode) b where a.plucode=b.plucode and a.orgcode=b.orgcode'
      exec(@ssql)
    end
    set @stmpdate=convert(varchar(10),dateadd(month,1,@stmpdate),120)
  end
  
  set @ssql='select distinct b.finacode,d.areaname as regnname,c.areaname,a.orgcode,b.orgname,
               h.clscode as dclscode,h.clsname as dclsname,g.clscode,g.clsname,a.plucode,e.pluname,
               (select top 1 unitcode from t_unit where unitname=e.unit order by unitcode) as unitcode,
               a.qccount,a.qcnetcost,a.jhcount,a.jhnetcost,a.thcount,a.thnetcost,a.xscount,a.xsnetcost,
               a.drcount,a.drnetcost,a.dccount,a.dcnetcost,a.pjhcount,a.pjhnetcost,a.pscount,a.psnetcost,
               a.tccount,a.tcnetcost,a.pthcount,a.pthnetcost,a.pycount,a.pynetcost,a.pkcount,a.pknetcost,
               a.bscount,a.bsnetcost,a.lycount,a.lynetcost,a.jgcount,a.jgnetcost,a.cjgcount,a.cjgnetcost,
               a.hccount,a.hcnetcost,a.zpscount,zpsnetcost,zthcount,zthnetcost,a.jccount,a.jcnetcost
               from ##tmpplujxc a
               left join t_shop b on a.orgcode=b.orgcode
               left join t_area c on b.areacode=c.areacode
               left join t_area d on c.uppercode1=d.areacode
               left join t_plu e on a.plucode=e.plucode
               left join t_gclass f on e.clscode=f.clscode
               left join t_gclass g on f.uppercode1=g.clscode
               left join t_gclass h on g.uppercode1=h.clscode
              where 0=0 order by b.finacode,a.orgcode,a.plucode'
  exec(@ssql)
  
  if exists(select name from tempdb..sysobjects where name='##tmpplujxc' and xtype='u')
    drop table ##tmpplujxc
end
go

/*采购订单查询*/
if exists(select * from sysobjects where name='p_rpt_orderrpt' and xtype='p')
  drop procedure p_rpt_orderrpt
go

create procedure p_rpt_orderrpt
(
  @pregncode varchar(1000),
  @pareacode varchar(1000),
  @porgcode varchar(1000),
  @psupcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pbillno varchar(1000),
  @pbilltype varchar(2),
  @pbillstate varchar(1),
  @pplucode varchar(2000),
  @pchkstatus varchar(1),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @stmpdate varchar(10)
  declare @sregncode varchar(1000)
  declare @sareacode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @ssupcode varchar(1000)
  declare @sbillno varchar(1000)
  declare @splucode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
  set @sregncode=replace(@sregncode,',',''',''')
  set @sareacode=replace(@sareacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @ssupcode=replace(@psupcode,',',''',''')
  set @sbillno=replace(@pbillno,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
  
  if exists(select name from tempdb..sysobjects where name='##tmporderrpt' and xtype='u') 
    drop table ##tmporderrpt
  create table ##tmporderrpt (
    orgcode varchar(10),rzdate varchar(10),jhrzdate varchar(10),billno varchar(20),billtype varchar(2),
    state varchar(1),supcode varchar(15),zdrname varchar(10),chkstatus varchar(1),plucode varchar(20),
    barcode varchar(20),pluname varchar(40),unit varchar(10),pcount numeric(19,4),jprice numeric(19,4),
    counts numeric(19,4),cost numeric(19,4),netcost numeric(19,4),jhcounts numeric(19,4),
    jhcost numeric(19,4),jhnetcost numeric(19,4))
    
  set @ssql='insert into ##tmporderrpt(orgcode,rzdate,jhrzdate,billno,billtype,state,supcode,zdrname,chkstatus,plucode,
                                       barcode,pluname,unit,pcount,jprice,counts,cost,netcost,jhcounts,jhcost,jhnetcost)
               select a.orgcode,a.rzdate,'''',a.billno,case when a.iszs=''1'' then ''03'' when a.supcode=''C2000'' then ''05'' else ''01'' end,
                      a.state,a.supcode,a.zdrname,a.chkstatus,b.plucode,b.barcode,b.pluname,b.unit,b.itemcnt,b.jprice,b.counts,b.cost,b.netcost,0,0,0
                 from t_order_head a,t_order_body b
                where a.billno=b.billno                  
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sregncode<>''
    set @ssql=@ssql+' and exists(select * from t_shop m,t_area n where m.areacode=n.areacode and m.orgcode=a.orgcode and n.uppercode1 in ('''+@sregncode+'''))'
  if @sareacode<>''
    set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.orgcode and areacode in ('''+@sareacode+'''))'
  if @sorgcode<>''
    set @ssql=@ssql+' and a.orgcode in ('''+@sorgcode+''')'
  if @ssupcode<>''
    set @ssql=@ssql+' and a.supcode in ('''+@ssupcode+''')'
  if @sbillno<>''
    set @ssql=@ssql+' and a.billno in ('''+@sbillno+''')'
  if @splucode<>''
    set @ssql=@ssql+' and b.plucode in ('''+@splucode+''')'
  if isnull(@pbilltype,'')='01'
    set @ssql=@ssql+' and a.iszs=''0'' and a.supcode<>''C2000'''
  else if isnull(@pbilltype,'')='03'
    set @ssql=@ssql+' and a.iszs=''1'''
  else if isnull(@pbilltype,'')='05'
    set @ssql=@ssql+' and a.supcode=''C2000'''
  else if isnull(@pbilltype,'')<>''
    set @ssql=@ssql+' and 0=1'
  if isnull(@pbillstate,'')<>''
    set @ssql=@ssql+' and a.state='''+@pbillstate+''''
  if isnull(@pchkstatus,'')<>'1'
    set @ssql=@ssql+' and a.rzdate between '''+@sbgndate+''' and '''+@senddate+''''
  else
    set @ssql=@ssql+' and isnull(a.rzdate,'''')='''' and a.chkstatus in (''0'',''9'')'
  exec(@ssql)
  
  set @ssql='update t set jhrzdate=s.rzdate,jhcounts=s.counts,jhcost=s.cost,jhnetcost=s.netcost
               from ##tmporderrpt t,
                    (select a.orderno,b.plucode,max(a.rzdate) rzdate,sum(b.counts) as counts,sum(b.cost) as cost,sum(b.netcost) as netcost
                       from t_accept_head a,t_accept_body b
                      where a.billno=b.billno and a.rzdate between '''+@sbgndate+''' and '''+@senddate+''''
  if @sregncode<>''
    set @ssql=@ssql+' and exists(select * from t_shop m,t_area n where m.areacode=n.areacode and m.orgcode=a.orgcode and n.uppercode1 in ('''+@sregncode+'''))'
  if @sareacode<>''
    set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.orgcode and areacode in ('''+@sareacode+'''))'
  if @sorgcode<>''
    set @ssql=@ssql+' and a.orgcode in ('''+@sorgcode+''')'
  if @ssupcode<>''
    set @ssql=@ssql+' and a.supcode in ('''+@ssupcode+''')'
  if @sbillno<>''
    set @ssql=@ssql+' and a.orderno in ('''+@sbillno+''')'
  if @splucode<>''
    set @ssql=@ssql+' and b.plucode in ('''+@splucode+''')'
  set @ssql=@ssql+' group by a.orderno,b.plucode) s
                    where t.billno=s.orderno and t.plucode=s.plucode'
  exec(@ssql)
  
  set @ssql='insert into ##tmporderrpt(orgcode,rzdate,jhrzdate,billno,billtype,state,supcode,zdrname,chkstatus,plucode,
                                       barcode,pluname,unit,pcount,jprice,counts,cost,netcost,jhcounts,jhcost,jhnetcost)
               select a.orgcode,a.rzdate,a.rzdate,a.billno,case when a.billtype=''2'' then ''04'' when a.supcode=''C2000'' then ''06'' else ''02'' end,
                      ''0'',a.supcode,a.zdrname,a.chkstatus,b.plucode,b.barcode,b.pluname,b.unit,b.itemcnt,b.jprice,b.counts,b.cost,b.netcost,b.counts,b.cost,b.netcost
                 from t_return_head a,t_return_body b
                where a.billno=b.billno and a.billtype in (''0'',''2'')
                  and a.rzdate between '''+@sbgndate+''' and '''+@senddate+'''
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sregncode<>''
    set @ssql=@ssql+' and exists(select * from t_shop m,t_area n where m.areacode=n.areacode and m.orgcode=a.orgcode and n.uppercode1 in ('''+@sregncode+'''))'
  if @sareacode<>''
    set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.orgcode and areacode in ('''+@sareacode+'''))'
  if @sorgcode<>''
    set @ssql=@ssql+' and a.orgcode in ('''+@sorgcode+''')'
  if @ssupcode<>''
    set @ssql=@ssql+' and a.supcode in ('''+@ssupcode+''')'
  if @sbillno<>''
    set @ssql=@ssql+' and a.billno in ('''+@sbillno+''')'
  if @splucode<>''
    set @ssql=@ssql+' and b.plucode in ('''+@splucode+''')'
  if isnull(@pbilltype,'')='02'
    set @ssql=@ssql+' and a.billtype=''0'' and a.supcode<>''C2000'''
  else if isnull(@pbilltype,'')='04'
    set @ssql=@ssql+' and a.billtype=''2'''
  else if isnull(@pbilltype,'')='06'
    set @ssql=@ssql+' and a.supcode=''C2000'''
  else if isnull(@pbilltype,'')<>''
    set @ssql=@ssql+' and 0=1'
  if isnull(@pbillstate,'')<>''
    set @ssql=@ssql+' and 0=1'
  exec(@ssql)
  
  set @ssql='select a.orgcode,b.orgname,a.rzdate,a.jhrzdate,a.billtype,
               case a.billtype when ''01'' then ''中央仓采购订单'' when ''02'' then ''中央仓采购退货单'' when ''03'' then ''便利店直送采购订单''
                               when ''04'' then ''便利店直送退货订单'' when ''05'' then ''母子公司采购订单'' when ''06'' then ''母子公司采购退货单''
                               else '''' end as c_billtype,
               case a.state when ''1'' then ''执行中'' when ''2'' then ''执行完成'' when ''3'' then ''手工关闭'' when ''4'' then ''过期关闭'' else '''' end as billstate,
               a.billno,a.supcode,e.supname,a.zdrname,case when a.chkstatus in (''1'',''2'') then ''√'' else '''' end as chkflag,
               a.plucode,a.barcode,a.pluname,a.counts,(select top 1 unitcode from t_unit where unitname=f.unit) as unitcode,a.unit,
               a.jprice,a.cost,a.netcost,a.cost-a.netcost as taxamt,a.jhcost,a.jhnetcost,a.jhcounts,h.clscode,h.clsname,
               case when a.billtype in (''02'',''04'',''06'') then ''×'' else '''' end as rtnflag,a.pcount,a.unit as punit,b.areacode,c.areaname,d.areacode as regncode,d.areaname as regnname
               from ##tmporderrpt a
               left join t_shop b on a.orgcode=b.orgcode
               left join t_area c on b.areacode=c.areacode
               left join t_area d on c.uppercode1=d.areacode
               left join t_supplier e on a.supcode=e.supcode
               left join t_plu f on a.plucode=f.plucode
               left join t_gclass g on f.clscode=g.clscode
               left join t_gclass h on g.uppercode1=h.clscode
               order by a.orgcode,a.rzdate,a.jhrzdate,a.billno,a.plucode'
  exec(@ssql)
  
  if exists(select name from tempdb..sysobjects where name='##tmporderrpt' and xtype='u')
    drop table ##tmporderrpt
end
go

/*中央仓配送报表*/
if exists(select * from sysobjects where name='p_rpt_distrpt' and xtype='p')
  drop procedure p_rpt_distrpt
go

create procedure p_rpt_distrpt
(
  @pregncode varchar(1000),
  @pareacode varchar(1000),
  @porgcode varchar(1000),
  @pshopcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pjhbgndate varchar(10),
  @pjhenddate varchar(10),
  @pbillno varchar(1000),  
  @pplucode varchar(2000),
  @pdifftype varchar(1),
  @pjhstate varchar(1),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @stmpdate varchar(10)
  declare @sregncode varchar(1000)
  declare @sareacode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @sshopcode varchar(1000)
  declare @sbillno varchar(1000)
  declare @splucode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
  declare @sjhbgndate varchar(10)
  declare @sjhenddate varchar(10)
begin
  set @sregncode=replace(@sregncode,',',''',''')
  set @sareacode=replace(@sareacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @sshopcode=replace(@pshopcode,',',''',''')
  set @sbillno=replace(@pbillno,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  set @sjhbgndate=@pjhbgndate
  set @sjhenddate=@pjhenddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
  if isnull(@sjhbgndate,'')=''
    set @sjhbgndate='2023-08-01'
  if isnull(@sjhenddate,'')=''
    set @sjhenddate='2099-12-31'
  
  if exists(select name from tempdb..sysobjects where name='##tmpdistrpt' and xtype='u') 
    drop table ##tmpdistrpt
  create table ##tmpdistrpt (
    orgcode varchar(10),orgname varchar(30),shopcode varchar(10),rzdate varchar(10),jhrzdate varchar(10),billno varchar(20),
    jhbillno varchar(20),plucode varchar(20),barcode varchar(20),pluname varchar(40),unit varchar(10),pcount numeric(19,4),
    zpcount numeric(19,4),counts numeric(19,4),thbillno varchar(20),thcount numeric(19,4),thrzdate varchar(10))
    
  set @ssql='insert into ##tmpdistrpt(orgcode,orgname,shopcode,rzdate,jhrzdate,billno,jhbillno,plucode,barcode,pluname,unit,pcount,zpcount,counts,thbillno,thcount,thrzdate)
               select a.supcode,a.supname,a.orgcode,a.bizdate,a.rzdate,a.orderno,a.billno,b.plucode,b.barcode,b.pluname,b.unit,b.itemcnt,b.cgcount,
                      case when isnull(a.rzdate,'''')='''' then 0 else b.cgcount end,'''',0,''''
                 from t_accept_head a,t_accept_body b
                where a.billno=b.billno and a.billtype=''3''
                  and a.bizdate between '''+@sbgndate+''' and '''+@senddate+'''
                  and (a.rzdate between '''+@sjhbgndate+''' and '''+@sjhenddate+''' or isnull(a.rzdate,'''')='''')
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sregncode<>''
    set @ssql=@ssql+' and exists(select * from t_shop m,t_area n where m.areacode=n.areacode and m.orgcode=a.shopcode and n.uppercode1 in ('''+@sregncode+'''))'
  if @sareacode<>''
    set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.shopcode and areacode in ('''+@sareacode+'''))'
  if @sorgcode<>''
    set @ssql=@ssql+' and a.supcode in ('''+@sorgcode+''')'
  if @sshopcode<>''
    set @ssql=@ssql+' and a.orgcode in ('''+@sshopcode+''')'
  if @sbillno<>''
    set @ssql=@ssql+' and a.orderno in ('''+@sbillno+''')'
  if @splucode<>''
    set @ssql=@ssql+' and b.plucode in ('''+@splucode+''')'
  if isnull(@pdifftype,'')='1'
    set @ssql=@ssql+' and b.cgcount<>b.counts'
  if isnull(@pjhstate,'')='1'
    set @ssql=@ssql+' and isnull(a.rzdate,'''')='''''
  else if isnull(@pjhstate,'')='2'
    set @ssql=@ssql+' and isnull(a.rzdate,'''')<>'''''
  exec(@ssql)
  
  set @ssql='update t set thbillno=b.genbillno
               from ##tmpdistrpt t,t_distdiffer_head a,t_distdiffer_body b
              where t.jhbillno=a.ywbillno and a.billno=b.billno and t.plucode=b.plucode and a.differtype=''0'''
  exec(@ssql)
  
  set @ssql='update t set thcount=b.counts,thrzdate=a.rzdate
               from ##tmpdistrpt t,t_return_head a,t_return_body b
              where t.thbillno=a.billno and a.billno=b.billno and t.plucode=b.plucode and a.billtype=''3'''
  exec(@ssql)
  
  set @ssql='select a.orgcode,a.orgname,a.billno,a.rzdate,a.plucode,a.pluname,a.barcode,a.unit,a.pcount,
               a.zpcount,a.counts,a.jhrzdate,a.zpcount-a.counts as diffcount,a.thbillno,a.thcount,a.thrzdate,
               b.areacode,c.areaname,d.areacode as regncode,d.areaname as regnname,a.shopcode,b.orgname as shopname,
               case when isnull(a.jhrzdate,'''')='''' then ''未收货'' else ''已收货'' end as r_jhstate
               from ##tmpdistrpt a
               left join t_shop b on a.shopcode=b.orgcode
               left join t_area c on b.areacode=c.areacode
               left join t_area d on c.uppercode1=d.areacode
               order by a.orgcode,a.rzdate,a.billno,a.plucode'
  exec(@ssql)
  
  if exists(select name from tempdb..sysobjects where name='##tmpdistrpt' and xtype='u')
    drop table ##tmpdistrpt
end
go

/*中央仓配送退货报表*/
if exists(select * from sysobjects where name='p_rpt_distrtnrpt' and xtype='p')
  drop procedure p_rpt_distrtnrpt
go

create procedure p_rpt_distrtnrpt
(
  @pregncode varchar(1000),
  @pareacode varchar(1000),
  @porgcode varchar(1000),
  @pshopcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pjhbgndate varchar(10),
  @pjhenddate varchar(10),
  @pbillno varchar(1000),  
  @pplucode varchar(2000),
  @pdifftype varchar(1),
  @pjhstate varchar(1),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @stmpdate varchar(10)
  declare @sregncode varchar(1000)
  declare @sareacode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @sshopcode varchar(1000)
  declare @sbillno varchar(1000)
  declare @splucode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
  declare @sjhbgndate varchar(10)
  declare @sjhenddate varchar(10)
begin
  set @sregncode=replace(@sregncode,',',''',''')
  set @sareacode=replace(@sareacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @sshopcode=replace(@pshopcode,',',''',''')
  set @sbillno=replace(@pbillno,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  set @sjhbgndate=@pjhbgndate
  set @sjhenddate=@pjhenddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
  if isnull(@sjhbgndate,'')=''
    set @sjhbgndate='2023-08-01'
  if isnull(@sjhenddate,'')=''
    set @sjhenddate='2099-12-31'
  
  if exists(select name from tempdb..sysobjects where name='##tmpdistrtnrpt' and xtype='u') 
    drop table ##tmpdistrtnrpt
  create table ##tmpdistrtnrpt (
    orgcode varchar(10),orgname varchar(30),shopcode varchar(10),rzdate varchar(10),jhrzdate varchar(10),billno varchar(20),
    jhbillno varchar(20),plucode varchar(20),barcode varchar(20),pluname varchar(40),unit varchar(10),pcount numeric(19,4),
    zpcount numeric(19,4),counts numeric(19,4),thbillno varchar(20),thcount numeric(19,4),thrzdate varchar(10))
    
  set @ssql='insert into ##tmpdistrtnrpt(orgcode,orgname,shopcode,rzdate,jhrzdate,billno,jhbillno,plucode,barcode,pluname,unit,pcount,zpcount,counts,thbillno,thcount,thrzdate)
               select a.supcode,a.supname,a.orgcode,a.bizdate,a.rzdate,'''',a.billno,b.plucode,b.barcode,b.pluname,b.unit,b.itemcnt,b.orncounts,
                      case when isnull(a.rzdate,'''')='''' then 0 else b.counts end,a.billno,0,''''
                 from t_return_head a,t_return_body b
                where a.billno=b.billno and a.billtype=''3''
                  and a.bizdate between '''+@sbgndate+''' and '''+@senddate+'''
                  and (a.rzdate between '''+@sjhbgndate+''' and '''+@sjhenddate+''' or isnull(a.rzdate,'''')='''')
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sregncode<>''
    set @ssql=@ssql+' and exists(select * from t_shop m,t_area n where m.areacode=n.areacode and m.orgcode=a.shopcode and n.uppercode1 in ('''+@sregncode+'''))'
  if @sareacode<>''
    set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.shopcode and areacode in ('''+@sareacode+'''))'
  if @sorgcode<>''
    set @ssql=@ssql+' and a.supcode in ('''+@sorgcode+''')'
  if @sshopcode<>''
    set @ssql=@ssql+' and a.orgcode in ('''+@sshopcode+''')'
  if @sbillno<>''
    set @ssql=@ssql+' and a.orderno in ('''+@sbillno+''')'
  if @splucode<>''
    set @ssql=@ssql+' and b.plucode in ('''+@splucode+''')'
  if isnull(@pdifftype,'')='1'
    set @ssql=@ssql+' and b.orncounts<>b.counts'
  if isnull(@pjhstate,'')='1'
    set @ssql=@ssql+' and isnull(a.rzdate,'''')='''''
  else if isnull(@pjhstate,'')='2'
    set @ssql=@ssql+' and isnull(a.rzdate,'''')<>'''''
  exec(@ssql)
  
  set @ssql='update t set billno=a.billno,thcount=b.zpcount,thrzdate=a.rzdate
               from ##tmpdistrtnrpt t,t_dist_head a,t_dist_body b
              where t.thbillno=a.billno and a.billno=b.billno and t.plucode=b.plucode and a.zpcount<0'
  exec(@ssql)
  
  set @ssql='select a.orgcode,a.orgname,a.billno,a.rzdate,a.plucode,a.pluname,a.barcode,a.unit,a.pcount,
               a.zpcount,a.counts,a.jhrzdate,a.zpcount-a.counts as diffcount,a.thbillno,a.thcount,a.thrzdate,
               b.areacode,c.areaname,d.areacode as regncode,d.areaname as regnname,a.shopcode,b.orgname as shopname,
               case when isnull(a.jhrzdate,'''')='''' then ''未收货'' else ''已收货'' end as r_jhstate
               from ##tmpdistrtnrpt a
               left join t_shop b on a.shopcode=b.orgcode
               left join t_area c on b.areacode=c.areacode
               left join t_area d on c.uppercode1=d.areacode
               order by a.orgcode,a.rzdate,a.billno,a.plucode'
  exec(@ssql)
  
  if exists(select name from tempdb..sysobjects where name='##tmpdistrtnrpt' and xtype='u')
    drop table ##tmpdistrtnrpt
end
go

/*母子公司购销明细汇总*/
if exists(select * from sysobjects where name='p_rpt_subsettle' and xtype='p')
  drop procedure p_rpt_subsettle
go

create procedure p_rpt_subsettle
(
  @pfinacode varchar(1000),
  @pshopcode varchar(1000),
  @porgcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pbilltype varchar(1),
  @pplucode varchar(2000),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @sfinacode varchar(1000)
  declare @sshopcode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @splucode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
  set @sfinacode=replace(@pfinacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @sshopcode=replace(@pshopcode,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'

  if exists(select name from tempdb..sysobjects where name='##tmpsubsettle' and xtype='u') 
    drop table ##tmpsubsettle
  create table ##tmpsubsettle (
    billno varchar(20),orgcode varchar(10),shopcode varchar(10),billtype varchar(1),rzdate varchar(10),
    plucode varchar(20),pluname varchar(40),jprice numeric(19,4),netjprice numeric(19,4),zpcount numeric(19,4),
    cost numeric(19,4),netcost numeric(19,4),jhrzdate varchar(10),jhjprice numeric(19,4),jhnetjprice numeric(19,4),
    jhcount numeric(19,4),jhcost numeric(19,4),jhnetcost numeric(19,4))

  set @ssql='insert into ##tmpsubsettle(billno,orgcode,shopcode,billtype,rzdate,plucode,pluname,jprice,netjprice,
                                        zpcount,cost,netcost,jhrzdate,jhjprice,jhnetjprice,jhcount,jhcost,jhnetcost)
               select a.billno,a.orgcode,a.shopcode,case when t.billtype=''7'' then ''1'' else ''0'' end,a.rzdate,
                      b.plucode,b.pluname,b.jprice,b.netjprice,b.zpcount,b.cost,b.netcost,'''',0,0,0,0,0
                 from t_pay_bill t,t_dist_head a,t_dist_body b
                where t.billno=a.billno and a.billno=b.billno and t.billtype in (''6'',''7'')
                  and a.rzdate between '''+@sbgndate+''' and '''+@senddate+'''
                  and exists(select * from t_shop where orgcode=a.orgcode and finacode<>t.finacode)
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')
                  and exists(select * from t_user_org where orgcode=a.shopcode and usercode='''+@ps_SysUserCode+''')'
  if @sfinacode<>''
    set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.shopcode and finacode in ('''+@sfinacode+'''))'
  if @sshopcode<>''
    set @ssql=@ssql+' and a.shopcode in ('''+@sshopcode+''')'
  if @sorgcode<>''
    set @ssql=@ssql+' and t.orgcode in ('''+@sorgcode+''')'
  if @splucode<>''
    set @ssql=@ssql+' and b.plucode in ('''+@splucode+''')'
  if isnull(@pbilltype,'')='0'
    set @ssql=@ssql+' and t.billtype=''6'''
  if isnull(@pbilltype,'')='1'
    set @ssql=@ssql+' and t.billtype=''7'''
  else if isnull(@pbilltype,'')='2'
    set @ssql=@ssql+' and 0=1'
  exec(@ssql)
  
  set @ssql='update t set jhrzdate=a.rzdate,jhjprice=b.jprice,jhnetjprice=b.netjprice,jhcount=b.counts,jhcost=b.cost,jhnetcost=b.netcost
               from ##tmpsubsettle t,t_accept_head a,t_accept_body b
              where t.billno=a.orderno and a.billno=b.billno and t.plucode=b.plucode
                and isnull(a.rzdate,'''')<>'''' and t.billtype=''0'' and a.billtype=''3'''
  exec(@ssql)
  
  set @ssql='update t set jhrzdate=a.rzdate,jhjprice=b.jprice,jhnetjprice=b.netjprice,jhcount=b.counts,jhcost=b.cost,jhnetcost=b.netcost
               from ##tmpsubsettle t,t_return_head a,t_return_body b
              where t.billno=a.billno and a.billno=b.billno and t.plucode=b.plucode
                and isnull(a.rzdate,'''')<>'''' and t.billtype=''1'' and a.billtype=''3'''
  exec(@ssql)

  set @ssql='insert into ##tmpsubsettle(billno,orgcode,shopcode,billtype,rzdate,plucode,pluname,jprice,netjprice,
                                        zpcount,cost,netcost,jhjprice,jhnetjprice,jhcount,jhcost,jhnetcost)
               select a.billno,''C009'',t.orgcode,''2'',a.rzdate,b.plucode,b.pluname,
                      b.newjprice-b.jprice,b.newnetjprice-b.netjprice,b.tzcount,b.cjcost,b.netcjcost,0,0,0,0,0
                 from t_pay_bill t,t_adjbuyprice_head a,t_adjbuyprice_body b
                where t.billno=a.billno and a.billno=b.billno and t.billtype in (''8'')
                  and a.rzdate between '''+@sbgndate+''' and '''+@senddate+'''
                  and exists(select * from t_shop where orgcode=''C009'' and finacode<>t.finacode)
                  and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sfinacode<>''
    set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.shopcode and finacode in ('''+@sfinacode+'''))'
  if @sshopcode<>''
    set @ssql=@ssql+' and a.orgcode in ('''+@sshopcode+''')'
  if @sorgcode<>''
    set @ssql=@ssql+' and t.orgcode in ('''+@sorgcode+''')'
  if @splucode<>''
    set @ssql=@ssql+' and b.plucode in ('''+@splucode+''')'
  if isnull(@pbilltype,'')='0'
    set @ssql=@ssql+' and 0=1'
  if isnull(@pbilltype,'')='1'
    set @ssql=@ssql+' and 0=1'
  else if isnull(@pbilltype,'')='2'
    set @ssql=@ssql+' and t.billtype=''8'''
  exec(@ssql)
  
  set @ssql='select b.finacode,c.finaname,a.shopcode,b.orgname as shopname,
               case a.billtype when ''1'' then ''配送退货单'' when ''2'' then ''配送进价调整单'' else ''配送单'' end as c_billtype,a.billno,
               a.plucode,a.pluname,f.clscode,f.clsname,d.staxrate,a.rzdate,a.zpcount,a.jprice,a.netjprice,a.cost,a.netcost,a.cost-a.netcost as taxamt,
               a.jhrzdate,a.jhcount,a.jhjprice,a.jhnetjprice,a.jhcost,a.jhnetcost,a.jhcost-a.jhnetcost as jhtaxamt,a.orgcode,g.orgname
               from ##tmpsubsettle a
               left join t_shop b on a.shopcode=b.orgcode
               left join t_financial_body c on b.finacode=c.finacode
               left join t_plu d on a.plucode=d.plucode
               left join t_gclass e on d.clscode=e.clscode
               left join t_gclass f on e.uppercode1=f.clscode
               left join t_shop g on a.orgcode=g.orgcode
               order by b.finacode,a.shopcode,a.billtype,a.plucode'
  exec(@ssql)

  if exists(select name from tempdb..sysobjects where name='##tmpsubsettle' and xtype='u')
    drop table ##tmpsubsettle
end
go

/*总部盘点结果报表*/
if exists(select * from sysobjects where name='p_rpt_cykrpt' and xtype='p')
  drop procedure p_rpt_cykrpt
go

create procedure p_rpt_cykrpt
(
  @pregncode varchar(1000),
  @pareacode varchar(1000),
  @porgcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pykreason varchar(2),
  @pplucode varchar(2000),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @sregncode varchar(1000)
  declare @sareacode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @splucode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
  set @sregncode=replace(@sregncode,',',''',''')
  set @sareacode=replace(@sareacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
  
  set @ssql='select e.areaname as regnname,d.areaname,a.orgcode,c.orgname,a.billno,a.accdate,
               b.plucode,f.pluname,h.clscode,h.clsname,b.jtaxrate,b.bookcount,b.realcount,b.ykcount,
               b.netjprice,b.yknetcost,b.price,b.yktotal,b.remark+''-''+i.name as ykreason,b.describe
               from t_differ_head a join t_differ_body b on a.billno=b.billno
               left join t_shop c on a.orgcode=c.orgcode
               left join t_area d on c.areacode=d.areacode
               left join t_area e on d.uppercode1=e.areacode
               left join t_plu f on b.plucode=f.plucode
               left join t_gclass g on f.clscode=g.clscode
               left join t_gclass h on g.uppercode1=h.clscode
               left join (select q.item_value,q.name from dict_data p,dict_data_item q where p.id=q.p_id and p.code=''PD0006'') i on b.remark=i.item_value
              where a.accdate between '''+@sbgndate+''' and '''+@senddate+''' and a.billtype in (''0'',''1'',''3'')
                and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sregncode<>''
    set @ssql=@ssql+' and exists(select * from t_shop m,t_area n where m.areacode=n.areacode and m.orgcode=a.orgcode and n.uppercode1 in ('''+@sregncode+'''))'
  if @sareacode<>''
    set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.orgcode and areacode in ('''+@sareacode+'''))'
  if @sorgcode<>''
    set @ssql=@ssql+' and a.orgcode in ('''+@sorgcode+''')'
  if @splucode<>''
    set @ssql=@ssql+' and b.plucode in ('''+@splucode+''')'
  if isnull(@pykreason,'')<>''
    set @ssql=@ssql+' and b.remark=''' + @pykreason + ''''
  set @ssql=@ssql+' order by e.areacode,d.areacode,a.orgcode,b.plucode'
  exec(@ssql)
end
go

/*油站盘点结果报表*/
if exists(select * from sysobjects where name='p_rpt_sykrpt' and xtype='p')
  drop procedure p_rpt_sykrpt
go

create procedure p_rpt_sykrpt
(
  @pregncode varchar(1000),
  @pareacode varchar(1000),
  @porgcode varchar(1000),
  @pbgndate varchar(10),
  @penddate varchar(10),
  @pykreason varchar(2),
  @pplucode varchar(2000),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @sregncode varchar(1000)
  declare @sareacode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @splucode varchar(1000)
  declare @sbgndate varchar(10)
  declare @senddate varchar(10)
begin
  set @sregncode=replace(@sregncode,',',''',''')
  set @sareacode=replace(@sareacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @splucode=replace(@pplucode,',',''',''')
  set @sbgndate=@pbgndate
  set @senddate=@penddate
  if isnull(@sbgndate,'')=''
    set @sbgndate='2023-08-01'
  if isnull(@senddate,'')=''
    set @senddate='2099-12-31'
  
  set @ssql='select e.areaname as regnname,d.areaname,a.orgcode,c.orgname,a.billno,a.rzdate,
               b.plucode,f.pluname,h.clscode,h.clsname,b.jtaxrate,b.bookcount,b.realcount,b.ykcount,
               b.price,b.yktotal,b.remark+''-''+i.name as ykreason,b.describe
               from t_differ_head a join t_differ_body b on a.billno=b.billno
               left join t_shop c on a.orgcode=c.orgcode
               left join t_area d on c.areacode=d.areacode
               left join t_area e on d.uppercode1=e.areacode
               left join t_plu f on b.plucode=f.plucode
               left join t_gclass g on f.clscode=g.clscode
               left join t_gclass h on g.uppercode1=h.clscode
               left join (select q.item_value,q.name from dict_data p,dict_data_item q where p.id=q.p_id and p.code=''PD0006'') i on b.remark=i.item_value
              where a.rzdate between '''+@sbgndate+''' and '''+@senddate+''' and a.billtype in (''0'',''1'',''3'')
                and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sregncode<>''
    set @ssql=@ssql+' and exists(select * from t_shop m,t_area n where m.areacode=n.areacode and m.orgcode=a.orgcode and n.uppercode1 in ('''+@sregncode+'''))'
  if @sareacode<>''
    set @ssql=@ssql+' and exists(select * from t_shop where orgcode=a.orgcode and areacode in ('''+@sareacode+'''))'
  if @sorgcode<>''
    set @ssql=@ssql+' and a.orgcode in ('''+@sorgcode+''')'
  if @splucode<>''
    set @ssql=@ssql+' and b.plucode in ('''+@splucode+''')'
  if isnull(@pykreason,'')<>''
    set @ssql=@ssql+' and b.remark=''' + @pykreason + ''''
  set @ssql=@ssql+' order by e.areacode,d.areacode,a.orgcode,b.plucode'
  exec(@ssql)
end
go

/*日结前数据检查*/
if exists(select * from sysobjects where name='p_rpt_overqry' and xtype='p')
  drop procedure p_rpt_overqry
go

create procedure p_rpt_overqry
(
  @pregncode varchar(1000),
  @pareacode varchar(1000),
  @porgcode varchar(1000),
  @ps_SysUserCode varchar(20)
)
as
  declare @ssql varchar(8000)
  declare @sregncode varchar(1000)
  declare @sareacode varchar(1000)
  declare @sorgcode varchar(1000)
  declare @sfilt varchar(8000)
  declare @stmpdate varchar(10)
  declare @thismonth varchar(6)
  declare @lastmonth varchar(6)
begin
  set @sregncode=replace(@sregncode,',',''',''')
  set @sareacode=replace(@sareacode,',',''',''')
  set @sorgcode=replace(@porgcode,',',''',''')
  set @stmpdate=convert(varchar(10),getdate(),120)
  set @thismonth=substring(@stmpdate,1,4)+substring(@stmpdate,6,2)
  set @stmpdate=convert(varchar(10),dateadd(month,-1,getdate()),120)
  set @lastmonth=substring(@stmpdate,1,4)+substring(@stmpdate,6,2)
  
  set @sfilt=' and exists(select * from t_shop where orgcode=a.orgcode and status=''1'')
               and exists(select * from t_user_org where orgcode=a.orgcode and usercode='''+@ps_SysUserCode+''')'
  if @sregncode<>''
    set @sfilt=@sfilt+' and exists(select * from t_shop m,t_area n where m.areacode=n.areacode and m.orgcode=a.orgcode and n.uppercode1 in ('''+@sregncode+'''))'
  if @sareacode<>''
    set @sfilt=@sfilt+' and exists(select * from t_shop where orgcode=a.orgcode and areacode in ('''+@sareacode+'''))'
  if @sorgcode<>''
    set @sfilt=@sfilt+' and a.orgcode in ('''+@sorgcode+''')'
  
  if exists(select name from tempdb..sysobjects where name='##tmpoverqry' and xtype='u') 
    drop table ##tmpoverqry
  create table ##tmpoverqry (
    orgcode varchar(10),errtype varchar(2),plucode varchar(20),xscount numeric(19,4),kccount numeric(19,4))
  
  set @ssql='insert into ##tmpoverqry(orgcode,errtype,plucode,xscount,kccount)
               select a.orgcode,''0'','''',0,0
                 from t_shop a
                where orgtype<>''2'' and overdate<convert(varchar(10),getdate()-1,120)
                  and not exists(select * from t_int_flow_total where orgcode=a.orgcode and accdate>a.overdate)'
  set @ssql=@ssql+@sfilt
  exec(@ssql)
  
  set @ssql='insert into ##tmpoverqry(orgcode,errtype,plucode,xscount,kccount)
               select a.orgcode,''1'','''',0,0
                 from t_shop a
                where orgtype<>''2'' and overdate<convert(varchar(10),getdate()-1,120)'
  if exists(select name from sysobjects where name='t_int_flow_pay_deily_'+@thismonth and xtype='u') or exists(select name from sysobjects where name='t_int_flow_pay_deily_'+@lastmonth and xtype='u')
  begin
    set @ssql=@ssql+' and not exists(select * from ('
    if exists(select name from sysobjects where name='t_int_flow_pay_deily_'+@thismonth and xtype='u')
      set @ssql=@ssql+' select * from t_int_flow_pay_deily_'+@thismonth+' where werks=a.orgcode and business_date>a.overdate'
    if exists(select name from sysobjects where name='t_int_flow_pay_deily_'+@lastmonth and xtype='u')
      set @ssql=@ssql+' union select * from t_int_flow_pay_deily_'+@lastmonth+' where werks=a.orgcode and business_date>a.overdate'
    set @ssql=@ssql+') b)'
  end
  set @ssql=@ssql+@sfilt
  exec(@ssql)
  
  set @ssql='insert into ##tmpoverqry(orgcode,errtype,plucode,xscount,kccount)
               select a.orgcode,''2'',a.plucode,sum(a.pluqty),0
                 from t_int_flow_item a
                where not exists(select * from t_plu where plucode=a.plucode)
                  and (plucode not like ''298%'' or plucode in (''298213'',''298214'',''298215'')) and plucode not like ''299%'' and plucode not in (''300809'')
                  and exists(select * from t_shop where orgcode=a.orgcode and overdate<a.accdate)'
  set @ssql=@ssql+@sfilt
  set @ssql=@ssql+' group by a.orgcode,a.plucode,a.pluname'
  exec(@ssql)
  
  set @ssql='insert into ##tmpoverqry(orgcode,errtype,plucode,xscount,kccount)
               select a.orgcode,''3'',a.plucode,0,0
                 from t_int_flow_item a,t_plu b,t_plu_org c
                where a.plucode=b.plucode and a.plucode=c.plucode and a.orgcode=c.orgcode
                  and (isnull(c.supcode,'''')<>'''' and not exists(select * from t_supplier where supcode=c.supcode)
                   or isnull(b.supcode,'''')<>'''' and not exists(select * from t_supplier where supcode=b.supcode))
                  and exists(select * from t_shop where orgcode=a.orgcode and overdate<a.accdate)'
  set @ssql=@ssql+@sfilt
  exec(@ssql)
  
  set @ssql='insert into ##tmpoverqry(orgcode,errtype,plucode,xscount,kccount)
               select a.orgcode,''4'',a.plucode,isnull(c.packpluqty,0)+isnull(d.pluqty,0),a.gcount+a.ccount
                 from t_plu_org a
                 left join t_plu b on a.plucode=b.plucode
                 left join (select i.orgcode,p.plucode,sum(i.pluqty*p.counts) packpluqty from t_plupack p,t_int_flow_item i
                             where p.packcode=i.plucode and exists(select * from t_shop where orgcode=i.orgcode and overdate<i.accdate) group by p.plucode,i.orgcode) c
                             on a.plucode=c.plucode and a.orgcode=c.orgcode
                 left join (select orgcode,plucode,sum(pluqty) as pluqty from t_int_flow_item j where exists(select * from t_shop where orgcode=j.orgcode and overdate<j.accdate)
                            group by plucode,orgcode) d on a.plucode=d.plucode and a.orgcode=d.orgcode
                where b.plutype<>''3'' and b.plutype<>''4'' and a.mngstock=''1''
                  and a.gcount+a.ccount-isnull(c.packpluqty,0)-isnull(d.pluqty,0)<0
                  and isnull(c.packpluqty,0)+isnull(d.pluqty,0)<>0
                  and not exists(select * from t_mix_product where plucode=a.plucode)'
  set @ssql=@ssql+@sfilt
  exec(@ssql)
  
  set @ssql='insert into ##tmpoverqry(orgcode,errtype,plucode,xscount,kccount)
               select a.orgcode,''5'',a.plucode,b.counts*c.mixcnt,a.gcount+a.ccount
                 from t_plu_org a,t_mix_stuff b,(select orgcode,mixcode,mixcnt
                      from (select i.orgcode,p.mixcode,i.plucode,ceiling(sum(i.pluqty)/p.counts) as mixcnt,
                                   row_number() over(partition by i.orgcode,p.mixcode,i.plucode order by ceiling(sum(i.pluqty)/p.counts) desc) as rowid
                              from t_int_flow_item i,t_mix_product p where i.plucode=p.plucode and exists(select * from t_shop where orgcode=i.orgcode and overdate<i.accdate)
                             group by i.orgcode,p.mixcode,i.plucode,p.counts) t where rowid=1) c
                     where a.plucode=b.plucode and a.orgcode=c.orgcode and b.mixcode=c.mixcode and b.counts*c.mixcnt>a.gcount+a.ccount'
  set @ssql=@ssql+@sfilt
  exec(@ssql)
  
  set @ssql='select d.areaname as regnname,c.areaname,a.orgcode,b.orgname,b.overdate,
                    case errtype when ''0'' then ''本日不存在销售流水'' when ''1'' then ''本日不存在支付日报'' when ''2'' then ''流水中有商品不存在''
                                 when ''3'' then ''流水中商品供应商不存在'' when ''4'' then ''存在负库存数据'' when ''5'' then ''BOM存在负库存数据'' else ''其他'' end as r_errtype,
                    a.plucode,e.pluname,a.xscount,a.kccount,a.xscount-a.kccount as diffcount
               from ##tmpoverqry a
               left join t_shop b on a.orgcode=b.orgcode
               left join t_area c on b.areacode=c.areacode
               left join t_area d on c.uppercode1=d.areacode
               left join t_plu e on a.plucode=e.plucode
              order by d.areacode,c.areacode,a.orgcode,a.plucode'
  exec(@ssql)
  
  if exists(select name from tempdb..sysobjects where name='##tmpoverqry' and xtype='u') 
    drop table ##tmpoverqry
end
go
