com.automaster.client.ui.paineis.tabs.financeiro.dialogs.NovoLancamento.java Source code

Java tutorial

Introduction

Here is the source code for com.automaster.client.ui.paineis.tabs.financeiro.dialogs.NovoLancamento.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.automaster.client.ui.paineis.tabs.financeiro.dialogs;

import com.automaster.client.Info;
import com.automaster.client.ui.paineis.tabs.financeiro.TabConsultaFaturaLancamento;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.CharacterCasing;
import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateTimeItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeMap;

/**
 *
 * @author Adriano
 */
public class NovoLancamento extends Window {

    private int contador;
    private double multa;
    private double juros;
    private ListGridRecord fats[] = null;
    private ListGridRecord fatsAux[] = null;
    //private count = 0;
    private String campo;
    private boolean flagItemValor = false;
    private double totalFaturasApagar = 0;
    private ArrayList<TreeMap<String, String>> arrayFaturasBD = new ArrayList<TreeMap<String, String>>();

    public NovoLancamento(final int codCliente, final ListGrid listLancamentos, final ListGrid listFaturas,
            final Label labelLanc, final Label labelfaturas, final Label labelSaldo,
            final TabConsultaFaturaLancamento objFaturaLancamento) {

        final NovoLancamento objLancamento = this;

        setTitle("Novo lanamento");
        setWidth(800);
        setHeight(550);
        setShowMinimizeButton(false);
        setIsModal(true);
        setShowModalMask(true);
        centerInPage();
        setLayoutAlign(Alignment.CENTER);
        setMembersMargin(5);

        final DynamicForm formNovoLancamento = new DynamicForm();
        formNovoLancamento.setAutoFocus(true);

        Label label = new Label("<strong><h3>Cadastrar novo Lanamento</h3></strong>");
        label.setAlign(Alignment.CENTER);
        label.setHeight(30);
        Label labelListaFat = new Label("<strong><h3>Faturas a pagar</h3></strong>");
        labelListaFat.setAlign(Alignment.CENTER);
        labelListaFat.setHeight(20);
        final Label saldoFaturasApagar = new Label();
        saldoFaturasApagar.setAlign(Alignment.RIGHT);
        saldoFaturasApagar.setHeight(20);
        saldoFaturasApagar.setWidth(750);
        Label labelListaFatSelecionadas = new Label(
                "<strong><h3>Faturas a ser alteradas pelo lanamento</h3></strong>");
        labelListaFatSelecionadas.setAlign(Alignment.CENTER);
        labelListaFatSelecionadas.setHeight(20);

        final DateTimeItem itemDataEhora = new DateTimeItem("dataEhora", "Data e Hora");
        itemDataEhora.setRequired(true);
        itemDataEhora.setWidth(300);
        itemDataEhora.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATETIME);
        itemDataEhora.setDefaultValue(new Date(System.currentTimeMillis()));
        itemDataEhora.setHint("<nbr>*</nbr>");

        final TextItem itemValor = new TextItem();
        itemValor.setTitle("Valor");
        itemValor.setName("valor");
        itemValor.setLength(16);
        itemValor.setRequired(true);
        itemValor.setHint("<nbr>*</nbr>");
        itemValor.setDefaultValue("");
        itemValor.setKeyPressFilter("[0-9]");
        itemValor.setMaskSaveLiterals(true);
        itemValor.setCharacterCasing(CharacterCasing.UPPER);
        itemValor.setWidth(300);
        itemValor.setHint("<nbr>*</nbr>");

        formNovoLancamento.setItems(itemDataEhora, itemValor);

        HLayout hButoes = new HLayout();
        hButoes.setWidth(200);
        hButoes.setMembersMargin(3);
        hButoes.setLayoutAlign(Alignment.CENTER);
        IButton cadastrar = new IButton("Cadastrar");
        IButton limpar = new IButton("Limpar");
        hButoes.setMargin(5);
        hButoes.addMember(cadastrar);
        hButoes.addMember(limpar);

        addCloseClickHandler(new CloseClickHandler() {

            @Override
            public void onCloseClick(CloseClickEvent event) {
                destroy();
            }
        });

        final ListGrid listaDeFaturas = new ListGrid() {

            @Override
            protected Canvas getExpansionComponent(final ListGridRecord record) {

                //final ListGrid grid = this;    
                final Label label = new Label(
                        "<strong style=\"font-size:10px; \">LANAMENTOS PARA ESTA FATURA</strong>");
                label.setAlign(Alignment.CENTER);
                label.setHeight(20);
                VLayout layout = new VLayout(5);
                layout.setPadding(5);
                //GWT.log("" + record.getAttribute("codigo"));
                final ListGridField codLancamento = new ListGridField("codLancamento", "Cdigo lanamento");
                codLancamento.setAlign(Alignment.CENTER);
                codLancamento.setHidden(true);
                final ListGridField codFatura = new ListGridField("codFatura", "Cdigo fatura");
                codFatura.setAlign(Alignment.CENTER);
                codFatura.setHidden(true);
                final ListGridField dataEhora = new ListGridField("dataEhora", "Data e Hora");
                dataEhora.setAlign(Alignment.CENTER);
                final ListGridField valor = new ListGridField("valor", "Valor lan.");
                valor.setAlign(Alignment.CENTER);
                final ListGridField valorPago = new ListGridField("valorPago", "Pago Fat.");
                valorPago.setAlign(Alignment.CENTER);
                final ListGridField estado = new ListGridField("estado", "Status");
                estado.setAlign(Alignment.CENTER);
                estado.setHidden(true);
                final ListGrid lancGrid = new ListGrid();
                lancGrid.setHeight(224);
                lancGrid.setCellHeight(22);
                lancGrid.setFields(codLancamento, codFatura, dataEhora, valor, valorPago, estado);
                Info.dbService.buscarFaturaHasLancamentoPorFatura(record.getAttributeAsInt("codigo"),
                        new AsyncCallback<ArrayList<TreeMap<String, String>>>() {

                            @Override
                            public void onFailure(Throwable caught) {
                                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                            }

                            @Override
                            public void onSuccess(ArrayList<TreeMap<String, String>> historico) {
                                if (historico != null) {
                                    for (TreeMap<String, String> histFat : historico) {
                                        Record regs = new Record();
                                        regs.setAttribute("codFatura", histFat.get("codFatura"));
                                        regs.setAttribute("codLancamento", histFat.get("codLancamento"));
                                        regs.setAttribute("dataEhora", DateTimeFormat.getShortDateTimeFormat()
                                                .format(new Date(Long.parseLong(histFat.get("dataEhora")))));
                                        regs.setAttribute("valor",
                                                Info.formataSaldo(Double.parseDouble(histFat.get("valor"))));
                                        regs.setAttribute("valorPago",
                                                Info.formataSaldo(Double.parseDouble(histFat.get("valorPago"))));
                                        regs.setAttribute("estado",
                                                (Boolean.parseBoolean(histFat.get("estado")) == false)
                                                        ? "<nbr class=\"negativo\">Em aberto</nbr>"
                                                        : "<nbr class=\"positivo\">Quitada</nbr>");
                                        lancGrid.addData(regs);
                                    }
                                }
                            }
                        });
                lancGrid.setShowAllRecords(true);
                lancGrid.setCanPickFields(false);
                layout.addMember(label);
                layout.addMember(lancGrid);
                return layout;
            }

            @Override
            protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {

                String fieldName = this.getFieldName(colNum);
                final ListGrid listAux = this;
                if (fieldName.equals("juroMulta")) {
                    HLayout recordCanvas = new HLayout(3);
                    recordCanvas.setHeight(22);
                    recordCanvas.setWidth100();
                    recordCanvas.setAlign(Alignment.CENTER);
                    ImgButton jurosMultaImg = new ImgButton();
                    jurosMultaImg.setShowDown(false);
                    jurosMultaImg.setShowRollOver(false);
                    jurosMultaImg.setLayoutAlign(Alignment.CENTER);
                    jurosMultaImg.setSrc("[SKIN]actions/remove.png");
                    jurosMultaImg.setPrompt("Remover juros e multa");
                    jurosMultaImg.setHeight(16);
                    jurosMultaImg.setWidth(16);
                    jurosMultaImg.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            //arrayFaturasBD.get(listAux.getRecordIndex(record)).put("count", String.valueOf(1));
                            EditarJurosFatura editarFatura = new EditarJurosFatura(listAux, record, arrayFaturasBD,
                                    saldoFaturasApagar, totalFaturasApagar, objLancamento);
                            editarFatura.setAnimateTime(1200);
                            editarFatura.animateShow(AnimationEffect.FADE);
                        }
                    });
                    recordCanvas.addMember(jurosMultaImg);
                    return recordCanvas;

                } else {
                    return null;
                }

            }
        };
        final ListGrid faturasSelecionadas = new ListGrid();
        faturasSelecionadas.setShowAllRecords(true);
        faturasSelecionadas.setAnimateRollUnder(true);
        faturasSelecionadas.setAlternateRecordStyles(false);
        faturasSelecionadas.setCanPickFields(false);
        faturasSelecionadas.setAnimateSelectionUnder(true);
        faturasSelecionadas.setWidth(789);
        faturasSelecionadas.setHeight(135);

        final ListGridField codClienteFat = new ListGridField("codCliente", "Cdigo Cliente");
        codClienteFat.setAlign(Alignment.CENTER);
        codClienteFat.setHidden(true);
        final ListGridField codigo = new ListGridField("codigo", "Cdigo");
        codigo.setAlign(Alignment.CENTER);
        codigo.setHidden(true);
        final ListGridField valor = new ListGridField("valor", "Valor");
        valor.setAlign(Alignment.CENTER);
        final ListGridField valorPago = new ListGridField("valorPago", "Valor pago");
        valorPago.setAlign(Alignment.CENTER);
        final ListGridField valorTotal = new ListGridField("valorTotal", "Valor Total");
        valorTotal.setAlign(Alignment.CENTER);
        final ListGridField jurosEmulta = new ListGridField("jurosEmulta", "Juros e multa");
        jurosEmulta.setAlign(Alignment.CENTER);
        final ListGridField valorApagar = new ListGridField("valorApagar", "a Pagar");
        valorApagar.setAlign(Alignment.CENTER);
        final ListGridField dataFatura = new ListGridField("dataFatura", "Data");
        dataFatura.setAlign(Alignment.CENTER);
        final ListGridField mes = new ListGridField("mesReferencia", "Ms");
        mes.setAlign(Alignment.CENTER);
        final ListGridField estadoFat = new ListGridField("estado", "Status");
        estadoFat.setAlign(Alignment.CENTER);
        ListGridField removeJurosMulta = new ListGridField("juroMulta", "remover M e J");
        removeJurosMulta.setAlign(Alignment.CENTER);
        ListGridField count = new ListGridField("count", "count");
        count.setAlign(Alignment.CENTER);
        count.setHidden(true);
        ///////////////////////////////////////////////////////////////////////////
        final ListGridField codClienteSelect = new ListGridField("codCliente", "Cdigo Cliente");
        codClienteSelect.setAlign(Alignment.CENTER);
        codClienteSelect.setHidden(true);
        final ListGridField codigoSelect = new ListGridField("codigo", "Cdigo");
        codigoSelect.setAlign(Alignment.CENTER);
        codigoSelect.setHidden(true);
        final ListGridField valorSelect = new ListGridField("valor", "Valor");
        valorSelect.setAlign(Alignment.CENTER);
        final ListGridField valorPagoSelect = new ListGridField("valorPago", "Crdito a lanar");
        valorPagoSelect.setAlign(Alignment.CENTER);
        final ListGridField valorTotalSelect = new ListGridField("valorTotal", "Valor Total");
        valorTotalSelect.setAlign(Alignment.CENTER);
        final ListGridField valorApagarSelect = new ListGridField("valorApagar", "Dbito restante");
        valorApagarSelect.setAlign(Alignment.CENTER);
        final ListGridField dataFaturaSelect = new ListGridField("dataFatura", "Data Venc.");
        dataFaturaSelect.setAlign(Alignment.CENTER);
        final ListGridField mesSelect = new ListGridField("mesReferencia", "Ms");
        mesSelect.setAlign(Alignment.CENTER);
        final ListGridField estadoFatSelect = new ListGridField("estado", "Status");
        estadoFatSelect.setAlign(Alignment.CENTER);
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        Info.dbService.buscarTodasTaxas(new AsyncCallback<ArrayList<TreeMap<String, String>>>() {

            @Override
            public void onFailure(Throwable caught) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void onSuccess(ArrayList<TreeMap<String, String>> taxas) {
                if (taxas != null) {
                    for (TreeMap<String, String> txs : taxas) {
                        if (Integer.parseInt(txs.get("codTaxa")) == 1) {
                            multa = Double.parseDouble(txs.get("valor"));
                        } else if (Integer.parseInt(txs.get("codTaxa")) == 2) {
                            juros = Double.parseDouble(txs.get("valor"));
                        }
                    }
                }
            }
        });
        //GWT.log("Linha NL - 01");
        listaDeFaturas.setFields(codClienteFat, codigo, dataFatura, valor, jurosEmulta, mes, valorTotal, valorPago,
                valorApagar, removeJurosMulta, count);
        //GWT.log("Linha NL - 02");
        listaDeFaturas.setWidth(789);
        listaDeFaturas.setHeight(135);
        listaDeFaturas.setShowAllRecords(true);
        listaDeFaturas.setCanPickFields(false);
        //listaDeFaturas.setSortDirection(SortDirection.DESCENDING);
        //listaDeFaturas.setSortField("dataFatura");
        listaDeFaturas.setShowRecordComponents(true);
        listaDeFaturas.setShowRecordComponentsByCell(true);
        listaDeFaturas.setCanResizeFields(true);
        listaDeFaturas.setDrawAheadRatio(4);
        listaDeFaturas.setCanExpandRecords(true);
        ///************************************************************************************************************************************************************////
        Info.dbService.buscarFaturaEmAbertoPorCliente(codCliente,
                new AsyncCallback<ArrayList<TreeMap<String, String>>>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }

                    @Override
                    public void onSuccess(ArrayList<TreeMap<String, String>> faturasCliente) {
                        //////////////////////////////////////////////////////////////////****************************************\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                        Date dataAtual = new Date(System.currentTimeMillis());
                        long diaAtual = dataAtual.getTime();
                        long diferencaDeDatas = 0;
                        long diaVencimento = 0;

                        TreeMap<String, String> faturaAuxiliar = new TreeMap<String, String>();
                        if (faturasCliente != null) {
                            for (TreeMap<String, String> fats : faturasCliente) {
                                if (faturasCliente.get(0).get("codFatura") != null
                                        && faturaAuxiliar.get(fats.get("codFatura")) == null) {
                                    Date dataDeVencimento = new Date(Long.parseLong(fats.get("dataVencimento")));
                                    diaVencimento = dataDeVencimento.getTime();
                                    diferencaDeDatas = diaAtual - diaVencimento;
                                    TreeMap<String, String> faturaBD = new TreeMap<String, String>();
                                    faturaBD.put("codFatura", fats.get("codFatura"));
                                    faturaBD.put("codCliente", fats.get("codCliente"));
                                    faturaBD.put("dataVencimento", fats.get("dataVencimento"));
                                    faturaBD.put("valor", String
                                            .valueOf(Info.formataSaldo(Double.parseDouble(fats.get("valor")))));
                                    faturaBD.put("estado", fats.get("estado"));
                                    faturaBD.put("valorPago",
                                            (fats.get("totalLancamentos") == null)
                                                    ? String.valueOf(Info.formataSaldo(0))
                                                    : String.valueOf(Info.formataSaldo(
                                                            Double.parseDouble(fats.get("totalLancamentos")))));
                                    faturaBD.put("mesReferencia", fats.get("mes"));
                                    faturaBD.put("count", "0");
                                    //GWT.log("" + fats.get("valorApagar"));
                                    double totalPago = (fats.get("totalLancamentos") == null) ? 0
                                            : (Double.parseDouble(fats.get("totalLancamentos")));
                                    //GWT.log("Valor a pagar"+(Double.parseDouble(fats.get("valor")) - (totalPago)));
                                    if (diferencaDeDatas <= 0) {
                                        double valor = Double.parseDouble(fats.get("valor"));
                                        double valorAtualFatura = (totalPago == 0) ? Info.formataDecimal(valor)
                                                : Info.formataDecimal(
                                                        Double.parseDouble(fats.get("valor")) - (totalPago));
                                        faturaBD.put("jurosEmulta",
                                                String.valueOf(Info.formataSaldo(Double.parseDouble("0"))));
                                        faturaBD.put("valorApagar", String.valueOf(
                                                Info.formataSaldo(Info.formataDecimal((valorAtualFatura)))));
                                        faturaBD.put("valorTotal", String.valueOf(Info.formataSaldo(
                                                Info.formataDecimal(Double.parseDouble(fats.get("valor"))))));
                                        diferencaDeDatas = 0;
                                    } else if (diferencaDeDatas > 0 && diferencaDeDatas < 86400000) {
                                        double valor = Double.parseDouble(fats.get("valor"));
                                        double valorAtualFatura = (totalPago == 0)
                                                ? Info.formataDecimal(valor) + (valor * multa)
                                                : Info.formataDecimal(Double.parseDouble(fats.get("valor"))
                                                        - (totalPago) + (valor * multa));
                                        faturaBD.put("jurosEmulta", String
                                                .valueOf(Info.formataSaldo((Info.formataDecimal(valor * multa)))));
                                        faturaBD.put("valorApagar",
                                                String.valueOf(Info.formataSaldo(valorAtualFatura)));
                                        faturaBD.put("valorTotal", String.valueOf(Info.formataSaldo(
                                                Info.formataDecimal(Double.parseDouble(fats.get("valor"))
                                                        + ((Double.parseDouble(fats.get("valor")) * multa))))));
                                        diferencaDeDatas = 0;
                                    } else if (diferencaDeDatas > 86400000) {
                                        double valor = Double.parseDouble(fats.get("valor"));
                                        double valorAtualFatura = (totalPago == 0)
                                                ? Info.formataDecimal(valor) + (valor * multa)
                                                        + ((diferencaDeDatas / 86400000) * (juros * valor))
                                                : Info.formataDecimal(Double.parseDouble(fats.get("valor"))
                                                        - (totalPago) + (valor * multa)
                                                        + ((diferencaDeDatas / 86400000) * (juros * valor)));
                                        faturaBD.put("jurosEmulta",
                                                String.valueOf(Info.formataSaldo(Info.formataDecimal((valor * multa)
                                                        + ((diferencaDeDatas / 86400000) * (juros * valor))))));
                                        faturaBD.put("valorApagar",
                                                String.valueOf(Info.formataSaldo(valorAtualFatura)));
                                        faturaBD.put("valorTotal", String.valueOf(Info.formataSaldo(
                                                Info.formataDecimal(Double.parseDouble(fats.get("valor"))
                                                        + ((Double.parseDouble(fats.get("valor")) * multa))
                                                        + ((diferencaDeDatas / 86400000) * (juros
                                                                * Double.parseDouble(fats.get("valor"))))))));
                                        diferencaDeDatas = 0;
                                    }
                                    arrayFaturasBD.add(faturaBD);
                                    faturaAuxiliar.put(fats.get("codFatura"), fats.get("codFatura"));
                                }
                            }
                        }
                        ////////////////////////////////////////////////////////////////*******************************************\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\            
                        for (TreeMap<String, String> fats : arrayFaturasBD) {
                            Record fat = new Record();
                            fat.setAttribute("codigo", fats.get("codFatura"));
                            fat.setAttribute("codCliente", fats.get("codCliente"));
                            fat.setAttribute("dataFatura", DateTimeFormat.getShortDateFormat()
                                    .format(new Date(Long.parseLong(fats.get("dataVencimento")))));
                            fat.setAttribute("valor", fats.get("valor"));
                            fat.setAttribute("estado",
                                    (Boolean.parseBoolean(fats.get("estado")) == false)
                                            ? "<nbr class=\negativo\"> Em aberto</nbr>"
                                            : "<nbr class=\"positivo\">Quitada</nbr>");
                            fat.setAttribute("valorPago", fats.get("valorPago"));
                            fat.setAttribute("mesReferencia", fats.get("mesReferencia"));
                            fat.setAttribute("valorApagar", fats.get("valorApagar"));
                            fat.setAttribute("valorTotal", fats.get("valorTotal"));
                            fat.setAttribute("jurosEmulta", fats.get("jurosEmulta"));
                            fat.setAttribute("count", Integer.parseInt(fats.get("count")));
                            listaDeFaturas.addData(fat);
                            totalFaturasApagar = totalFaturasApagar
                                    + Double.parseDouble(fats.get("valorApagar").replaceAll(",", "\\."));
                        }
                        saldoFaturasApagar.redraw();
                        saldoFaturasApagar
                                .setContents("<strong style=\"font-size:22px; color:red;\">total de dbito: - R$"
                                        + Info.formataSaldo(Info.formataDecimal(totalFaturasApagar))
                                        + "  </strong>");
                    }
                });
        ///************************************************************************************************************************************************************////
        //fats = listaDeFaturas.getRecords();
        faturasSelecionadas.setFields(codClienteSelect, codigoSelect, dataFaturaSelect, valorSelect, mesSelect,
                valorTotalSelect, valorPagoSelect, valorApagarSelect);

        itemValor.addKeyPressHandler(new KeyPressHandler() {
            @Override
            public void onKeyPress(KeyPressEvent event) {
                String tecla;
                tecla = event.getKeyName();
                //GWT.log("Tecla " + tecla + " KeyCod = " + event.getCharacterValue());
                //GWT.log("Objeto JavaScript "+ Info.FormataMoeda(itemValor.getValueAsString(), ".", ",", tecla));
                String aux = "";
                if (event.getCharacterValue() != null && event.getCharacterValue() >= 48
                        && event.getCharacterValue() <= 57) {
                    aux = Info.FormataMoeda(
                            (itemValor.getValueAsString() != null) ? itemValor.getValueAsString() : "0", ".", ",",
                            tecla);
                    flagItemValor = true;
                }
                //GWT.log("Aux=" + aux);
                //itemValor.setValue(aux);
                campo = aux;
                //Info.FormataMoeda(itemValor.getJsObj(), ".", ",", tecla);
            }
        });
        final TreeMap<String, String> faturaAux = new TreeMap<String, String>();

        itemValor.addChangedHandler(new ChangedHandler() {

            @Override
            public void onChanged(ChangedEvent event) {
                faturasSelecionadas.setData(new RecordList());
                //GWT.log("Entrou no changed: " + event.getValue());
                if (flagItemValor) {
                    itemValor.setValue(campo);
                }
                flagItemValor = false;
                final String novoLancamento = campo;
                if ((event.getValue() != null && !novoLancamento.equalsIgnoreCase(""))) {
                    double valorNovoApagar = 0;
                    double valorNovoLancamento = 0;
                    //GWT.log("valorNovoLancamento ReplaceAll: " + itemValor.getDisplayValue().replaceAll("\\.", "").replaceAll(",", "\\."));
                    valorNovoLancamento = Double
                            .parseDouble(itemValor.getDisplayValue().replaceAll("\\.", "").replaceAll(",", "\\."));
                    //GWT.log("valorNovoLancamento: " + valorNovoLancamento);
                    valorNovoLancamento = Info.formataDecimal(valorNovoLancamento);
                    fats = listaDeFaturas.getRecords();
                    // GWT.log("fats.length: " + fats.length);
                    for (int i = 0; i < fats.length; i++) {
                        double valorAtualFatura = Double
                                .parseDouble(arrayFaturasBD.get(i).get("valorApagar").replaceAll(",", "\\."));
                        if (valorAtualFatura <= valorNovoLancamento) {
                            fats[i].setAttribute("valorApagar", String.valueOf(Info.formataSaldo(0)));
                            faturasSelecionadas.addData(fats[i]);
                            valorNovoLancamento = valorNovoLancamento - valorAtualFatura;
                            valorNovoLancamento = Info.formataDecimal(valorNovoLancamento);
                            /*Linha 451*/
                            fats[i].setAttribute("valorPago", String.valueOf(Info.formataSaldo(valorAtualFatura)));
                        } else {
                            //GWT.log("valorNovoLancamento ELSE: " + valorNovoLancamento);
                            if (valorNovoLancamento > 0) {
                                //GWT.log("Valor a pagar ELSE: " + fats[i].getAttribute("valorApagar").replaceAll("\\.", "").replaceAll(",", "\\."));
                                valorNovoApagar = Double.parseDouble((arrayFaturasBD.get(i).get("valorApagar")
                                        .replaceAll("\\.", "").replaceAll(",", "\\."))) - valorNovoLancamento;
                                valorNovoApagar = Info.formataDecimal(valorNovoApagar);
                                fats[i].setAttribute("valorPago",
                                        String.valueOf(Info.formataSaldo(valorNovoLancamento)));
                                fats[i].setAttribute("valorApagar",
                                        String.valueOf(Info.formataSaldo(valorNovoApagar)));
                                faturasSelecionadas.addData(fats[i]);
                                valorNovoLancamento = 0;
                            }
                        }
                    }
                } else {
                    fatsAux = faturasSelecionadas.getRecords();
                    faturaAux.clear();
                    if (!faturasSelecionadas.getRecordList().isEmpty()) {
                        for (int i = 0; i < fatsAux.length; i++) {
                            faturasSelecionadas.removeData(fatsAux[i]);
                        }
                    }
                    fats = listaDeFaturas.getRecords();
                    for (int i = 0; i < fats.length; i++) {
                        listaDeFaturas.removeData(fats[i]);
                    }
                    for (TreeMap<String, String> fats : arrayFaturasBD) {
                        Record fat = new Record();
                        fat.setAttribute("codigo", fats.get("codFatura"));
                        fat.setAttribute("codCliente", fats.get("codCliente"));
                        fat.setAttribute("dataFatura", DateTimeFormat.getShortDateFormat()
                                .format(new Date(Long.parseLong(fats.get("dataVencimento")))));
                        fat.setAttribute("valor", fats.get("valor"));
                        fat.setAttribute("estado",
                                (Boolean.parseBoolean(fats.get("estado")) == false)
                                        ? "<nbr class=\negativo\"> Em aberto</nbr>"
                                        : "<nbr class=\"positivo\">Quitada</nbr>");
                        fat.setAttribute("valorPago", fats.get("valorPago"));
                        fat.setAttribute("mesReferencia", fats.get("mesReferencia"));
                        fat.setAttribute("valorApagar", fats.get("valorApagar"));
                        fat.setAttribute("valorTotal", fats.get("valorTotal"));
                        fat.setAttribute("jurosEmulta", fats.get("jurosEmulta"));
                        fat.setAttribute("count", Integer.parseInt(fats.get("count")));
                        listaDeFaturas.addData(fat);

                    }

                }
            }
        });
        //GWT.log("arrayFaturasBD: " + arrayFaturasBD);
        cadastrar.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                if (formNovoLancamento.validate()) {
                    contador = 0;
                    final Window janelaCarregando = new Window();
                    janelaCarregando.setShowTitle(false);
                    janelaCarregando.setWidth(100);
                    janelaCarregando.setHeight(50);
                    janelaCarregando.setShowEdges(false);
                    janelaCarregando.setShowCloseButton(false);
                    janelaCarregando.setShowMinimizeButton(false);
                    janelaCarregando.setIsModal(true);
                    janelaCarregando.setShowModalMask(true);
                    janelaCarregando.centerInPage();
                    janelaCarregando.setLayoutAlign(Alignment.CENTER);
                    final Label mensagem = new Label("Carregando..");
                    mensagem.setHeight(16);

                    new Timer() {
                        public void run() {

                            if (contador != 100) {
                                contador += 1 + (int) (5 * Math.random());
                                schedule(1 + (int) (50 * Math.random()));
                                mensagem.setContents("Carregando: " + contador + "%");
                                janelaCarregando.addItem(mensagem);
                                janelaCarregando.setAnimateTime(1200);
                                janelaCarregando.animateShow(AnimationEffect.FADE);

                                if (contador >= 100) {
                                    mensagem.setContents("Carregando: 100%");
                                    /* TreeMap que vai para o banco como Novo lanamento*/
                                    TreeMap<String, String> novoLancamento = new TreeMap<String, String>();
                                    novoLancamento.put("codCliente", String.valueOf(codCliente));
                                    Timestamp dataEhora = new Timestamp(itemDataEhora.getValueAsDate().getTime());
                                    String valorFinalLanc = itemValor.getDisplayValue().replaceAll("\\.", "")
                                            .replaceAll(",", "\\.");
                                    //GWT.log("Valor Sem virgula:" + valorFinalLanc);
                                    novoLancamento.put("valor", valorFinalLanc);
                                    /* TreeMap que vai para o banco como Novo lanamento*/

                                    /* arrayList de TreeMap que vai para o banco como Novo fatura_has_lancamento*/
                                    ArrayList<TreeMap<String, String>> arrayListFaturas = new ArrayList<TreeMap<String, String>>();
                                    if (!faturasSelecionadas.getRecordList().isEmpty()) {
                                        ListGridRecord fats[] = faturasSelecionadas.getRecords();
                                        for (int i = 0; i < fats.length; i++) {
                                            //GWT.log("valorApagar: " + arrayFaturasBD.get(i).get("valorApagar"));
                                            TreeMap<String, String> treeMapFatura = new TreeMap<String, String>();
                                            treeMapFatura.put("codCliente", String.valueOf(codCliente));
                                            treeMapFatura.put("codFatura", fats[i].getAttribute("codigo"));
                                            treeMapFatura.put("valorTotal",
                                                    Info.formataReaisFloat(fats[i].getAttribute("valorTotal")));
                                            treeMapFatura.put("valorApagar",
                                                    Info.formataReaisFloat(fats[i].getAttribute("valorApagar")));
                                            treeMapFatura.put("valorPago",
                                                    Info.formataReaisFloat(fats[i].getAttribute("valorPago")));
                                            arrayListFaturas.add(treeMapFatura);
                                        }
                                    }

                                    Info.dbService.cadastrarLancamento(novoLancamento, arrayListFaturas, dataEhora,
                                            new AsyncCallback<Void>() {

                                                @Override
                                                public void onFailure(Throwable caught) {
                                                    //GWT.log("caught: "+caught);
                                                    throw new UnsupportedOperationException(
                                                            "Not supported yet.!!!!!!!!!!!!!!"); //To change body of generated methods, choose Tools | Templates.
                                                }

                                                @Override
                                                public void onSuccess(Void result) {
                                                    listFaturas.setData(new RecordList());
                                                    arrayFaturasBD.clear();
                                                    //GWT.log("" + arrayFaturasBD);

                                                    Info.dbService.buscarUltimoLancamento(
                                                            new AsyncCallback<TreeMap<String, String>>() {

                                                                @Override
                                                                public void onFailure(Throwable caught) {
                                                                    throw new UnsupportedOperationException(
                                                                            "Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                                                                }

                                                                @Override
                                                                public void onSuccess(
                                                                        TreeMap<String, String> ultimoLancamento) {
                                                                    if (ultimoLancamento != null) {
                                                                        Record regLancamento = new Record();
                                                                        //GWT.log("codUltimoLanc: " + ultimoLancamento.get("codLancamento"));
                                                                        regLancamento.setAttribute("codCliente",
                                                                                ultimoLancamento.get("codCliente"));
                                                                        regLancamento.setAttribute("codigo",
                                                                                ultimoLancamento
                                                                                        .get("codLancamento"));
                                                                        regLancamento.setAttribute("data",
                                                                                DateTimeFormat
                                                                                        .getShortDateTimeFormat()
                                                                                        .format(new Date(
                                                                                                Long.parseLong(
                                                                                                        ultimoLancamento
                                                                                                                .get("dataEhora")))));
                                                                        regLancamento.setAttribute("valor",
                                                                                "R$" + Info.formataValor(Float
                                                                                        .parseFloat(ultimoLancamento
                                                                                                .get("valor"))));
                                                                        listLancamentos.addData(regLancamento);
                                                                        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                                        Info.dbService.buscarTodasTaxas(
                                                                                new AsyncCallback<ArrayList<TreeMap<String, String>>>() {

                                                                                    @Override
                                                                                    public void onFailure(
                                                                                            Throwable caught) {
                                                                                        throw new UnsupportedOperationException(
                                                                                                "Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                                                                                    }

                                                                                    @Override
                                                                                    public void onSuccess(
                                                                                            ArrayList<TreeMap<String, String>> taxas) {
                                                                                        if (taxas != null) {
                                                                                            for (TreeMap<String, String> txs : taxas) {
                                                                                                if (Integer
                                                                                                        .parseInt(
                                                                                                                txs.get("codTaxa")) == 1) {
                                                                                                    multa = Double
                                                                                                            .parseDouble(
                                                                                                                    txs.get("valor"));
                                                                                                } else if (Integer
                                                                                                        .parseInt(
                                                                                                                txs.get("codTaxa")) == 2) {
                                                                                                    juros = Double
                                                                                                            .parseDouble(
                                                                                                                    txs.get("valor"));
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                });
                                                                        Info.dbService.buscarFaturaPorCliente(
                                                                                codCliente,
                                                                                new AsyncCallback<ArrayList<TreeMap<String, String>>>() {

                                                                                    @Override
                                                                                    public void onFailure(
                                                                                            Throwable caught) {
                                                                                        throw new UnsupportedOperationException(
                                                                                                "Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                                                                                    }

                                                                                    @Override
                                                                                    public void onSuccess(
                                                                                            ArrayList<TreeMap<String, String>> faturasCliente) {
                                                                                        Date dataAtual = new Date(
                                                                                                System.currentTimeMillis());
                                                                                        long diaAtual = dataAtual
                                                                                                .getTime();
                                                                                        long diferencaDeDatas = 0;
                                                                                        long diaVencimento = 0;
                                                                                        TreeMap<String, String> faturaAuxiliar = new TreeMap<String, String>();
                                                                                        if (faturasCliente != null) {
                                                                                            for (TreeMap<String, String> fats : faturasCliente) {
                                                                                                if (faturasCliente
                                                                                                        .get(0)
                                                                                                        .get("codFatura") != null
                                                                                                        && faturaAuxiliar
                                                                                                                .get(fats
                                                                                                                        .get("codFatura")) == null) {
                                                                                                    Date dataDeVencimento = new Date(
                                                                                                            Long.parseLong(
                                                                                                                    fats.get(
                                                                                                                            "dataVencimento")));
                                                                                                    diaVencimento = dataDeVencimento
                                                                                                            .getTime();
                                                                                                    diferencaDeDatas = diaAtual
                                                                                                            - diaVencimento;
                                                                                                    TreeMap<String, String> faturaBD = new TreeMap<String, String>();
                                                                                                    faturaBD.put(
                                                                                                            "codFatura",
                                                                                                            fats.get(
                                                                                                                    "codFatura"));
                                                                                                    faturaBD.put(
                                                                                                            "codCliente",
                                                                                                            fats.get(
                                                                                                                    "codCliente"));
                                                                                                    faturaBD.put(
                                                                                                            "dataVencimento",
                                                                                                            fats.get(
                                                                                                                    "dataVencimento"));
                                                                                                    faturaBD.put(
                                                                                                            "valor",
                                                                                                            String.valueOf(
                                                                                                                    Info.formataSaldo(
                                                                                                                            Double.parseDouble(
                                                                                                                                    fats.get(
                                                                                                                                            "valor")))));
                                                                                                    faturaBD.put(
                                                                                                            "estado",
                                                                                                            fats.get(
                                                                                                                    "estado"));
                                                                                                    faturaBD.put(
                                                                                                            "valorPago",
                                                                                                            (fats.get(
                                                                                                                    "totalLancamentos") == null)
                                                                                                                            ? String.valueOf(
                                                                                                                                    Info.formataSaldo(
                                                                                                                                            0))
                                                                                                                            : String.valueOf(
                                                                                                                                    Info.formataSaldo(
                                                                                                                                            Double.parseDouble(
                                                                                                                                                    fats.get(
                                                                                                                                                            "totalLancamentos")))));
                                                                                                    faturaBD.put(
                                                                                                            "mesReferencia",
                                                                                                            fats.get(
                                                                                                                    "mes"));
                                                                                                    faturaBD.put(
                                                                                                            "count",
                                                                                                            "0");
                                                                                                    //GWT.log("" + fats.get("valorApagar"));
                                                                                                    double totalPago = (fats
                                                                                                            .get("totalLancamentos") == null)
                                                                                                                    ? 0
                                                                                                                    : (Double
                                                                                                                            .parseDouble(
                                                                                                                                    fats.get(
                                                                                                                                            "totalLancamentos")));
                                                                                                    //GWT.log("Valor a pagar"+(Double.parseDouble(fats.get("valor")) - (totalPago)));
                                                                                                    if (diferencaDeDatas <= 0) {
                                                                                                        double valor = Double
                                                                                                                .parseDouble(
                                                                                                                        fats.get(
                                                                                                                                "valor"));
                                                                                                        double valorAtualFatura = (totalPago == 0)
                                                                                                                ? Info.formataDecimal(
                                                                                                                        valor)
                                                                                                                : Info.formataDecimal(
                                                                                                                        Double.parseDouble(
                                                                                                                                fats.get(
                                                                                                                                        "valor"))
                                                                                                                                - (totalPago));
                                                                                                        faturaBD.put(
                                                                                                                "jurosEmulta",
                                                                                                                String.valueOf(
                                                                                                                        Info.formataSaldo(
                                                                                                                                Double.parseDouble(
                                                                                                                                        "0"))));
                                                                                                        faturaBD.put(
                                                                                                                "valorApagar",
                                                                                                                String.valueOf(
                                                                                                                        Info.formataSaldo(
                                                                                                                                Info.formataDecimal(
                                                                                                                                        (valorAtualFatura)))));
                                                                                                        faturaBD.put(
                                                                                                                "valorTotal",
                                                                                                                String.valueOf(
                                                                                                                        Info.formataSaldo(
                                                                                                                                Info.formataDecimal(
                                                                                                                                        Double.parseDouble(
                                                                                                                                                fats.get(
                                                                                                                                                        "valor"))))));
                                                                                                        diferencaDeDatas = 0;
                                                                                                    } else if (diferencaDeDatas > 0
                                                                                                            && diferencaDeDatas < 86400000) {
                                                                                                        double valor = Double
                                                                                                                .parseDouble(
                                                                                                                        fats.get(
                                                                                                                                "valor"));
                                                                                                        double valorAtualFatura = (totalPago == 0)
                                                                                                                ? Info.formataDecimal(
                                                                                                                        valor)
                                                                                                                        + (valor * multa)
                                                                                                                : Info.formataDecimal(
                                                                                                                        Double.parseDouble(
                                                                                                                                fats.get(
                                                                                                                                        "valor"))
                                                                                                                                - (totalPago)
                                                                                                                                + (valor * multa));
                                                                                                        faturaBD.put(
                                                                                                                "jurosEmulta",
                                                                                                                String.valueOf(
                                                                                                                        Info.formataSaldo(
                                                                                                                                (Info.formataDecimal(
                                                                                                                                        valor * multa)))));
                                                                                                        faturaBD.put(
                                                                                                                "valorApagar",
                                                                                                                String.valueOf(
                                                                                                                        Info.formataSaldo(
                                                                                                                                valorAtualFatura)));
                                                                                                        faturaBD.put(
                                                                                                                "valorTotal",
                                                                                                                String.valueOf(
                                                                                                                        Info.formataSaldo(
                                                                                                                                Info.formataDecimal(
                                                                                                                                        Double.parseDouble(
                                                                                                                                                fats.get(
                                                                                                                                                        "valor"))
                                                                                                                                                + ((Double
                                                                                                                                                        .parseDouble(
                                                                                                                                                                fats.get(
                                                                                                                                                                        "valor"))
                                                                                                                                                        * multa))))));
                                                                                                        diferencaDeDatas = 0;
                                                                                                    } else if (diferencaDeDatas > 86400000) {
                                                                                                        double valor = Double
                                                                                                                .parseDouble(
                                                                                                                        fats.get(
                                                                                                                                "valor"));
                                                                                                        double valorAtualFatura = (totalPago == 0)
                                                                                                                ? Info.formataDecimal(
                                                                                                                        valor)
                                                                                                                        + (valor * multa)
                                                                                                                        + ((diferencaDeDatas
                                                                                                                                / 86400000)
                                                                                                                                * (juros * valor))
                                                                                                                : Info.formataDecimal(
                                                                                                                        Double.parseDouble(
                                                                                                                                fats.get(
                                                                                                                                        "valor"))
                                                                                                                                - (totalPago)
                                                                                                                                + (valor * multa)
                                                                                                                                + ((diferencaDeDatas
                                                                                                                                        / 86400000)
                                                                                                                                        * (juros * valor)));
                                                                                                        faturaBD.put(
                                                                                                                "jurosEmulta",
                                                                                                                String.valueOf(
                                                                                                                        Info.formataSaldo(
                                                                                                                                Info.formataDecimal(
                                                                                                                                        (valor * multa)
                                                                                                                                                + ((diferencaDeDatas
                                                                                                                                                        / 86400000)
                                                                                                                                                        * (juros * valor))))));
                                                                                                        faturaBD.put(
                                                                                                                "valorApagar",
                                                                                                                String.valueOf(
                                                                                                                        Info.formataSaldo(
                                                                                                                                valorAtualFatura)));
                                                                                                        faturaBD.put(
                                                                                                                "valorTotal",
                                                                                                                String.valueOf(
                                                                                                                        Info.formataSaldo(
                                                                                                                                Info.formataDecimal(
                                                                                                                                        Double.parseDouble(
                                                                                                                                                fats.get(
                                                                                                                                                        "valor"))
                                                                                                                                                + ((Double
                                                                                                                                                        .parseDouble(
                                                                                                                                                                fats.get(
                                                                                                                                                                        "valor"))
                                                                                                                                                        * multa))
                                                                                                                                                + ((diferencaDeDatas
                                                                                                                                                        / 86400000)
                                                                                                                                                        * (juros * Double
                                                                                                                                                                .parseDouble(
                                                                                                                                                                        fats.get(
                                                                                                                                                                                "valor"))))))));
                                                                                                        diferencaDeDatas = 0;
                                                                                                    }
                                                                                                    arrayFaturasBD
                                                                                                            .add(faturaBD);
                                                                                                    faturaAuxiliar
                                                                                                            .put(fats
                                                                                                                    .get("codFatura"),
                                                                                                                    fats.get(
                                                                                                                            "codFatura"));
                                                                                                }
                                                                                            }
                                                                                            objFaturaLancamento
                                                                                                    .setTotalLancamentos(
                                                                                                            0);
                                                                                            objFaturaLancamento
                                                                                                    .setTotalFaturas(
                                                                                                            0);
                                                                                            objFaturaLancamento
                                                                                                    .setSaldo(0);
                                                                                            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++////////////
                                                                                            for (TreeMap<String, String> fatsGrid : arrayFaturasBD) {
                                                                                                Record fat = new Record();
                                                                                                fat.setAttribute(
                                                                                                        "codigo",
                                                                                                        fatsGrid.get(
                                                                                                                "codFatura"));
                                                                                                fat.setAttribute(
                                                                                                        "codCliente",
                                                                                                        fatsGrid.get(
                                                                                                                "codCliente"));
                                                                                                fat.setAttribute(
                                                                                                        "dataFatura",
                                                                                                        DateTimeFormat
                                                                                                                .getShortDateFormat()
                                                                                                                .format(new Date(
                                                                                                                        Long.parseLong(
                                                                                                                                fatsGrid.get(
                                                                                                                                        "dataVencimento")))));
                                                                                                fat.setAttribute(
                                                                                                        "valor",
                                                                                                        fatsGrid.get(
                                                                                                                "valor"));
                                                                                                fat.setAttribute(
                                                                                                        "estado",
                                                                                                        (Boolean.parseBoolean(
                                                                                                                fatsGrid.get(
                                                                                                                        "estado")) == false)
                                                                                                                                ? "<nbr class=\negativo\"> Em aberto</nbr>"
                                                                                                                                : "<nbr class=\"positivo\">Quitada</nbr>");
                                                                                                fat.setAttribute(
                                                                                                        "valorPago",
                                                                                                        fatsGrid.get(
                                                                                                                "valorPago"));
                                                                                                fat.setAttribute(
                                                                                                        "mesReferencia",
                                                                                                        fatsGrid.get(
                                                                                                                "mesReferencia"));
                                                                                                fat.setAttribute(
                                                                                                        "valorApagar",
                                                                                                        (Boolean.parseBoolean(
                                                                                                                fatsGrid.get(
                                                                                                                        "estado")) == false)
                                                                                                                                ? fatsGrid
                                                                                                                                        .get("valorApagar")
                                                                                                                                : Info.formataSaldo(
                                                                                                                                        0));
                                                                                                fat.setAttribute(
                                                                                                        "valorTotal",
                                                                                                        fatsGrid.get(
                                                                                                                "valorTotal"));
                                                                                                fat.setAttribute(
                                                                                                        "jurosEmulta",
                                                                                                        (Boolean.parseBoolean(
                                                                                                                fatsGrid.get(
                                                                                                                        "estado")) == false)
                                                                                                                                ? fatsGrid
                                                                                                                                        .get("jurosEmulta")
                                                                                                                                : Info.formataSaldo(
                                                                                                                                        0));
                                                                                                fat.setAttribute(
                                                                                                        "count",
                                                                                                        Integer.parseInt(
                                                                                                                fatsGrid.get(
                                                                                                                        "count")));
                                                                                                listFaturas.addData(
                                                                                                        fat);
                                                                                                //GWT.log("fatsGrid.get(\"codFatura\"): "+fatsGrid.get("codFatura"));
                                                                                                //GWT.log("fatsGrid.get(\"estado\")): " + fatsGrid.get("estado"));
                                                                                                //GWT.log("fatsGrid.get(\"valorPago\"): " + fatsGrid.get("valorPago"));
                                                                                                objFaturaLancamento
                                                                                                        .setTotalFaturas(
                                                                                                                objFaturaLancamento
                                                                                                                        .getTotalFaturas()
                                                                                                                        + ((Double
                                                                                                                                .parseDouble(
                                                                                                                                        fat.getAttributeAsString(
                                                                                                                                                "valorPago")
                                                                                                                                                .replaceAll(
                                                                                                                                                        "\\.",
                                                                                                                                                        "")
                                                                                                                                                .replaceAll(
                                                                                                                                                        ",",
                                                                                                                                                        ".")) > 0
                                                                                                                                && Boolean
                                                                                                                                        .parseBoolean(
                                                                                                                                                fatsGrid.get(
                                                                                                                                                        "estado")) == true)
                                                                                                                                                                ? Double.parseDouble(
                                                                                                                                                                        fat.getAttributeAsString(
                                                                                                                                                                                "valorPago")
                                                                                                                                                                                .replaceAll(
                                                                                                                                                                                        "\\.",
                                                                                                                                                                                        "")
                                                                                                                                                                                .replaceAll(
                                                                                                                                                                                        ",",
                                                                                                                                                                                        "."))
                                                                                                                                                                : (Double
                                                                                                                                                                        .parseDouble(
                                                                                                                                                                                fat.getAttributeAsString(
                                                                                                                                                                                        "valorPago")
                                                                                                                                                                                        .replaceAll(
                                                                                                                                                                                                "\\.",
                                                                                                                                                                                                "")
                                                                                                                                                                                        .replaceAll(
                                                                                                                                                                                                ",",
                                                                                                                                                                                                "."))
                                                                                                                                                                        + Double.parseDouble(
                                                                                                                                                                                fat.getAttributeAsString(
                                                                                                                                                                                        "valorApagar")
                                                                                                                                                                                        .replaceAll(
                                                                                                                                                                                                "\\.",
                                                                                                                                                                                                "")
                                                                                                                                                                                        .replaceAll(
                                                                                                                                                                                                ",",
                                                                                                                                                                                                ".")))));
                                                                                            }
                                                                                            Info.dbService
                                                                                                    .buscarTotalLancamentos(
                                                                                                            codCliente,
                                                                                                            new AsyncCallback<Double>() {

                                                                                                                @Override
                                                                                                                public void onFailure(
                                                                                                                        Throwable caught) {
                                                                                                                    throw new UnsupportedOperationException(
                                                                                                                            "Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                                                                                                                }

                                                                                                                @Override
                                                                                                                public void onSuccess(
                                                                                                                        Double totLanc) {
                                                                                                                    labelLanc
                                                                                                                            .redraw();
                                                                                                                    labelLanc
                                                                                                                            .setContents(
                                                                                                                                    "<strong style=\"font-size:20px; \">Total de Lanamentos: R$"
                                                                                                                                            + Info.formataSaldo(
                                                                                                                                                    totLanc)
                                                                                                                                            + "</strong>");
                                                                                                                    objFaturaLancamento
                                                                                                                            .setTotalLancamentos(
                                                                                                                                    totLanc);
                                                                                                                    labelfaturas
                                                                                                                            .redraw();
                                                                                                                    labelfaturas
                                                                                                                            .setContents(
                                                                                                                                    "<strong style=\"font-size:20px; \">Total de Faturas: R$"
                                                                                                                                            + Info.formataSaldo(
                                                                                                                                                    objFaturaLancamento
                                                                                                                                                            .getTotalFaturas())
                                                                                                                                            + "</strong>");
                                                                                                                    objFaturaLancamento
                                                                                                                            .setSaldo(
                                                                                                                                    objFaturaLancamento
                                                                                                                                            .getTotalLancamentos()
                                                                                                                                            - objFaturaLancamento
                                                                                                                                                    .getTotalFaturas());
                                                                                                                    if (objFaturaLancamento
                                                                                                                            .getSaldo() == 0) {
                                                                                                                        labelSaldo
                                                                                                                                .redraw();
                                                                                                                        labelSaldo
                                                                                                                                .setContents(
                                                                                                                                        "<strong style=\"font-size:25px; \">Saldo Atual: R$00,00\n</strong>");
                                                                                                                    } else if (objFaturaLancamento
                                                                                                                            .getSaldo() > 0) {
                                                                                                                        labelSaldo
                                                                                                                                .redraw();
                                                                                                                        labelSaldo
                                                                                                                                .setContents(
                                                                                                                                        "<strong class=\"saldo-positivo\">Crdito Atual: R$"
                                                                                                                                                + Info.formataSaldo(
                                                                                                                                                        Info.formataDecimal(
                                                                                                                                                                objFaturaLancamento
                                                                                                                                                                        .getSaldo()))
                                                                                                                                                + "\n</strong>");
                                                                                                                    } else {
                                                                                                                        labelSaldo
                                                                                                                                .redraw();
                                                                                                                        labelSaldo
                                                                                                                                .setContents(
                                                                                                                                        "<strong class=\"saldo-negativo\">Dbito Atual: R$"
                                                                                                                                                + Info.formataSaldo(
                                                                                                                                                        Info.formataDecimal(
                                                                                                                                                                objFaturaLancamento
                                                                                                                                                                        .getSaldo()))
                                                                                                                                                + "\n</strong>");
                                                                                                                    }
                                                                                                                }
                                                                                                            });
                                                                                        } else {
                                                                                            Info.dbService
                                                                                                    .buscarTotalLancamentos(
                                                                                                            codCliente,
                                                                                                            new AsyncCallback<Double>() {

                                                                                                                @Override
                                                                                                                public void onFailure(
                                                                                                                        Throwable caught) {
                                                                                                                    throw new UnsupportedOperationException(
                                                                                                                            "Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                                                                                                                }

                                                                                                                @Override
                                                                                                                public void onSuccess(
                                                                                                                        Double totLanc) {
                                                                                                                    objFaturaLancamento
                                                                                                                            .setTotalLancamentos(
                                                                                                                                    totLanc);
                                                                                                                    labelLanc
                                                                                                                            .redraw();
                                                                                                                    labelLanc
                                                                                                                            .setContents(
                                                                                                                                    "<strong style=\"font-size:20px; \">Total de Lanamentos: R$"
                                                                                                                                            + Info.formataSaldo(
                                                                                                                                                    Info.formataDecimal(
                                                                                                                                                            objFaturaLancamento
                                                                                                                                                                    .getTotalLancamentos()))
                                                                                                                                            + "</strong>");
                                                                                                                    labelfaturas
                                                                                                                            .redraw();
                                                                                                                    //GWT.log("objFaturaLancamento.getTotalFaturas()): " + objFaturaLancamento.getTotalFaturas());
                                                                                                                    labelfaturas
                                                                                                                            .setContents(
                                                                                                                                    "<strong style=\"font-size:20px; \">Total de Faturas: R$"
                                                                                                                                            + Info.formataSaldo(
                                                                                                                                                    Info.formataDecimal(
                                                                                                                                                            objFaturaLancamento
                                                                                                                                                                    .getTotalFaturas()))
                                                                                                                                            + "</strong>");
                                                                                                                    objFaturaLancamento
                                                                                                                            .setSaldo(
                                                                                                                                    objFaturaLancamento
                                                                                                                                            .getTotalLancamentos()
                                                                                                                                            - objFaturaLancamento
                                                                                                                                                    .getTotalFaturas());
                                                                                                                    if (objFaturaLancamento
                                                                                                                            .getSaldo() == 0) {
                                                                                                                        labelSaldo
                                                                                                                                .redraw();
                                                                                                                        labelSaldo
                                                                                                                                .setContents(
                                                                                                                                        "<strong style=\"font-size:25px; \">Saldo Atual: R$00,00\n</strong>");
                                                                                                                    } else if (objFaturaLancamento
                                                                                                                            .getSaldo() > 0) {
                                                                                                                        labelSaldo
                                                                                                                                .redraw();
                                                                                                                        labelSaldo
                                                                                                                                .setContents(
                                                                                                                                        "<strong class=\"saldo-positivo\">Crdito Atual: R$"
                                                                                                                                                + Info.formataSaldo(
                                                                                                                                                        Info.formataDecimal(
                                                                                                                                                                objFaturaLancamento
                                                                                                                                                                        .getSaldo()))
                                                                                                                                                + "\n</strong>");
                                                                                                                    } else {
                                                                                                                        labelSaldo
                                                                                                                                .redraw();
                                                                                                                        labelSaldo
                                                                                                                                .setContents(
                                                                                                                                        "<strong class=\"saldo-negativo\">Dbito Atual: R$"
                                                                                                                                                + Info.formataSaldo(
                                                                                                                                                        Info.formataDecimal(
                                                                                                                                                                objFaturaLancamento
                                                                                                                                                                        .getSaldo()))
                                                                                                                                                + "\n</strong>");
                                                                                                                    }
                                                                                                                }
                                                                                                            });
                                                                                        }

                                                                                    }
                                                                                });
                                                                        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////                                                        
                                                                    }
                                                                }
                                                            });

                                                }
                                            });

                                    janelaCarregando.addItem(mensagem);
                                    janelaCarregando.setAnimateTime(1200);
                                    janelaCarregando.animateHide(AnimationEffect.FADE);
                                    contador = 100;
                                    destroy();
                                }
                            } else {
                                contador = 0;
                            }

                        }
                    }.schedule(50);
                }
            }

        });
        limpar.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                itemValor.setDefaultValue("");
                itemDataEhora.setValue(new Date(System.currentTimeMillis()));
                formNovoLancamento.focusInItem(itemValor);
            }
        });
        addItem(label);
        addItem(formNovoLancamento);
        addItem(labelListaFat);
        addItem(listaDeFaturas);
        addItem(saldoFaturasApagar);
        addItem(labelListaFatSelecionadas);
        addItem(faturasSelecionadas);
        addItem(hButoes);

    }

    public double getTotalFaturasApagar() {
        return totalFaturasApagar;
    }

    public void setTotalFaturasApagar(double totalFaturasApagar) {
        this.totalFaturasApagar = totalFaturasApagar;
    }

}