You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
14 KiB

if exists(select * from sysobjects where name='p_rz_prepare_jj' and xtype='p')
drop procedure p_rz_prepare_jj
go
create procedure p_rz_prepare_jj
(
@psorgcode varchar(10),
@psbillno varchar(20),
@psparams varchar(200),
@psmsg varchar(2000) out
)
as
declare @sitemvalue varchar(200)
begin
declare @ssupcode varchar(15)
declare @ssupstatus varchar(1)
declare @ssupvalidstatus varchar(1)
declare @ssupcontractstatus varchar(1)
select @ssupcode=supcode from t_adjbuyprice_head where billno=@psbillno
if not exists(select * from t_supplier where supcode=@ssupcode)
begin
set @psmsg='单据中指定的供应商不存在,不能记账'
return(-1)
end
select @ssupstatus=supstatus,
@ssupvalidstatus=case when (isnull(validdate,'')='' or validdate>=convert(varchar(100),getdate(),23)) then '1' else '0' end,
@ssupcontractstatus=case when (not exists(select * from t_contract where supcode=t_supplier.supcode and contrstatus='0')) or
(exists(select * from t_contract where supcode=t_supplier.supcode and contrstatus='0' and convert(varchar(100),getdate(),23) between bgndate and enddate)) then '1' else '0' end
from t_supplier where supcode=@ssupcode
if @ssupstatus <> '0'
begin
set @psmsg='单据中指定的供应商状态非正常,不能记账'
return(-1)
end
if @ssupvalidstatus='0'
begin
set @psmsg='单据中指定的供应商已过有效期,不能记账'
return(-1)
end
if @ssupcontractstatus='0'
begin
set @psmsg='单据中指定的供应商合同已过期,不能记账'
return(-1)
end
set @psmsg=''
return(1)
end
go
if exists(select * from sysobjects where name='p_rz_jj' and xtype='p')
drop procedure p_rz_jj
go
create procedure p_rz_jj
(
@psorgcode varchar(10),
@psbillno varchar(20),
@psparams varchar(200),
@psmsg varchar(2000) out
)
as
declare @sorgcode varchar(10)
declare @finacode varchar(10)
declare @outfinacode varchar(10) --对外财务主体
declare @ssameaccorg varchar(1)
declare @sisdbdeploy varchar(1)
declare @sorgtype varchar(1)
declare @sver varchar(1)
declare @stztype varchar(1)
declare @stag varchar(1)
declare @siszsvendor varchar(1)
declare @sdate varchar(10)
declare @svbktbl varchar(20)
declare @bookdate varchar(10)
declare @booktime varchar(8)
declare @ssql varchar(8000)
begin
set @psmsg=''
select @sver=itemvalue from t_syscfg where section='SYSTEM' and itemname='VER'
select @sorgcode=orgcode,@stztype=tztype,@stag=tag from t_adjbuyprice_head where billno=@psbillno
select @sisdbdeploy=isdbdeploy,@sorgtype=orgtype,@finacode=isnull(finacode,'') from t_shop where orgcode=@sorgcode
select @sdate=convert(varchar(10),getdate(),20)
if @@error <> 0
begin
set @psmsg='进价调整单记账初始化变量失败'
return -1
end
exec p_get_finacode 'C009',@sorgcode,@outfinacode out,@ssameaccorg out ,@psmsg out
if @psmsg <> ''
begin
return (-1)
end
if @stztype='2'
begin
update o set ojprice=d.newjprice
from t_plu_org o,t_adjbuyprice_body d
where d.billno=@psbillno and o.orgcode=@sorgcode and o.plucode=d.plucode
if @@error <> 0
begin
set @psmsg='进价调整单记账更新最新进货价失败'
return -1
end
else
return 1
end
select @svbktbl='t_vbk_'+substring(@sdate,1,4)+substring(@sdate,6,2)
exec p_create_vbk @svbktbl,@psmsg out
if @psmsg<>''
begin
return -1
end
if @sver='0' or @stztype='0' or @sisdbdeploy='1'
begin
if ((@sver='0') or (@sver='1' and @sisdbdeploy='1' and @stag='1'))
begin
if exists(select name from tempdb..sysobjects where name='##tmpjjtz_d' and xtype='u')
drop table ##tmpjjtz_d
create table ##tmpjjtz_d
(billno varchar(20),plucode varchar(20),jprice numeric(19,4),netjprice numeric(19,4),newjprice numeric(19,4),newnetjprice numeric(19,4),
tzcount numeric(19,4),cjcost numeric(19,4),netcjcost numeric(19,4),stock_counts numeric(19,4),stock_cost numeric(19,4),
tzcjcost numeric(19,4),nettzcjcost numeric(19,4),g_jtaxrate numeric(19,4),g_jprice numeric(19,4),g_tzamount numeric(19,4))
insert into ##tmpjjtz_d(billno,plucode,jprice,netjprice,newjprice,newnetjprice,tzcount,cjcost,netcjcost,
stock_counts,stock_cost,tzcjcost,nettzcjcost,g_jtaxrate,g_jprice,g_tzamount)
select billno,d.plucode,d.jprice,d.netjprice,d.newjprice,d.newnetjprice,d.tzcount,d.cjcost,d.netcjcost,
o.gcount+o.ccount,o.cost,0 tzcjcost,0 tznetcjcost,g.jtaxrate,o.jprice,o.tzamount
from t_adjbuyprice_body d join t_plu_org o on d.plucode=o.plucode join t_plu g on d.plucode=g.plucode
where d.billno=@psbillno and o.orgcode=@sorgcode
update ##tmpjjtz_d
set cjcost=round(newjprice*stock_counts,2)-round(jprice*stock_counts,2),
netcjcost=round(round(newjprice*stock_counts,2)/(1+g_jtaxrate/100.00),2)-round(round(jprice*stock_counts,2)/(1+g_jtaxrate/100.00),2),
tzcjcost=cjcost-(round(newjprice*stock_counts,2)-round(jprice*stock_counts,2)),
nettzcjcost=netcjcost-round(round(newjprice*stock_counts,2)/(1+g_jtaxrate/100.00),2) -round(round(jprice*stock_counts,2)/(1+g_jtaxrate/100.00),2)
where stock_counts>0 and tzcount>stock_counts
update ##tmpjjtz_d
set g_jprice=case when g_jprice>newjprice then newjprice else g_jprice end
where stock_counts>0 and (stock_cost+cjcost<0)
if @stztype='0'
begin
print('调历史')
--update o set o.jprice=case when (stock_counts>0) and (stock_cost+tmp.cjcost>=0) then round((stock_cost+tmp.cjcost)/stock_counts,4)
-- when (stock_counts>0) and (stock_cost+tmp.cjcost<0) then g_jprice else o.jprice end,
-- o.cost=case when (stock_counts>0) and (stock_cost+tmp.cjcost>=0) then round(round((stock_cost+tmp.cjcost)/stock_counts,4)*stock_counts,2)
-- when (stock_counts>0) and (stock_cost+tmp.cjcost<0) then round(g_jprice*stock_counts,2) else o.cost end,
-- o.tzamount=case when stock_counts<=0 then o.tzamount+tmp.cjcost
-- when (stock_counts>0) and (stock_cost+tmp.cjcost>=0) then o.tzamount+tmp.tzcjcost+stock_cost+tmp.cjcost-round(round((stock_cost+tmp.cjcost)/stock_counts,4)*stock_counts,2)
-- when (stock_counts>0) and (stock_cost+tmp.cjcost<0) then o.tzamount+stock_cost+d.cjcost-round(g_jprice*stock_counts,2) else o.tzamount end
-- from t_plu_org o
-- join t_adjbuyprice_body d on o.plucode=d.plucode and o.orgcode=@sorgcode and d.billno=@psbillno
-- join ##tmpjjtz_d tmp on d.plucode=tmp.plucode
-- where o.isright='1'
--update o set o.netjprice=case when (stock_counts > 0) then round(o.jprice/(1+g_jtaxrate/100.00),4) else o.netjprice end,
-- o.netcost=case when (stock_counts > 0) then o.netcost+round(tmp.g_tzamount/(1+g_jtaxrate/100.00),2)+d.netcjcost-round(o.tzamount/(1+g_jtaxrate/100.00),2) else o.netcost end
-- from t_plu_org o
-- join t_adjbuyprice_body d on o.plucode=d.plucode and o.orgcode=@sorgcode and d.billno=@psbillno
-- join ##tmpjjtz_d tmp on d.plucode=tmp.plucode
-- where o.isright='1'
--update o set ojprice=d.newjprice
-- from t_plu_org o,t_adjbuyprice_body d
-- where o.orgcode=@sorgcode and o.plucode=d.plucode and d.billno=@psbillno and d.tzcount=0
--update g set jprice=d.newjprice,netjprice=d.newnetjprice
-- from t_plu g,t_adjbuyprice_body d
-- where g.plucode=d.plucode and d.billno=@psbillno and d.tzcount=0
end
else
begin
update d set jprice=o.jprice,
netjprice=o.netjprice,
tzcount=o.gcount+o.ccount,
cost=o.cost,
netcost=o.netcost,
cjcost=round(d.newjprice*(o.gcount+o.ccount)-o.cost,2),
netcjcost=round(d.newnetjprice*(o.gcount+o.ccount)-o.netcost,2),
newcost=round(d.newjprice*(o.gcount+o.ccount),2),
newnetcost=round(d.newnetjprice*(o.gcount+o.ccount),2)
from t_adjbuyprice_body d join t_plu_org o on d.plucode=o.plucode and d.billno=@psbillno and o.orgcode=@sorgcode
where o.isright='1'
update g set jprice=d.newjprice,
netjprice=d.newnetjprice
from t_plu g join t_adjbuyprice_body d on g.plucode=d.plucode and d.billno=@psbillno
update o set jprice=d.newjprice,
netjprice=d.newnetjprice,
ojprice=d.newjprice,
hjprice=case when hjprice>d.newjprice then hjprice else d.newjprice end,
ljprice=case when ljprice<d.newjprice then ljprice else d.newjprice end,
cost=round(d.newjprice*(o.gcount+o.ccount),2),
netcost=o.netcost+round(tmp.g_tzamount/(1+g_jtaxrate/100.00),2)+d.netcjcost-round(o.tzamount/(1+g_jtaxrate/100.00),2)
from t_adjbuyprice_body d join t_plu_org o on d.plucode=o.plucode and d.billno=@psbillno and o.orgcode=@sorgcode
join ##tmpjjtz_d tmp on d.plucode=tmp.plucode
where o.isright='1'
--
select @bookdate=convert(varchar(10),getdate(),20),@booktime=convert(varchar(8),getdate(),8)
update t_gbk_yyyymm set jprice=round((fscost+d.cjcost)/(fscount+0),4),
netjprice=round((fsnetcost+d.netcjcost)/(fscount+0),4),
price=o.price,
fscost=fscost+d.cjcost,
fsnetcost=fsnetcost+d.netcjcost,
jccount=o.gcount+o.ccount,
jccost=o.cost+o.tzamount,
jcnetcost=o.netcost+round(o.tzamount/(1+d.jtaxrate/100.00),2),
jctotal=o.total,
tag='',
accdate=''
from t_gbk_yyyymm gbk join t_adjbuyprice_body d on gbk.plucode=d.plucode and d.billno=@psbillno
join t_plu_org o on d.plucode=o.plucode and o.orgcode=@sorgcode
where bookdate=@bookdate and booktime=@booktime and gbk.plucode=d.plucode and gbk.depcode=d.depcode
and d.billno=@psbillno and billtype='4' and gbk.orgcode=@sorgcode
if @@error<>0
begin
set @psmsg='更新商品台账失败'
return -1
end
insert into t_gbk_yyyymm
(bookdate,booktime,plucode,depcode,billno,billtype,jprice,netjprice,price,fscount,
fscost,fsnetcost,fstotal,jccount,jccost,jcnetcost,jctotal,tag,orgcode,accdate)
select @bookdate,@booktime,d.plucode,d.depcode,@psbillno,'4',o.jprice,o.netjprice,o.price,0,d.cjcost,d.netcjcost,0,
o.gcount+o.ccount,o.cost+o.tzamount,o.netcost+round(o.tzamount/(1+d.jtaxrate/100.00),2),o.total,'',@sorgcode,''
from t_adjbuyprice_body d join t_plu_org o on d.plucode=o.plucode and d.billno=@psbillno and o.orgcode=@sorgcode
where not exists(select plucode from t_gbk_yyyymm where bookdate=@bookdate and booktime=@booktime and plucode=d.plucode
and depcode=d.depcode and billno=@psbillno and billtype='4' and orgcode=@sorgcode)
if @@error<>0
begin
set @psmsg='插入商品台账失败'
return -1
end
end
if @stztype='1'
begin
update a set cost=b.cost,
netcost=b.netcost,
cjcost=b.cjcost,
netcjcost=b.netcjcost,
newcost=b.newcost,
newnetcost=b.newnetcost,
tzcount=b.tzcount
from t_adjbuyprice_head a,
(select sum(cost) cost,sum(netcost) netcost,sum(cjcost) cjcost,sum(netcjcost) netcjcost,
sum(newcost) newcost,sum(newnetcost) newnetcost,sum(tzcount) tzcount
from t_adjbuyprice_body where billno=@psbillno) b
where (a.billno=@psbillno)
end
end
end --@sver='0' or @stztype='0' or @sisdbdeploy='1'
--if @stztype='1'
begin
if not (@sver='1' and @stztype='1' and @sisdbdeploy='0')
begin
update t_supplier set tjtotal=tjtotal+w.cjcost
from t_supplier v join t_adjbuyprice_head w on v.supcode=w.supcode
where w.billno=@psbillno
select @bookdate=convert(varchar(10),getdate(),20),@booktime=convert(varchar(8),getdate(),8)
set @ssql='insert into '+@svbktbl+'(orgcode,bookdate,booktime,supcode,billno,billtype,fsamount,yfjytotal,prejytotal)
select '''+@sorgcode+''','''+@bookdate+''','''+@booktime+''',w.supcode ,'''+@psbillno+''',
''4'',w.cjcost,v.jhtotal-v.thtotal+v.tjtotal,v.prepay
from t_supplier v join t_adjbuyprice_head w on v.supcode=w.supcode
where billno='''+@psbillno+''''
exec (@ssql)
end
end
if not (@sver='1' and @stztype='1' and @sisdbdeploy='0')
begin
if exists (select supcode from t_supplier where supcode=(select supcode from t_adjbuyprice_head where billno=@psbillno) and suptype=@sver)
set @siszsvendor='1'
else
set @siszsvendor='0'
insert into t_pay_bill(billtype,billno,orgcode,bizdate,supcode ,sqauremode,cost,paydate,jscost,zrcost,isfinished,contrno,finacode)
select billtype=case when @siszsvendor='1' and @sver='1' then '5' else '2' end,
@psbillno,@sorgcode,rzdate,supcode,sqauremode,cjcost,paydate,0.00,0.00,
isfinished=case when cjcost=0 then '1' else '0' end,contrno,@outfinacode
from t_adjbuyprice_head
where billno=@psbillno
if @ssameaccorg <> '1'
begin
insert into t_pay_bill(billtype,billno,orgcode,bizdate,supcode ,sqauremode,cost,paydate,jscost,zrcost,isfinished,contrno,finacode)
select '8' as billtype,
@psbillno,@sorgcode,rzdate,'C2000',sqauremode,cjcost,paydate,0.00,0.00,
isfinished=case when cjcost=0 then '1' else '0' end,contrno,@finacode
from t_adjbuyprice_head
where billno=@psbillno
end
end
if exists(select a.plucode from t_adjbuyprice_body a,t_plu b where a.plucode=b.plucode and isnull(b.iszfcode,'0')='1' and a.billno=@psbillno)
begin
update a set jprice=b.jprice,netjprice=b.netjprice from t_plu_org a,
(select plucode,jprice,netjprice,orgcode from t_plu_org where plucode in (select plucode from t_adjbuyprice_body where billno=@psbillno)) b,
(select plucode,innercode from t_plu where iszfcode='2') c
where a.plucode=c.plucode and b.plucode=c.innercode and a.orgcode=b.orgcode
end
set @psmsg=''
return(1)
end
go