Search in sources :

Example 6 with ImportTransaction

use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.

the class ImportPageTwoController method buildTableView.

private void buildTableView() {
    final TableColumn<ImportTransaction, ImportState> stateColumn = new TableColumn<>();
    stateColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getState()));
    stateColumn.setCellFactory(param -> {
        TableCell<ImportTransaction, ImportState> cell = new ImportStateTableCell();
        cell.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
            if (event.getClickCount() > 1) {
                final ImportTransaction t = tableView.getItems().get(((TableCell<?, ?>) event.getSource()).getTableRow().getIndex());
                switch(t.getState()) {
                    case EQUAL:
                        t.setState(ImportState.NOT_EQUAL);
                        break;
                    case NOT_EQUAL:
                        t.setState(ImportState.EQUAL);
                        break;
                    case NEW:
                        t.setState(ImportState.IGNORE);
                        break;
                    case IGNORE:
                        t.setState(ImportState.NEW);
                        break;
                }
                JavaFXUtils.runLater(tableView::refresh);
            }
        });
        return cell;
    });
    tableView.getColumns().add(stateColumn);
    final TableColumn<ImportTransaction, LocalDate> dateColumn = new TableColumn<>(resources.getString("Column.Date"));
    dateColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getDatePosted()));
    dateColumn.setCellFactory(param -> new ShortDateTableCell<>());
    tableView.getColumns().add(dateColumn);
    final TableColumn<ImportTransaction, String> numberColumn = new TableColumn<>(resources.getString("Column.Num"));
    numberColumn.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getCheckNumber()));
    tableView.getColumns().add(numberColumn);
    final TableColumn<ImportTransaction, String> payeeColumn = new TableColumn<>(resources.getString("Column.Payee"));
    payeeColumn.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getPayee()));
    tableView.getColumns().add(payeeColumn);
    final TableColumn<ImportTransaction, String> memoColumn = new TableColumn<>(resources.getString("Column.Memo"));
    memoColumn.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getMemo()));
    tableView.getColumns().add(memoColumn);
    final TableColumn<ImportTransaction, Account> accountColumn = new TableColumn<>(resources.getString("Column.Account"));
    accountColumn.setCellValueFactory(param -> {
        if (param.getValue() != null && param.getValue().getAccount() != null) {
            return new SimpleObjectProperty<>(param.getValue().getAccount());
        }
        return null;
    });
    accountColumn.setCellFactory(param -> new AccountComboBoxTableCell<>());
    accountColumn.setEditable(true);
    accountColumn.setOnEditCommit(event -> {
        event.getTableView().getItems().get(event.getTablePosition().getRow()).setAccount(event.getNewValue());
        lastAccount = event.getNewValue();
        JavaFXUtils.runLater(tableViewManager::packTable);
    });
    tableView.getColumns().add(accountColumn);
    incomeAccountColumn = new TableColumn<>(resources.getString("Column.Income"));
    incomeAccountColumn.setCellValueFactory(param -> {
        if (param.getValue() != null && param.getValue().getGainsAccount() != null) {
            return new SimpleObjectProperty<>(param.getValue().getGainsAccount());
        }
        return null;
    });
    incomeAccountColumn.setCellFactory(param -> new IncomeAccountComboBoxTableCell<>());
    incomeAccountColumn.setEditable(true);
    incomeAccountColumn.setOnEditCommit(event -> {
        event.getTableView().getItems().get(event.getTablePosition().getRow()).setGainsAccount(event.getNewValue());
        lastGainsAccount = event.getNewValue();
        JavaFXUtils.runLater(tableViewManager::packTable);
    });
    tableView.getColumns().add(incomeAccountColumn);
    expenseAccountColumn = new TableColumn<>(resources.getString("Column.Expense"));
    expenseAccountColumn.setCellValueFactory(param -> {
        if (param.getValue() != null && param.getValue().getFeesAccount() != null) {
            if (param.getValue().getFees().compareTo(BigDecimal.ZERO) != 0) {
                return new SimpleObjectProperty<>(param.getValue().getFeesAccount());
            }
            // nop account
            return new SimpleObjectProperty<>(NOP_EXPENSE_ACCOUNT);
        }
        return null;
    });
    expenseAccountColumn.setCellFactory(param -> new ExpenseAccountComboBoxTableCell<>());
    expenseAccountColumn.setEditable(true);
    expenseAccountColumn.setOnEditCommit(event -> {
        event.getTableView().getItems().get(event.getTablePosition().getRow()).setFeesAccount(event.getNewValue());
        JavaFXUtils.runLater(tableViewManager::packTable);
    });
    tableView.getColumns().add(expenseAccountColumn);
    final TableColumn<ImportTransaction, BigDecimal> amountColumn = new TableColumn<>(resources.getString("Column.Amount"));
    amountColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getAmount()));
    amountColumn.setCellFactory(param -> new BigDecimalTableCell<>(numberFormat));
    amountColumn.setCellFactory(param -> {
        final TableCell<ImportTransaction, BigDecimal> cell = new BigDecimalTableCell<>(numberFormat);
        // add tool tip
        cell.indexProperty().addListener((observable, oldValue, newValue) -> {
            final int index = newValue.intValue();
            if (index >= 0 && index < tableView.itemsProperty().get().size()) {
                cell.setTooltip(new Tooltip(tableView.itemsProperty().get().get(index).getToolTip()));
            }
        });
        return cell;
    });
    tableView.getColumns().add(amountColumn);
    typeColumn = new TableColumn<>(resources.getString("Column.Type"));
    typeColumn.setCellValueFactory(param -> {
        TransactionType transactionType = TransactionType.SINGLENTRY;
        if (param.getValue().isInvestmentTransaction()) {
            transactionType = param.getValue().getTransactionType();
        } else if (!param.getValue().getAccount().equals(baseAccount)) {
            transactionType = TransactionType.DOUBLEENTRY;
        }
        return new SimpleStringProperty(transactionType.toString());
    });
    tableView.getColumns().add(typeColumn);
}
Also used : Account(jgnash.engine.Account) TransactionType(jgnash.engine.TransactionType) ImportState(jgnash.convert.importat.ImportState) LocalDate(java.time.LocalDate) ImportTransaction(jgnash.convert.importat.ImportTransaction) BigDecimalTableCell(jgnash.uifx.control.BigDecimalTableCell) TableCell(javafx.scene.control.TableCell) ShortDateTableCell(jgnash.uifx.control.ShortDateTableCell) BigDecimalTableCell(jgnash.uifx.control.BigDecimalTableCell) Tooltip(javafx.scene.control.Tooltip) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) TableColumn(javafx.scene.control.TableColumn) BigDecimal(java.math.BigDecimal) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty)

Example 7 with ImportTransaction

use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.

the class Ofx2Test method parseBankOneCommas.

/**
 * Test for amounts that use a comma as a decimal SEPARATOR
 */
@Test
void parseBankOneCommas() {
    final String testFile = "/bank1-commas.ofx";
    URL url = Ofx2Test.class.getResource(testFile);
    String encoding;
    try {
        encoding = FileMagic.getOfxV1Encoding(Paths.get(url.toURI()));
        try (InputStream stream = Ofx2Test.class.getResourceAsStream(testFile)) {
            parser.parse(OfxV1ToV2.convertToXML(stream), encoding);
            assertEquals("ENG", parser.getLanguage());
            assertEquals("INFO", parser.getStatusSeverity());
            assertEquals(0, parser.getStatusCode());
            assertEquals(0, parser.getBank().statusCode);
            assertEquals("INFO", parser.getBank().statusSeverity);
            assertNull(parser.getBank().statusMessage);
            assertEquals(new BigDecimal("524.10"), parser.getBank().ledgerBalance);
            assertEquals(new BigDecimal("519.10"), parser.getBank().availBalance);
            List<ImportTransaction> transactions = parser.getBank().getTransactions();
            assertEquals(new BigDecimal("-130.00"), transactions.get(0).getAmount());
            assertEquals(new BigDecimal("-120.00"), transactions.get(1).getAmount());
            assertEquals(new BigDecimal("300.01"), transactions.get(2).getAmount());
            assertEquals(new BigDecimal("160.50"), transactions.get(3).getAmount());
            assertFalse(parser.getBank().isInvestmentAccount());
        } catch (IOException e) {
            logSevere(Ofx2Test.class, e);
            fail();
        }
    } catch (URISyntaxException e) {
        logSevere(Ofx2Test.class, e);
        fail();
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) BigDecimal(java.math.BigDecimal) ImportTransaction(jgnash.convert.importat.ImportTransaction) Test(org.junit.jupiter.api.Test)

Example 8 with ImportTransaction

use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.

the class OfxExportText method testExport.

@Test
void testExport() throws Exception {
    Transaction transaction = TransactionFactory.generateDoubleEntryTransaction(checkingAccount, usdBankAccount, BigDecimal.TEN, LocalDate.now(), "Transfer Test", "Transfer", "");
    assertTrue(e.addTransaction(transaction));
    Path path = Files.createTempFile("j", ".ofx");
    OfxExport ofxExport = new OfxExport(usdBankAccount, LocalDate.now().minusDays(1), LocalDate.now().plusDays(1), path.toFile());
    ofxExport.exportAccount();
    assertTrue(Files.exists(path));
    OfxBank ofxBank = OfxV2Parser.parse(path);
    assertNotNull(ofxBank);
    assertEquals(0, ofxBank.statusCode);
    assertEquals("INFO", ofxBank.statusSeverity);
    assertEquals(1, ofxBank.getTransactions().size());
    ImportTransaction importTransaction = ofxBank.getTransactions().get(0);
    assertEquals("10001-C01", importTransaction.getAccountTo());
    Files.delete(path);
}
Also used : Path(java.nio.file.Path) ImportTransaction(jgnash.convert.importat.ImportTransaction) Transaction(jgnash.engine.Transaction) OfxExport(jgnash.convert.exportantur.ofx.OfxExport) ImportTransaction(jgnash.convert.importat.ImportTransaction) AbstractEngineTest(jgnash.engine.AbstractEngineTest) Test(org.junit.jupiter.api.Test)

Example 9 with ImportTransaction

use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.

the class OfxV2Parser method parseBankTransaction.

/**
 * Parses a STMTTRN element
 *
 * @param reader shared XMLStreamReader
 * @throws XMLStreamException XML parsing error has occurred
 */
private void parseBankTransaction(final XMLStreamReader reader) throws XMLStreamException {
    logger.entering(OfxV2Parser.class.getName(), "parseBankTransaction");
    final QName parsingElement = reader.getName();
    final ImportTransaction tran = new ImportTransaction();
    parse: while (reader.hasNext()) {
        final int event = reader.next();
        switch(event) {
            case XMLStreamConstants.START_ELEMENT:
                switch(reader.getLocalName()) {
                    case TRNTYPE:
                        tran.setTransactionTypeDescription(reader.getElementText());
                        break;
                    case DTPOSTED:
                        tran.setDatePosted(parseDate(reader.getElementText()));
                        break;
                    case DTUSER:
                        tran.setDateUser(parseDate(reader.getElementText()));
                        break;
                    case TRNAMT:
                        tran.setAmount(parseAmount(reader.getElementText()));
                        break;
                    case FITID:
                        tran.setFITID(reader.getElementText());
                        break;
                    case CHECKNUM:
                        tran.setCheckNumber(reader.getElementText());
                        break;
                    case NAME:
                    case // either PAYEE or NAME will be used
                    PAYEE:
                        tran.setPayee(removeExtraWhiteSpace(reader.getElementText()));
                        break;
                    case MEMO:
                        tran.setMemo(removeExtraWhiteSpace(reader.getElementText()));
                        break;
                    case // Chase bank mucking up the OFX standard
                    CATEGORY:
                        break;
                    case SIC:
                        tran.setSIC(reader.getElementText());
                        break;
                    case REFNUM:
                        tran.setRefNum(reader.getElementText());
                        break;
                    case PAYEEID:
                        tran.setPayeeId(removeExtraWhiteSpace(reader.getElementText()));
                        break;
                    case CURRENCY:
                    case ORIGCURRENCY:
                        tran.setCurrency(reader.getElementText());
                        break;
                    case // transfer into / out off an investment account
                    SUBACCTFUND:
                        tran.setSubAccount(reader.getElementText());
                        break;
                    case // consume, occurs with an investment account transfer
                    STMTTRN:
                        break;
                    case BANKACCTTO:
                    case CCACCTTO:
                    case INVACCTTO:
                        parseAccountInfo(tran, parseAccountInfo(reader));
                        break;
                    default:
                        logger.log(Level.WARNING, "Unknown STMTTRN element: {0}", reader.getLocalName());
                        break;
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (reader.getName().equals(parsingElement)) {
                    logger.fine("Found the end of the bank transaction");
                    break parse;
                }
            default:
        }
    }
    bank.addTransaction(tran);
    logger.exiting(OfxV2Parser.class.getName(), "parseBankTransaction");
}
Also used : QName(javax.xml.namespace.QName) ImportTransaction(jgnash.convert.importat.ImportTransaction)

Example 10 with ImportTransaction

use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.

the class OfxV2Parser method postProcess.

/**
 * Post processes the OFX transactions for import.  Income transactions with a reinvestment transaction will be
 * stripped out. jGnash has a reinvested dividend transaction that reduces overall transaction count.
 *
 * @param ofxBank OfxBank to process
 * @return OfxBank with post processed transactions
 */
private static OfxBank postProcess(final OfxBank ofxBank) {
    // Clone the original list
    final List<ImportTransaction> importTransactions = ofxBank.getTransactions();
    // Create a list of Reinvested dividends
    final List<ImportTransaction> reinvestedDividends = importTransactions.stream().filter(importTransaction -> importTransaction.getTransactionType() == TransactionType.REINVESTDIV).collect(Collectors.toList());
    // Search through the list and remove matching income transactions
    for (final ImportTransaction reinvestDividend : reinvestedDividends) {
        final Iterator<ImportTransaction> iterator = importTransactions.iterator();
        while (iterator.hasNext()) {
            final ImportTransaction otherTran = iterator.next();
            // if this was OFX income and the securities match and the amount match, remove the transaction
            if (reinvestDividend != otherTran && OfxTags.INCOME.equals(otherTran.getTransactionTypeDescription())) {
                if (otherTran.getAmount().compareTo(reinvestDividend.getAmount().abs()) == 0) {
                    if (otherTran.getSecurityId().equals(reinvestDividend.getSecurityId())) {
                        // remove it
                        iterator.remove();
                        // reverse sign
                        reinvestDividend.setAmount(reinvestDividend.getAmount().abs());
                    }
                }
            }
        }
    }
    return ofxBank;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) XMLInputFactory(javax.xml.stream.XMLInputFactory) SimpleFormatter(java.util.logging.SimpleFormatter) Sanitize.sanitize(jgnash.convert.importat.ofx.Sanitize.sanitize) NumberFormat(java.text.NumberFormat) Level(java.util.logging.Level) BigDecimal(java.math.BigDecimal) FileHandler(java.util.logging.FileHandler) XMLStreamConstants(javax.xml.stream.XMLStreamConstants) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Locale(java.util.Locale) XMLStreamException(javax.xml.stream.XMLStreamException) ParseException(java.text.ParseException) Path(java.nio.file.Path) OfxTags(jgnash.convert.common.OfxTags) ImportTransaction(jgnash.convert.importat.ImportTransaction) NotNull(jgnash.util.NotNull) Iterator(java.util.Iterator) Files(java.nio.file.Files) FileMagic(jgnash.util.FileMagic) DecimalFormat(java.text.DecimalFormat) IOException(java.io.IOException) Logger(java.util.logging.Logger) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TransactionType(jgnash.engine.TransactionType) ResourceUtils(jgnash.resource.util.ResourceUtils) List(java.util.List) ImportSecurity(jgnash.convert.importat.ImportSecurity) LocalDate(java.time.LocalDate) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) QName(javax.xml.namespace.QName) Handler(java.util.logging.Handler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InputStream(java.io.InputStream) ImportTransaction(jgnash.convert.importat.ImportTransaction)

Aggregations

ImportTransaction (jgnash.convert.importat.ImportTransaction)14 BigDecimal (java.math.BigDecimal)5 InputStream (java.io.InputStream)4 Account (jgnash.engine.Account)4 Transaction (jgnash.engine.Transaction)4 Test (org.junit.jupiter.api.Test)4 InputStreamReader (java.io.InputStreamReader)3 LocalDate (java.time.LocalDate)3 List (java.util.List)3 QName (javax.xml.namespace.QName)3 ImportState (jgnash.convert.importat.ImportState)3 Engine (jgnash.engine.Engine)3 TransactionType (jgnash.engine.TransactionType)3 ResourceUtils (jgnash.resource.util.ResourceUtils)3 IOException (java.io.IOException)2 LineNumberReader (java.io.LineNumberReader)2 Charset (java.nio.charset.Charset)2 Path (java.nio.file.Path)2 NumberFormat (java.text.NumberFormat)2 Map (java.util.Map)2