Search in sources :

Example 1 with CurrencyUnit

use of name.abuchen.portfolio.money.CurrencyUnit in project portfolio by buchen.

the class SecurityTransactionDialog method createFormElements.

@Override
protected void createFormElements(Composite editArea) {
    // 
    // input elements
    // 
    // security
    ComboInput securities = new ComboInput(editArea, Messages.ColumnSecurity);
    securities.value.setInput(including(client.getActiveSecurities(), model().getSecurity()));
    securities.bindValue(Properties.security.name(), Messages.MsgMissingSecurity);
    securities.bindCurrency(Properties.securityCurrencyCode.name());
    // portfolio + reference account
    ComboInput portfolio = new ComboInput(editArea, Messages.ColumnPortfolio);
    portfolio.value.setInput(including(client.getActivePortfolios(), model().getPortfolio()));
    portfolio.bindValue(Properties.portfolio.name(), Messages.MsgMissingPortfolio);
    ComboInput comboInput = new ComboInput(editArea, null);
    if (model() instanceof BuySellModel) {
        comboInput.value.setInput(including(client.getActiveAccounts(), ((BuySellModel) model()).getAccount()));
        comboInput.bindValue(Properties.account.name(), Messages.MsgMissingAccount);
    } else {
        List<CurrencyUnit> availableCurrencies = CurrencyUnit.getAvailableCurrencyUnits();
        Collections.sort(availableCurrencies);
        comboInput.value.setInput(availableCurrencies);
        comboInput.bindValue(Properties.transactionCurrency.name(), Messages.MsgMissingAccount);
    }
    // date
    Label lblDate = new Label(editArea, SWT.RIGHT);
    lblDate.setText(Messages.ColumnDate);
    DatePicker valueDate = new DatePicker(editArea);
    context.bindValue(new SimpleDateTimeDateSelectionProperty().observe(valueDate.getControl()), BeanProperties.value(Properties.date.name()).observe(model));
    DateTime valueTime = new DateTime(editArea, SWT.TIME | SWT.SHORT | SWT.DROP_DOWN | SWT.BORDER);
    context.bindValue(new SimpleDateTimeTimeSelectionProperty().observe(valueTime), BeanProperties.value(Properties.time.name()).observe(model));
    // other input fields
    Input shares = new Input(editArea, Messages.ColumnShares);
    shares.bindValue(Properties.shares.name(), Messages.ColumnShares, Values.Share, true);
    // $NON-NLS-1$
    Input quote = new Input(editArea, "x " + Messages.ColumnQuote);
    quote.bindBigDecimal(Properties.quote.name(), Values.Quote.pattern());
    quote.bindCurrency(Properties.securityCurrencyCode.name());
    // $NON-NLS-1$
    Input grossValue = new Input(editArea, "=");
    grossValue.bindValue(Properties.grossValue.name(), Messages.ColumnSubTotal, Values.Amount, true);
    grossValue.bindCurrency(Properties.securityCurrencyCode.name());
    // $NON-NLS-1$ //$NON-NLS-2$
    Input exchangeRate = new Input(editArea, useIndirectQuotation ? "/ " : "x ");
    exchangeRate.bindBigDecimal(useIndirectQuotation ? Properties.inverseExchangeRate.name() : Properties.exchangeRate.name(), Values.ExchangeRate.pattern());
    exchangeRate.bindCurrency(useIndirectQuotation ? Properties.inverseExchangeRateCurrencies.name() : Properties.exchangeRateCurrencies.name());
    model().addPropertyChangeListener(Properties.exchangeRate.name(), e -> exchangeRate.value.setToolTipText(AbstractModel.createCurrencyToolTip(model().getExchangeRate(), model().getTransactionCurrencyCode(), model().getSecurityCurrencyCode())));
    // $NON-NLS-1$
    final Input convertedGrossValue = new Input(editArea, "=");
    convertedGrossValue.bindValue(Properties.convertedGrossValue.name(), Messages.ColumnSubTotal, Values.Amount, true);
    convertedGrossValue.bindCurrency(Properties.transactionCurrencyCode.name());
    // fees
    Label plusForexFees = new Label(editArea, SWT.NONE);
    // $NON-NLS-1$
    plusForexFees.setText("+");
    Input forexFees = new Input(editArea, sign() + Messages.ColumnFees);
    forexFees.bindValue(Properties.forexFees.name(), Messages.ColumnFees, Values.Amount, false);
    forexFees.bindCurrency(Properties.securityCurrencyCode.name());
    Input fees = new Input(editArea, sign() + Messages.ColumnFees);
    fees.bindValue(Properties.fees.name(), Messages.ColumnFees, Values.Amount, false);
    fees.bindCurrency(Properties.transactionCurrencyCode.name());
    // taxes
    Label plusForexTaxes = new Label(editArea, SWT.NONE);
    // $NON-NLS-1$
    plusForexTaxes.setText("+");
    Input forexTaxes = new Input(editArea, sign() + Messages.ColumnTaxes);
    forexTaxes.bindValue(Properties.forexTaxes.name(), Messages.ColumnTaxes, Values.Amount, false);
    forexTaxes.bindCurrency(Properties.securityCurrencyCode.name());
    Input taxes = new Input(editArea, sign() + Messages.ColumnTaxes);
    taxes.bindValue(Properties.taxes.name(), Messages.ColumnTaxes, Values.Amount, false);
    taxes.bindCurrency(Properties.transactionCurrencyCode.name());
    // total
    String label = getTotalLabel();
    // $NON-NLS-1$
    Input total = new Input(editArea, "= " + label);
    total.bindValue(Properties.total.name(), label, Values.Amount, model().getType() != PortfolioTransaction.Type.DELIVERY_OUTBOUND);
    total.bindCurrency(Properties.transactionCurrencyCode.name());
    // note
    Label lblNote = new Label(editArea, SWT.LEFT);
    lblNote.setText(Messages.ColumnNote);
    Text valueNote = new Text(editArea, SWT.BORDER);
    context.bindValue(WidgetProperties.text(SWT.Modify).observe(valueNote), BeanProperties.value(Properties.note.name()).observe(model));
    // 
    // form layout
    // 
    int width = amountWidth(grossValue.value);
    int currencyWidth = currencyWidth(grossValue.currency);
    int labelWidth = widest(securities.label, portfolio.label, lblDate, shares.label, lblNote);
    startingWith(securities.value.getControl(), securities.label).suffix(securities.currency).thenBelow(portfolio.value.getControl()).label(portfolio.label).suffix(// 
    comboInput.value.getControl()).thenBelow(valueDate.getControl()).label(lblDate).thenRight(valueTime);
    startingWith(securities.label).width(labelWidth);
    // shares - quote - gross value
    startingWith(valueDate.getControl()).thenBelow(shares.value).width(width).label(shares.label).thenRight(quote.label).thenRight(quote.value).width(width).thenRight(quote.currency).width(width).thenRight(grossValue.label).thenRight(grossValue.value).width(width).thenRight(grossValue.currency);
    startingWith(quote.value).thenBelow(exchangeRate.value).width(width).label(exchangeRate.label).thenRight(exchangeRate.currency).width(width);
    startingWith(grossValue.value).thenBelow(convertedGrossValue.value).width(width).label(convertedGrossValue.label).suffix(convertedGrossValue.currency).thenBelow(fees.value).width(width).label(fees.label).suffix(fees.currency).thenBelow(taxes.value).width(width).label(taxes.label).suffix(taxes.currency).thenBelow(total.value).width(width).label(total.label).suffix(total.currency).thenBelow(valueNote).left(securities.value.getControl()).right(total.value).label(lblNote);
    startingWith(fees.value).thenLeft(plusForexFees).thenLeft(forexFees.currency).width(currencyWidth).thenLeft(forexFees.value).width(width).thenLeft(forexFees.label);
    startingWith(taxes.value).thenLeft(plusForexTaxes).thenLeft(forexTaxes.currency).width(currencyWidth).thenLeft(forexTaxes.value).width(width).thenLeft(forexTaxes.label);
    // 
    // hide / show exchange rate if necessary
    // 
    model.addPropertyChangeListener(Properties.exchangeRateCurrencies.name(), event -> {
        // NOSONAR
        String securityCurrency = model().getSecurityCurrencyCode();
        String accountCurrency = model().getTransactionCurrencyCode();
        // make exchange rate visible if both are set but different
        boolean visible = securityCurrency.length() > 0 && accountCurrency.length() > 0 && !securityCurrency.equals(accountCurrency);
        exchangeRate.setVisible(visible);
        convertedGrossValue.setVisible(visible);
        forexFees.setVisible(visible);
        plusForexFees.setVisible(visible);
        fees.label.setVisible(!visible);
        forexTaxes.setVisible(visible);
        plusForexTaxes.setVisible(visible);
        taxes.label.setVisible(!visible);
        // set fx taxes and tx fees to 0 if not visible
        if (!visible) {
            model().setForexFees(0);
            model().setForexTaxes(0);
        }
    });
    WarningMessages warnings = new WarningMessages(this);
    warnings.add(() -> model().getDate().isAfter(LocalDate.now()) ? Messages.MsgDateIsInTheFuture : null);
    warnings.add(() -> new StockSplitWarning().check(model().getSecurity(), model().getDate()));
    model.addPropertyChangeListener(Properties.security.name(), e -> warnings.check());
    model.addPropertyChangeListener(Properties.date.name(), e -> warnings.check());
    // $NON-NLS-1$
    model.firePropertyChange(Properties.exchangeRateCurrencies.name(), "", model().getExchangeRateCurrencies());
}
Also used : CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) SimpleDateTimeDateSelectionProperty(name.abuchen.portfolio.ui.util.SimpleDateTimeDateSelectionProperty) Label(org.eclipse.swt.widgets.Label) Text(org.eclipse.swt.widgets.Text) DateTime(org.eclipse.swt.widgets.DateTime) DatePicker(name.abuchen.portfolio.ui.util.DatePicker) SimpleDateTimeTimeSelectionProperty(name.abuchen.portfolio.ui.util.SimpleDateTimeTimeSelectionProperty)

Example 2 with CurrencyUnit

use of name.abuchen.portfolio.money.CurrencyUnit in project portfolio by buchen.

the class CheckCurrenciesAction method process.

@Override
public Status process(Security security) {
    String currency = security.getCurrencyCode();
    CurrencyUnit unit = CurrencyUnit.getInstance(currency);
    return unit != null ? Status.OK_STATUS : new Status(Status.Code.ERROR, MessageFormat.format(Messages.MsgCheckUnsupportedCurrency, currency));
}
Also used : CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit)

Example 3 with CurrencyUnit

use of name.abuchen.portfolio.money.CurrencyUnit in project portfolio by buchen.

the class CSVPortfolioTransactionExtractor method extract.

@Override
void extract(List<Item> items, String[] rawValues, Map<String, Column> field2column) throws ParseException {
    // if forex gross amount is available then assume that is the currency
    // of the security to be created
    // check if we have a security
    Security security = getSecurity(rawValues, field2column, s -> {
        String currency = getText(Messages.CSVColumn_CurrencyGrossAmount, rawValues, field2column);
        if (currency == null || currency.isEmpty())
            currency = getText(Messages.CSVColumn_TransactionCurrency, rawValues, field2column);
        if (currency != null) {
            CurrencyUnit unit = CurrencyUnit.getInstance(currency.trim());
            s.setCurrencyCode(unit == null ? getClient().getBaseCurrency() : unit.getCurrencyCode());
        }
    });
    if (security == null)
        throw new ParseException(MessageFormat.format(Messages.CSVImportMissingSecurity, // $NON-NLS-1$
        new StringJoiner(", ").add(Messages.CSVColumn_ISIN).add(Messages.CSVColumn_TickerSymbol).add(Messages.CSVColumn_WKN).toString()), 0);
    // check for the transaction amount
    Money amount = getMoney(rawValues, field2column);
    // determine type (if not explicitly given by import)
    Type type = inferType(rawValues, field2column, amount);
    // determine remaining fields
    LocalDateTime date = getDate(Messages.CSVColumn_Date, rawValues, field2column);
    if (date == null)
        throw new ParseException(MessageFormat.format(Messages.CSVImportMissingField, Messages.CSVColumn_Date), 0);
    Long shares = getShares(Messages.CSVColumn_Shares, rawValues, field2column);
    Long fees = getAmount(Messages.CSVColumn_Fees, rawValues, field2column);
    Long taxes = getAmount(Messages.CSVColumn_Taxes, rawValues, field2column);
    String note = getText(Messages.CSVColumn_Note, rawValues, field2column);
    Unit grossAmount = extractGrossAmount(rawValues, field2column, amount);
    switch(type) {
        case BUY:
        case SELL:
            items.add(createBuySell(rawValues, field2column, type, security, amount, fees, taxes, date, note, shares, grossAmount));
            break;
        case TRANSFER_IN:
        case TRANSFER_OUT:
            items.add(createTransfer(security, amount, fees, taxes, date, note, shares, grossAmount));
            break;
        case DELIVERY_INBOUND:
        case DELIVERY_OUTBOUND:
            items.add(createDelivery(rawValues, field2column, type, security, amount, fees, taxes, date, note, shares, grossAmount));
            break;
        default:
            throw new IllegalArgumentException(type.toString());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Money(name.abuchen.portfolio.money.Money) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Type(name.abuchen.portfolio.model.PortfolioTransaction.Type) ParseException(java.text.ParseException) Security(name.abuchen.portfolio.model.Security) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) StringJoiner(java.util.StringJoiner)

Example 4 with CurrencyUnit

use of name.abuchen.portfolio.money.CurrencyUnit in project portfolio by buchen.

the class NewClientWizard method performFinish.

@Override
public boolean performFinish() {
    CurrencyUnit currency = page.getSelectedCurrency();
    ClientFactory.setAllCurrencies(client, currency.getCurrencyCode());
    return true;
}
Also used : CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit)

Example 5 with CurrencyUnit

use of name.abuchen.portfolio.money.CurrencyUnit in project portfolio by buchen.

the class BindingHelper method bindCurrencyCodeCombo.

public final ComboViewer bindCurrencyCodeCombo(Composite editArea, String label, String property) {
    Label l = new Label(editArea, SWT.NONE);
    l.setText(label);
    ComboViewer combo = new ComboViewer(editArea, SWT.READ_ONLY);
    combo.setContentProvider(ArrayContentProvider.getInstance());
    combo.setLabelProvider(new LabelProvider());
    List<CurrencyUnit> currencies = new ArrayList<>();
    currencies.add(CurrencyUnit.EMPTY);
    currencies.addAll(CurrencyUnit.getAvailableCurrencyUnits().stream().sorted().collect(Collectors.toList()));
    combo.setInput(currencies);
    GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).applyTo(combo.getControl());
    UpdateValueStrategy targetToModel = new UpdateValueStrategy();
    targetToModel.setConverter(new CurrencyUnitToStringConverter());
    UpdateValueStrategy modelToTarget = new UpdateValueStrategy();
    modelToTarget.setConverter(new StringToCurrencyUnitConverter());
    // 
    context.bindValue(// 
    ViewersObservables.observeSingleSelection(combo), BeanProperties.value(property).observe(model), targetToModel, modelToTarget);
    return combo;
}
Also used : UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) ComboViewer(org.eclipse.jface.viewers.ComboViewer) Label(org.eclipse.swt.widgets.Label) ArrayList(java.util.ArrayList) IBaseLabelProvider(org.eclipse.jface.viewers.IBaseLabelProvider) LabelProvider(org.eclipse.jface.viewers.LabelProvider)

Aggregations

CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)8 Label (org.eclipse.swt.widgets.Label)3 ComboViewer (org.eclipse.jface.viewers.ComboViewer)2 Composite (org.eclipse.swt.widgets.Composite)2 PropertyChangeListener (java.beans.PropertyChangeListener)1 ParseException (java.text.ParseException)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 StringJoiner (java.util.StringJoiner)1 Inject (javax.inject.Inject)1 Client (name.abuchen.portfolio.model.Client)1 Type (name.abuchen.portfolio.model.PortfolioTransaction.Type)1 Security (name.abuchen.portfolio.model.Security)1 Unit (name.abuchen.portfolio.model.Transaction.Unit)1 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)1 CurrencyConverterImpl (name.abuchen.portfolio.money.CurrencyConverterImpl)1 ExchangeRateProviderFactory (name.abuchen.portfolio.money.ExchangeRateProviderFactory)1