messages.addAll(register.getValue());
messages.add(process(register.getKey(), account));
}
protected Message process(Register register, Account account) {
Message message = new Message(register, MESSAGE_TRANSACTION_CREATED);
if ((register != null) && (!register.isSkipped())) {
AccountTransaction transaction = new AccountTransaction();
transaction.setAccount(account);
transaction.setMemo(register.getDescription());
transaction.setOperationDate(register.getDate());
transaction.setAmount(new BigDecimal(register.getAmount()));
if (Register.Operation.DEPOSIT_OR_WITHDRAWAL.equals(register.getOperation())) {
transaction.setReconciled(register.getReconciled());
transaction.setPayee(getOrCreatePayee(register, account));
transaction.setSubcategory(getOrCreateSubcategory(account.getOwner(), register));
} else if (register.isInvestment()) {
transaction.setReconciled(Boolean.FALSE);
Investment investment = getOrCreateInvestment(register, account);
transaction.setPayee(investment);
InvestmentTransaction investmentTransaction = new InvestmentTransaction();
investment.addInvestment(investmentTransaction);
investmentTransaction.setAccountTransaction(transaction);
if (register.getPrice() != null) {
investmentTransaction.setPrice(new InvestmentPrice());
investmentTransaction.getPrice().setInvestment(investment);
investmentTransaction.getPrice().setPrice(register.getPrice());
investmentTransaction.getPrice().setUpdateTime(transaction.getOperationDate());
investmentTransaction.setQuantity(register.getQuantity());
} else {
investmentTransaction.setPrice(null);
investmentTransaction.setQuantity(new Double(0D));
}
transaction.setInvestment(investmentTransaction);
String subcat = Register.Operation.BUY.equals(register.getOperation()) ? "category.investment.buy" : Register.Operation.SELL.equals(register.getOperation()) ? "category.investment.sell" : "category.investment.interest";
if (Register.Operation.BUY.equals(register.getOperation())) {
transaction.setAmount(transaction.getAmount().negate());
}
transaction.setSubcategory(Subcategory.findBySubcategory(subcat, account.getOwner()));
} else {
transaction.setReconciled(Boolean.FALSE);
boolean withdrawal = register.getAmount() < 0;
String subcat = withdrawal ? "category.transfer.out" : "category.transfer.in";
if (account.isCreditCard() & !withdrawal) {
subcat = "category.cc.payment.in";
} else {
Account targetAccount = Account.findAccount(register.getTargetAccount(), account.getOwner());
if ((targetAccount != null) && (targetAccount.isCreditCard())) {
subcat = "category.cc.payment.out";
}
}
if (subcat.endsWith("in")) {
transaction.setPayee(Payee.findMySelf(account.getOwner()));
transaction.setOriginOfTheFunds(register.getTargetAccount());
} else {
register.setPayee(register.getTargetAccount());
transaction.setPayee(getOrCreatePayee(register, account));
}
transaction.setSubcategory(Subcategory.findBySubcategory(subcat, account.getOwner()));
}
transaction.persist();
message.setConfiguration(dFormat.format(transaction.getOperationDate()) + " " + transaction.getPayee().getName() + " " + nFormat.format(transaction.getAmount()));
}
return message;
}