001 /*****************************************************************************
002
003 jBudget.
004 Copyright (C) 2006-2008 Dmitri Petrov
005
006 This program is free software: you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation, either version 3 of the License, or
009 (at your option) any later version.
010
011 This program is distributed in the hope that it will be useful,
012 but WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 GNU General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with this program. If not, see <http://www.gnu.org/licenses/>.
018
019 *****************************************************************************/
020
021
022 package org.jbudget.gui.month;
023
024 import java.awt.BorderLayout;
025 import java.awt.Dimension;
026 import java.util.HashSet;
027 import java.util.List;
028 import java.util.Set;
029 import javax.swing.JEditorPane;
030 import javax.swing.JPanel;
031 import javax.swing.border.SoftBevelBorder;
032 import javax.swing.text.html.HTMLEditorKit;
033 import org.jbudget.Core.MoneyPit;
034 import org.jbudget.Core.Month;
035 import org.jbudget.Core.Transaction;
036 import org.jbudget.io.DataManager;
037 import org.jbudget.Core.TransactionFilter;
038 import org.jbudget.Core.User;
039 import org.jbudget.gui.FilterChangeDispatcher;
040 import org.jbudget.gui.FilterChangeListener;
041 import org.jbudget.util.CurrencyFormater;
042
043 /**
044 * A Panel that is used to display a report of activity in a selection.
045 *
046 * @author petrov
047 */
048 public class ReportPanel extends JPanel
049 implements FilterChangeListener,
050 DataManager.UserListener,
051 DataManager.DataSourceListener
052 {
053
054 //============================= DATA ================================//
055
056 /** Filter that is used to select transactions that will be included in
057 * the report. */
058 private TransactionFilter filter;
059
060 private final JEditorPane textPane = new JEditorPane();
061
062 //========================= CONSTRUCTORS ============================//
063
064 /** Creates a new report panel for a given month. By default no fitering is
065 * used. */
066 public ReportPanel()
067 {
068 this(new TransactionFilter());
069 }
070
071 /** Creates a new report panel for a given month. The transactions and
072 * allocations that will be included in the report are selected using
073 * a given filter object. */
074 public ReportPanel(TransactionFilter filter)
075 {
076 setPreferredSize(new Dimension(450,100));
077 this.filter = filter;
078 setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
079
080 textPane.setEditorKit(new HTMLEditorKit());
081 textPane.setEditable(false);
082 updateReport();
083
084 setLayout(new BorderLayout());
085 add(textPane, BorderLayout.CENTER);
086
087 FilterChangeDispatcher.getInstance().addListener(this);
088 DataManager.getInstance().addUserListener(this);
089 DataManager.getInstance().addDataSourceListener(this);
090 }
091
092 public void dispose()
093 {
094 FilterChangeDispatcher.getInstance().removeListener(this);
095 DataManager.getInstance().removeUserListener(this);
096 DataManager.getInstance().removeDataSourceListener(this);
097 }
098
099 //============================ METHODS =============================//
100
101 /** Changes the filter */
102 public void filterUpdated(TransactionFilter newFilter, Object initiator)
103 {
104 filter = newFilter;
105 updateReport();
106 }
107
108 /** Recreates the report. */
109 private void updateReport()
110 {
111 Set<MoneyPit> sources = filter.getSources();
112 Set<MoneyPit> destinations = filter.getDestinations();
113
114 if (filter.showAllDestinations || filter.showAllSources)
115 {
116 List<Month> months = DataManager.getInstance().getMonths(filter);
117
118 if (filter.showAllDestinations)
119 {
120 destinations = new HashSet<MoneyPit>();
121 for (Month month : months)
122 destinations.addAll(month.getBudget().getAllDestinations());
123 }
124
125 if (filter.showAllSources)
126 {
127 sources = new HashSet<MoneyPit>();
128 for (Month month : months)
129 sources.addAll(month.getBudget().getAllSources());
130 }
131 }
132
133 Set<MoneyPit> accounts = new HashSet<MoneyPit>();
134 Set<MoneyPit> expenses = new HashSet<MoneyPit>();
135
136 boolean haveAccounts = false;
137 boolean haveExpenses = false;
138
139 for (MoneyPit source : sources)
140 {
141 if (source.getType() == MoneyPit.Type.ACCOUNT) {
142 haveAccounts = true;
143 accounts.add(source);
144 }
145 else if (source.getType() == MoneyPit.Type.EXPENSE) {
146 haveExpenses = true;
147 expenses.add(source);
148 }
149 }
150
151 for (MoneyPit destination : destinations)
152 {
153 if (destination.getType() == MoneyPit.Type.ACCOUNT) {
154 haveAccounts = true;
155 accounts.add(destination);
156 }
157 else if (destination.getType() == MoneyPit.Type.EXPENSE) {
158 haveExpenses = true;
159 expenses.add(destination);
160 }
161 }
162
163 // Income to selected accounts.
164 long income = 0L;
165 // Refunds to selected accounts.
166 long refundToAccounts = 0L;
167 // Refunds to selected accounts from selected Expense categories.
168 long refundToAccountsFromCategories = 0L;
169 // Transfer to selected accounts from other accounts.
170 long transferFromOther = 0L;
171 // Transfer from selected accounts to other accounts.
172 long transferToOther = 0L;
173 // Amount of money transfered between selected accounts.
174 long transferBetween = 0L;
175 // Amount of money spent from selected accounts to other accounts
176 long spentFromAccounts = 0L;
177 // Amount of money spent from selected accounts to selected categories.
178 long spentFromAccountsToCategories = 0L;
179
180 // Amount of money allocated to selected categories.
181 long allocated = 0L;
182 // Amount of money spent in the selected categories.
183 long spentInCategories = 0L;
184 // Amount of money refunded to the selected categories.
185 long refundedToCategories = 0L;
186
187 // Process transactions.
188 List<Transaction> transactions =
189 DataManager.getInstance().getTransactions(filter);
190
191 for (Transaction transaction : transactions)
192 for (Transaction.Transfer transfer : transaction.getTransfers())
193 {
194 MoneyPit source = transfer.getSource();
195 MoneyPit destination = transfer.getDestination();
196 long amount = transfer.getAmount();
197
198 // Income
199 if (source == MoneyPit.NONE ||
200 source.getType() == MoneyPit.Type.INCOME)
201 {
202 if (accounts.contains(destination))
203 income += amount;
204 continue;
205 }
206
207 if (accounts.contains(source))
208 {
209 if (destination.getType() == MoneyPit.Type.EXPENSE)
210 {
211 if (amount > 0) // Expense
212 {
213 spentFromAccounts += amount;
214 if (expenses.contains(destination)) {
215 spentInCategories += amount;
216 spentFromAccountsToCategories += amount;
217 }
218 }
219 else // Refund
220 {
221 refundToAccounts -= amount;
222 if (expenses.contains(destination)) {
223 refundedToCategories -= amount;
224 refundToAccountsFromCategories -= amount;
225 }
226 }
227 continue;
228 }
229
230 if (destination.getType() == MoneyPit.Type.ACCOUNT)
231 {
232 // Transfer between selected accounts.
233 if (accounts.contains(destination))
234 {
235 transferBetween += amount;
236 continue;
237 }
238
239 // Transfer to/from other accounts
240 if (amount > 0)
241 transferToOther += amount;
242 else
243 transferFromOther -= amount;
244 continue;
245 }
246 }
247
248 if (source.getType() == MoneyPit.Type.ACCOUNT)
249 {
250 if (accounts.contains(destination))
251 {
252 if (amount > 0)
253 transferFromOther += amount;
254 else
255 transferToOther -= amount;
256 continue;
257 }
258 if (expenses.contains(destination))
259 {
260 if (amount > 0)
261 spentInCategories += amount;
262 else
263 refundedToCategories -= amount;
264 continue;
265 }
266 }
267 }
268
269 // Now allocations
270 List<Transaction> allocations =
271 DataManager.getInstance().getAllocations(filter);
272
273 for (Transaction allocation : allocations)
274 for (Transaction.Transfer transfer : allocation.getTransfers())
275 {
276 MoneyPit source = transfer.getSource();
277 MoneyPit destination = transfer.getDestination();
278 long amount = transfer.getAmount();
279
280 if (expenses.contains(source) &&
281 expenses.contains(destination))
282 {
283 continue;
284 }
285
286 if (expenses.contains(source))
287 {
288 allocated -= amount;
289 continue;
290 }
291 if (expenses.contains(destination))
292 allocated += amount;
293 }
294
295 // Now create the HTML text.
296 StringBuffer buffer = new StringBuffer();
297 buffer.append("<html>");
298
299 if (haveAccounts)
300 {
301 buffer.append("<h1>Selected Accounts</h1>");
302 buffer.append("<hr><BR>");
303 buffer.append("<b>Income:</b> ");
304 buffer.append(CurrencyFormater.format(income));
305 buffer.append("<BR>");
306 buffer.append("<b>Spent from selected accounts:</b> ");
307 buffer.append(CurrencyFormater.format(spentFromAccounts));
308 buffer.append("<BR>");
309 if (haveExpenses)
310 {
311 buffer.append("<b> Spent to selected categories:</b> ");
312 buffer.append(CurrencyFormater.format(spentFromAccountsToCategories));
313 buffer.append("<BR>");
314 }
315 buffer.append("<b>Refunded to selected accounts:</b> ");
316 buffer.append(CurrencyFormater.format(refundToAccounts));
317 buffer.append("<BR>");
318 if (haveExpenses)
319 {
320 buffer.append("<b> Refunded from selected categories:</b> ");
321 buffer.append(CurrencyFormater.format(refundToAccountsFromCategories));
322 buffer.append("<BR>");
323 }
324 buffer.append("<b>Transfered from other accounts:</b> ");
325 buffer.append(CurrencyFormater.format(transferFromOther));
326 buffer.append("<BR>");
327 buffer.append("<b>Transfered to other accounts:</b> ");
328 buffer.append(CurrencyFormater.format(transferToOther));
329 buffer.append("<BR>");
330 buffer.append("<b>Transfered between selected accounts:</b> ");
331 buffer.append(CurrencyFormater.format(transferBetween));
332 buffer.append("<BR>");
333
334 if (haveExpenses)
335 buffer.append("<BR><HR><BR>");
336 }
337
338 if (haveExpenses)
339 {
340 buffer.append("<h1>Selected Expense Categories</h1>");
341 buffer.append("<hr><BR>");
342 buffer.append("<b>Allocated to selected categories:</b> ");
343 buffer.append(CurrencyFormater.format(allocated));
344 buffer.append("<BR>");
345 buffer.append("<b>Spent in selected categories:</b> ");
346 buffer.append(CurrencyFormater.format(spentInCategories));
347 buffer.append("<BR>");
348 buffer.append("<b>Refunded to selected categories:</b> ");
349 buffer.append(CurrencyFormater.format(refundedToCategories));
350 buffer.append("<BR>");
351 }
352
353 textPane.setText(buffer.toString());
354 }
355
356 /** Called when the user has changed. */
357 public void userChanged(User user)
358 {
359 updateReport();
360 }
361
362 /** Implementing the interface. */
363 public void userRemoved(int uid)
364 {}
365
366 /** Implementing the interface. */
367 public void userAdded(User user)
368 {}
369
370 /** Method that is called when the data directory has changed. */
371 public void dataSourceChanged()
372 {
373 DataManager.getInstance().addUserListener(this);
374 DataManager.getInstance().addDataSourceListener(this);
375 filter = new TransactionFilter();
376 updateReport();
377 }
378
379 }