if exists(select * from sysobjects where name='p_rz_prepare_pd' and xtype='p')
  drop procedure p_rz_prepare_pd
go
create procedure p_rz_prepare_pd
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as 
begin
  declare @spdtaskno varchar(20)
  declare @sbilltype varchar(1)
  declare @sbillno varchar(20)
  declare @splucode varchar(20)

  select @spdtaskno=pdno,@sbilltype=billtype from t_check_head where billno=@psbillno
  
  if not exists(select pdno from t_check_task where pdno=@spdtaskno and pdstatus='0')
  begin
    set @psmsg='盘点任务['+@spdtaskno+']已经结束'
    return(-1)
  end

  declare cur_pdprepare cursor for select distinct d.billno billno,fp.plucode plucode from t_check_head f,t_check_body d,t_check_body fp 
    where pdno=@spdtaskno and billtype='1' and rzdate<>'' and rzdate is not null and f.billno=d.billno and d.plucode=fp.plucode and d.billno<>@psbillno and fp.billno=@psbillno
  open cur_pdprepare
  fetch next from cur_pdprepare into @sbillno,@splucode
  while @@fetch_status=0
  begin
    if isnull(@psmsg,'')=''
      set @psmsg='['+@sbillno++'-'+@splucode+']'
    else
      set @psmsg=@psmsg+char(13)+'['+@sbillno++'-'+@splucode+']'
    fetch next from cur_pdprepare into @sbillno,@splucode
  end
  close cur_pdprepare
  deallocate cur_pdprepare
  if isnull(@psmsg,'')<>'' 
  begin
    set @psmsg='如下商品存在于其他已经记账的复盘单中'+char(13)+@psmsg+char(13)+'如果仍然要记账当前单据，请将其他复盘单取消记账'
    return(-1)
  end

  if @sbilltype='1'
  begin
    if exists(select billno from t_check_head where pdno=@spdtaskno and billtype='0' and (rzdate='' or rzdate is null))
    begin
      set @psmsg='盘点任务['+@spdtaskno+']存在未记账的实盘单'
      return(-1)
    end
  end

  set @psmsg=''
  return(1)
end	
go

if exists(select * from sysobjects where name='p_rz_pd' and xtype='p')
  drop procedure p_rz_pd
go
create procedure p_rz_pd
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as 
begin
  declare @spdtaskno varchar(20)
  declare @sbilltype varchar(1)
  declare @scurdate varchar(10)
  declare @scurtime varchar(8)
  declare @psnewbillno varchar(20)
  declare @psmsg1 varchar(2000)
  declare @iRtn integer

  select @scurdate=convert(varchar(100),getdate(),120),@scurtime=convert(varchar(100),getdate(),108)

  select @spdtaskno=pdno,@sbilltype=billtype from t_check_head where billno=@psbillno

  update a set pcount=case when @sbilltype='0' then pcount+b.counts when @sbilltype='1' then b.counts end,upddate=b.upddate
    from t_check_snap a,(select pdno,plucode,sum(d.counts) as counts,max(d.upddate) as upddate from t_check_head f,t_check_body d
         where f.billno=d.billno and f.billno=@psbillno and pdno=@spdtaskno group by pdno,plucode) b
   where a.pdno=b.pdno and a.plucode=b.plucode

  update a set upddate=b.upddate from t_check_snap a,(select pdno,plucode,max(d.upddate) as upddate from t_check_head f,t_check_body d
  where f.billno=d.billno and f.billno=@psbillno and pdno=@spdtaskno and rzdate<>'' and rzdate is not null group by pdno,plucode) b
  where a.pdno=b.pdno and a.plucode=b.plucode

  --产生已记账的商品盈亏单，盈亏原因和备注转入，记录数据来源为月度盘点

  while 1=1
  begin
    exec f_get_billno 'yk',@psorgcode,'1',@psnewbillno output
    if exists(select 1 from t_differ_head where billno=@psnewbillno)
    continue
    else
    break
  end
  if @@error<>0 goto lblfail

  insert into t_differ_body(billno,serialno,plucode,pluname,barcode,cargono,spec,unit,price,
                              jprice,netjprice,bookcount,realcount,bookcost,realcost,booknetcost,realnetcost,booktotal,realtotal,
                              ykcount,ykcost,yknetcost,yktotal,jtaxrate,remark)
  select @psnewbillno,a.serialno,a.plucode,g.pluname,a.barcode,g.cargono,g.spec,g.unit,a.price,
             a.jprice,a.jprice/(1+isnull(g.jtaxrate,0)/100.00) as netjprice,ocount as bookcount,counts as realcount,
             round(ocount*a.jprice,2) as bookcost,round(counts*a.jprice,2) as realcost,
             round(ocount*(a.jprice/(1+isnull(g.jtaxrate,0)/100.00)),2) as booknetcost,
             round(counts*(a.jprice/(1+isnull(g.jtaxrate,0)/100.00)),2) as realnetcost,
             ocount*o.price as booktotal,counts*o.price as realtotal,
             (counts - ocount) as ykcount,round((counts - ocount)*a.jprice,2) as ykcost,
             round((counts - ocount)*(a.jprice/(1+isnull(g.jtaxrate,0)/100.00)),2) as yknetcost,
             (counts - ocount)*o.price as yktotal,g.jtaxrate, a.remark
        from t_check_body a join t_plu g on a.plucode=g.plucode
                                 join t_plu_org o on a.plucode=o.plucode and o.orgcode=@psorgcode and o.isright='1'
       where a.billno=@psbillno
  if @@error<>0 
  begin
    set @psmsg='保存盈亏单表体失败'  
    return (0)
  end

  insert into t_differ_head(billno,orgcode,bizdate,billtype,lrdate,lrtime,place,
                            zdrcode,zdrname,usercode,username,
                            chkstatus,remark,supcode)
    select @psnewbillno,upper(@psorgcode),@scurdate,'3',@scurdate,@scurtime,place,
           zdrcode,zdrname,usercode,username,'1' as chkstatus,'数据来源:月度盘点',''
      from t_check_head d where d.billno=@psbillno  
  
  update h set h.ykcost=d.ykcost,h.yknetcost=d.yknetcost,h.yktotal=d.yktotal,h.ykcount = d.ykcount
    from t_differ_head h,(select billno,sum(ykcount) as ykcount, sum(ykcost) as ykcost,sum(yknetcost) as yknetcost,sum(yktotal) as yktotal from t_differ_body where billno=@psnewbillno group by billno) d
   where h.billno=d.billno and h.billno=@psnewbillno

  if @@error<>0 
  begin
    set @psmsg='保存盈亏单表头失败'  
    return (0)
  end
  
  exec p_rz_yk @psorgcode,@psnewbillno,'',@psmsg1
  if @@error<>0 
  begin
    set @psmsg='盈亏单记账失败，'+@psmsg1  
    return (0)
  end

  update t_check_head set rzdate=@scurdate,rztime=@scurtime where billno=@psbillno

  update t_check_task set pdstatus = '1' where pdno = @spdtaskno 

  set @psmsg=''
  return(1)
  lblfail:
  begin
    set @psmsg='实盘单记账失败：自动生成盈亏单失败'
    return (0)
  end
end	
go