SELECT DISTINCT 
		CASE WHEN ivcf.id_abastecimento IS NOT NULL
			THEN ci.id_categoria_item
			ELSE '999999'
			END AS id_categoria_item,
		
		CASE WHEN ivcf.id_abastecimento IS NOT NULL
			THEN ci.denominacao
			ELSE 'LUBRIFICANTE / DIVERSOS'
			END AS categoria,
	
		CASE WHEN ivcf.id_abastecimento IS NOT NULL
			THEN i.codigo
			ELSE '999999'
			END AS codigo, 
		CASE WHEN ivcf.id_abastecimento IS NOT NULL
			THEN i.denominacao
			ELSE 'LUBRIFICANTE / DIVERSOS'
			END AS item 	
		
		FROM movimento_venda_terminal AS mvt
		INNER JOIN venda_cf AS vcf ON (vcf.id_movimento_venda_terminal = mvt.id_movimento_venda_terminal)
		INNER JOIN item_venda_cf AS ivcf ON (ivcf.id_venda_cf = vcf.id_venda_cf)
		INNER JOIN item AS i ON (i.id_item = ivcf.id_item)
		INNER JOIN categoria_item AS ci ON (i.id_categoria_item = ci.id_categoria_item)		
		INNER JOIN sis_empresa AS se ON (se.id_empresa = mvt.id_empresa)
		
	WHERE	mvt.data_movimento BETWEEN date_trunc('month',CAST(:DataFinal AS DATE)) AND :DataFinal
	AND	DATE(vcf.data_cupom) BETWEEN date_trunc('month',CAST(:DataFinal AS DATE)) AND :DataFinal
	AND	DATE(ivcf.data_venda) BETWEEN date_trunc('month',CAST(:DataFinal AS DATE)) AND :DataFinal
	AND	mvt.id_empresa = :IdEmpresaObrigatorio
	
	AND	vcf.cancelada = 'N'
	AND	ivcf.cancelado = 'N'

ORDER BY 1,3