Calculate the total Sales Revenue

1

I have a query page that shows the total revenue for each product and I want to calculate the total sum of the revenue but the account is going wrong. The correct doing the sum in hand gave 264,660.00 and doing in java gave 18,830.00

link

To calculate this sum I used the following:

public Long buscaTotalParaDiretor(Long codigo) {
    String codigoProduto = "";
    String codigoProdutoAnterior ="";
    List<Produto> produtos = manager
            .createQuery("select p from Produto p where p.empresa.codigo =:codigo order by trim(p.codigoProduto)",
                    Produto.class)
            .setParameter("codigo", codigo).getResultList();
    Long totalReceita = 0L;
    Long receita = 0L;
    Long soma = 0L;

    int i = 0;
    for (Produto p : produtos) {
        if (i == 0) {
            codigoProdutoAnterior = p.getCodigoProduto().trim();
        }
        if (codigoProdutoAnterior.equals(p.getCodigoProduto().trim())) {
            soma += p.getQuantidadeRecente();
             System.out.println("Codigo " + codigoProduto +" Soma + " + soma + "Valor: " + p.getValor());
            receita = soma * p.getValor().longValue();
            System.out.println("Receita + " + receita);
            totalReceita += receita;
            System.out.println("Total + " + totalReceita);
        }
        codigoProduto = p.getCodigoProduto().trim();
        i++;
    }

    System.out.println("Total : " + totalReceita);
    return totalReceita;
}

The bean:

public class PrevisaoDiretorBean implements Serializable {
private static final long serialVersionUID = 1L;

@Inject
private Produtos previsoes;

@Inject
@UsuarioLogado
private Usuario usuario;

private List<Filtro> listaDePrevisoes = new ArrayList<>();

public List<Filtro> getPrevisoes() {
    return listaDePrevisoes;
}

public void inicializar() {
    if (FacesUtil.isNotPostback()) {

        listaDePrevisoes = previsoes.listarProdutosDoDiretor(usuario.getEmpresa().getCodigo());

    }
}

public List<Filtro> getListaDePrevisoes() {
    return listaDePrevisoes;
}

public void setListaDePrevisoes(List<Filtro> listaDePrevisoes) {
    this.listaDePrevisoes = listaDePrevisoes;
}

public Long getTotal() {

    Long totalReceita = previsoes.buscaTotalParaDiretor(usuario.getEmpresa().getCodigo());
    return totalReceita;
}

}

and xhtml:

<p:column headerText=" Receita Total"
                    style="text-align: center; width: 140px">
                    <h:outputText value="#{produto.receita}">
                        <f:convertNumber locale="pt_BR" minFractionDigits="2"
                            maxFractionDigits="2" />
                    </h:outputText>
                </p:column>


                <p:columnGroup type="footer">
                    <p:row>
                        <p:column colspan="6" footerText="Receita Total :"
                            style="text-align: right;background:navy;color: Snow;" />
                        <p:column style="text-align: right;background:navy;color: Snow;">
                            <f:facet name="footer">
                                <h:outputText value="#{previsaoDiretorBean.total}" >
                                <f:convertNumber locale="pt_BR" minFractionDigits="2"
                                    maxFractionDigits="2" />
                                </h:outputText>

                            </f:facet>
                        </p:column>
                    </p:row>
                </p:columnGroup>


            </p:dataTable>


        </h:form>

The output on the console is as follows:

Codigo  Soma + 102Valor: 2.30
Receita + 204
Total + 204
Codigo 0124940 Soma + 151Valor: 2.30
Receita + 302
Total + 506
Total : 506
Como se pode ver não é o resultado que quero.
    
asked by anonymous 18.07.2017 / 21:06

1 answer

0

Dude, apparently your summation is correct, maybe the problem is not in the first iteration of the loop?

For the first iteration in this if

 if (codigoProduto.equals(p.getCodigoProduto().trim()))

Your product code is equal to "", so I think it does not add up to the first value of your recipe.

Maybe if you initialize your variable with the value of the first position in the list, its sum will give the correct result.

    
19.07.2017 / 16:27