{% extends "_layout.html" %}

{% import '_query_table.html' as querytable %}

{% set page_title = _('Holdings') %}

{% set holdings_all = 'SELECT account,
    units(sum(position)) as units,
    cost_number as cost,
    first(getprice(currency, cost_currency)) as price,
    cost(sum(position)) as book_value,
    value(sum(position)) as market_value,
    cost_date as acquisition_date
  WHERE account_sortkey(account) ~ "^[01]"
  GROUP BY account, cost_date, currency, cost_currency, cost_number, account_sortkey(account)
  ORDER BY account_sortkey(account), currency, cost_date' %}
{% set holdings_by = {
'account': 'SELECT account,
    units(sum(position)) as units,
    cost(sum(position)) as book_value,
    value(sum(position)) as market_value,
    safediv((abs(sum(number(value(position)))) - abs(sum(number(cost(position))))), sum(number(cost(position)))) * 100 as unrealized_profit_pct
  WHERE account_sortkey(account) ~ "^[01]"
  GROUP BY account, cost_currency, account_sortkey(account), currency
  ORDER BY account_sortkey(account), currency',
'currency': 'SELECT
    units(sum(position)) as units,
    SAFEDIV(number(only(first(cost_currency), cost(sum(position)))), number(only(first(currency), units(sum(position))))) as average_cost,
    cost(sum(position)) as book_value,
    value(sum(position)) as market_value,
    safediv((abs(sum(number(value(position)))) - abs(sum(number(cost(position))))), sum(number(cost(position)))) * 100 as unrealized_profit_pct
  WHERE account_sortkey(account) ~ "^[01]"
  GROUP BY currency, cost_currency
  ORDER BY currency, cost_currency',
'cost_currency': 'SELECT
    units(sum(position)) as units,
    cost(sum(position)) as book_value,
    value(sum(position)) as market_value,
    safediv((abs(sum(number(value(position)))) - abs(sum(number(cost(position))))), sum(number(cost(position)))) * 100 as unrealized_profit_pct
  WHERE account_sortkey(account) ~ "^[01]"
  GROUP BY cost_currency
  ORDER BY cost_currency',
} %}
{% set units_column = {
  'account': 1,
  'currency': 0,
  'cost_currency': 0,
} %}


{% block content %}
<div class="headerline">
  <h3>{% if aggregation_key %}<a href="{{ url_for('report', report_name='holdings') }}">{{ _('Holdings') }}</a>{% else %}{{ _('Holdings') }}{% endif %}</h3>
  {% for key, label in [('account', _('Account')), ('currency', _('Currency')), ('cost_currency', _('Cost currency'))] %}
  <h3>{% if not (aggregation_key == key) %}<a href="{{ url_for('holdings_by', aggregation_key=key) }}">{{ _('Holdings by') }} {{ label }}</a>{% else %}{{ _('Holdings by') }} {{ label }}{% endif %}</h3>
  {% endfor %}
</div>

{% set query_string = holdings_by.get(aggregation_key, holdings_all) %}
<p>
<a href="{{ url_for('report', report_name='query', query_string=query_string) }}">Query</a>
{{ querytable.download_links(query_string) }}
</p>
{% set contents, result_types, result_rows = ledger.query_shell.execute_query(g.filtered.entries, query_string) %}
{{ querytable.querytable(ledger, contents, result_types, result_rows, filter_empty=units_column.get(aggregation_key, 1)) }}
{% endblock %}
