/** Writes all investment account transactions within the date range */ private void writeInvestmentTransactions() { for (Transaction transaction : account.getTransactions(startDate, endDate)) { if (transaction instanceof InvestmentTransaction) { InvestmentTransaction invTransaction = (InvestmentTransaction) transaction; switch (invTransaction.getTransactionType()) { case ADDSHARE: case BUYSHARE: writeBuyStockTransaction(invTransaction); break; case REMOVESHARE: case SELLSHARE: writeSellStockTransaction(invTransaction); break; case DIVIDEND: writeDividendTransaction(invTransaction); break; case REINVESTDIV: writeReinvestStockTransaction(invTransaction); break; default: break; } } else { // bank transaction, write it indentedWriter.println(wrapOpen(INVBANKTRAN), indentLevel++); writeBankTransaction(transaction); indentedWriter.println(wrapClose(INVBANKTRAN), --indentLevel); } } }
private void writeFitID(final Transaction transaction) { // write out the banks transaction id if previously imported if (transaction.getFitid() != null && !transaction.getFitid().isEmpty()) { indentedWriter.println(wrap(FITID, transaction.getFitid()), indentLevel); } else { indentedWriter.println(wrap(FITID, transaction.getUuid()), indentLevel); } }
private void writeSecID(final SecurityNode node) { // write security information indentedWriter.println(wrapOpen(SECID), indentLevel++); if (node.getISIN() != null && !node.getISIN().isEmpty()) { indentedWriter.println(wrap(UNIQUEID, node.getISIN()), indentLevel); } else { indentedWriter.println(wrap(UNIQUEID, node.getSymbol()), indentLevel); } indentedWriter.println(wrap(UNIQUEIDTYPE, "CUSIP"), indentLevel); indentedWriter.println(wrapClose(SECID), --indentLevel); }
/** * Writes one bank transaction * * @param transaction <code>Transaction</code> to write */ private void writeBankTransaction(final Transaction transaction) { indentedWriter.println(wrapOpen(STMTTRN), indentLevel++); indentedWriter.println( wrapOpen(TRNTYPE) + (transaction.getAmount(account).compareTo(BigDecimal.ZERO) == 1 ? CREDIT : DEBIT), indentLevel); indentedWriter.println(wrapOpen(DTPOSTED) + encodeDate(transaction.getDate()), indentLevel); indentedWriter.println( wrapOpen(TRNAMT) + transaction.getAmount(account).toPlainString(), indentLevel); indentedWriter.println(wrapOpen(REFNUM) + transaction.getUuid(), indentLevel); indentedWriter.println(wrapOpen(NAME) + transaction.getPayee(), indentLevel); indentedWriter.println(wrapOpen(MEMO) + transaction.getMemo(), indentLevel); // write the check number if applicable if (account.getAccountType() == AccountType.CHECKING && !transaction.getNumber().isEmpty()) { indentedWriter.println(wrapOpen(CHECKNUM) + transaction.getNumber(), indentLevel); } // write out the banks transaction id if previously imported writeFitID(transaction); indentedWriter.println(wrapClose(STMTTRN), --indentLevel); }
public void exportAccount() { final Date exportDate = new Date(); if (account == null || startDate == null || endDate == null || file == null) { throw new RuntimeException(); } // force a correct file extension final String fileName = FileUtils.stripFileExtension(file.getAbsolutePath()) + ".ofx"; try (IndentedPrintWriter writer = new IndentedPrintWriter( new BufferedWriter( new OutputStreamWriter( new FileOutputStream(fileName), Charset.forName("windows-1252"))))) { indentedWriter = writer; // write the required header for (String line : OFXHEADER) { writer.println(line, indentLevel); } writer.println(); // start of data writer.println(wrapOpen(OFX), indentLevel++); // write sign-on response writer.println(wrapOpen(SIGNONMSGSRSV1), indentLevel++); writer.println(wrapOpen(SONRS), indentLevel++); writer.println(wrapOpen(STATUS), indentLevel++); writer.println(wrapOpen(CODE) + "0", indentLevel); writer.println(wrapOpen(SEVERITY) + "INFO", indentLevel); writer.println(wrapClose(STATUS), --indentLevel); writer.println(wrapOpen(DTSERVER) + encodeDate(exportDate), indentLevel); writer.println(wrapOpen(LANGUAGE) + "ENG", indentLevel); writer.println(wrapClose(SONRS), --indentLevel); writer.println(wrapClose(SIGNONMSGSRSV1), --indentLevel); writer.println(wrapOpen(getBankingMessageSetAggregate(account)), indentLevel++); writer.println(wrapOpen(getResponse(account)), indentLevel++); writer.println(wrapOpen(TRNUID) + "1", indentLevel); writer.println(wrapOpen(STATUS), indentLevel++); writer.println(wrapOpen(CODE) + "0", indentLevel); writer.println(wrapOpen(SEVERITY) + "INFO", indentLevel); writer.println(wrapClose(STATUS), --indentLevel); // begin start of statement response writer.println(wrapOpen(getStatementResponse(account)), indentLevel++); writer.println(wrapOpen(CURDEF) + account.getCurrencyNode().getSymbol(), indentLevel); // write account identification writer.println(wrapOpen(getAccountFromAggregate(account)), indentLevel++); switch (account.getAccountType()) { case INVEST: case MUTUAL: writer.println( wrapOpen(BROKERID), indentLevel); // required for investment accounts, but jGnash does not manage a // broker ID, normally a web URL break; default: writer.println( wrapOpen(BANKID) + account.getBankId(), indentLevel); // savings and checking only break; } writer.println(wrapOpen(ACCTID) + account.getAccountNumber(), indentLevel); // write the required account type switch (account.getAccountType()) { case CHECKING: writer.println(wrapOpen(ACCTTYPE) + CHECKING, indentLevel); break; case ASSET: case BANK: case CASH: writer.println(wrapOpen(ACCTTYPE) + SAVINGS, indentLevel); break; case CREDIT: case LIABILITY: writer.println(wrapOpen(ACCTTYPE) + CREDITLINE, indentLevel); break; case SIMPLEINVEST: writer.println(wrapOpen(ACCTTYPE) + MONEYMRKT, indentLevel); break; default: break; } writer.println(wrapClose(getAccountFromAggregate(account)), --indentLevel); // begin start of transaction list writer.println(wrapOpen(getTransactionList(account)), indentLevel++); writer.println(wrapOpen(DTSTART) + encodeDate(startDate), indentLevel); writer.println(wrapOpen(DTEND) + encodeDate(endDate), indentLevel); // write the transaction list if (account.getAccountType() == AccountType.INVEST || account.getAccountType() == AccountType.MUTUAL) { writeInvestmentTransactions(); } else { writeBankTransactions(); } // end of transaction list writer.println(wrapClose(getTransactionList(account)), --indentLevel); // write ledger balance writer.println(wrapOpen(LEDGERBAL), indentLevel++); writer.println(wrapOpen(BALAMT) + account.getBalance(endDate).toPlainString(), indentLevel); writer.println(wrapOpen(DTASOF) + encodeDate(exportDate), indentLevel); writer.println(wrapClose(LEDGERBAL), --indentLevel); // end of statement response writer.println(wrapClose(getStatementResponse(account)), --indentLevel); writer.println(wrapClose(getResponse(account)), --indentLevel); writer.println(wrapClose(getBankingMessageSetAggregate(account)), --indentLevel); // finished writer.println(wrapClose(OFX), --indentLevel); } catch (IOException e) { Logger.getLogger(OfxExport.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e); } }
private void writeDividendTransaction(final InvestmentTransaction transaction) { indentedWriter.println(wrapOpen(INCOME), indentLevel++); indentedWriter.println(wrapOpen(INVTRAN), indentLevel++); writeFitID(transaction); // write the FITID indentedWriter.println(wrap(DTTRADE, encodeDate(transaction.getDate())), indentLevel); indentedWriter.println(wrap(DTSETTLE, encodeDate(transaction.getDate())), indentLevel); indentedWriter.println( wrap(MEMO, "Dividend: " + transaction.getSecurityNode().getSymbol()), indentLevel); indentedWriter.println(wrapClose(INVTRAN), --indentLevel); // write security information writeSecID(transaction.getSecurityNode()); indentedWriter.println(wrap(INCOMETYPE, "DIV"), indentLevel); indentedWriter.println( wrap(TOTAL, transaction.getTotal(account).abs().toPlainString()), indentLevel); indentedWriter.println(wrap(SUBACCTSEC, "CASH"), indentLevel); indentedWriter.println(wrap(SUBACCTFUND, "CASH"), indentLevel); indentedWriter.println(wrapClose(INCOME), --indentLevel); }
/** * Reinvested transaction is a two part process. Need to show Income into cash and then the * reinvestment from cash * * @param transaction transaction to write */ private void writeReinvestStockTransaction(final InvestmentTransaction transaction) { // Part one, show dividend income to cash writeDividendTransaction(transaction); // Part two, show reinvest from cash indentedWriter.println(wrapOpen(REINVEST), indentLevel++); indentedWriter.println(wrapOpen(INVTRAN), indentLevel++); // write the FITID writeFitID(transaction); indentedWriter.println(wrap(DTTRADE, encodeDate(transaction.getDate())), indentLevel); indentedWriter.println(wrap(DTSETTLE, encodeDate(transaction.getDate())), indentLevel); indentedWriter.println( wrap(MEMO, "Distribution reinvestment: " + transaction.getSecurityNode().getSymbol()), indentLevel); indentedWriter.println(wrapClose(INVTRAN), --indentLevel); // write security information writeSecID(transaction.getSecurityNode()); indentedWriter.println(wrap(INCOMETYPE, "DIV"), indentLevel); indentedWriter.println( wrap(TOTAL, transaction.getTotal(account).abs().negate().toPlainString()), indentLevel); indentedWriter.println(wrap(SUBACCTSEC, "CASH"), indentLevel); indentedWriter.println(wrap(UNITS, transaction.getQuantity().toPlainString()), indentLevel); indentedWriter.println(wrap(UNITPRICE, transaction.getPrice().toPlainString()), indentLevel); indentedWriter.println(wrap(COMMISSION, transaction.getFees().toPlainString()), indentLevel); indentedWriter.println(wrapClose(REINVEST), --indentLevel); }
private void writeSellStockTransaction(final InvestmentTransaction transaction) { indentedWriter.println(wrapOpen(SELLSTOCK), indentLevel++); indentedWriter.println(wrapOpen(INVSELL), indentLevel++); indentedWriter.println(wrapOpen(INVTRAN), indentLevel++); // write the FITID writeFitID(transaction); indentedWriter.println(wrap(DTTRADE, encodeDate(transaction.getDate())), indentLevel); indentedWriter.println(wrap(DTSETTLE, encodeDate(transaction.getDate())), indentLevel); indentedWriter.println(wrapClose(INVTRAN), --indentLevel); // write security information writeSecID(transaction.getSecurityNode()); indentedWriter.println(wrap(UNITS, transaction.getQuantity().toPlainString()), indentLevel); indentedWriter.println(wrap(UNITPRICE, transaction.getPrice().toPlainString()), indentLevel); indentedWriter.println(wrap(COMMISSION, transaction.getFees().toPlainString()), indentLevel); indentedWriter.println(wrap(TOTAL, transaction.getTotal(account).toPlainString()), indentLevel); indentedWriter.println(wrap(SUBACCTSEC, "CASH"), indentLevel); indentedWriter.println(wrap(SUBACCTFUND, "CASH"), indentLevel); indentedWriter.println(wrapClose(INVSELL), --indentLevel); indentedWriter.println(wrap(SELLTYPE, "SELL"), indentLevel); indentedWriter.println(wrapClose(SELLSTOCK), --indentLevel); }