001: /*
002: * Created on 17/03/2005
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 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: package br.com.gfp.dao;
023:
024: import java.sql.SQLException;
025: import java.util.Calendar;
026:
027: import javax.swing.event.EventListenerList;
028:
029: import br.com.gfp.data.AccountType;
030: import br.com.gfp.data.CheckingAccount;
031: import br.com.gfp.data.CreditCard;
032: import br.com.gfp.data.Transaction;
033: import br.com.gfp.data.TransactionType;
034: import br.com.gfp.internationalization.TipoDeLancamentosMessages;
035: import br.com.gfpshare.db.AbstractDAO;
036: import br.com.gfpshare.db.SQLCondition;
037: import br.com.gfpshare.db.SQLCondition.Condicao;
038:
039: /**
040: * Controler para as aplicações financeiras
041: * A idéia é que esta classe realize a formulação de relatórios.
042: * @author Igor Regis da Silva Simoes
043: * @param <A> Tipo do PersistentObject que esse controller irá gerenciar
044: */
045: public class CreditCardDAO extends AbstractDAO<CreditCard> {
046: /**
047: * Lista de listeners de eventos do controller
048: */
049: private static final EventListenerList listeners = new EventListenerList();
050:
051: /**
052: * Ultimo cartão de crédito utilizado
053: */
054: private CreditCard creditCard = null;
055:
056: private TransactionDAO<Transaction> transactionDAO = new TransactionDAO<Transaction>();
057:
058: private TransactionTypeDAO transactionTypeDAO = new TransactionTypeDAO();
059:
060: /**
061: * @see br.com.gfpshare.db.AbstractDAO#getListeners()
062: */
063: @Override
064: protected EventListenerList getListeners() {
065: return listeners;
066: }
067:
068: /**
069: * @param mes Data da qual se deseja saber o valor da fatura
070: * @return
071: * @throws SQLException
072: */
073: public Double getValorFaturaNoMes(int mes, int ano, Integer conta)
074: throws SQLException {
075: Transaction compras = new Transaction();
076: compras.setTipo(transactionTypeDAO.getBy(
077: new TransactionType(TipoDeLancamentosMessages
078: .getMessages().getString("Despesas"))).getId());
079: compras.setConta(conta);
080:
081: if (creditCard == null || !creditCard.getId().equals(conta)) {
082: creditCard = getBy(new CreditCard(conta));
083: }
084:
085: //Montamos a condição que restringe nossa busca aos últimos 12 meses
086: Calendar inicio = Calendar.getInstance();
087: Calendar fim = Calendar.getInstance();
088:
089: //40 dias antes do pagamento
090: inicio.set(Calendar.YEAR, ano);
091: inicio.set(Calendar.MONTH, mes);
092: inicio.set(Calendar.DAY_OF_MONTH, creditCard.getDataPagamento()
093: .intValue());
094: inicio.add(Calendar.DAY_OF_MONTH, -1
095: * (creditCard.getDiasParaPagamento().intValue() + 1));
096: //10 dias antes do pagamento (começo de nova fatura)
097: fim.setTime(inicio.getTime());
098: fim.add(Calendar.DAY_OF_MONTH, 30);
099:
100: compras.addCondicaoExtra(new SQLCondition<java.sql.Date>("Dia",
101: new java.sql.Date(inicio.getTime().getTime()),
102: new java.sql.Date(fim.getTime().getTime()),
103: Condicao.BETWEEN));
104:
105: String tipoContaCartaoCredito = new AccountTypeDAO().getBy(
106: new AccountType("cartaoCredito")).getId().toString();
107:
108: compras.setTipoConta(tipoContaCartaoCredito);
109: compras.setIncluirSubtipos(true);
110: return Double.valueOf(""
111: + (-1 * transactionDAO.getTotalNoPeriodo(compras)));
112: }
113:
114: @Override
115: public synchronized boolean atualizar(CreditCard atualizacao)
116: throws SQLException {
117: deletarDebitosAutomaticos(new TransactionDAO<Transaction>(),
118: atualizacao);
119: return super .atualizar(atualizacao);
120: }
121:
122: /**
123: * Here we remove the old credit card bill value predictions
124: * @param lancamentoController
125: * @param creditCard
126: * @return
127: * @throws SQLException
128: */
129: private Transaction deletarDebitosAutomaticos(
130: TransactionDAO<Transaction> lancamentoController,
131: CreditCard creditCard) throws SQLException {
132: CreditCard old = getBy(new CreditCard(creditCard.getId()));
133: Transaction lancamentoPagamentoCartao;
134: lancamentoPagamentoCartao = new Transaction();
135: lancamentoPagamentoCartao.setConta(old.getContaDebito());
136: lancamentoPagamentoCartao.setTipoConta(new CheckingAccount()
137: .getTipoConta());
138: lancamentoPagamentoCartao
139: .setEntidadeEnvolvida(old.getBanco() == null ? old
140: .getOperadora() : old.getBanco());
141: lancamentoPagamentoCartao.setTipo(new TransactionTypeDAO()
142: .getBy(
143: new TransactionType(TipoDeLancamentosMessages
144: .getMessages().getString(
145: "PagamentoCartao"))).getId());
146: lancamentoPagamentoCartao.setEhPrevisao(true);
147:
148: //deletamos os antigos
149: lancamentoController.setCheckIntegridade(false);
150: lancamentoController.deletar(lancamentoPagamentoCartao);
151: lancamentoController.setCheckIntegridade(true);
152: return lancamentoPagamentoCartao;
153: }
154:
155: }
|