| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.model.impl; |
| 5 | |
|
| 6 | |
import java.util.Date; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.Map; |
| 9 | |
import java.util.logging.Level; |
| 10 | |
import java.util.logging.Logger; |
| 11 | |
|
| 12 | |
import org.homeunix.thecave.buddi.model.Account; |
| 13 | |
import org.homeunix.thecave.buddi.model.AccountType; |
| 14 | |
import org.homeunix.thecave.buddi.model.Document; |
| 15 | |
import org.homeunix.thecave.buddi.model.ModelObject; |
| 16 | |
import org.homeunix.thecave.buddi.model.Transaction; |
| 17 | |
import org.homeunix.thecave.buddi.model.TransactionSplit; |
| 18 | |
import org.homeunix.thecave.buddi.plugin.api.exception.InvalidValueException; |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | 0 | public class AccountImpl extends SourceImpl implements Account { |
| 28 | |
private long startingBalance; |
| 29 | |
private long balance; |
| 30 | |
private long overdraftCreditLimit; |
| 31 | |
private long interestRate; |
| 32 | |
private Day startDate; |
| 33 | |
private AccountType type; |
| 34 | |
|
| 35 | |
public long getStartingBalance() { |
| 36 | 0 | return startingBalance; |
| 37 | |
} |
| 38 | |
public void setStartingBalance(long startingBalance) { |
| 39 | 0 | if (this.startingBalance != startingBalance) |
| 40 | 0 | setChanged(); |
| 41 | 0 | this.startingBalance = startingBalance; |
| 42 | 0 | } |
| 43 | |
public Date getStartDate() { |
| 44 | 0 | List<Transaction> ts = getDocument().getTransactions(this); |
| 45 | 0 | if (ts.size() > 0){ |
| 46 | 0 | if (startDate == null || startDate.after(ts.get(0).getDate())) |
| 47 | 0 | return ts.get(0).getDate(); |
| 48 | |
else |
| 49 | 0 | return startDate; |
| 50 | |
} |
| 51 | 0 | if (startDate == null) |
| 52 | 0 | return new Date(); |
| 53 | 0 | return startDate; |
| 54 | |
} |
| 55 | |
public void setStartDate(Date startDate) { |
| 56 | 0 | if (startDate != null) |
| 57 | 0 | this.startDate = new Day(startDate); |
| 58 | |
else |
| 59 | 0 | this.startDate = null; |
| 60 | 0 | } |
| 61 | |
public long getBalance() { |
| 62 | 0 | return balance; |
| 63 | |
} |
| 64 | |
public void setBalance(long balance) { |
| 65 | 0 | if (this.balance != balance) |
| 66 | 0 | setChanged(); |
| 67 | 0 | this.balance = balance; |
| 68 | 0 | } |
| 69 | |
public AccountType getAccountType() { |
| 70 | 0 | return type; |
| 71 | |
} |
| 72 | |
public void setAccountType(AccountType type) { |
| 73 | 0 | this.type = type; |
| 74 | 0 | setChanged(); |
| 75 | 0 | } |
| 76 | |
@Override |
| 77 | |
public void setName(String name) throws InvalidValueException { |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | 0 | super.setName(name); |
| 85 | 0 | } |
| 86 | |
public void updateBalance(){ |
| 87 | 0 | if (getDocument() == null) |
| 88 | 0 | return; |
| 89 | 0 | long balance = this.getStartingBalance(); |
| 90 | |
|
| 91 | 0 | List<Transaction> transactions = getDocument().getTransactions(this); |
| 92 | |
|
| 93 | 0 | for (Transaction transaction : transactions) { |
| 94 | |
try { |
| 95 | 0 | if (!transaction.isDeleted()){ |
| 96 | |
|
| 97 | 0 | if (transaction.getTo().equals(this)){ |
| 98 | 0 | balance += transaction.getAmount(); |
| 99 | 0 | transaction.setBalance(this.getUid(), balance); |
| 100 | |
} |
| 101 | |
|
| 102 | 0 | else if (transaction.getFrom().equals(this)){ |
| 103 | 0 | balance -= transaction.getAmount(); |
| 104 | 0 | transaction.setBalance(this.getUid(), balance); |
| 105 | |
} |
| 106 | |
|
| 107 | 0 | for (TransactionSplit split : transaction.getToSplits()) { |
| 108 | 0 | if(split.getSource().equals(this)) |
| 109 | 0 | balance += split.getAmount(); |
| 110 | |
} |
| 111 | |
|
| 112 | 0 | for (TransactionSplit split : transaction.getFromSplits()) { |
| 113 | 0 | if(split.getSource().equals(this)) |
| 114 | 0 | balance -= split.getAmount(); |
| 115 | |
} |
| 116 | |
} |
| 117 | |
} |
| 118 | 0 | catch (InvalidValueException ive){ |
| 119 | 0 | Logger.getLogger(AccountImpl.class.getName()).log(Level.WARNING, "Incorrect value", ive); |
| 120 | 0 | } |
| 121 | |
} |
| 122 | 0 | setBalance(balance); |
| 123 | 0 | } |
| 124 | |
|
| 125 | |
public String getFullName() { |
| 126 | 0 | return this.getName() + " (" + getAccountType().getName() + ")"; |
| 127 | |
} |
| 128 | |
public long getBalance(Date d) { |
| 129 | 0 | if (getDocument() == null) |
| 130 | 0 | return 0; |
| 131 | 0 | if (d.before(getStartDate())) |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 0 | return getStartingBalance(); |
| 138 | |
|
| 139 | 0 | List<Transaction> ts = getDocument().getTransactions(this, getStartDate(), d); |
| 140 | 0 | if (ts.size() > 0){ |
| 141 | 0 | Transaction t = ts.get(ts.size() - 1); |
| 142 | 0 | if (t.getFrom().equals(this) || t.getTo().equals(this)) |
| 143 | 0 | return t.getBalance(this.getUid()); |
| 144 | 0 | } |
| 145 | |
else { |
| 146 | |
|
| 147 | 0 | return getStartingBalance(); |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | 0 | return 0; |
| 152 | |
} |
| 153 | |
@Override |
| 154 | |
public int compareTo(ModelObject arg0) { |
| 155 | 0 | if (arg0 instanceof AccountImpl){ |
| 156 | 0 | AccountImpl a = (AccountImpl) arg0; |
| 157 | 0 | if (this.getAccountType().isCredit() != a.getAccountType().isCredit()){ |
| 158 | 0 | if (this.getAccountType().isCredit()) |
| 159 | 0 | return 1; |
| 160 | 0 | return -1; |
| 161 | |
} |
| 162 | 0 | return this.getName().compareTo(a.getName()); |
| 163 | |
} |
| 164 | 0 | return super.compareTo(arg0); |
| 165 | |
} |
| 166 | |
|
| 167 | |
public long getOverdraftCreditLimit() { |
| 168 | 0 | return overdraftCreditLimit; |
| 169 | |
} |
| 170 | |
|
| 171 | |
public void setOverdraftCreditLimit(long overdraftCreditLimit) throws InvalidValueException { |
| 172 | 0 | if (overdraftCreditLimit < 0) |
| 173 | 0 | throw new InvalidValueException("Overdraft limit must be positive"); |
| 174 | 0 | if (this.overdraftCreditLimit != overdraftCreditLimit) |
| 175 | 0 | setChanged(); |
| 176 | 0 | this.overdraftCreditLimit = overdraftCreditLimit; |
| 177 | 0 | } |
| 178 | |
|
| 179 | |
Account clone(Map<ModelObject, ModelObject> originalToCloneMap) throws CloneNotSupportedException { |
| 180 | |
|
| 181 | 0 | if (originalToCloneMap.get(this) != null) |
| 182 | 0 | return (Account) originalToCloneMap.get(this); |
| 183 | |
|
| 184 | 0 | AccountImpl a = new AccountImpl(); |
| 185 | |
|
| 186 | 0 | a.document = (Document) originalToCloneMap.get(document); |
| 187 | 0 | a.type = (AccountType) originalToCloneMap.get(getAccountType()); |
| 188 | 0 | a.balance = balance; |
| 189 | 0 | a.deleted = isDeleted(); |
| 190 | 0 | if (modifiedTime != null) |
| 191 | 0 | a.modifiedTime = new Time(modifiedTime); |
| 192 | 0 | a.name = name; |
| 193 | 0 | a.notes = notes; |
| 194 | 0 | a.overdraftCreditLimit = overdraftCreditLimit; |
| 195 | 0 | a.interestRate = interestRate; |
| 196 | 0 | if (startDate != null) |
| 197 | 0 | a.startDate = new Day(startDate); |
| 198 | 0 | a.startingBalance = startingBalance; |
| 199 | |
|
| 200 | 0 | originalToCloneMap.put(this, a); |
| 201 | |
|
| 202 | 0 | return a; |
| 203 | |
} |
| 204 | |
|
| 205 | |
public long getInterestRate() { |
| 206 | 0 | return interestRate; |
| 207 | |
} |
| 208 | |
public void setInterestRate(long interestRate) throws InvalidValueException { |
| 209 | 0 | if (interestRate < 0) |
| 210 | 0 | throw new InvalidValueException("Interest rate must be positive"); |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | 0 | if (this.interestRate != interestRate) |
| 216 | 0 | setChanged(); |
| 217 | 0 | this.interestRate = interestRate; |
| 218 | 0 | } |
| 219 | |
} |