	SELECT
			iv.invoiceFullNR,
			--CASE WHEN iv.status = 0 THEN 'SZKIC'				--szkic
			--	WHEN iv.status = 1 THEN 'ZATW'						--zatwierdzony
			--	WHEN iv.status = 2 THEN 'SFIN' END invoicestatus,	--FK
			CONVERT(VARCHAR(10), iv.invoiceIssueDate, 120) invoiceIssueDate,
			YEAR(iv.invoiceIssueDate) [year],
			MONTH(iv.invoiceIssueDate) [month],
			CONVERT(VARCHAR(7), iv.invoiceIssueDate, 120) yearmonth,
			CONVERT(VARCHAR(10), iv.[saleDate], 120) [saleDate],
			iv.paymentTypeID, --cd_paymentType.dictionaryValue  paymentType,
			CONVERT(VARCHAR(10), iv.[paymentDate], 120) [paymentDate],
			pay.firmnr payerNR,
			pay.firmNameshort payer,
			cd_countrycode.dictionarykey + ' '+ cd_countrycode.dictionaryValue countrycode, -- upper(pay.countrycode) countrycode,
			rec.firmnr  recipientNR,
			rec.firmNameshort recipient,
			UPPER(iv.currency) currency,
			iv.[currencyExchangeRate],
			bill.invoiceFullNR billFullNR,
			iv.netValue	,
			iv.vatValue	,
			iv.grossValue ,
			--iv.sysnetValue,
			--iv.sysvatValue	,
			--iv.sysgrossValue ,
			--iv.invoiceCategory,
			--iv.invoiceNR,
			uu.usernamefull addUsername
		FROM document.invoice iv
			INNER JOIN #tab t ON iv.groupID = t.groupId
			INNER JOIN document.documentType dt ON dt.documentType = iv.invoiceCategory AND dt.componentName = 'saleinvoice'
			LEFT JOIN firm.firm rec ON rec.firmID = iv.recipientID
			LEFT JOIN firm.firm pay ON pay.firmID = iv.payerID
			LEFT JOIN common.dictionary cd_countrycode ON rec.countrycode = cd_countrycode.dictionarykey AND cd_countrycode.dictionaryName = 'countrycode'
			--LEFT JOIN common.dictionary cd_paymentType ON iv.paymentTypeID = cd_paymentType.dictionarykeyGuid AND cd_paymentType.dictionaryName = 'paymenttype'
			LEFT JOIN document.documentRelation br on br.invoiceID = iv.invoiceID and br.documentRelationType = 4--pokazanie paragonów do faktur
			LEFT JOIN [document].[invoice] bill on bill.invoiceID = br.invoiceID2
			LEFT JOIN document.documentRelation br2 on br2.invoiceID = iv.invoiceID and br2.documentRelationType = 5--aby odfiltrować paragony mające faktury
			LEFT JOIN [user].[user] uu on uu.username = iv.addUsername
		WHERE iv.[status]	> -1
			 dt.isProforma =0
			AND br2.documentRelationID is null
		/*
			AND (@dateFROM IS NULL OR (iv.invoiceIssueDate >= @dateFROM AND iv.invoiceIssueDate <= @dateto))
			AND (@priceFROM IS NULL OR (iv.netValue >= @priceFROM AND iv.netValue <= @priceto))
			AND (@payer IS NULL OR pay.firmnr= @payer)
			AND (@recipient IS NULL OR rec.firmnr= @recipient)
			AND (@payerfull IS NULL OR rec.firmName like  '%'+@payerfull+'%')
			AND (@invoiceCategory IS NULL OR filtr.invoiceCategory IS not NULL)
			AND (@countrycode IS NULL OR  pay.countrycode= @countrycode)
			AND (@invoicestatus IS NULL OR  iv.status= @invoicestatus)
			*/
