Calculate the total Sales Revenue

Asked

Viewed 514 times

1

I have a consultation page that shows the total of the revenue for each product and I want to calculate the total of the sum of the recipes but is going wrong. The correct summing in the hand gave 264.660,00 and doing in java gave 18.830,00

https://snag.gy/SGFPKw.jpg

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 the 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>

Console output 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.

1 answer

0

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

For in the first iteration in this if

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

your code Product is equal to "", so I believe that then it ceases to add the first value of your revenue.

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

  • the problem is in logic itself. . while the product codes are equal I do the sum and calculate the recipe which is the sum * value . The total revenue is the sum of the recipes. This is what I’m having trouble implementing. I changed the code .

  • Have you ever tried to print the values on the list? It may be that you have some fault in select and at that point it is not bringing all the data you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.