Pocetno stanje iz tHE_Stock

use zuins
go

declare @acWarehouse char (30),
@adDate smalldatetime,
@cKey char (13),
@acIdent char (16),
@anQty float,
@cOK varchar(2), -- status of adding
@nPoz int, -- addid position
@cError VarChar(1024)

set @adDate = '2020-01-01'

declare btsPoz cursor local fast_forward for
select distinct acWarehouse from [ZUINSdo2019].dbo.tHE_Stock
open btsPoz
fetch from btsPoz into @acWarehouse
while @@fetch_status = 0
begin
-- Kreiraj glavu
exec pHE_MoveCreAll '1PS0', @acWarehouse, @acWarehouse, @adDate, 2, '', @cKey output, 'F'


insert into tHE_MoveItem (anPVValue, anPVOCVATBase, anPVOCValue, anPVOCStockValue, anPVOCVATBaseWoExc,
anPVOCForPay, anPVOCForPayWoExc,
anPVOCIncVAT, anPVOCBeatShare, anPVOCIncVATWoExc, anPVOCBeatShareWoExc,
anPriceCurrency, anQty, acKey, anNo, acIdent, acName, acUm, anPrice, anRTPrice, anSalePrice,
acVATCode, anVAT, anStockPrice, anInRTPrice, anInSalePrice,
acVATCodeTR, anVATIn)
select t.anLastPrice * t.anStock, t.anLastPrice * t.anStock, t.anLastPrice * t.anStock, t.anLastPrice * t.anStock, t.anLastPrice * t.anStock,
(t.anLastPrice * t.anStock) * (m.anVAT / 100 + 1 ), (t.anLastPrice * t.anStock) * (m.anVAT / 100 + 1 ),
(t.anLastPrice * t.anStock) * m.anVAT / 100 , (t.anLastPrice * t.anStock) * m.anVAT / 100 , (t.anLastPrice * t.anStock) * m.anVAT / 100 , (t.anLastPrice * t.anStock) * m.anVAT / 100 ,
t.anLastPrice, t.anStock, @cKey, row_number() over (order by m.acName), m.acIdent, m.acName, m.acUM, t.anLastPrice, case when c.anRTPrice is null then m.anRTPrice else c.anRTPrice end, case when c.anSalePrice is null then m.anSalePrice else c.anSalePrice end,
m.acVATCode, m.anVAT, t.anLastPrice, case when c.anRTPrice is null then m.anRTPrice else c.anRTPrice end, case when c.anSalePrice is null then m.anSalePrice else c.anSalePrice end,
m.acVATCode, m.anVAT
from tHE_SetItem m
inner join [ZUINSdo2019].dbo.tHE_Stock t on m.acIdent = t.acIdent and t.acWarehouse = @acWarehouse
left join tHE_SetItemPriceForWrh c on m.acIdent = c.acIdent and c.acWarehouse = @acWarehouse

fetch from btsPoz into @acWarehouse
end
close btsPoz
deallocate btsPoz