Java tutorial
/* * 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.util.SC; import com.smartgwt.client.widgets.IButton; 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.form.DynamicForm; import com.smartgwt.client.widgets.form.fields.DateItem; 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.ListGridRecord; import com.smartgwt.client.widgets.layout.HLayout; import java.util.ArrayList; import java.util.Date; import java.util.TreeMap; /** * * @author Adriano */ public class EditarFatura extends Window { private int contador; private String campo; private boolean flagItemValor = false; private double multa; private double juros; private ArrayList<TreeMap<String, String>> arrayFaturasBD = new ArrayList<TreeMap<String, String>>(); // Pega o cdigo da Unidade do Usurio Logado Cliente ou Funiconrio int codigoUnidade = Integer.parseInt( (Info.usuarioLogado.get("codUnidadeCliente") == null) ? Info.usuarioLogado.get("codUnidadeFuncionario") : Info.usuarioLogado.get("codUnidadeCliente")); public EditarFatura(final int codCliente, final ListGrid listLancamentos, final ListGrid listFaturas, final ListGridRecord recordFatura, final Label labelLanc, final Label labelfaturas, final Label labelSaldo, final TabConsultaFaturaLancamento objFaturaLancamento) { final EditarFatura novaFaturaAux = this; setTitle("Editar fatura"); setWidth(500); setHeight(300); setShowMinimizeButton(false); setShowCloseButton(false); setIsModal(true); setShowModalMask(true); centerInPage(); setLayoutAlign(Alignment.CENTER); setMembersMargin(5); Label label = new Label("<strong><h3>Cadastrar nova Fatura</h3></strong>"); label.setAlign(Alignment.CENTER); final DynamicForm formNovaFatura = new DynamicForm(); formNovaFatura.setAutoFocus(true); formNovaFatura.setWidth(490); formNovaFatura.setMargin(15); formNovaFatura.setLayoutAlign(Alignment.CENTER); final TextItem itemMesRef = new TextItem(); itemMesRef.setTitle("Ms de ref."); itemMesRef.setName("mesRef"); itemMesRef.setMask(">LLL/####"); itemMesRef.setRequired(true); itemMesRef.setHint("<nbr>*</nbr>"); itemMesRef.setMaskSaveLiterals(true); itemMesRef.setCharacterCasing(CharacterCasing.UPPER); itemMesRef.setWidth(300); itemMesRef.setHint("<nbr>*</nbr>"); itemMesRef.setValue(recordFatura.getAttribute("mesReferencia")); final DateItem dataVencimeto = new DateItem(); dataVencimeto.setTitle("Data de Venc."); dataVencimeto.setName("dataVencimento"); dataVencimeto.setUseTextField(true); dataVencimeto.setHint("<nbr>*</nbr>"); dataVencimeto.setUseMask(true); dataVencimeto.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE); dataVencimeto.setRequired(true); dataVencimeto.setWidth(300); dataVencimeto.setValue(recordFatura.getAttribute("dataFatura")); final TextItem itemValor = new TextItem(); itemValor.setTitle("Valor"); itemValor.setName("valor"); itemValor.setRequired(true); itemValor.setHint("<nbr>*</nbr>"); itemValor.setMaskSaveLiterals(true); itemValor.setCharacterCasing(CharacterCasing.UPPER); itemValor.setWidth(300); itemValor.setHint("<nbr>*</nbr>"); itemValor.setValue(recordFatura.getAttribute("valor")); 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); } }); itemValor.addChangedHandler(new ChangedHandler() { @Override public void onChanged(ChangedEvent event) { //GWT.log("Entrou no changed: " + event.getValue()); if (flagItemValor) { itemValor.setValue(campo); } flagItemValor = false; } }); formNovaFatura.setItems(itemMesRef, dataVencimeto, itemValor); // formNovaFatura.addDrawHandler(new DrawHandler() { // // @Override // public void onDraw(DrawEvent event) { // formNovaFatura.focusInItem(itemValor); // } // }); HLayout hButoes = new HLayout(); hButoes.setWidth(100); hButoes.setMembersMargin(3); hButoes.setLayoutAlign(Alignment.CENTER); IButton salvar = new IButton("Salvar"); hButoes.setMargin(5); hButoes.addMember(salvar); salvar.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { if (formNovaFatura.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%"); listFaturas.setData(new RecordList()); TreeMap<String, String> novaFatura = new TreeMap<String, String>(); novaFatura.put("codFatura", recordFatura.getAttributeAsString("codigo")); novaFatura.put("codCliente", String.valueOf(codCliente)); novaFatura.put("mes", itemMesRef.getValueAsString()); novaFatura.put("valor", itemValor.getValueAsString().replaceAll("\\.", "") .replaceAll(",", "\\.")); Date dataVencimento = dataVencimeto.getValueAsDate(); Info.dbService.alterarFatura(novaFatura, dataVencimento, new AsyncCallback<Boolean>() { @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(Boolean result) { if (result == true) { SC.say("Fatura atualizada com sucesso"); //////////////////*******************************************************/////////////////////////////////////////////////// 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); 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( Info.formataDecimal( totLanc)) + "</strong>"); objFaturaLancamento .setTotalLancamentos( totLanc); labelfaturas.redraw(); 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>"); } } }); } } }); //////////////////*******************************************************/////////////////////////////////////////////////// } else { SC.warn("ERRO ao atualizar fatura"); } } }); janelaCarregando.addItem(mensagem); janelaCarregando.setAnimateTime(1200); janelaCarregando.animateHide(AnimationEffect.FADE); contador = 100; janelaCarregando.destroy(); novaFaturaAux.destroy(); } } else { contador = 0; } } }.schedule(50); } } }); addItem(label); addItem(formNovaFatura); addItem(hButoes); } }