Roba, PS

use Susa2013arhiva
go

if object_id('tempdb..#z')is not null
drop table #z

create table #z
(acWareHouse char (30),
  acIdent char (16),
  anQty float)

 

insert into #z (acWareHouse, acIdent, anQty)
select acWarehouse, acIdent, sum(anQty) as anQty from
(select g.acReceiver as acWarehouse, p.acIdent, sum(p.anQty) as anQty
from tHE_Move g
inner join tHE_MoveItem p on g.acKey = p.acKey and g.adDate <= '2013-12-31'
inner join tPA_setDocType d on g.acDocType = d.acDoctype and d.acEvidence = 'F'
inner join tHE_SetItem m on p.acIdent = m.acIdent and m.acSetOfItem in (select vrstams from vrstams where tip not in ('S', 'U') and vrstams <> '')
where g.acReceiverStock = 'Y'
group by g.acReceiver, p.acIdent
    union all
select g.acIssuer as acWarehouse, p.acIdent, sum(p.anQty*-1) as anQty
from tHE_Move g
inner join tHE_MoveItem p on g.acKey = p.acKey and g.adDate <= '2013-12-31'
inner join tPA_setDocType d on g.acDocType = d.acDoctype and d.acEvidence = 'F'
inner join tHE_SetItem m on p.acIdent = m.acIdent and m.acSetOfItem in (select vrstams from vrstams where tip not in ('S', 'U') and vrstams <> '')
where g.acIssuerStock = 'Y'
group by g.acIssuer, p.acIdent) as tbl
group by acWarehouse, acIdent
having sum(anQty) <> 0

use susa
go

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



declare btsPoz cursor local fast_forward for
    select acWarehouse from #z group by acWarehouse
open btsPoz
fetch from btsPoz into @acWarehouse
while @@fetch_status = 0
begin

-- Kreiraj glavu
exec pHE_MoveCreAll 'PSS0', @acWarehouse, @acWarehouse, '2013-12-31', 2, '', @cKey output, 'F'

if object_id('tempdb..#mitem') is not null
    drop table #mitem

create table #mitem
(acIdent char (16),
anQty float,
anNo int identity (1,1) not null)

insert into #mitem (acIdent, anQty)
select acIdent, anQty from #z where acWarehouse = @acWarehouse

 

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 m.anBuyPrice * t.anQty, m.anBuyPrice * t.anQty, m.anBuyPrice * t.anQty, m.anBuyPrice * t.anQty, m.anBuyPrice * t.anQty, 
    (m.anBuyPrice * t.anQty) * (m.anVAT / 100 + 1 ), (m.anBuyPrice * t.anQty) * (m.anVAT / 100 + 1 ),
    (m.anBuyPrice * t.anQty) * m.anVAT / 100 , (m.anBuyPrice * t.anQty) * m.anVAT / 100 , (m.anBuyPrice * t.anQty) * m.anVAT / 100 , (m.anBuyPrice * t.anQty) * m.anVAT / 100 ,
    m.anBuyPrice, t.anQty, @cKey, t.anNo, m.acIdent, m.acName, m.acUM, m.anBuyPrice, 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, m.anBuyPrice, 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 #mitem t on m.acIdent = t.acIdent
    left join tHE_SetItemPriceForWrh c on m.acIdent = c.acIdent and c.acWarehouse = @acWarehouse

fetch from btsPoz into @acWarehouse
end
close btsPoz
deallocate btsPoz