trans.setDatePosted(datePosted);
Transaction payeeTrans = PayeeUtils.findLastTransactionForPayee(payee, this.accountID);
if (payeeTrans != null) {
List transSplitList = payeeTrans.getTransSplitList();
TransSplit transSplit = null;
TransSplit newSplit = null;
double oldTotal = 0;
for (int i=0; i<transSplitList.size(); i++) {
transSplit = (TransSplit)transSplitList.get(i);
if (transSplit.getAmount().doubleValue() >= 0)
oldTotal += transSplit.getAmount().doubleValue();
}
for (int i=0; i<transSplitList.size(); i++) {
transSplit = (TransSplit)transSplitList.get(i);
newSplit = new TransSplit();
newSplit.setParentTransaction(trans);
newSplit.setAccount(transSplit.getAccount());
double splitAmount = Math.abs((transSplit.getAmount().doubleValue()/oldTotal)*amount);
if (newSplit.getAccountAccountID().equals(accountID)) {
if (amount >= 0) {
newSplit.setAmount(new Double(splitAmount));
}
else {
newSplit.setAmount(new Double(-splitAmount));
}
}
else {
if (amount >= 0) {
newSplit.setAmount(new Double(-splitAmount));
}
else {
newSplit.setAmount(new Double(splitAmount));
}
}
trans.appendTransSplit(newSplit);
}
}
else {
RegisterDAO registerDAO = (RegisterDAO)factory.getDAO("register");
List payeeTransList = registerDAO.loadTransactionsForAccountPayeeDate(this.accountID, payee.getPayeeID(), trans.getDatePosted());
if (payeeTransList.size()>0) {
boolean createTrans = false;
Transaction tempTrans = null;
for (int i=0; i<payeeTransList.size(); i++) {
tempTrans = (Transaction)payeeTransList.get(i);
for (int j=0; j<transIDList.size(); j++) {
if (transIDList.get(j).toString().equals(tempTrans.getTransactionID())) {
createTrans = true;
}
} //TODO: This is wrong.
}
if (!createTrans)
return;
}
AccountDAO accountDAO = (AccountDAO)factory.getDAO("account");
Account newAccount = (Account)accountDAO.load("WHERE TITLE = 'Imbalance-USD'", null, true).get(0);
TransSplit transSplit = new TransSplit();
transSplit.setParentTransaction(trans);
Account account = new Account();
account.setAccountID(accountID);
transSplit.setAccount(account);
transSplit.setAmount(new Double(amount));
trans.appendTransSplit(transSplit);
TransSplit newSplit = new TransSplit();
newSplit.setParentTransaction(trans);
newSplit.setAccount(newAccount);
newSplit.setAmount(new Double(-amount));
trans.appendTransSplit(newSplit);
}
}
TransactionDAO transDAO = (TransactionDAO)factory.getDAO("transaction");
transDAO.store(trans, true);