set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
--------- sql---------------
ALTER procedure [dbo].[aa_autofinishedbatch]
@finishedbillid integer
as
declare @vitemno integer;
declare @vdrawitemno integer;
declare @vgoodsid integer;
declare @vquantity t_quantity;
declare @vflowid integer;
declare @vdrawbillid integer ;
declare @vdrawqty t_quantity;
declare @vdrawreferqty t_quantity;
declare @vmanuordbillid integer;
declare @vbatchqty t_quantity;
declare @local_tab table(vitemno int,
vgoodsid int,
vquantity double precision);
declare @localtmp_tab table(vflowid int,
vdrawbillid int,
vdrawitemno int,
vdrawqty double precision,
vdrawreferqty double precision);
declare ar_reckon_cursor1 cursor for
select *
from @local_tab;
declare ar_reckon_cursor2 cursor for
select *
from @localtmp_tab;
begin
select @vmanuordbillid = referbillid from mnf_finished where billid = @finishedbillid
/* 非个别计价的材料要自动配对 领料的数量 */
insert into @local_tab
select a.itemno,a.goodsid,a.quantity from mnf_finisheddetail a , l_goods b
where a.goodsid = b.goodsid
and billid = @finishedbillid and b.costtype <> 2 ;
open ar_reckon_cursor1
fetch next from ar_reckon_cursor1
into @vitemno,@vgoodsid,@vquantity;
while @@fetch_status = 0
begin
/* 选出同一加工单的领料记录www.52mvc.com */
delete from @localtmp_tab
insert into @localtmp_tab
select f.id,d.billid,d.itemno,d.quantity,d.referqty
from i_drawdetail d , aa_billflow f
where d.billid = f.billid and d.itemno=f.itemno and f.billtype = 25
and d.quantity <> d.referqty and d.goodsid = @vgoodsid
and d.referbillid = @vmanuordbillid
order by f.billdate,f.id ;
open ar_reckon_cursor2
fetch next from ar_reckon_cursor2
into @vflowid, @vdrawbillid,@vdrawitemno,@vdrawqty,@vdrawreferqty
while @@fetch_status = 0
begin
if (@vquantity >0.0000001 )
begin
if (@vdrawqty < 0 )
begin
set @vbatchqty = @vdrawqty - @vdrawreferqty ;
end
else if (@vdrawqty - @vdrawreferqty - @vquantity > 0.0000001 )
begin
set @vbatchqty = @vquantity;
end
else begin
set @vbatchqty = @vdrawqty - @vdrawreferqty;
end
/*增加mnf_finishedbatch*/
insert into mnf_finishedbatch (billid,itemno,breferid,quantity)
values (@finishedbillid,@vitemno,@vflowid,@vbatchqty) ;
/*更新领料单累计引用数*/
update i_drawdetail set referqty = referqty + @vbatchqty,
refercount = refercount +1
where billid = @vdrawbillid and itemno = @vdrawitemno;
set @vquantity = @vquantity - @vbatchqty ;
end
fetch next from ar_reckon_cursor2
into @vflowid, @vdrawbillid,@vdrawitemno,@vdrawqty,@vdrawreferqty
end--while @@fetch_status = 0
close ar_reckon_cursor2
if (@vquantity > 0.0000001)
begin
exec throwexception 'mnf_finishedautobaterror';
break
end
fetch next from ar_reckon_cursor1
into @vitemno,@vgoodsid,@vquantity;
end--while @@fetch_status = 0
close ar_reckon_cursor1
deallocate ar_reckon_cursor1;
deallocate ar_reckon_cursor2;
end
set QUOTED_IDENTIFIER ON
go
--------- sql---------------
ALTER procedure [dbo].[aa_autofinishedbatch]
@finishedbillid integer
as
declare @vitemno integer;
declare @vdrawitemno integer;
declare @vgoodsid integer;
declare @vquantity t_quantity;
declare @vflowid integer;
declare @vdrawbillid integer ;
declare @vdrawqty t_quantity;
declare @vdrawreferqty t_quantity;
declare @vmanuordbillid integer;
declare @vbatchqty t_quantity;
declare @local_tab table(vitemno int,
vgoodsid int,
vquantity double precision);
declare @localtmp_tab table(vflowid int,
vdrawbillid int,
vdrawitemno int,
vdrawqty double precision,
vdrawreferqty double precision);
declare ar_reckon_cursor1 cursor for
select *
from @local_tab;
declare ar_reckon_cursor2 cursor for
select *
from @localtmp_tab;
begin
select @vmanuordbillid = referbillid from mnf_finished where billid = @finishedbillid
/* 非个别计价的材料要自动配对 领料的数量 */
insert into @local_tab
select a.itemno,a.goodsid,a.quantity from mnf_finisheddetail a , l_goods b
where a.goodsid = b.goodsid
and billid = @finishedbillid and b.costtype <> 2 ;
open ar_reckon_cursor1
fetch next from ar_reckon_cursor1
into @vitemno,@vgoodsid,@vquantity;
while @@fetch_status = 0
begin
/* 选出同一加工单的领料记录www.52mvc.com */
delete from @localtmp_tab
insert into @localtmp_tab
select f.id,d.billid,d.itemno,d.quantity,d.referqty
from i_drawdetail d , aa_billflow f
where d.billid = f.billid and d.itemno=f.itemno and f.billtype = 25
and d.quantity <> d.referqty and d.goodsid = @vgoodsid
and d.referbillid = @vmanuordbillid
order by f.billdate,f.id ;
open ar_reckon_cursor2
fetch next from ar_reckon_cursor2
into @vflowid, @vdrawbillid,@vdrawitemno,@vdrawqty,@vdrawreferqty
while @@fetch_status = 0
begin
if (@vquantity >0.0000001 )
begin
if (@vdrawqty < 0 )
begin
set @vbatchqty = @vdrawqty - @vdrawreferqty ;
end
else if (@vdrawqty - @vdrawreferqty - @vquantity > 0.0000001 )
begin
set @vbatchqty = @vquantity;
end
else begin
set @vbatchqty = @vdrawqty - @vdrawreferqty;
end
/*增加mnf_finishedbatch*/
insert into mnf_finishedbatch (billid,itemno,breferid,quantity)
values (@finishedbillid,@vitemno,@vflowid,@vbatchqty) ;
/*更新领料单累计引用数*/
update i_drawdetail set referqty = referqty + @vbatchqty,
refercount = refercount +1
where billid = @vdrawbillid and itemno = @vdrawitemno;
set @vquantity = @vquantity - @vbatchqty ;
end
fetch next from ar_reckon_cursor2
into @vflowid, @vdrawbillid,@vdrawitemno,@vdrawqty,@vdrawreferqty
end--while @@fetch_status = 0
close ar_reckon_cursor2
if (@vquantity > 0.0000001)
begin
exec throwexception 'mnf_finishedautobaterror';
break
end
fetch next from ar_reckon_cursor1
into @vitemno,@vgoodsid,@vquantity;
end--while @@fetch_status = 0
close ar_reckon_cursor1
deallocate ar_reckon_cursor1;
deallocate ar_reckon_cursor2;
end