





CREATE VIEW [dbo].[ApiStockView2]
AS

select p.ID BatchId,p.ID LineId, z.Towar ItemId, z.Magazyn MagId,  RTRIM(LTRIM(lok.Data)) LocationName,
Kierunek FlowDirection, p.ID SourceBatchId,
PartiaData Date, d.NumerPelny Batch, '' Description,
CONVERT(DECIMAL(16, 4),(PartiaWartosc/z.IloscValue))  Price, 
CONVERT(DECIMAL(16, 4),(z.IloscValue * Kierunek)) Stock,
CONVERT(DECIMAL(16, 4),(z.IloscValue * Kierunek)) OrgStock,
 0 Type
from dbo.Zasoby  z JOIN dbo.DokHandlowe d ON z.PartiaDokument = d.Id
JOIN dbo.PozycjeDokHan p ON p.Dokument = d.ID and p.Ident = z.PartiaPozycjaIdent
JOIN  dbo.OkresyMag o  ON o.ID = z.Okres and o.Zamkniety = 0
--LEFT OUTER JOIN [dbo].[Features] f ON f.Name='DstLokalizacja' and f.ParentType='PozycjeDokHan' and Parent = p.ID
LEFT OUTER JOIN [dbo].[Features] lok ON lok.Name='Lokalizacja' and lok.ParentType='Towary' and lok.Parent = z.Towar and lok.Lp=0
where Kierunek =1 and  PartiaTyp =1 and z.Magazyn =1 
--and z.Towar = 16246















GO

