<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Classificacao ABC" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="782" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" uuid="258943ae-f5c3-45ba-b50e-622290a5d31f">
	<property name="ireport.scriptlethandling" value="0"/>
	<property name="ireport.encoding" value="UTF-8"/>
	<property name="ireport.zoom" value="1.5"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<import value="net.sf.jasperreports.engine.*"/>
	<import value="java.util.*"/>
	<import value="net.sf.jasperreports.engine.data.*"/>
	<parameter name="EMPRESA" class="java.lang.String" isForPrompting="false"/>
	<parameter name="USUARIO" class="java.lang.String" isForPrompting="false"/>
	<parameter name="ARQUIVO" class="java.lang.String" isForPrompting="false">
		<defaultValueExpression><![CDATA["JKListagemVenda-002.jrxml"]]></defaultValueExpression>
	</parameter>
	<parameter name="DATA_INICIAL" class="java.util.Date"/>
	<parameter name="DATA_FINAL" class="java.util.Date"/>
	<parameter name="TITULO" class="java.lang.String" isForPrompting="false"/>
	<parameter name="CLASS_A" class="java.math.BigDecimal"/>
	<parameter name="CLASS_B" class="java.math.BigDecimal"/>
	<queryString>
		<![CDATA[WITH vendas (posicao, id, descricao, valorvendido) AS (
SELECT ROW_NUMBER() OVER (ORDER BY SUM(COALESCE(fat_docto_i.valor_unitario_liquido,fat_docto_i.valor_unitario)*fat_docto_i.qtde) DESC)  As Posicao,
       id, fat_produto.descricao As descricao,
    CAST(SUM(COALESCE(fat_docto_i.valor_unitario_liquido,fat_docto_i.valor_unitario)*fat_docto_i.qtde) As DECIMAL) As ValorVendido
--INTO Vendas6
FROM fat_produto As fat_produto
    RIGHT JOIN fat_docto_i As fat_docto_i ON fat_produto.id = fat_docto_i.fat_produto_id
    LEFT JOIN fat_docto_c  ON fat_docto_i.fat_docto_c_controle = fat_docto_c.controle

WHERE fat_docto_c.emissao between $P{DATA_INICIAL} AND $P{DATA_FINAL}
  AND (select Left(propriedade,1) FROM view_transacao WHERE codigo_transacao = fat_docto_c.fat_transacao_id AND diretiva = 154) = 'S'
GROUP BY id, fat_produto.descricao
ORDER BY ValorVendido DESC),

Res (Posicao, id, descricao, ValorVendido, TotalVendido) As (
SELECT
    Posicao, id, descricao, ValorVendido,
    SUM(ValorVendido) OVER (PARTITION BY 1) As TotalVendido
FROM Vendas),

QPerc (Posicao, id, descricao, ValorVendido, TotalVendido, Perc) As (

SELECT
    Posicao, id, descricao, ValorVendido, TotalVendido,
    ValorVendido / CAST(TotalVendido As Decimal(12,2)) As Perc
FROM Res),

percAcum (Posicao, id, descricao, ValorVendido, TotalVendido, Perc, PercAcum) AS (
SELECT
    Posicao, id, descricao, ValorVendido::decimal, TotalVendido::decimal, Perc::decimal,
    ROUND((SELECT SUM(TInt.Perc) FROM QPerc As TInt
        WHERE TInt.Posicao <= TOut.Posicao),4) As PercAcum
FROM QPerc As TOut ),

Intervalos (Valor) As (
SELECT ($P{CLASS_A}/100) UNION ALL
SELECT ($P{CLASS_B}/100) UNION ALL
SELECT 1.00)

SELECT
    Posicao, id, descricao, ValorVendido::decimal, PercAcum::decimal,
    MIN(Valor)::decimal As Valor, CASE
        WHEN MIN(Valor) = ($P{CLASS_A}/100) THEN 'A'
        WHEN MIN(Valor) = ($P{CLASS_B}/100) THEN 'B'
        ELSE 'C' END As Intervalo
FROM percAcum As C
INNER JOIN Intervalos As I ON C.PercAcum <= I.Valor
GROUP BY Posicao, id, descricao, ValorVendido, PercAcum
ORDER BY Posicao, descricao, ValorVendido, PercAcum]]>
	</queryString>
	<field name="posicao" class="java.lang.Long"/>
	<field name="id" class="java.lang.Integer"/>
	<field name="descricao" class="java.lang.String"/>
	<field name="valorvendido" class="java.math.BigDecimal"/>
	<field name="percacum" class="java.math.BigDecimal"/>
	<field name="valor" class="java.math.BigDecimal"/>
	<field name="intervalo" class="java.lang.String"/>
	<group name="PRODUTO">
		<groupExpression><![CDATA[]]></groupExpression>
		<groupHeader>
			<band splitType="Stretch"/>
		</groupHeader>
		<groupFooter>
			<band splitType="Stretch"/>
		</groupFooter>
	</group>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band splitType="Stretch"/>
	</title>
	<pageHeader>
		<band height="47" splitType="Stretch">
			<line>
				<reportElement key="line-1" positionType="Float" x="5" y="21" width="782" height="1" uuid="336731e4-eda9-4e98-b4e4-1c3915b02a02"/>
			</line>
			<textField isBlankWhenNull="false">
				<reportElement key="textField" positionType="Float" x="0" y="0" width="100" height="18" uuid="35134146-1437-453a-9ace-0703eac27a94"/>
				<textElement>
					<font fontName="Arial"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{EMPRESA}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement key="staticText-8" positionType="Float" x="708" y="0" width="38" height="16" uuid="6314f051-c8b3-49ae-8d47-b3fd7ce7d5ef"/>
				<textElement textAlignment="Center">
					<font fontName="Arial" size="12" isBold="true" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<text><![CDATA[PAG:]]></text>
			</staticText>
			<textField isBlankWhenNull="false">
				<reportElement key="textField" positionType="Float" x="746" y="0" width="36" height="16" uuid="8ac20e92-e0b2-4687-af8d-ab1be772a2cc"/>
				<textElement textAlignment="Center">
					<font fontName="Arial" isBold="true" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement positionType="Float" x="8" y="25" width="768" height="20" uuid="b27ac955-7c48-4c8b-b058-4d8686e1bd6a"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font fontName="Arial"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{TITULO}.trim()+ " A="+$P{CLASS_A} + "% B="+$P{CLASS_B}+"%"]]></textFieldExpression>
			</textField>
		</band>
	</pageHeader>
	<columnHeader>
		<band height="32" splitType="Stretch">
			<staticText>
				<reportElement key="staticText-3" positionType="Float" x="6" y="3" width="408" height="21" uuid="e7576c5e-8419-455d-8703-2a201c9aa9a1"/>
				<textElement verticalAlignment="Bottom">
					<font fontName="Arial" size="9" isBold="true" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<text><![CDATA[PRODUTO]]></text>
			</staticText>
			<staticText>
				<reportElement key="staticText-4" positionType="Float" x="414" y="3" width="77" height="21" uuid="f7e163cd-aec5-4495-8fcd-6adc741cfe42"/>
				<textElement textAlignment="Right" verticalAlignment="Bottom">
					<font fontName="Arial" size="9" isBold="true" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<text><![CDATA[VALOR VENDIDO]]></text>
			</staticText>
			<staticText>
				<reportElement key="staticText-5" positionType="Float" x="611" y="3" width="65" height="21" uuid="2d767a54-4d40-4be2-9db5-18ad7c3e4ee5"/>
				<textElement textAlignment="Right" verticalAlignment="Bottom">
					<font fontName="Arial" size="9" isBold="true" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<text><![CDATA[(%)]]></text>
			</staticText>
			<staticText>
				<reportElement key="staticText-6" positionType="Float" x="524" y="3" width="65" height="21" uuid="387b95c2-8172-447b-8376-c22903f66f9d"/>
				<textElement textAlignment="Right" verticalAlignment="Bottom">
					<font fontName="Arial" size="9" isBold="true" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<text><![CDATA[ACUMULADO(%)]]></text>
			</staticText>
			<staticText>
				<reportElement key="staticText-7" positionType="Float" x="700" y="3" width="76" height="21" uuid="521b7d26-b71e-414e-86d9-04230146d84e"/>
				<textElement textAlignment="Right" verticalAlignment="Bottom">
					<font fontName="Arial" size="9" isBold="true" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<text><![CDATA[CLASSIFICAÇÃO]]></text>
			</staticText>
			<line>
				<reportElement key="line-1" positionType="Float" x="6" y="28" width="760" height="1" uuid="1dead286-21d0-4b76-8204-24486fa6a801"/>
			</line>
		</band>
	</columnHeader>
	<detail>
		<band height="12" splitType="Stretch">
			<textField isBlankWhenNull="false">
				<reportElement key="textField" positionType="Float" x="5" y="0" width="375" height="12" uuid="77ae7c50-1bf2-4baf-be6e-4dc3d12c7932"/>
				<textElement>
					<font fontName="Arial" size="8"/>
				</textElement>
				<textFieldExpression><![CDATA[String.format("%05d", $F{id})+" "+$F{descricao}]]></textFieldExpression>
			</textField>
			<textField pattern="#,##0.00" isBlankWhenNull="true">
				<reportElement key="textField" positionType="Float" x="414" y="0" width="77" height="12" uuid="8fbc085e-c2fa-4a90-bcc6-3fd683a98f2d"/>
				<textElement textAlignment="Right">
					<font fontName="Arial" size="8"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{valorvendido}]]></textFieldExpression>
			</textField>
			<textField pattern="#,##0.0000 %" isBlankWhenNull="true">
				<reportElement key="textField" positionType="Float" x="524" y="0" width="64" height="12" uuid="c5992684-a65e-404d-97d8-402410de9fb3"/>
				<textElement textAlignment="Right">
					<font fontName="Arial" size="9" isBold="false" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{percacum}]]></textFieldExpression>
			</textField>
			<textField pattern="#,##0.00 %" isBlankWhenNull="true">
				<reportElement key="textField" positionType="Float" x="604" y="0" width="72" height="12" uuid="680dd3e1-dcef-4721-b7b1-e74e0f6c7c47"/>
				<textElement textAlignment="Right">
					<font fontName="Arial" size="9" isBold="false" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{valor}]]></textFieldExpression>
			</textField>
			<textField pattern="#,##0.00" isBlankWhenNull="false">
				<reportElement key="textField" positionType="Float" x="700" y="0" width="66" height="12" uuid="c6a2164f-7054-4533-872b-f8e7eb3a325d"/>
				<textElement textAlignment="Center">
					<font fontName="Arial" size="9" isBold="false" pdfFontName="Helvetica-Bold"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{intervalo}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<columnFooter>
		<band splitType="Stretch"/>
	</columnFooter>
	<pageFooter>
		<band height="24" splitType="Stretch">
			<textField isBlankWhenNull="false">
				<reportElement key="textField" positionType="Float" x="2" y="6" width="774" height="13" uuid="bcd75065-2f54-4d9a-9085-47a219f0773c"/>
				<textElement>
					<font fontName="Arial" size="8"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{ARQUIVO} + "  -  " + $P{USUARIO} + "\t\t" + new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm").format(new java.util.Date())]]></textFieldExpression>
			</textField>
			<line>
				<reportElement key="line-2" positionType="Float" x="0" y="5" width="782" height="1" uuid="c1fe9759-73ae-4e8f-849c-97eb07bccb5f"/>
			</line>
		</band>
	</pageFooter>
	<summary>
		<band splitType="Stretch"/>
	</summary>
</jasperReport>
