
/****** Object:  View [dbo].[ApiHistLinesView]    Script Date: 13.09.2021 15:15:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[ApiHistLinesView]
AS
SELECT     TOP (100) PERCENT poz.ID AS ErpId, dok.ID AS IdDocId, poz.Lp AS lp, dok.Kontrahent AS CustId, dok.NumerPelny AS NumberString, dok.Data AS Date, poz.Towar AS itemId, 
                      poz.Nazwa AS itemName, poz.IloscMagazynuSymbol AS itemUM, CAST(poz.Rabat AS Decimal(18, 2)) AS discount, CAST(poz.CenaValue AS Decimal(18, 2)) AS itemOrgPrice, 
                      CAST(poz.IloscMagazynuValue AS Decimal(18, 2)) AS quantity, CAST(poz.SumaNetto / poz.IloscMagazynuValue AS Decimal(18, 2)) AS itemPrice, CAST(poz.SumaNetto AS Decimal(18, 2)) AS value, 
                      c.vendorId
FROM         dbo.PozycjeDokHan AS poz INNER JOIN
                      dbo.DokHandlowe AS dok ON poz.Dokument = dok.ID INNER JOIN
                      dbo.ApiCustomerView AS c ON c.CustId = dok.Kontrahent
WHERE     (dok.Data > DATEADD(MONTH, - 1, CURRENT_TIMESTAMP)) AND (dok.Definicja = 1)
ORDER BY Date DESC


GO


