Skip to content

Commit

Permalink
add ledger.filterFirst and ledger.filterLast (#80)
Browse files Browse the repository at this point in the history
* add ledger.filterFirst and ledger.filterLast

Resolves: #78

* update lockfiles
  • Loading branch information
andreasgerstmayr authored Aug 13, 2024
1 parent 21f5fbe commit 50a0b7c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
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

0 comments on commit 50a0b7c

Please sign in to comment.