if exists(select * from sysobjects where name='p_rz_oq' and xtype='p')
  drop procedure p_rz_oq
go
create procedure p_rz_oq
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as 
begin
  declare @sorgcode varchar(10)
  declare @squotetype varchar(1)
  declare @scontrno varchar(40)
  declare @ssupcode varchar(15)
  declare @scurver varchar(1)
  declare @scurdate varchar(10)
  declare @scurtime varchar(8)  
  declare @susercode varchar(6)
  declare @susername varchar(10)
  declare @sstationid varchar(15)
  declare @soptcontent varchar(100)
  declare @ilen int
  declare @iindex int
  declare @stmp varchar(100)  
  
  select @susername=substring(@stmp,1,@iindex-1),@sstationid=substring(@stmp,@iindex+1,@ilen)
  select @sorgcode=orgcode,@squotetype=quotetype,@scontrno=isnull(contrno,''),@ssupcode=supcode from t_orderquote_head where billno=@psbillno
  select @scurdate=convert(varchar(100),getdate(),23),@scurtime=convert(varchar(100),getdate(),108)

  if @squotetype='0'
  begin
    update a set a.jtaxrate=src.jtaxrate,
                 a.jprice=src.jprice,
                 a.netjprice=src.netjprice,
                 a.billno=src.billno,
                 a.usercode=src.usercode,
                 a.username=src.username,
                 a.rzdate=src.rzdate,
                 a.rztime=src.rztime,
                 a.dsccode=src.dsccode,
                 a.remark=src.remark
    from t_order_quote a,
      (select f.billno,s.orgcode,f.quotetype,f.supcode,f.supname,f.contrno,isnull(f.bgndate,'') bgndate,isnull(f.enddate,'') enddate,
              d.plucode,d.pluname,d.barcode,d.spec,d.cargono,d.unit,d.jtaxrate,s.jprice,s.netjprice,f.usercode,f.username,f.rzdate,f.rztime,f.dsccode,d.remark
         from t_orderquote_head f,t_orderquote_body d,t_orderquote_org s
        where f.billno=d.billno and d.plucode=s.plucode and d.billno=s.billno and f.billno=@psbillno)src
        where a.orgcode=src.orgcode and a.quotetype=src.quotetype and a.supcode=src.supcode and a.contrno=src.contrno and a.plucode=src.plucode
    if @@error <> 0
    begin
      set @psmsg='更新价格失败'
      return(-1)
    end
    insert into t_order_quote(billno,orgcode,quotetype,supcode,supname,contrno,bgndate,enddate,plucode,pluname,barcode,spec,cargono,unit,jtaxrate,jprice,netjprice,usercode,username,rzdate,rztime,dsccode,remark)
      select f.billno,s.orgcode,f.quotetype,f.supcode,f.supname,f.contrno,isnull(f.bgndate,''),isnull(f.enddate,'') ,
             d.plucode,d.pluname,d.barcode,d.spec,d.cargono,d.unit,d.jtaxrate,s.jprice,s.netjprice,f.usercode,f.username,f.rzdate,f.rztime,f.dsccode,d.remark
        from t_orderquote_head f,t_orderquote_body d,t_orderquote_org s
       where f.billno=d.billno and d.plucode=s.plucode and d.billno=s.billno and f.billno=@psbillno
             and not exists(select 1 from t_order_quote where orgcode=s.orgcode and quotetype=f.quotetype and supcode=f.supcode and contrno=f.contrno and plucode=d.plucode)
    if @@error <> 0
    begin
      set @psmsg='插入价格失败'
      return(-1)
    end
  end
  else if @squotetype='1'
  begin
    update a set billno=b.billno,jprice=b.jprice,netjprice=b.netjprice,usercode=b.usercode,username=b.username,rzdate=b.rzdate,rztime=b.rztime,dsccode=b.dsccode,remark=b.remark
	  from t_order_quote a,
	  (select f.billno,s.orgcode,f.quotetype,f.supcode,f.supname,f.contrno,isnull(f.bgndate,'') bgndate,isnull(f.enddate,'') enddate,
             d.plucode,d.pluname,d.barcode,d.spec,d.cargono,d.unit,d.jtaxrate,s.jprice,s.netjprice,f.usercode,f.username,f.rzdate,f.rztime,f.dsccode,d.remark
        from t_orderquote_head f,t_orderquote_body d,t_orderquote_org s
       where f.billno=d.billno and d.plucode=s.plucode and d.billno=s.billno and f.billno=@psbillno) b
	  where a.orgcode=b.orgcode and a.quotetype=b.quotetype and a.supcode=b.supcode and a.contrno=b.contrno and a.plucode=b.plucode and a.bgndate=b.bgndate and a.enddate=b.enddate
    insert into t_order_quote(billno,orgcode,quotetype,supcode,supname,contrno,bgndate,enddate,plucode,pluname,barcode,spec,cargono,unit,jtaxrate,jprice,netjprice,usercode,username,rzdate,rztime,dsccode,remark)
      select f.billno,s.orgcode,f.quotetype,f.supcode,f.supname,f.contrno,isnull(f.bgndate,''),isnull(f.enddate,''),
             d.plucode,d.pluname,d.barcode,d.spec,d.cargono,d.unit,d.jtaxrate,s.jprice,s.netjprice,f.usercode,f.username,f.rzdate,f.rztime,f.dsccode,d.remark
        from t_orderquote_head f,t_orderquote_body d,t_orderquote_org s
       where f.billno=d.billno and d.plucode=s.plucode and d.billno=s.billno and f.billno=@psbillno
             and not exists(select 1 from t_order_quote where orgcode=s.orgcode and quotetype=f.quotetype 
                            and supcode=f.supcode and contrno=f.contrno and plucode=d.plucode and bgndate=f.bgndate and enddate=f.enddate)
    if @@error<>0
    begin
      set @psmsg='插入价格失败'
      return(-1)
    end
  end
  
  --若合同不为*且不为空，则更新对应合同商品中的明细
  if (@scontrno <> '*') and (@scontrno <> '')
  begin
	INSERT INTO t_contr_plu(contrno,serialno,plucode,barcode,pluname,cargono,spec,unit,jtaxrate,jprice,netjprice,remark)
	   SELECT @scontrno,row_number() over(order by a.plucode) AS serialno,a.plucode,barcode,pluname,cargono,spec,unit,jtaxrate,a.jprice,netjprice,'合同报价自动增加' AS remark  FROM
		(SELECT oq.plucode,MIN(oq.JPrice) AS JPrice FROM t_order_quote oq WHERE oq.billno=@psbillno AND oq.contrno=@scontrno AND oq.supcode=@ssupcode
		GROUP BY plucode) a, t_plu b WHERE a.PluCode=b.PluCode
		and not exists(select 1 from t_contr_plu where contrno=@scontrno and plucode=a.plucode)
  end
  
  update t_orderquote_head set rzdate=@scurdate,rztime=@scurtime where billno=@psbillno
  set @soptcontent='合同报价：'+@psbillno+'入账'

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