diff options
author | xengineering <me@xengineering.eu> | 2024-09-04 20:56:03 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-09-08 17:21:32 +0200 |
commit | 877b50ef8802f8b59d81ccabcab22fe185e898d7 (patch) | |
tree | fa322ebc22f146805ac085e82d5f2b575a5230d7 /finance/flow.py | |
parent | b85de39cd765164d5a31836cb7ca106ad6e328a7 (diff) | |
download | finance-py-877b50ef8802f8b59d81ccabcab22fe185e898d7.tar finance-py-877b50ef8802f8b59d81ccabcab22fe185e898d7.tar.zst finance-py-877b50ef8802f8b59d81ccabcab22fe185e898d7.zip |
Rename income to flow
Modeling income and expenses separately does not make sense since the
only difference is the sign of the amount. Separate definitions would
lead to a lot of duplicated code.
Diffstat (limited to 'finance/flow.py')
-rw-r--r-- | finance/flow.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/finance/flow.py b/finance/flow.py new file mode 100644 index 0000000..fb28228 --- /dev/null +++ b/finance/flow.py @@ -0,0 +1,33 @@ +import dataclasses +from datetime import datetime +from decimal import Decimal + + +@dataclasses.dataclass(kw_only=True, frozen=True) +class Flow: + """Time-discrete flow of money paid on the first day of a month""" + + amount: Decimal + + def integrate(self, start: datetime, end: datetime) -> Decimal: + """Integrate the flow between two dates to an amount of money""" + + retval = Decimal(0.0) + + current = datetime(start.year, start.month, 1) + + if start == current: + retval += self.amount + + while True: + if current.month == 12: + current = datetime(current.year + 1, 1, 1) + else: + current = datetime(current.year, current.month + 1, 1) + + if current <= end: + retval += self.amount + else: + break + + return retval |