Search in sources :

Example 66 with PortfolioTransaction

use of name.abuchen.portfolio.model.PortfolioTransaction in project portfolio by buchen.

the class OnvistaPDFExtractor method addCapitalReductionTransaction.

private void addCapitalReductionTransaction() {
    DocumentType type = new DocumentType("Kapitalherabsetzung");
    this.addDocumentTyp(type);
    Block block = new Block("(Aus|Ein)buchung:(.*)");
    type.addBlock(block);
    Transaction<PortfolioTransaction> pdfTransaction = new Transaction<>();
    pdfTransaction.subject(() -> {
        PortfolioTransaction entry = new PortfolioTransaction();
        entry.setType(PortfolioTransaction.Type.DELIVERY_INBOUND);
        return entry;
    });
    block.set(pdfTransaction);
    pdfTransaction.section("date").optional().match("(^\\w{3}+) (\\d{1,3}(\\.\\d{3})*(,\\d{3})?) (?<date>\\d+.\\d+.\\d{4}+)?(.*)").assign((t, v) -> {
        t.setDateTime(asDate(v.get("date")));
        type.getCurrentContext().put("date", v.get("date"));
    }).section("name", // 
    "isin").find(// 
    "Gattungsbezeichnung ISIN").match(// 
    "(?<name>.*) (?<isin>[^ ]\\S*)$").assign((t, v) -> t.setSecurity(getOrCreateSecurity(v))).section(// 
    "transactiontype").match(// 
    "^(?<transactiontype>.*buchung:)(.*)").assign((t, v) -> {
        String transactiontype = v.get("transactiontype");
        if ("Einbuchung:".equalsIgnoreCase(transactiontype)) {
            t.setType(PortfolioTransaction.Type.DELIVERY_INBOUND);
        } else if ("Ausbuchung:".equalsIgnoreCase(transactiontype)) {
            t.setType(PortfolioTransaction.Type.DELIVERY_OUTBOUND);
        } else {
        // TODO: evtl. Warnung/Hinweis ausgeben?
        }
    }).section("notation", // 
    "shares").find("Nominal(.*)").match(// 
    "(?<notation>^\\w{3}+) (?<shares>\\d{1,3}(\\.\\d{3})*(,\\d{3,})?)(.*)").assign((t, v) -> {
        String notation = v.get("notation");
        if (notation != null && !notation.equalsIgnoreCase("STK")) {
            // Prozent-Notierung, Workaround..
            t.setShares((asShares(v.get("shares")) / 100));
        } else {
            t.setShares(asShares(v.get("shares")));
        }
        t.setCurrencyCode(asCurrencyCode(t.getSecurity().getCurrencyCode()));
        if (t.getDateTime() == null) {
            t.setDateTime(asDate(type.getCurrentContext().get("date")));
        }
    }).wrap(TransactionItem::new);
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) IOException(java.io.IOException) Matcher(java.util.regex.Matcher) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) Map(java.util.Map) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Pattern(java.util.regex.Pattern) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block)

Example 67 with PortfolioTransaction

use of name.abuchen.portfolio.model.PortfolioTransaction in project portfolio by buchen.

the class CheckCurrenciesAction method process.

@Override
public Status process(PortfolioTransaction transaction, Portfolio portfolio) {
    Security security = transaction.getSecurity();
    if (security == null)
        return new Status(Status.Code.ERROR, MessageFormat.format(Messages.MsgCheckMissingSecurity, transaction.getType().toString()));
    Status status = checkGrossValueAndUnitsAgainstSecurity(transaction);
    if (status.getCode() != Status.Code.OK)
        return status;
    if (transaction.getType() == PortfolioTransaction.Type.DELIVERY_OUTBOUND || transaction.getType() == PortfolioTransaction.Type.SELL) {
        // tax + fees must be < than transaction amount
        Money taxAndFees = // 
        transaction.getUnits().filter(// 
        u -> u.getType() == Unit.Type.TAX || u.getType() == Unit.Type.FEE).map(// 
        u -> u.getAmount()).collect(MoneyCollectors.sum(transaction.getCurrencyCode()));
        if (!transaction.getMonetaryAmount().isGreaterOrEqualThan(taxAndFees))
            return new Status(Status.Code.ERROR, MessageFormat.format(Messages.MsgCheckTaxAndFeesTooHigh, Values.Money.format(transaction.getMonetaryAmount()), Values.Money.format(taxAndFees)));
    }
    return Status.OK_STATUS;
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Transaction(name.abuchen.portfolio.model.Transaction) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Set(java.util.Set) Security(name.abuchen.portfolio.model.Security) Messages(name.abuchen.portfolio.Messages) MessageFormat(java.text.MessageFormat) Unit(name.abuchen.portfolio.model.Transaction.Unit) MoneyCollectors(name.abuchen.portfolio.money.MoneyCollectors) Optional(java.util.Optional) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) ImportAction(name.abuchen.portfolio.datatransfer.ImportAction) EnumSet(java.util.EnumSet) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) Money(name.abuchen.portfolio.money.Money) Security(name.abuchen.portfolio.model.Security)

Example 68 with PortfolioTransaction

use of name.abuchen.portfolio.model.PortfolioTransaction in project portfolio by buchen.

the class AktienfreundeNetExporter method exportAllTransactions.

/**
 * Export all transactions in 'aktienfreunde.net' Format
 */
public void exportAllTransactions(File file, Client client) throws IOException {
    // collect transactions
    List<? extends Transaction> transactions = Stream.concat(client.getAccounts().stream(), client.getPortfolios().stream()).flatMap(// 
    l -> l.getTransactions().stream()).sorted(// 
    new Transaction.ByDate()).collect(Collectors.toList());
    // write to file
    try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
        CSVPrinter printer = new CSVPrinter(writer);
        printer.setStrategy(CSVExporter.STRATEGY);
        writeHeader(printer);
        // only buy/sell/dividend transactions
        for (Transaction t : transactions) {
            if (t instanceof AccountTransaction)
                writeDividend(printer, (AccountTransaction) t);
            else if (t instanceof PortfolioTransaction)
                writeBuySell(printer, (PortfolioTransaction) t);
        }
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.model.Transaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 69 with PortfolioTransaction

use of name.abuchen.portfolio.model.PortfolioTransaction in project portfolio by buchen.

the class CSVExporter method exportPortfolioTransactions.

public void exportPortfolioTransactions(File file, Portfolio portfolio) throws IOException {
    try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
        CSVPrinter printer = new CSVPrinter(writer);
        printer.setStrategy(STRATEGY);
        printer.println(new String[] { // 
        Messages.CSVColumn_Date, // 
        Messages.CSVColumn_Type, // 
        Messages.CSVColumn_Value, // 
        Messages.CSVColumn_TransactionCurrency, // 
        Messages.CSVColumn_GrossAmount, // 
        Messages.CSVColumn_CurrencyGrossAmount, // 
        Messages.CSVColumn_ExchangeRate, // 
        Messages.CSVColumn_Fees, // 
        Messages.CSVColumn_Taxes, // 
        Messages.CSVColumn_Shares, // 
        Messages.CSVColumn_ISIN, // 
        Messages.CSVColumn_WKN, // 
        Messages.CSVColumn_TickerSymbol, // 
        Messages.CSVColumn_SecurityName, Messages.CSVColumn_Note });
        for (PortfolioTransaction t : portfolio.getTransactions()) {
            printer.print(t.getDateTime().toString());
            printer.print(t.getType().toString());
            printer.print(Values.Amount.format(t.getType().isLiquidation() ? -t.getAmount() : t.getAmount()));
            printer.print(t.getCurrencyCode());
            // gross amount
            Optional<Unit> grossAmount = t.getUnit(Unit.Type.GROSS_VALUE);
            if (grossAmount.isPresent()) {
                Money forex = grossAmount.get().getForex();
                printer.print(Values.Amount.format(forex.getAmount()));
                printer.print(forex.getCurrencyCode());
                printer.print(Values.ExchangeRate.format(grossAmount.get().getExchangeRate()));
            } else {
                // $NON-NLS-1$
                printer.print("");
                // $NON-NLS-1$
                printer.print("");
                // $NON-NLS-1$
                printer.print("");
            }
            printer.print(Values.Amount.format(t.getUnitSum(Unit.Type.FEE).getAmount()));
            printer.print(Values.Amount.format(t.getUnitSum(Unit.Type.TAX).getAmount()));
            printer.print(Values.Share.format(t.getShares()));
            printSecurityInfo(printer, t);
            printer.print(escapeNull(t.getNote()));
            printer.println();
        }
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Money(name.abuchen.portfolio.money.Money) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Unit(name.abuchen.portfolio.model.Transaction.Unit) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 70 with PortfolioTransaction

use of name.abuchen.portfolio.model.PortfolioTransaction in project portfolio by buchen.

the class TransactionCurrencyCheck method execute.

@Override
public List<Issue> execute(Client client) {
    Set<Object> transactions = new HashSet<Object>();
    for (Account account : client.getAccounts()) {
        account.getTransactions().stream().filter(t -> t.getCurrencyCode() == null).forEach(t -> transactions.add(t.getCrossEntry() != null ? t.getCrossEntry() : new TransactionPair<AccountTransaction>(account, t)));
    }
    for (Portfolio portfolio : client.getPortfolios()) {
        portfolio.getTransactions().stream().filter(t -> t.getCurrencyCode() == null).forEach(t -> transactions.add(t.getCrossEntry() != null ? t.getCrossEntry() : new TransactionPair<PortfolioTransaction>(portfolio, t)));
    }
    List<Issue> issues = new ArrayList<Issue>();
    for (Object t : transactions) {
        if (t instanceof TransactionPair<?>) {
            @SuppressWarnings("unchecked") TransactionPair<Transaction> pair = (TransactionPair<Transaction>) t;
            issues.add(new TransactionMissingCurrencyIssue(client, pair));
        } else if (t instanceof BuySellEntry) {
            // attempt to fix it if both currencies are identical. If a fix
            // involves currency conversion plus exchange rates, just offer
            // to delete the transaction.
            BuySellEntry entry = (BuySellEntry) t;
            String accountCurrency = entry.getAccount().getCurrencyCode();
            String securityCurrency = entry.getPortfolioTransaction().getSecurity().getCurrencyCode();
            @SuppressWarnings("unchecked") TransactionPair<Transaction> pair = new TransactionPair<Transaction>((TransactionOwner<Transaction>) entry.getOwner(entry.getAccountTransaction()), entry.getAccountTransaction());
            issues.add(new TransactionMissingCurrencyIssue(client, pair, Objects.equals(accountCurrency, securityCurrency)));
        } else if (t instanceof AccountTransferEntry) {
            // same story as with purchases: only offer to fix if currencies
            // match
            AccountTransferEntry entry = (AccountTransferEntry) t;
            String sourceCurrency = entry.getSourceAccount().getCurrencyCode();
            String targetCurrency = entry.getTargetAccount().getCurrencyCode();
            @SuppressWarnings("unchecked") TransactionPair<Transaction> pair = new TransactionPair<Transaction>((TransactionOwner<Transaction>) entry.getOwner(entry.getSourceTransaction()), entry.getSourceTransaction());
            issues.add(new TransactionMissingCurrencyIssue(client, pair, Objects.equals(sourceCurrency, targetCurrency)));
        } else if (t instanceof PortfolioTransferEntry) {
            // transferring a security involves no currency change because
            // the currency is defined the security itself
            PortfolioTransferEntry entry = (PortfolioTransferEntry) t;
            @SuppressWarnings("unchecked") TransactionPair<Transaction> pair = new TransactionPair<Transaction>((TransactionOwner<Transaction>) entry.getOwner(entry.getSourceTransaction()), entry.getSourceTransaction());
            issues.add(new TransactionMissingCurrencyIssue(client, pair));
        } else {
            throw new IllegalArgumentException();
        }
    }
    return issues;
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Client(name.abuchen.portfolio.model.Client) Transaction(name.abuchen.portfolio.model.Transaction) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) TransactionPair(name.abuchen.portfolio.model.TransactionPair) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Set(java.util.Set) QuickFix(name.abuchen.portfolio.checks.QuickFix) Messages(name.abuchen.portfolio.Messages) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) Issue(name.abuchen.portfolio.checks.Issue) HashSet(java.util.HashSet) Objects(java.util.Objects) Check(name.abuchen.portfolio.checks.Check) List(java.util.List) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner) LocalDate(java.time.LocalDate) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) TransactionPair(name.abuchen.portfolio.model.TransactionPair) Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Issue(name.abuchen.portfolio.checks.Issue) Portfolio(name.abuchen.portfolio.model.Portfolio) ArrayList(java.util.ArrayList) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.model.Transaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) HashSet(java.util.HashSet) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry)

Aggregations

PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)131 Security (name.abuchen.portfolio.model.Security)85 Test (org.junit.Test)84 Client (name.abuchen.portfolio.model.Client)70 ArrayList (java.util.ArrayList)64 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)64 Unit (name.abuchen.portfolio.model.Transaction.Unit)56 Money (name.abuchen.portfolio.money.Money)56 IOException (java.io.IOException)47 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)44 List (java.util.List)43 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)42 Values (name.abuchen.portfolio.money.Values)41 LocalDateTime (java.time.LocalDateTime)39 Portfolio (name.abuchen.portfolio.model.Portfolio)39 CoreMatchers.is (org.hamcrest.CoreMatchers.is)35 Assert.assertThat (org.junit.Assert.assertThat)35 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)33 Collectors (java.util.stream.Collectors)30 BuySellEntryItem (name.abuchen.portfolio.datatransfer.Extractor.BuySellEntryItem)28