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

SET QUOTED_IDENTIFIER ON
GO




CREATE VIEW [dbo].[ApiPaymentView]
AS
SELECT     roz.ID AS Id, c.Id AS CustId, roz.Numer AS Document, roz.ID ErpId,
ISNULL([dbo].[ApiPozycjeDok](p.Dokument),'') Description,
cast (ISNULL(p.Dokument,0) as int) DocId,cast (ISNULL(p.Dokument,0) as int)  DocErpId,
 convert(datetime,roz.Data,120) AS DocumentDate,
 convert(datetime,roz.DataRozliczenia,120) SettlementDate, roz.KwotaValue AS Total,
  convert(datetime,roz.Termin,120) AS DueDate, roz.KwotaValue - roz.KwotaRozliczonaValue AS ToPay, roz.KwotaSymbol Currency,
  p.Kierunek Direction, 0 PaymentType, CURRENT_TIMESTAMP TS,
                     0 AS VendorId
FROM         dbo.RozrachunkiIdx AS roz  
                      --JOIN dbo.Opiekunowie op On op.Kontrahent = roz.Podmiot and roz.PodmiotType = 'Kontrahenci' and op.Typ = 0 and op.Aktywny=1
					    LEFT OUTER JOIN 
						dbo.Platnosci p ON p.DokumentType='DokHandlowe' and p.ID = roz.Dokument
JOIN dbo.ApiCustomers c ON c.ErpId = roz.Podmiot

WHERE     (roz.PodmiotType = 'Kontrahenci') AND (roz.DokumentType = 'Platnosci') AND (convert(datetime,roz.DataRozliczenia,120) =  convert(datetime,'2099-12-31',120)) AND (roz.Zwrot = 0)
and roz.Typ =10






GO


