001: /*
002: * Created on 23/03/2007
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2007 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023: package br.com.gfp.ofc.tools;
024:
025: import java.sql.SQLException;
026: import java.util.ArrayList;
027: import java.util.Calendar;
028:
029: import javax.swing.JFrame;
030: import javax.swing.JOptionPane;
031:
032: import br.com.gfp.dao.GFPController;
033: import br.com.gfp.dao.ImportByHistoryMappingDAO;
034: import br.com.gfp.dao.TransactionDAO;
035: import br.com.gfp.dao.TransactionTypeDAO;
036: import br.com.gfp.data.ImportByHistoryMapping;
037: import br.com.gfp.data.PendingTransaction;
038: import br.com.gfp.data.Transaction;
039: import br.com.gfp.internationalization.ActionsMessages;
040: import br.com.gfp.util.SimpleLog;
041:
042: public class ImportUtils {
043: /**
044: * We use it to discover the code for expenses, revenues and transfers
045: */
046: private static TransactionTypeDAO transactionTypeDAO = new TransactionTypeDAO();
047:
048: public static void conciliate(
049: ArrayList<PendingTransaction> lancamentos) {
050: Transaction transaction = new Transaction();
051: TransactionDAO<Transaction> lancamentoController = new TransactionDAO<Transaction>();
052: TransactionDAO<PendingTransaction> lancamentoPendenteController = new TransactionDAO<PendingTransaction>();
053: boolean erro = false;
054: for (PendingTransaction lancamentoPendente : lancamentos) {
055: transaction.setTipoConta(lancamentoPendente.getTipoConta());
056: transaction.setConta(lancamentoPendente.getConta());
057: transaction.setValor(lancamentoPendente.getValor());
058: transaction.setDia(lancamentoPendente.getDia());
059: if (existTransaction(lancamentoController,
060: lancamentoPendente, 0)
061: || existTransaction(lancamentoController,
062: lancamentoPendente, -1)
063: || existTransaction(lancamentoController,
064: lancamentoPendente, -2))
065: continue;
066: lancamentoPendente.setEfetivar(false);
067: lancamentoPendente.setEhDeSistema(false);
068: lancamentoPendente.setEhPrevisao(false);
069: try {
070: lancamentoPendenteController
071: .adicionarNovo(lancamentoPendente);
072: } catch (SQLException e) {
073: erro = true;
074: ((SimpleLog) GFPController.getGFPController()
075: .getContexto().get(GFPController.LOG))
076: .log("Error on add temporary transaction");
077: ((SimpleLog) GFPController.getGFPController()
078: .getContexto().get(GFPController.LOG)).log(e
079: .getLocalizedMessage());
080: ((SimpleLog) GFPController.getGFPController()
081: .getContexto().get(GFPController.LOG)).log(e);
082: }
083: }
084: if (erro)
085: JOptionPane.showMessageDialog((JFrame) GFPController
086: .getGFPController().getContexto().get(
087: GFPController.FRAME_PRINCIPAL),
088: ActionsMessages.getMessages().getString(
089: "ErroAoRegistrarLancamentosPendentes"));
090: }
091:
092: /**
093: * This method check the existence of a transaction
094: * @param desvioDia Days that will be added to the actual search day.
095: * @return
096: */
097: public static boolean existTransaction(
098: TransactionDAO<Transaction> lancamentoController,
099: PendingTransaction lancamentoPendente, int desvioDia) {
100: Transaction filtro = new Transaction();
101: Calendar calendar = Calendar.getInstance();
102: calendar.setTime(lancamentoPendente.getDia());
103: calendar.add(Calendar.DAY_OF_MONTH, desvioDia);
104:
105: filtro.setDia(calendar.getTime());
106: filtro.setConta(lancamentoPendente.getConta());
107: filtro.setTipoConta(lancamentoPendente.getTipoConta());
108: filtro.setValor(lancamentoPendente.getValor());
109: try {
110: if (lancamentoController.getBy(filtro) != null) {
111: // filtro.setDados(null); Not needed for now
112: return true;
113: }
114: } catch (SQLException e) {
115: }
116: return false;
117: }
118:
119: /**
120: * Here we set the default type for the temporary transaction
121: * @param pendingTransaction
122: */
123: public static void setDefaultType(
124: PendingTransaction pendingTransaction) {
125: try {
126: if (pendingTransaction.getValor() < 0)
127: pendingTransaction.setTipo(transactionTypeDAO
128: .getExpense().getId());
129: else
130: pendingTransaction.setTipo(transactionTypeDAO
131: .getRevenues().getId());
132: } catch (SQLException e) {
133: ((SimpleLog) GFPController.getGFPController().getContexto()
134: .get(GFPController.LOG))
135: .log("Error when defining a default type of transaction for account mapping");
136: ((SimpleLog) GFPController.getGFPController().getContexto()
137: .get(GFPController.LOG)).log(e
138: .getLocalizedMessage());
139: ((SimpleLog) GFPController.getGFPController().getContexto()
140: .get(GFPController.LOG)).log(e);
141: pendingTransaction.setTipo(0);
142: }
143: }
144:
145: /**
146: * This method will define the type of each transaction that was
147: * parsed from ofc file. We will look at database to see what mapping
148: * definition user have made.
149: * The default is to set negative transactions as expense
150: * and positive as revenue.
151: *
152: * This method don't return data, it manipulates date from parser
153: * @throws SQLException
154: */
155: public static void defineTypeForTransactions(
156: ArrayList<PendingTransaction> transactions) {
157: ImportByHistoryMappingDAO importByHistoryMappingDAO = new ImportByHistoryMappingDAO();
158: ImportByHistoryMapping importByHistoryMapping = new ImportByHistoryMapping();
159:
160: for (PendingTransaction lancamentoPendente : transactions) {
161: importByHistoryMapping
162: .setStringAMapear(parseDescription(lancamentoPendente
163: .getDescricao()));
164: importByHistoryMapping.setContaValido(lancamentoPendente
165: .getConta());
166: importByHistoryMapping.setTipoContaValido(Integer
167: .parseInt(lancamentoPendente.getTipoConta()));
168: ImportByHistoryMapping mapeamento = null;
169: try {
170: mapeamento = importByHistoryMappingDAO
171: .getBy(importByHistoryMapping);
172: } catch (SQLException e) {
173: //Type not mapped, we ignore this error since we will take the default value
174: }
175: //So we try to get a mapping that is valid for any other account to use as default
176: importByHistoryMapping.setTipoContaValido(null);
177: importByHistoryMapping.setContaValido(null);
178: try {
179: mapeamento = importByHistoryMappingDAO
180: .getBy(importByHistoryMapping);
181: } catch (SQLException e) {
182: //Type not mapped, we ignore this error since we will take the default value
183: }
184:
185: if (mapeamento != null) {
186: lancamentoPendente.setEntidadeEnvolvida(mapeamento
187: .getContatoComOQualMapear());
188: lancamentoPendente.setTipo(mapeamento
189: .getTipoLancamentoComQueMapear());
190: } else {
191: setDefaultType(lancamentoPendente);
192: mapeamento = new ImportByHistoryMapping();
193: mapeamento.setStringAMapear(importByHistoryMapping
194: .getStringAMapear());
195: mapeamento
196: .setTipoLancamentoComQueMapear(lancamentoPendente
197: .getTipo());
198: mapeamento
199: .setContaValido(lancamentoPendente.getConta());
200: mapeamento.setTipoContaValido(Integer
201: .parseInt(lancamentoPendente.getTipoConta()));
202: try {
203: importByHistoryMappingDAO.adicionarNovo(mapeamento);
204: } catch (SQLException e) {
205: ((SimpleLog) GFPController.getGFPController()
206: .getContexto().get(GFPController.LOG))
207: .log("Error when registering a new import mapping");
208: ((SimpleLog) GFPController.getGFPController()
209: .getContexto().get(GFPController.LOG))
210: .log(e.getLocalizedMessage());
211: ((SimpleLog) GFPController.getGFPController()
212: .getContexto().get(GFPController.LOG))
213: .log(e);
214: }
215: }
216: }
217: }
218:
219: private static String parseDescription(String description) {
220: //This if is useful for OFC, OFX files from BB
221: if (description.startsWith("Compra com Cartao")) {
222: description = description.substring(description
223: .indexOf(':') + 4);
224: }
225: return description;
226: }
227: }
|