
/****** Object:  View [dbo].[ApiLinesView]    Script Date: 13.09.2021 15:18:01 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE VIEW [dbo].[ApiLinesView] as
SELECT     0 AS Id, p.ID AS ErpId, 0 AS lineId, 0 AS documentId, p.Dokument AS IdDocId, p.Lp AS lp, t.ID AS itemId, p.Nazwa AS itemName, t.Kod AS itemCode, 
                      p.Opis AS itemDescription, p.IloscMagazynuSymbol AS itemUM,'' itemUM2, CAST(p.SumaNetto / p.IloscMagazynuValue AS Decimal(18, 2)) AS itemPrice, 
                      CAST(p.CenaValue AS Decimal(18, 2)) AS itemOrgPrice, CAST(p.Rabat AS Decimal(18, 2)) AS discount, 
					  
					  CAST( p.IloscMagazynuValue AS Decimal(18, 3)) AS quantity, 

                      CAST(p.SumaNetto AS Decimal(18, 2)) AS value, CAST(0 AS decimal) AS veryfiedQuantity, t.EAN AS itemEAN, 0 vendorId,
                      '' LocationName, '' Batch, 0 BatchId, '' BatchEAN,					                        
					  0 CategoryId, '' CategoryName, '' CategoryName2, 0 Status, 0 ItemStatus, '' lineStatus, 0 LineChance, '' lineType,
					  p.SumaNetto Netto, p.SumaBrutto Brutto, 
					   ISNULL(stawka.Kod, '') AS TaxDesc, ISNULL(CONVERT(DECIMAL(16, 4),  stawka.StawkaProcent * 100), 0) AS TaxRate

FROM         dbo.PozycjeDokHan AS p INNER JOIN
                      dbo.Towary AS t ON t.ID = p.Towar INNER JOIN
                      dbo.ApiDocumentsView AS d ON d.ErpId = p.Dokument		
					   JOIN dbo.DefStawekVat AS stawka ON t.DefinicjaStawki = stawka.ID 	
where  p.IloscMagazynuValue <> 0







GO


