001 /*
002 * Created on Aug 29, 2007
003 */
004 package com.x8ing.mc.bp;
005
006 import java.text.NumberFormat;
007 import java.text.SimpleDateFormat;
008 import java.util.ArrayList;
009 import java.util.Arrays;
010 import java.util.Calendar;
011 import java.util.Collection;
012 import java.util.HashSet;
013 import java.util.Iterator;
014 import java.util.List;
015 import java.util.Set;
016
017 /**
018 * Takes care of all Moneytransactions and calculates the balance (saldo).
019 *
020 * @author Patrick Heusser
021 */
022 public class BalanceSheet {
023
024 private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.GLOBAL_DATE_FORMAT);
025
026 /**
027 * keeps a set of all accounts ever used.
028 */
029 private Set currentUsedAccounts = new HashSet();
030
031 private NumberFormat numberFormat = Constants.createCurrencyFormater();
032
033 /**
034 * type: MoneyTransaction
035 */
036 private List moneyTransactions = new ArrayList();
037
038 public void addTransaction(MoneyTransaction moneyTransaction) {
039
040 // add account to accountSet
041 currentUsedAccounts.add(moneyTransaction.getBalanceAccount());
042
043 moneyTransactions.add(moneyTransaction);
044
045 if (Constants.TRACE_DEBUG) {
046
047 System.out.println(simpleDateFormat.format(moneyTransaction.getTransactionDate().getTime()) + "\tBalanceSheet:\t"
048 + moneyTransaction.getAmount() + "\t" + moneyTransaction.getBookingDescription() + "\tcurrentBalance="
049 + numberFormat.format(calculateCurrentBalance()));
050 }
051
052 }
053
054 public void addTransaction(String bookingDescription, double amount, Calendar date, BalanceAccount account) {
055 addTransaction(new MoneyTransaction(bookingDescription, date, amount, account));
056 }
057
058 /**
059 *
060 * @param accounts
061 * @return List with type MoneyTransaction
062 */
063 public List searchMoneyTransactionOfAccountTypes(BalanceAccount[] accounts) {
064
065 List ret = new ArrayList();
066
067 if (accounts == null || accounts.length == 0) {
068 return ret;
069 }
070
071 Collection c = Arrays.asList(accounts);
072 Set accountSet = new HashSet(c);
073
074 for (Iterator it = moneyTransactions.iterator(); it.hasNext();) {
075 MoneyTransaction moneyTransaction = (MoneyTransaction) it.next();
076
077 if (accountSet.contains(moneyTransaction.getBalanceAccount())) {
078 ret.add(moneyTransaction);
079 }
080 }
081
082 return ret;
083
084 }
085
086 /**
087 * calculates the balance only for the passed accounts
088 */
089 public double calculateCurrentBalanceForAccounts(BalanceAccount[] accounts) {
090
091 List subset = searchMoneyTransactionOfAccountTypes(accounts);
092
093 return calculateCurrentBalanceImpl(subset);
094
095 }
096
097 /**
098 * calculate grand overal balance.
099 */
100 public double calculateCurrentBalance() {
101
102 return calculateCurrentBalanceImpl(moneyTransactions);
103
104 }
105
106 private static double calculateCurrentBalanceImpl(List listOfMoneyTransactions) {
107
108 double sum = 0;
109
110 for (Iterator it = listOfMoneyTransactions.iterator(); it.hasNext();) {
111 MoneyTransaction moneyTransaction = (MoneyTransaction) it.next();
112 sum += moneyTransaction.getAmount();
113 }
114
115 return sum;
116
117 }
118
119 public BalanceAccount[] getAllUsedAccounts() {
120 return (BalanceAccount[]) currentUsedAccounts.toArray(new BalanceAccount[0]);
121 }
122
123 public String printBalanceSheetAccountOverview() {
124 return printBalanceSheetAccountOverview(getAllUsedAccounts());
125 }
126
127 public String printBalanceSheetAccountOverview(BalanceAccount[] accounts) {
128
129 StringBuffer sb = new StringBuffer();
130
131 for (int i = 0; i < accounts.length; i++) {
132
133 BalanceAccount account = accounts[i];
134
135 sb.append("Account: \t");
136 sb.append(account.getName());
137 sb.append("\t");
138 sb.append(numberFormat.format(calculateCurrentBalanceForAccounts(new BalanceAccount[] { account })));
139
140 sb.append("\n");
141 }
142
143 sb.append("\n\t\tgrand total:\t" + numberFormat.format(calculateCurrentBalance()));
144 sb.append("\n");
145
146 return sb.toString();
147
148 }
149
150 public String printBalanceSheetByAccount(boolean supressSameDates) {
151 return printBalanceSheetByAccount(supressSameDates, getAllUsedAccounts());
152 }
153
154 public String printBalanceSheetByAccount(boolean supressSameDates, BalanceAccount[] accounts) {
155
156 StringBuffer sb = new StringBuffer();
157 final String SEP = ",";
158
159 for (int i = 0; i < accounts.length; i++) {
160
161 BalanceAccount account = accounts[i];
162
163 List subsetMoneyTransactions = searchMoneyTransactionOfAccountTypes(new BalanceAccount[] { account });
164
165 sb.append("Account: " + account.getName() + " (" + account.getDescription() + ")");
166 sb.append("\n");
167
168 String lastPrintedDate = "";
169
170 for (Iterator it = subsetMoneyTransactions.iterator(); it.hasNext();) {
171
172 sb.append("\t");
173
174 MoneyTransaction moneyTransaction = (MoneyTransaction) it.next();
175
176 String currentDate = simpleDateFormat.format(moneyTransaction.getTransactionDate().getTime());
177
178 if (supressSameDates && currentDate.equals(lastPrintedDate)) {
179 sb.append(" ");
180 } else {
181 sb.append(currentDate);
182 }
183
184 sb.append(SEP).append("\t");
185
186 sb.append(numberFormat.format(moneyTransaction.getAmount()));
187 sb.append(SEP).append("\t");
188
189 sb.append(moneyTransaction.getBookingDescription());
190
191 sb.append("\n");
192
193 }
194
195 sb.append("Account total =\t");
196 sb.append(numberFormat.format(calculateCurrentBalanceForAccounts(new BalanceAccount[] { account })));
197 sb.append("\n\n\n");
198
199 }
200
201 sb.append("GRAND TOTAL OVER ALL ACCOUNTS: " + numberFormat.format(calculateCurrentBalance()));
202 sb.append("\n");
203
204 return sb.toString();
205
206 }
207
208 public String printBalanceSheetByDate(boolean supressSameDates) {
209
210 StringBuffer sb = new StringBuffer();
211
212 final String SEP = ",";
213
214 sb.append("date").append(SEP).append("\tamount").append(SEP).append("\taccount \tdescription \n\n");
215
216 String lastPrintedDate = "";
217
218 for (Iterator it = moneyTransactions.iterator(); it.hasNext();) {
219
220 MoneyTransaction moneyTransaction = (MoneyTransaction) it.next();
221
222 String currentDate = simpleDateFormat.format(moneyTransaction.getTransactionDate().getTime());
223
224 if (supressSameDates && currentDate.equals(lastPrintedDate)) {
225 sb.append(" ");
226 } else {
227 sb.append(currentDate);
228 }
229 sb.append(SEP).append("\t");
230
231 sb.append(moneyTransaction.getBalanceAccount().getName());
232 sb.append(SEP).append("\t");
233
234 sb.append(numberFormat.format(moneyTransaction.getAmount()));
235 sb.append(SEP).append("\t");
236
237 sb.append(moneyTransaction.getBookingDescription());
238 sb.append(SEP).append("\t");
239
240 sb.append("\n");
241
242 }
243
244 sb.append("\n\nBalance grand total = ").append(numberFormat.format(calculateCurrentBalance()));
245
246 return sb.toString();
247
248 }
249
250 public String toString() {
251 return printBalanceSheetByAccount(false, getAllUsedAccounts());
252 }
253
254 /**
255 * for bean access.
256 */
257 public String getPrintBalanceSheetByDate() {
258
259 return printBalanceSheetByDate(true);
260
261 }
262
263 /**
264 * for bean access.
265 */
266 public String getPrintBalanceSheetByAccount() {
267
268 BalanceAccount[] accounts = getAllUsedAccounts();
269 return printBalanceSheetByAccount(true, accounts);
270
271 }
272
273 /**
274 * for bean access.
275 */
276 public String getPrintBalanceSheetAccountOverview() {
277
278 return printBalanceSheetAccountOverview();
279
280 }
281
282
283 }
|