Verified Commit 03962168 authored by Jakob Moser's avatar Jakob Moser
Browse files

Add BugetaryGroup class

parent 733b1384
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
from dataclasses import dataclass
from typing import Self
from collections.abc import Iterable, Sequence

from ..cash_flow.CashFlowDirection import CashFlowDirection
from .BudgetaryItem import BudgetaryItem


@dataclass(frozen=True, eq=False)
class BudgetaryGroup:
    """Group of budgetary items.

    Has a name and an id, and the items it consists of. Furthermore, a group has a cash flow direction that specifies whether its
    items are expenses (CashFlowDirection.OUTGOING) or income (CashFlowDirection.INCOMING). This means that a budget plan has always at least
    two groups, one for outgoing items and one for incoming items.

    Examples (every line is one group):

    * 1: Verwaltungseinnahmen
    * 2: gemischte Einnahmen
    * 3: Rücklagen aus 2023
    * 5: Verwaltungs- und Betriebsaufwand
    * 7: Projekte der FS
    """

    id: str
    name: str
    cash_flow_direction: CashFlowDirection
    _items: dict[str, BudgetaryItem]

    @classmethod
    def from_items(
        cls,
        id: str,
        name: str,
        cash_flow_direction: CashFlowDirection,
        items: Iterable[BudgetaryItem],
    ) -> Self:
        return cls(
            id=id,
            name=name,
            cash_flow_direction=cash_flow_direction,
            _items={item.id: item for item in items},
        )

    @property
    def items(self) -> Sequence[BudgetaryItem]:
        return tuple(self._items.values())

    def item(self, id: str) -> BudgetaryItem:
        return self._items[id]