Example #1
0
 def __init__(
     self,
     namespace: str,
     cash: Account = None,
     ledger: Account = None,
     period: Period = None,
 ):
     self._period = Period() if period is None else period
     self._cash = get_default_cash_account() if cash is None else cash
     self._ledger = get_default_ledger_account()\
         if ledger is None else ledger
     self._namespace = namespace
Example #2
0
    def to_python(self, value):
        """Returns the period by the period specification.

        Args:
            value (str): The period specification.

        Returns:
            Period: The period.

        Raises:
            ValueError: When the period specification is invalid.
        """
        first_txn = Transaction.objects.order_by("date").first()
        data_start = first_txn.date if first_txn is not None else None
        last_txn = Transaction.objects.order_by("-date").first()
        data_end = last_txn.date if last_txn is not None else None
        # Raises ValueError
        return Period(value, data_start, data_end)
Example #3
0
def balance_sheet(request: HttpRequest, period: Period) -> HttpResponse:
    """The balance sheet.

    Args:
        request: The request.
        period: The period.

    Returns:
        The response.
    """
    # The accounts
    accounts = list(
        Account.objects.filter(
            Q(record__transaction__date__lte=period.end),
            (Q(code__startswith="1")
             | Q(code__startswith="2")
             | Q(code__startswith="3")),
            ~Q(code=Account.ACCUMULATED_BALANCE)).annotate(amount=Sum(
                Case(When(record__is_credit=True, then=-1), default=1) *
                F("record__amount"),
                output_field=DecimalField())).filter(
                    Q(amount__isnull=False), ~Q(amount=0)).order_by("code"))
    for account in accounts:
        account.url = reverse("accounting:ledger",
                              args=[account, period],
                              current_app=request.resolver_match.namespace)
    balance = Record.objects \
        .filter(
            Q(transaction__date__lt=period.start)
            & ~((Q(account__code__startswith="1")
                 | Q(account__code__startswith="2")
                 | Q(account__code__startswith="3"))
                & ~Q(account__code=Account.ACCUMULATED_BALANCE))) \
        .aggregate(
            balance=Sum(Case(When(is_credit=True, then=-1),
                             default=1) * F("amount"),
                        output_field=DecimalField()))["balance"]
    if balance is not None and balance != 0:
        brought_forward = Account.objects.get(code=Account.ACCUMULATED_BALANCE)
        brought_forward.amount = balance
        brought_forward.url = reverse(
            "accounting:income-statement",
            args=[period.period_before()],
            current_app=request.resolver_match.namespace)
        accounts.append(brought_forward)
    balance = Record.objects \
        .filter(
            Q(transaction__date__gte=period.start)
            & Q(transaction__date__lte=period.end)
            & ~((Q(account__code__startswith="1")
                 | Q(account__code__startswith="2")
                 | Q(account__code__startswith="3"))
                & ~Q(account__code=Account.ACCUMULATED_BALANCE))) \
        .aggregate(
            balance=Sum(Case(When(is_credit=True, then=-1),
                             default=1) * F("amount"),
                        output_field=DecimalField()))["balance"]
    if balance is not None and balance != 0:
        net_change = Account.objects.get(code=Account.NET_CHANGE)
        net_change.amount = balance
        net_change.url = reverse("accounting:income-statement",
                                 args=[period],
                                 current_app=request.resolver_match.namespace)
        accounts.append(net_change)
    for account in [x for x in accounts if x.code[0] in "23"]:
        account.amount = -account.amount
    groups = list(
        Account.objects.filter(code__in=[x.code[:2] for x in accounts]))
    sections = list(
        Account.objects.filter(Q(code="1") | Q(code="2")
                               | Q(code="3")).order_by("code"))
    for section in sections:
        section.groups = [x for x in groups if x.code[:1] == section.code]
        for group in section.groups:
            group.details = [x for x in accounts if x.code[:2] == group.code]
            group.amount = sum([x.amount for x in group.details])
        section.amount = sum([x.amount for x in section.groups])
    by_code = {x.code: x for x in sections}
    return render(
        request, "accounting/report-balance-sheet.html", {
            "assets": by_code["1"],
            "liabilities": by_code["2"],
            "owners_equity": by_code["3"],
        })
Example #4
0
 def get_redirect_url(self, *args, **kwargs):
     kwargs["period"] = Period.default_spec()
     return super().get_redirect_url(*args, **kwargs)
Example #5
0
 def get_redirect_url(self, *args, **kwargs):
     kwargs["account"] = utils.get_default_cash_account()
     kwargs["period"] = Period.default_spec()
     return super().get_redirect_url(*args, **kwargs)