Uporabite lahko stari, preverjeni pPA_StockRecalc tu spodaj, dokler DL ne izda popravka, če gre res za napako.
ALTER procedure [dbo].[pPA_StockRecalc]
@cIdent VarChar(16) = '',
@cSkladisce VarChar(30) = ''
as
set nocount on
create table #lPA_StockRecalc (acWarehouse Char(30) null,
acIdent Char(16) null,
anStock Decimal(19, 6) null,
anValue Float null)
create table #lPA_StockRecalcItem (acIdent Char(16))
create table #lPA_StockRecalcWH (acWarehouse Char(30))
create table #lPA_StockRecalcDT (acDocType Char(4),
acIsValueBasedPost Char(1))
create table #MinOptMaxZaloga (acWarehouse Char(30) null,
acIdent Char(16) null,
anMinStock Decimal(19, 6) null,
anOptStock Decimal(19, 6) null,
anMaxStock Decimal(19, 6) null)
declare
@cpIzdajateljZal Char(1),
@cpPrejemnikZal Char(1),
@cpKljuc Char(13),
@npPoz integer,
@cpIdent Char(16),
@npKolicina Decimal(19, 6),
@cSkladSestBlago Char(30)
select @cSkladSestBlago = acStockForMadeItems
from vPA_SysParam
insert into #lPA_StockRecalcItem (acIdent)
select M.acIdent
from tHE_SetItem M, tHE_SetItemType V
where M.acSetOfItem = V.acSetOfItem and V.acType <> 'S' and V.acType <> 'U' and (@cIdent = '' or M.acIdent like @cIdent)
create index #lPA_StockRecalcItem_0 on #lPA_StockRecalcItem (acIdent)
insert into #lPA_StockRecalcWH (acWarehouse)
select acSubject
from tHE_SetSubj
where acWarehouse = 'T' and acStockManage='R' and (@cSkladisce = '' or acSubject like @cSkladisce)
create index #lPA_StockRecalcWH_0 on #lPA_StockRecalcWH (acWarehouse)
insert into #lPA_StockRecalcDT (acDocType, acIsValueBasedPost)
select acDocType, acIsValueBasedPost
from tPA_SetDocType
where acSetOf in ('F', 'C', 'Q') and acEvidence <> 'T'
create index #lPA_StockRecalcDT_0 on #lPA_StockRecalcDT (acDocType)
insert into #lPA_StockRecalc (acWarehouse, acIdent, anStock, anValue)
select G.acReceiver, P.acIdent, Sum(P.anQty), Sum(P.anQty * P.anStockPrice)
from tHE_Move G, tHE_MoveItem P, #lPA_StockRecalcItem M, #lPA_StockRecalcDT D, #lPA_StockRecalcWH S
where G.acKey = P.acKey and P.acIdent = M.acIdent and G.acDocType = D.acDocType and G.acReceiverStock = 'Y' and
G.acReceiver = S.acWarehouse
group by G.acReceiver, P.acIdent
insert into #lPA_StockRecalc (acWarehouse, acIdent, anStock, anValue)
select G.acReceiver, P.acIdent, Sum(P.anQty), Sum(P.anStockPrice)
from tHE_Move G, tHE_MoveItem P, #lPA_StockRecalcItem M, #lPA_StockRecalcDT D, #lPA_StockRecalcWH S
where G.acKey = P.acKey and P.acIdent = M.acIdent and G.acDocType = D.acDocType and G.acReceiverStock = 'Y' and P.anQty = 0 and D.acIsValueBasedPost = 'T' and
G.acReceiver = S.acWarehouse
group by G.acReceiver, P.acIdent
insert into #lPA_StockRecalc (acWarehouse, acIdent, anStock, anValue)
select G.acIssuer, P.acIdent, Sum(P.anQty * -1), Sum(P.anQty * P.anStockPrice * -1)
from tHE_Move G, tHE_MoveItem P, #lPA_StockRecalcItem M, #lPA_StockRecalcDT D, #lPA_StockRecalcWH S
where G.acKey = P.acKey and P.acIdent = M.acIdent and G.acDocType = D.acDocType and G.acIssuerStock = 'Y' and
G.acIssuer = S.acWarehouse
group by G.acIssuer, P.acIdent
insert into #lPA_StockRecalc (acWarehouse, acIdent, anStock, anValue)
select G.acIssuer, P.acIdent, Sum(P.anQty * -1), Sum(P.anStockPrice * -1)
from tHE_Move G, tHE_MoveItem P, #lPA_StockRecalcItem M, #lPA_StockRecalcDT D, #lPA_StockRecalcWH S
where G.acKey = P.acKey and P.acIdent = M.acIdent and G.acDocType = D.acDocType and G.acIssuerStock = 'Y' and P.anQty = 0 and D.acIsValueBasedPost = 'T' and
G.acIssuer = S.acWarehouse
group by G.acIssuer, P.acIdent
if (@cSkladSestBlago <> '') and /*(@cIdent = '') and*/ exists (select M.acIdent from tHE_SetItem M, tHE_SetItemType V where M.acSetOfItem = V.acSetOfItem and V.acType = 'C') begin
declare crPromet cursor local fast_forward for select G.acIssuerStock, G.acReceiverStock, G.acKey, P.anNo, P.acIdent, P.anQty
from tHE_Move G, tHE_MoveItem P, tHE_SetItem M, tHE_SetItemType V
where G.acKey = P.acKey and P.acIdent = M.acIdent and M.acSetOfItem = V.acSetOfItem and V.acType = 'C' and (@cIdent = '' or M.acIdent=@cIdent)
open crPromet
fetch next from crPromet into @cpIzdajateljZal, @cpPrejemnikZal, @cpKljuc, @npPoz, @cpIdent, @npKolicina
while (@@FETCH_STATUS = 0) begin
if (@cpIzdajateljZal = 'N') and (@cpPrejemnikZal = 'Y')
exec pPA_StockForMAdeItemsRecalc @cSkladSestBlago, @cpKljuc, @npPoz, @cpIdent, @npKolicina, 0
if (@cpIzdajateljZal = 'Y') and (@cpPrejemnikZal = 'N') begin
set @npKolicina = @npKolicina * -1
exec pPA_StockForMAdeItemsRecalc @cSkladSestBlago, @cpKljuc, @npPoz, @cpIdent, @npKolicina, 0
end
fetch next from crPromet into @cpIzdajateljZal, @cpPrejemnikZal, @cpKljuc, @npPoz, @cpIdent, @npKolicina
end
close crPromet
deallocate crPromet
end
create index #lPA_StockRecalc_0 on #lPA_StockRecalc (acWarehouse, acIdent)
create index #MinOptMaxZaloga_0 on #MinOptMaxZaloga (acWarehouse, acIdent)
if @cSkladisce <> ''
delete from #lPA_StockRecalc where acWarehouse <> @cSkladisce
insert into #MinOptMaxZaloga (acWarehouse, acIdent, anMinStock, anOptStock, anMaxStock)
select acWarehouse, acIdent, anMinStock, anOptStock, anMaxStock
from tHE_Stock
where anMinStock <> 0 or anOptStock <> 0 or anMaxStock <> 0
/*
if (@cIdent <> '') or (@cSkladisce <> '')
delete from tHE_Stock where (@cIdent = '' or acIdent like @cIdent) and (@cSkladisce = '' or acWarehouse like @cSkladisce)
else
truncate table tHE_Stock
*/
delete from tHE_Stock
where
exists(select 1 from #lPA_StockRecalc R where R.acWarehouse=tHE_Stock.acWarehouse and R.acIdent=tHE_Stock.acIdent)
insert into tHE_Stock (acWarehouse, acIdent, anStock, anValue)
select acWarehouse, acIdent, Sum(anStock), Sum(anValue)
from #lPA_StockRecalc
group by acWarehouse, acIdent
update tHE_Stock set anLastPrice = 0
where anStock = 0 and (@cIdent = '' or acIdent like @cIdent) and (@cSkladisce = '' or acWarehouse like @cSkladisce)
update tHE_Stock set anLastPrice = anValue/anStock
where anStock <> 0 and (@cIdent = '' or acIdent like @cIdent) and (@cSkladisce = '' or acWarehouse like @cSkladisce)
update t1
set anMinStock = t2.anMinStock,
anOptStock = t2.anOptStock,
anMaxStock = t2.anMaxStock
from tHE_Stock t1
join #MinOptMaxZaloga t2 on t1.acWarehouse = t2.acWarehouse and t1.acIdent = t2.acIdent
insert tHE_Stock (acWarehouse, acIdent, anMinStock, anOptStock, anMaxStock)
select acWarehouse, acIdent, anMinStock, anOptStock, anMaxStock from #MinOptMaxZaloga
where not exists (select * from tHE_Stock where acWarehouse =#MinOptMaxZaloga.acWarehouse and
acIdent = #MinOptMaxZaloga.acIdent)