Changeset - a0ff9e6834aa
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-08-18 19:15:02
brettcsmith@brettcsmith.org
balance_sheet: Support arbitrary date ranges.

This lets you do year-over-year comparisons of smaller ranges of time, like
a quarter.
2 files changed with 33 insertions and 19 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/balance_sheet.py
Show inline comments
...
 
@@ -73,5 +73,7 @@ class Period(enum.IntFlag):
 
    PRIOR = enum.auto()
 
    MIDDLE = enum.auto()
 
    PERIOD = enum.auto()
 
    BEFORE_PERIOD = OPENING | PRIOR
 
    ANY = OPENING | PRIOR | PERIOD
 
    THRU_PRIOR = OPENING | PRIOR
 
    THRU_MIDDLE = THRU_PRIOR | MIDDLE
 
    ANY = THRU_MIDDLE | PERIOD
 

	
...
 
@@ -94,7 +96,16 @@ class Balances:
 
    ) -> None:
 
        self.prior_range = ranges.DateRange(
 
            cliutil.diff_year(start_date, -1),
 
            cliutil.diff_year(stop_date, -1),
 
        )
 
        assert self.prior_range.stop <= start_date
 
        year_diff = (stop_date - start_date).days // 365
 
        if year_diff == 0:
 
            self.prior_range = ranges.DateRange(
 
                cliutil.diff_year(start_date, -1),
 
                cliutil.diff_year(stop_date, -1),
 
            )
 
            self.period_desc = "Period"
 
        else:
 
            self.prior_range = ranges.DateRange(
 
                cliutil.diff_year(start_date, -year_diff),
 
                start_date,
 
            )
 
            self.period_desc = f"Year{'s' if year_diff > 1 else ''}"
 
        self.middle_range = ranges.DateRange(self.prior_range.stop, start_date)
 
        self.period_range = ranges.DateRange(start_date, stop_date)
...
 
@@ -104,10 +115,12 @@ class Balances:
 
            post_date = post.meta.date
 
            if post_date in self.period_range:
 
            if post_date >= stop_date:
 
                continue
 
            elif post_date in self.period_range:
 
                period = Period.PERIOD
 
            elif post_date in self.middle_range:
 
                period = Period.MIDDLE
 
            elif post_date in self.prior_range:
 
                period = Period.PRIOR
 
            elif post_date < self.prior_range.start:
 
                period = Period.OPENING
 
            else:
 
                continue
 
                period = Period.OPENING
 
            if post.account == 'Expenses:CurrencyConversion':
...
 
@@ -216,2 +229,3 @@ class Report(core.BaseODS[Sequence[None], None]):
 
        self.balances = balances
 
        self.period_desc = balances.period_desc
 
        self.date_fmt = date_fmt
...
 
@@ -361,3 +375,3 @@ class Report(core.BaseODS[Sequence[None], None]):
 
            {'period': Period.ANY},
 
            {'period': Period.BEFORE_PERIOD},
 
            {'period': Period.THRU_PRIOR},
 
        ]
...
 
@@ -402,3 +416,3 @@ class Report(core.BaseODS[Sequence[None], None]):
 
            ["With Donor", "Restrictions"],
 
            totals_prefix=["Total Year Ended"],
 
            totals_prefix=[f"Total {self.period_desc} Ended"],
 
        )
...
 
@@ -484,3 +498,3 @@ class Report(core.BaseODS[Sequence[None], None]):
 
            if kwargs['period'] is Period.PERIOD:
 
                kwargs['period'] = Period.BEFORE_PERIOD
 
                kwargs['period'] = Period.THRU_MIDDLE
 
            else:
...
 
@@ -504,3 +518,3 @@ class Report(core.BaseODS[Sequence[None], None]):
 
            ["Fundraising"],
 
            totals_prefix=["Total Year Ended"],
 
            totals_prefix=[f"Total {self.period_desc} Ended"],
 
        )
...
 
@@ -560,3 +574,3 @@ class Report(core.BaseODS[Sequence[None], None]):
 
            self.balances.total(classification=self.C_CASH, period=period)
 
            for period in [Period.BEFORE_PERIOD, Period.OPENING]
 
            for period in [Period.THRU_MIDDLE, Period.OPENING]
 
        ]
...
 
@@ -574,4 +588,4 @@ class Report(core.BaseODS[Sequence[None], None]):
 
            ["Classification"],
 
            ["Balance Ending", self.opening_name],
 
            totals_prefix=["Change During", "Year Ending"],
 
            ["Balance Beginning", self.balances.period_range.start.strftime(self.date_fmt)],
 
            totals_prefix=["Change During", f"{self.period_desc} Ending"],
 
            title_fmt="Chart of Accounts with DRAFT {sheet_name}",
setup.py
Show inline comments
...
 
@@ -7,3 +7,3 @@ setup(
 
    description="Plugin, library, and reports for reading Conservancy's books",
 
    version='1.8.3',
 
    version='1.8.4',
 
    author='Software Freedom Conservancy',
0 comments (0 inline, 0 general)