if exists(select * from sysobjects where name='p_circle_proc_by_trans' and xtype='p')
  drop procedure p_circle_proc_by_trans
go
create procedure p_circle_proc_by_trans
(
  @pstrmode varchar(1),
  @psbillno varchar(20),
  @psorgcode varchar(10),
  @psmsg varchar(2000) out
)
as
begin
  declare @curdate varchar(10)
  declare @curtime varchar(8)
  declare @pisout varchar(1)
  declare @poutorgcode varchar(10)
  declare @pinorgcode varchar(10)
  declare @splace varchar(1)
  declare @pbilltype varchar(1)
  declare @pplucode varchar(20)
  declare @detail_jprice numeric(19,4)
  declare @detail_netjprice numeric(19,4)
  declare @detail_price numeric(19,4)
  declare @detail_cost numeric(19,4)
  declare @detail_netcost numeric(19,4)
  declare @detail_total numeric(19,4)
  declare @detail_counts numeric(19,4)
  declare @detail_plancounts numeric(19,4)
  declare @detail_depcode varchar(10)
  declare @goods_price numeric(19,4)
  declare @goods_netjprice numeric(19,4)
  declare @goods_jprice numeric(19,4)
  declare @goods_plutype varchar(1)
  declare @goods_mngstock varchar(1)
  declare @goods_jtaxrate numeric(19,4)
  declare @goods_iszfcode varchar(1)
  declare @stock_gcount numeric(19,4)
  declare @stock_ccount numeric(19,4)
  declare @stock_cost numeric(19,4)
  declare @stock_netcost numeric(19,4)
  declare @stock_total numeric(19,4)
  declare @stock_tzamount numeric(19,4)
  declare @jynetcost numeric(19,4)
  declare @differnetcost numeric(19,4)
  declare @sisneedadjnetcost varchar(1)
  declare @kccount numeric(19,4)
  declare @sumtotal numeric(19,4)
  declare @oldj numeric(19,4)
  declare @oldn numeric(19,4)
  declare @oldc numeric(19,4)
  declare @oldt numeric(19,4)
  declare @fxj numeric(19,4)
  declare @fxn numeric(19,4)
  declare @fxc numeric(19,4)
  declare @newj numeric(19,4)
  declare @newn numeric(19,4)
  declare @newc numeric(19,4)
  declare @newt numeric(19,4)
  declare @newjprice numeric(19,4)
  declare @newnetjprice numeric(19,4)
  declare @newstock_tzamount numeric(19,4)
  declare @pgbkformtype varchar(2)
  declare @pgbkjccost numeric(19,4)
  declare @pgbkjcnetcost numeric(19,4)
  declare @jycost numeric(19,4)
  declare @bprichchgflag varchar(1)
  declare @irtncode int
  declare @pdiffertype varchar(1)
  declare @scybillno varchar(20)

  set @bprichchgflag='0'
  set @newjprice=0
  set @newnetjprice=0
  set @sumtotal=0
  set @sisneedadjnetcost='0'
  select @curdate=convert(varchar(10),getdate(),126)
  select @curtime=convert(varchar(10),getdate(),8)
  select @pisout=isout,@poutorgcode=outorgcode,@pinorgcode=inorgcode,@splace=place,@pbilltype=billtype
  from t_outtrans_head where billno=@psbillno

  update t_outtrans_head set rzdate=@curdate,rztime=@curtime where billno=@psbillno and orgcode=@psorgcode
  if @@error<>0
  begin
    set @psmsg='修改单据记账日期失败'
    return(-1)
  end

  if @pisout='0'
  begin
    set @pdiffertype=''
    if @pbilltype='0'
      set @pdiffertype='2'
    else if @pbilltype='1'
      set @pdiffertype='1'
    if exists(select 1 from t_outtrans_body where billno=@psbillno and counts <> PlanCounts)
	begin
	  set @scybillno = ''
	  exec f_get_billno 'cy',@pinorgcode,'1',@scybillno output
	  if @scybillno <> ''
	  begin
	    insert into t_distdiffer_body(billno,supcode,plucode,pluname,jprice,netjprice,counts,differcount,differcost,differnetcost,remark,differmode,depcode,depname)
		select @scybillno,(select supcode from t_plu where plucode = t_outtrans_body.plucode) as supcode,plucode,pluname,JPrice,NetJPrice,PlanCounts,counts-PlanCounts,
		      round((counts-PlanCounts)*JPrice,2),round((counts-PlanCounts)*netjprice,2),'调拨差异生成','0',depcode,depname
		from t_outtrans_body where billno=@psbillno and counts <> PlanCounts

		insert into t_distdiffer_head
		(billno,ywbillNo ,orgcode,bizdate,differtype,contrno,differcount,differcost,differnetcost,lrdate,lrtime,rzdate,rztime,
		 accdate,usercode,username,zdrcode,zdrname,shrcode,shrname,chkstatus,depcode,depname,remark
		)
		select
			@scybillno   -- billno - varchar(20)
		  , @psbillno   -- ywbillNo - varchar(20)
		  , @pinorgcode   -- orgcode - varchar(10)
		  , @curdate   -- bizdate - varchar(10)
		  , @pdiffertype   -- differtype - varchar(1)
		  , ''   -- contrno - varchar(40)
		  , 0 -- differcount - numeric(19, 4)
		  , 0 -- differcost - numeric(19, 4)
		  , 0 -- differnetcost - numeric(19, 4)
		  , @curdate   -- lrdate - varchar(10)
		  , @curtime   -- lrtime - varchar(8)
		  , ''   -- rzdate - varchar(10)
		  , ''   -- rztime - varchar(8)
		  , ''   -- accdate - varchar(10)
		  , usercode   -- usercode - varchar(6)
		  , username   -- username - varchar(10)
		  , zdrcode   -- zdrcode - varchar(6)
		  , zdrname   -- zdrname - varchar(10)
		  , ''   -- shrcode - varchar(6)
		  , ''   -- shrname - varchar(10)
		  , '0'   -- chkstatus - varchar(1)
		  , depcode   -- depcode - varchar(10)
		  , depname   -- depname - varchar(16)
		  , ''   -- remark - varchar(200)
		from t_outtrans_head where billno = @psbillno
		--
		update a
		set differcount=b.differcount,
			differcost=b.differcost,
			differnetcost=b.differnetcost
		from t_distdiffer_head a,(select sum(differcount) differcount, sum(differcost) differcost,sum(differnetcost) differnetcost
								  from t_distdiffer_body where billno = @scybillno )b
		where a.billno = @scybillno

	  end
    end
	update b set counts=PlanCounts,punit='',pcount=0,itemcnt=0,sglcount=PlanCounts,b.cost=round(PlanCounts*b.jprice,2),b.netcost=round(PlanCounts*b.netjprice,2),
		   b.total=case when plutype='2' then 0 else round(PlanCounts*b.price,2) end
	from t_outtrans_body b,t_plu c
	where b.plucode=c.plucode and b.billno=@psbillno and b.counts<>b.PlanCounts
	if @@ROWCOUNT<>0 
	begin
	update a set total=b.total
      from t_outtrans_head a,(select sum(cost) as cost,sum(netcost) as netcost,sum(total) as total from t_outtrans_body where billno=@psbillno) b
     where a.billno=@psbillno
	end
  end
  declare cur_usp_circleprocedurebytrans cursor for select plucode,depcode,jprice,netjprice,price,cost,netcost,total,counts,plancounts from t_outtrans_body where billno=@psbillno
  open cur_usp_circleprocedurebytrans
  fetch next from cur_usp_circleprocedurebytrans into @pplucode,@detail_depcode,@detail_jprice,@detail_netjprice,@detail_price,@detail_cost,@detail_netcost,@detail_total,@detail_counts,@detail_plancounts
  while @@fetch_status=0
  begin
    select @goods_price=o.price,@goods_netjprice=case when g.plutype='2' then 0 else o.netjprice end,
           @goods_jprice=case when g.plutype='2' then 0 else o.jprice end,
           @goods_plutype=g.plutype,@goods_mngstock=o.mngstock,@goods_jtaxrate=g.jtaxrate,
           @stock_gcount=o.gcount,@stock_ccount=o.ccount,
           @stock_cost=case when g.plutype='2' then 0 else o.cost end,
           @stock_netcost=case when g.plutype='2' then 0 else o.netcost end,
           @stock_total=case when g.plutype='2' then 0 else o.total end,
           @stock_tzamount=o.tzamount,
           @goods_iszfcode=isnull(g.iszfcode,'0')
    from t_plu_org o,t_plu g
    where o.plucode=g.plucode and o.plucode=@pplucode and o.orgcode=case when @pisout='1' then @poutorgcode else @pinorgcode end and o.isright='1'
    if @@rowcount=0
    begin
      if @pisout='1'
        set @psmsg='机构['+@poutorgcode+']没有商品['+@pplucode+ ']的经营权限'
      else
        set @psmsg='机构['+@pinorgcode+']没有商品['+@pplucode+ ']的经营权限'
      close cur_usp_circleprocedurebytrans
      deallocate cur_usp_circleprocedurebytrans
      return(-1)
    end

    set @kccount=@stock_gcount+@stock_ccount
    set @sumtotal=@sumtotal+@detail_total

    if @pstrmode='0'
    begin
      if @kccount>0
      begin
        set @sisneedadjnetcost='0'
        if @splace='0'
          set @stock_gcount=@stock_gcount+@detail_plancounts
        else
          set @stock_ccount=@stock_ccount+@detail_plancounts
        set @kccount=@stock_gcount+@stock_ccount
        if @stock_tzamount<>0 
        begin
          if (@stock_tzamount<0) and (@stock_tzamount+@detail_cost<0)
          begin
            set @newstock_tzamount=@stock_tzamount+@detail_cost
            set @stock_cost=@stock_cost
            set @stock_netcost=@stock_netcost
          end
          else
          begin
            set @newstock_tzamount=0
            set @stock_cost=@stock_cost+@detail_cost+@stock_tzamount
            set @stock_netcost=@stock_netcost+@detail_netcost+round((@stock_tzamount/(1+@goods_jtaxrate/100.00)),2)
          end
          set @stock_tzamount=@newstock_tzamount
        end
        else
        begin
          set @stock_cost=@stock_cost+@detail_cost
          set @stock_netcost=@stock_netcost+@detail_netcost
        end
        if @goods_plutype='2'
          set @stock_total=0.00 
        else
          set @stock_total=round((@goods_price*@kccount),2)

        set @newjprice=@stock_cost/@kccount
        set @newnetjprice=@stock_netcost/@kccount
      end
      else
      begin
        set @sisneedadjnetcost='1'
        if @splace='0'
          set @stock_gcount=@stock_gcount+@detail_plancounts
        else
          set @stock_ccount=@stock_ccount+@detail_plancounts
        set @kccount=@stock_gcount+@stock_ccount
        set @newjprice=@detail_jprice
        set @newnetjprice=@detail_netjprice
        set @jycost=@stock_cost+@detail_cost
        set @jynetcost=@stock_netcost+@detail_netcost+round((@stock_tzamount/(1+@goods_jtaxrate/100.00)),2)
        set @stock_cost=round((@newjprice*@kccount),2)
        set @stock_netcost=round((@newnetjprice*@kccount),2)
        if @goods_plutype='2'
          set @stock_total=0.00
        else
          set @stock_total=round((@goods_price*@kccount),2)  
        set @stock_tzamount=@stock_tzamount+@jycost-@stock_cost
      end
    end
    else
    begin
      set @sisneedadjnetcost='1'
      set @oldj=@goods_jprice
      set @oldn=@stock_gcount+@stock_ccount
      set @oldc=@stock_cost
      set @oldt=@stock_tzamount
      set @fxj=@detail_jprice
      set @fxn=@detail_counts
      set @fxc=@detail_cost
              
      if @splace='0'
        set @stock_gcount=@stock_gcount-@detail_counts
      else
        set @stock_ccount=@stock_ccount-@detail_counts
      set @jynetcost=@stock_netcost-@detail_netcost+round((@stock_tzamount/(1+@goods_jtaxrate/100.00)),2)
      set @kccount=@stock_gcount+@stock_ccount
      if ((@fxn>=@oldn) and (@fxc<=@oldc)) or ((@fxn<=@oldn) and (@fxc>=@oldc))
      begin
        set @newj=@oldj
        set @newn=@oldn-@fxn
        set @newc=round((@newj*@newn),2)
        set @newt=@oldt+@oldc-@fxc-@newc
        set @newjprice=@newj
        set @newnetjprice=@goods_netjprice
        set @stock_cost=@newc
        set @stock_netcost=round((@newnetjprice*@newn),2)
        if @goods_plutype='2'
          set @stock_total=0.00
        else
          set @stock_total=round((@goods_price*@newn),2)
        set @stock_tzamount=@newt
      end
      else
      begin
        if @stock_tzamount<>0
        begin
          if (@stock_tzamount>0) and (@stock_tzamount-@detail_cost>0)
          begin
            set @newstock_tzamount=@stock_tzamount-@detail_cost
            set @stock_cost=@stock_cost
            set @stock_netcost=@stock_netcost
          end
          else
          begin
            set @newstock_tzamount=0
            set @stock_cost=@stock_cost-@detail_cost+@stock_tzamount
            set @stock_netcost=@stock_netcost-@detail_netcost+round((@stock_tzamount/(1+@goods_jtaxrate/100.00)),2)
          end
          set @stock_tzamount=@newstock_tzamount
        end
        else
        begin
          set @stock_cost=@stock_cost-@detail_cost
          set @stock_netcost=@stock_netcost-@detail_netcost
        end
        if @goods_plutype='2'
          set @stock_total=0.00
        else
          set @stock_total=round((@goods_price*@kccount),2)
        set @newjprice=@stock_cost/@kccount
        set @newnetjprice=@stock_netcost/@kccount
      end
    end
    if @goods_price<>@detail_price  
    begin    
      update b set b.price=@goods_price,b.total=case when g.plutype='2' then 0 else @goods_price*@detail_plancounts end
      from t_outtrans_body b left join t_plu g on b.plucode=g.plucode
      where 1=1 and (b.billno=@psbillno) and b.plucode=@pplucode
      if @@error<>0
      begin
        set @psmsg='修改单据体中商品['+@pplucode+']的售价信息失败'
        close cur_usp_circleprocedurebytrans
        deallocate cur_usp_circleprocedurebytrans
        return(-1)
      end
      set @bprichchgflag='1'
    end    

    if (@sisneedadjnetcost='1') and ((@jynetcost-@stock_netcost-round(@stock_tzamount/(1+@goods_jtaxrate/100.00),2))<>0)
    begin
      set @stock_netcost=@jynetcost-round(@stock_tzamount/(1+@goods_jtaxrate/100.00),2)
    end
    update t_plu_org set jprice=@newjprice,netjprice=@newnetjprice,
                         gcount=@stock_gcount,ccount=@stock_ccount,
                         tzamount=@stock_tzamount,
                         cost=@stock_cost,netcost=@stock_netcost,total=@stock_total,
                         yjdate=@curdate
    where plucode=@pplucode and orgcode=case when @pisout='1' then @poutorgcode else @pinorgcode end and isright='1'
    if @@error<>0
    begin
      set @psmsg='修改商品['+@pplucode+']的库存信息失败'
      close cur_usp_circleprocedurebytrans
      deallocate cur_usp_circleprocedurebytrans
      return(-1)
    end
    if @goods_iszfcode='2'
    begin
      update t_plu_org set jprice=@newjprice,netjprice=@newnetjprice 
       where plucode in (select plucode from t_plu where innercode=@pplucode and isnull(iszfcode,'0')='2') 
             and orgcode=case when @pisout='1' then @poutorgcode else @pinorgcode end
   end

    set  @pgbkformtype=case when @pbilltype='0' then 'G' else 'F' end
    set @pgbkjccost=@stock_cost+@stock_tzamount
    set @pgbkjcnetcost=@stock_netcost+round(@stock_tzamount/(1+@goods_jtaxrate/100.00),2)

    if @pisout='1'
      exec @irtncode=p_create_gbk_data @curdate,@curtime,@pplucode,@detail_depcode,@psbillno,@pgbkformtype,@detail_jprice,@detail_netjprice,@detail_price,
                                       @detail_counts,@detail_cost,@detail_netcost,@detail_total,@kccount,@pgbkjccost,@pgbkjcnetcost,@stock_total,@poutorgcode,'','',@psmsg out
    else
    begin
      set @detail_plancounts=-@detail_plancounts
      set @detail_cost=-@detail_cost
      set @detail_netcost=-@detail_netcost
      set @detail_total=-@detail_total
      exec @irtncode=p_create_gbk_data @curdate,@curtime,@pplucode,@detail_depcode,@psbillno,@pgbkformtype,@detail_jprice,@detail_netjprice,@detail_price,
                                       @detail_plancounts,@detail_cost,@detail_netcost,@detail_total,@kccount,@pgbkjccost,@pgbkjcnetcost,@stock_total,@pinorgcode,'','',@psmsg out
    end
    if @@error<>0
    begin
      set @psmsg='记录商品['+@pplucode+']的台账信息失败'
      close cur_usp_circleprocedurebytrans
      deallocate cur_usp_circleprocedurebytrans
      return(-1)
    end
    if @irtncode=-1 
    begin
      set @psmsg='记录商品['+@pplucode+']的台账信息失败：'+isnull(@psmsg,'')
      close cur_usp_circleprocedurebytrans
      deallocate cur_usp_circleprocedurebytrans
      return(-1)
    end

    fetch next from cur_usp_circleprocedurebytrans into @pplucode,@detail_depcode,@detail_jprice,@detail_netjprice,@detail_price,@detail_cost,@detail_netcost,@detail_total,@detail_counts,@detail_plancounts
  end
  close cur_usp_circleprocedurebytrans
  deallocate cur_usp_circleprocedurebytrans
  if @bprichchgflag='1'
  begin
    update a set total=b.total
      from t_outtrans_head a,(select sum(cost) as cost,sum(netcost) as netcost,sum(total) as total from t_outtrans_body where billno=@psbillno) b
     where a.billno=@psbillno
    if @@error<>0
    begin
      set @psmsg='修改单据头中的售价金额汇总数据失败'
      return(-1)
    end
  end

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

if exists(select * from sysobjects where name='p_out_trans_create_in_bill' and xtype='p')
  drop procedure p_out_trans_create_in_bill
go
create procedure p_out_trans_create_in_bill
(
  @psbillno varchar(20),
  @psoutorgcode varchar(10),
  @psusercode varchar(6),
  @psusername varchar(10),
  @psnewbillno varchar(20) out,
  @pserrmsg varchar(2000) out
)
as
begin
  declare @pinorgcode varchar(10)
  declare @poutorgcode varchar(10)
  declare @pinplace varchar(1)
  declare @pbilltype varchar(4)
  declare @pintransno varchar(20)
  declare @pinorgtype varchar(1)
  declare @pmakertype varchar(1)
  declare @curver varchar(1)
  declare @curorgcode varchar(10)

  select @curorgcode=itemvalue from t_syscfg where itemname='branchno'
  select @curver=itemvalue from t_syscfg where itemname='ver'

  select @pmakertype=makertype,@pintransno=intransno,@pinorgcode=inorgcode,@poutorgcode=outorgcode,@pinplace=place,@pbilltype=case when billtype='0' then 'yd' else 'dd' end
    from t_outtrans_head where billno=@psbillno and orgcode=@psoutorgcode
  if @@error<>0
  begin
    set @pserrmsg='查询调出单信息出错'
    return(-1)
  end
  if @pbilltype is null
  begin
    set @pserrmsg='没有查询到调出单信息'
    return(-1)
  end

  select @pinplace=case when orgtype='2' then '1' when orgtype<>'2' and isdbdeploy='1' then '0' end from t_shop where orgcode=@pinorgcode
  if @pinplace is null
  begin
    set @pserrmsg='没有查询到调入机构信息'
    return(-1)
  end  

  if @pmakertype='0'
    exec f_get_billno @pbilltype,@curorgcode,'1',@psnewbillno output 
  else
  begin
    if @curver='0'
      set @psnewbillno=@pintransno
    else
      exec f_get_billno @pbilltype,@curorgcode,'1',@psnewbillno output 
  end
  if @psnewbillno is null
  begin
    set @pserrmsg='生成调入单单据号失败'
    return(-1)
  end  

  insert into t_outtrans_head(billno,intransno,bizdate,outorgcode,outorgname,depcode,depname,inorgcode,inorgname,place,
                              counts,cost,netcost,total,prntimes,accdate,rzdate,rztime,chkstatus,billtype,zdrcode,zdrname,usercode,
                              username,shrcode,shrname,isout,orgcode,makertype,qrdate,lrdate,lrtime)
  select @psnewbillno,@psbillno,bizdate,outorgcode,outorgname,depcode,depname,inorgcode,inorgname,@pinplace,
         counts,cost,netcost,total,0,accdate,'','','0',billtype,@psusercode,@psusername,@psusercode,
         @psusername,'','','0',inorgcode,makertype,'',convert(varchar(10),getdate(),126),convert(varchar(8),getdate(),8)
    from t_outtrans_head
   where (billno=@psbillno) and orgcode=@psoutorgcode
  if @@error<>0
  begin
    set @pserrmsg='生成调入单单头信息失败'
    return(-1)
  end  

  insert into t_outtrans_body(billno,serialno,plucode,depcode,depname,barcode,pluname,cargono,spec,unit,punit,itemcnt,pcount,
                             sglcount,jprice,netjprice,price,cost,netcost,total,counts,plancounts,jtaxrate,remark)
  select @psnewbillno,a.serialno,a.plucode,case when g.plucode is null then a.depcode else g.depcode end,case when g.plucode is null then a.depname else d.depname end,
         case when g.plucode is null then a.barcode else g.barcode end,a.pluname,case when g.plucode is null then a.cargono else g.cargono end,
         case when g.plucode is null then a.spec else g.spec end,a.unit,a.punit,a.itemcnt,a.pcount,
         a.sglcount,a.jprice,a.netjprice,case when g.plucode is null then a.price else o.price end,a.cost,a.netcost,
         case when g.plucode is null and g.plutype='2' then 0.00
              when g.plucode is null and g.plutype<>'2' then a.total
              when not g.plucode is null and g.plutype='2' then 0.00
              when not g.plucode is null and g.plutype<>'2' then o.price*a.counts
         end,a.counts,a.plancounts,a.jtaxrate,a.remark
  from t_outtrans_body a left join t_plu g on a.plucode=g.plucode
                         left join t_dept d on g.depcode=d.depcode
                         left join t_plu_org o on a.plucode=o.plucode and o.orgcode=@pinorgcode
  where a.billno=@psbillno
  if @@error<>0
  begin
    set @pserrmsg='生成调入单单体信息失败'
    return(-1)
  end  

  update a set total=b.total
    from t_outtrans_head a,(select sum(cost) as cost,sum(netcost) as netcost,sum(total) as total from t_outtrans_body
   where billno=@psnewbillno) b
  where a.billno=@psnewbillno
  if @@error<>0
  begin
    set @pserrmsg='修改调入单单头信息失败'
    return(-1)
  end

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

if exists(select * from sysobjects where name='p_rz_prepare_dd' and xtype='p')
  drop procedure p_rz_prepare_dd
go
create procedure p_rz_prepare_dd
(
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as
begin
  declare @splace varchar(1)
  declare @serrmsg varchar(2000)
  declare @irtncode int
  declare @pisout varchar(1)
  declare @poutorgcode varchar(10)
  declare @pinorgcode varchar(10)
  declare @pplucode varchar(20)
  declare @ppluname varchar(40)
  
  set @psmsg=''

  select @pisout=isout,@poutorgcode=outorgcode,@pinorgcode=inorgcode from t_outtrans_head where billno=@psbillno
  
  declare cur_rzprocprepare_dd cursor for
    select g.plucode,g.pluname from t_plu g left join t_plu_org o on g.plucode=o.plucode and o.orgcode=case when @pisout='1' then @poutorgcode else @pinorgcode end
     where g.plucode in (select plucode from t_outtrans_body where billno=@psbillno) and isnull(o.isright,'0')='0'
  open cur_rzprocprepare_dd
  fetch next from cur_rzprocprepare_dd into @pplucode,@ppluname
  while @@fetch_status=0
  begin
    if @psmsg=''
      set @psmsg=@ppluname
    else
      set @psmsg=@psmsg+char(13)+@ppluname
    fetch next from cur_rzprocprepare_dd into @pplucode,@ppluname
  end
  close cur_rzprocprepare_dd
  deallocate cur_rzprocprepare_dd
  if @psmsg<>''
  begin
    set @psmsg='如下商品在机构['+case when @pisout='1' then @poutorgcode else @pinorgcode end+']中没有经营权限：'+char(13)+@psmsg
    return(-1)
  end 
  if @pisout='1'
  begin
  select @splace=place from t_outtrans_head where billno=@psbillno
  exec @irtncode=p_check_plu_stock_rz @poutorgcode,@psbillno,@splace,'t_outtrans_head','t_outtrans_body',@psparams,@serrmsg output
  if @@error<>0
  begin
    set @psmsg='判断是否负库存失败：未知错误'
    return(-1)
  end
  else if @irtncode=-1
  begin
    set @psmsg='判断是否负库存失败：'+isnull(@serrmsg,'')
    return(-1)
  end
  else if isnull(@serrmsg,'')<>''
  begin
    set @psmsg='以下商品负库存：'+@serrmsg
	return(-1)
  end
  end

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

if exists(select * from sysobjects where name='p_rz_prepare_yd' and xtype='p')
  drop procedure p_rz_prepare_yd
go
create procedure p_rz_prepare_yd
(
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as
begin
  declare @irtncode int
  exec @irtncode=p_rz_prepare_dd @psorgcode,@psbillno,@psparams,@psmsg out
  if @@error <>0 
  begin
    set @psmsg='记账前判断执行出错'
    return(-1)
  end
  if @irtncode=-1
  begin
    set @psmsg='记账前判断执行出错：'+isnull(@psmsg,'')
    return(-1)
  end

  set @psmsg=''
  return(1)
end

go

if exists(select * from sysobjects where name='p_rz_dd' and xtype='p')
  drop procedure p_rz_dd
go
create procedure p_rz_dd
(
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as
begin
  declare @pcurorgtype varchar(1)
  declare @pcurorgcode varchar(10)
  declare @pisout varchar(1)
  declare @pmakertype varchar(1)
  declare @poutorgcode varchar(10)
  declare @pinorgcode varchar(10)
  declare @pbilltype varchar(1)
  declare @splace varchar(1)
  declare @poutorgtype varchar(1)
  declare @pinorgtype varchar(1)
  declare @curdate varchar(10)
  declare @curtime varchar(8)
  declare @pplucode varchar(20)  
  declare @kccount numeric(19,4)
  declare @sumtotal numeric(19,4)
  declare @pintranbillno varchar(20)
  declare @irtncode int
  declare @pdiffertype varchar(1)
  declare @pusercode varchar(6)
  declare @pusername varchar(10)
  
  set @pusercode=''
  set @pusername=''
  set @pusercode=@psparams
  select @pusername=username from t_user where usercode=@pusercode
  select @curdate=convert(varchar(10),getdate(),126)
  select @curtime=convert(varchar(10),getdate(),8)
  select @pcurorgtype=itemvalue from t_syscfg where itemname='ver'
  select @pcurorgcode=itemvalue from t_syscfg where itemname='branchno'
  select @pisout=isout,@pmakertype=makertype,@poutorgcode=outorgcode,@pinorgcode=inorgcode,@pbilltype=billtype,@splace=place from t_outtrans_head where billno=@psbillno
  select @poutorgtype=isdbdeploy from t_shop where orgcode=@poutorgcode
  select @pinorgtype=isdbdeploy from t_shop where orgcode=@pinorgcode

  set @sumtotal=0
  set @kccount=0

  if @pcurorgtype='1'
  begin
    if @pbilltype='1'
    begin
      if @pisout='1'
      begin
        if @poutorgtype='0'
        begin
          set @psmsg='该单据是独立门店的调出单，总部不能为其记账'
          return(-1)
        end
        else if @poutorgtype='1'
        begin
          exec @irtncode=p_circle_proc_by_trans '1',@psbillno,@poutorgcode,@psmsg out
          if @irtncode=-1
          begin
            set @psmsg='循环处理库存失败：'+isnull(@psmsg,'')
            return(-1)
          end

          update t_outtrans_head set qrdate=@curdate
          where billno=@psbillno and orgcode=@poutorgcode
          if @@error <>0
          begin
            set @psmsg='设置确认日期失败'
            return(-1)
          end

          if @pinorgtype='1'
          begin
            exec @irtncode=p_out_trans_create_in_bill @psbillno,@poutorgcode,@pusercode,@pusername,@pintranbillno out,@psmsg out
            if @@error <>0
            begin
              set @psmsg='生成调入单失败'
              return(-1)
            end
            if @irtncode=-1
            begin
              set @psmsg='生成调入单失败：'+isnull(@psmsg,'')
              return(-1)
            end
			if not exists(select * from t_shop where orgcode=@pinorgcode and isdbconfirm='1')
			begin
				exec @irtncode=p_rz_sum 'dd',@pinorgcode,@pintranbillno,'',@psmsg out
				if @irtncode=-1
				begin
				  set @psmsg='记账生成的调入单【'+@pintranbillno+'】失败：'+isnull(@psmsg,'')
				  return(-1)
				end
			end
          end
        end
      end
      else if @pisout='0'
      begin
        if @pinorgtype='0'
        begin
          set @psmsg='该单据是独立门店的调入单，总部不能为其记账'
          return(-1)
        end
        else if @pinorgtype='1'
        begin
          exec @irtncode=p_circle_proc_by_trans '0',@psbillno,@pinorgcode,@psmsg out
          if @irtncode=-1
          begin
            set @psmsg='循环处理库存失败：'+isnull(@psmsg,'')
            return(-1)
          end
          
          if not exists(select*from t_shop where orgcode=@pinorgcode and isdbconfirm='1')
          begin
            update t_outtrans_head set qrdate=@curdate where billno=@psbillno and orgcode=@pinorgcode
            if @@error<>0
            begin
              set @psmsg='修改单据确认日期失败' 
              return(-1)
            end
          end
        end
      end
    end
    else if @pbilltype='0'
    begin
      if @pisout='1'
      begin
        exec @irtncode=p_circle_proc_by_trans '1',@psbillno,@poutorgcode,@psmsg out
        if @irtncode=-1
        begin
          set @psmsg='循环处理库存失败：'+isnull(@psmsg,'')
          return(-1)
        end
        update t_outtrans_head set qrdate=@curdate where billno=@psbillno and orgcode=@poutorgcode
        if @@error<>0
        begin
          set @psmsg='设置单据确认日期失败'
          return(-1)
        end

		exec @irtncode=p_out_trans_create_in_bill @psbillno,@poutorgcode,@pusercode,@pusername,@pintranbillno out,@psmsg out
		if @irtncode=-1
		begin
			set @psmsg='生成调入单失败：'+isnull(@psmsg,'')
			return(-1)
		end
		
		--移库单不用移入库再点生效；自动生效
		--if not exists(select * from t_shop where orgcode=@pinorgcode and isdbconfirm='1')
		begin
			exec @irtncode=p_rz_sum 'yd',@pinorgcode,@pintranbillno,'',@psmsg out
			if @irtncode=-1
			begin
			  set @psmsg='记账生成的调入单['+@pintranbillno+']失败：'+isnull(@psmsg,'')
			  return(-1)
			end
		end
      end
      else if @pisout='0'
      begin
        exec @irtncode=p_circle_proc_by_trans '0',@psbillno,@pinorgcode,@psmsg out
        if @irtncode=-1
        begin
          set @psmsg='循环处理库存失败：'+isnull(@psmsg,'')
          return(-1)
        end
        if not exists(select * from t_shop where orgcode=@pinorgcode and isdbconfirm='1')
        begin
          update t_outtrans_head set qrdate=@curdate where billno=@psbillno and orgcode=@pinorgcode
          if @@error<>0
          begin
            set @psmsg='设置单据确认日期失败'
            return(-1)
          end
        end
      end
    end
  end

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

if exists(select*from sysobjects where name='p_rz_yd' and xtype='p')
  drop procedure p_rz_yd
go
create procedure p_rz_yd
(
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as
begin
  declare @irtncode int
  exec @irtncode=p_rz_dd @psorgcode,@psbillno,@psparams,@psmsg out
  if @@error<>0
  begin
    set @psmsg='执行记账过程失败'
    return(-1)
  end
  if @irtncode=-1
  begin
    set @psmsg='执行记账过程失败：'+isnull(@psmsg,'')
    return(-1)
  end

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