Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ledger.filterFirst and ledger.filterLast #80

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ The following variables and functions are available:
* `panel`: the current (augmented) panel definition. The results of the BQL queries can be accessed with `panel.queries[i].result`.
* `ledger.dateFirst`: start date of the current date filter, or first transaction date of the ledger
* `ledger.dateLast`: end date of the current date filter, or last transaction date of the ledger
* `ledger.filterFirst`: start date of the current date filter, or null if no date filter is set
* `ledger.filterLast`: end date of the current date filter, or null if no date filter is set
* `ledger.operatingCurrencies`: configured operating currencies of the ledger
* `ledger.ccy`: shortcut for the first configured operating currency of the ledger
* `ledger.accounts`: declared accounts of the ledger
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# all-features: false
# with-sources: false
# generate-hashes: false
# universal: false

-e file:.
astroid==3.1.0
Expand Down
1 change: 1 addition & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# all-features: false
# with-sources: false
# generate-hashes: false
# universal: false

-e file:.
babel==2.14.0
Expand Down
19 changes: 14 additions & 5 deletions src/fava_dashboards/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def get_ledger(self):
raise FavaAPIError("no operating currency specified in the ledger")

if g.filtered.date_range:
date_first = g.filtered.date_range.begin
date_last = g.filtered.date_range.end - datetime.timedelta(days=1)
filter_first = g.filtered.date_range.begin
filter_last = g.filtered.date_range.end - datetime.timedelta(days=1)

# Adjust the dates in case the date filter is set to e.g. 2023-2024,
# however the ledger only contains data up to summer 2024.
Expand All @@ -148,10 +148,17 @@ def get_ledger(self):
ledger_date_first, ledger_date_last = self.get_ledger_duration(
self.ledger.all_entries
)
if not (date_last < ledger_date_first or date_first > ledger_date_last):
date_first = max(date_first, ledger_date_first)
date_last = min(date_last, ledger_date_last)

if filter_last < ledger_date_first or filter_first > ledger_date_last:
date_first = filter_first
date_last = filter_last
else:
# use min/max only if there is some overlap between filter and ledger dates
date_first = max(filter_first, ledger_date_first)
date_last = min(filter_last, ledger_date_last)
else:
# No time filter applied.
filter_first = filter_last = None
# Use filtered ledger here, as another filter (e.g. tag filter) could be applied.
date_first, date_last = self.get_ledger_duration(g.filtered.entries)

Expand All @@ -160,6 +167,8 @@ def get_ledger(self):
return {
"dateFirst": date_first,
"dateLast": date_last,
"filterFirst": filter_first,
"filterLast": filter_last,
"operatingCurrencies": operating_currencies,
"ccy": operating_currencies[0],
"accounts": accounts,
Expand Down