SELECT
CONVERT(VARCHAR(10),dok_DataWyst,121) as Data,
dok_NrPelny as Numer,
isnull(adr_Nazwa,'kontrahent nieokreślony') as Kontrahent,
CASE WHEN dok_typ in (14,6) THEN isnull(dok_KwGotowka,0) * -1 ELSE isnull(dok_KwGotowka,0) END as Gotówka,
CASE WHEN dok_typ in (14,6) THEN isnull( dok_KwDoZaplaty,0) * -1 ELSE isnull( dok_KwDoZaplaty,0) END as Przelew,
CASE WHEN dok_typ in (14,6) THEN isnull(dok_KwKarta,0) * -1 ELSE isnull(dok_KwKarta,0) END as Karta,
CASE WHEN dok_typ in (14,6) THEN isnull(dok_KwKarta,0) * -1 ELSE isnull(dok_KwPrzelew,0) END as [Zapłacono przelewem]
FROM
dok__Dokument dok LEFT JOIN adr__Ewid adr ON (dok.dok_OdbiorcaId = adr.adr_IdObiektu AND adr.adr_TypAdresu = 1)
WHERE
(dok.dok_Typ IN (2,6,21,14))
AND
(dok_DataWyst BETWEEN {DR: Z okresu::})
AND
(dbo.fnMAKE_DOKPARAM(dok_Typ, dok_Podtyp)!=131073)
ORDER BY Data, dok_Id