Fix various type hints
This commit is contained in:
@@ -68,6 +68,7 @@ def __parse_spec(text: str) -> tuple[dt.date | None, dt.date | None]:
|
||||
"""
|
||||
if text == "-":
|
||||
return None, None
|
||||
m: re.Match[str] | None
|
||||
m = re.match(f"^{DATE_SPEC_RE}$", text)
|
||||
if m is not None:
|
||||
return __get_start(m[1], m[2], m[3]), \
|
||||
|
||||
@@ -27,12 +27,18 @@ def get_spec(start: dt.date | None, end: dt.date | None) -> str:
|
||||
:param end: The end of the period.
|
||||
:return: The period specification.
|
||||
"""
|
||||
if start is None and end is None:
|
||||
return "-"
|
||||
if end is None:
|
||||
return __get_since_spec(start)
|
||||
if start is None:
|
||||
return __get_until_spec(end)
|
||||
return "-" if end is None else __get_until_spec(end)
|
||||
return __get_since_spec(start) if end is None else __get_spec(start, end)
|
||||
|
||||
|
||||
def __get_spec(start: dt.date, end: dt.date) -> str:
|
||||
"""Returns the period specification with both start and end.
|
||||
|
||||
:param start: The start of the period.
|
||||
:param end: The end of the period.
|
||||
:return: The period specification.
|
||||
"""
|
||||
try:
|
||||
return __get_year_spec(start, end)
|
||||
except ValueError:
|
||||
|
||||
@@ -121,9 +121,9 @@ class AccountCollector:
|
||||
|
||||
:return: The balances.
|
||||
"""
|
||||
sub_conditions: list[sa.BinaryExpression] \
|
||||
sub_conditions: list[sa.ColumnElement[bool]] \
|
||||
= [Account.base_code.startswith(x) for x in {"1", "2", "3"}]
|
||||
conditions: list[sa.BinaryExpression] \
|
||||
conditions: list[sa.ColumnElement[bool]] \
|
||||
= [JournalEntryLineItem.currency_code == self.__currency.code,
|
||||
sa.or_(*sub_conditions)]
|
||||
if self.__period.end is not None:
|
||||
@@ -180,7 +180,7 @@ class AccountCollector:
|
||||
"""
|
||||
if self.__period.start is None:
|
||||
return None
|
||||
conditions: list[sa.BinaryExpression] \
|
||||
conditions: list[sa.ColumnElement[bool]] \
|
||||
= [JournalEntryLineItem.currency_code == self.__currency.code,
|
||||
JournalEntry.date < self.__period.start]
|
||||
return self.__query_balance(conditions)
|
||||
@@ -199,7 +199,7 @@ class AccountCollector:
|
||||
|
||||
:return: The net income or loss for current period.
|
||||
"""
|
||||
conditions: list[sa.BinaryExpression] \
|
||||
conditions: list[sa.ColumnElement[bool]] \
|
||||
= [JournalEntryLineItem.currency_code == self.__currency.code]
|
||||
if self.__period.start is not None:
|
||||
conditions.append(JournalEntry.date >= self.__period.start)
|
||||
@@ -208,7 +208,7 @@ class AccountCollector:
|
||||
return self.__query_balance(conditions)
|
||||
|
||||
@staticmethod
|
||||
def __query_balance(conditions: list[sa.BinaryExpression])\
|
||||
def __query_balance(conditions: list[sa.ColumnElement[bool]])\
|
||||
-> Decimal:
|
||||
"""Queries the balance.
|
||||
|
||||
|
||||
@@ -119,12 +119,12 @@ class LineItemCollector:
|
||||
balance_func: sa.Function = sa.func.sum(sa.case(
|
||||
(JournalEntryLineItem.is_debit, JournalEntryLineItem.amount),
|
||||
else_=-JournalEntryLineItem.amount))
|
||||
select: sa.Select = sa.Select(balance_func)\
|
||||
select: sa.Select[tuple[Decimal]] = sa.Select(balance_func)\
|
||||
.join(JournalEntry).join(Account)\
|
||||
.filter(JournalEntryLineItem.currency_code == self.__currency.code,
|
||||
self.__account_condition,
|
||||
JournalEntry.date < self.__period.start)
|
||||
balance: int | None = db.session.scalar(select)
|
||||
balance: Decimal | None = db.session.scalar(select)
|
||||
if balance is None:
|
||||
return None
|
||||
line_item: ReportLineItem = ReportLineItem()
|
||||
@@ -144,7 +144,7 @@ class LineItemCollector:
|
||||
|
||||
:return: The line items.
|
||||
"""
|
||||
conditions: list[sa.BinaryExpression] \
|
||||
conditions: list[sa.ColumnElement[bool]] \
|
||||
= [JournalEntryLineItem.currency_code == self.__currency.code,
|
||||
self.__account_condition]
|
||||
if self.__period.start is not None:
|
||||
@@ -170,7 +170,7 @@ class LineItemCollector:
|
||||
selectinload(JournalEntryLineItem.journal_entry))]
|
||||
|
||||
@property
|
||||
def __account_condition(self) -> sa.BinaryExpression:
|
||||
def __account_condition(self) -> sa.ColumnElement[bool]:
|
||||
if self.__account.code == CurrentAccount.CURRENT_AL_CODE:
|
||||
return CurrentAccount.sql_condition()
|
||||
return Account.id == self.__account.id
|
||||
|
||||
@@ -254,9 +254,9 @@ class IncomeStatement(BaseReport):
|
||||
|
||||
:return: The balances.
|
||||
"""
|
||||
sub_conditions: list[sa.BinaryExpression] \
|
||||
sub_conditions: list[sa.ColumnElement[bool]] \
|
||||
= [Account.base_code.startswith(str(x)) for x in range(4, 10)]
|
||||
conditions: list[sa.BinaryExpression] \
|
||||
conditions: list[sa.ColumnElement[bool]] \
|
||||
= [JournalEntryLineItem.currency_code == self.__currency.code,
|
||||
sa.or_(*sub_conditions)]
|
||||
if self.__period.start is not None:
|
||||
|
||||
@@ -185,7 +185,7 @@ class Journal(BaseReport):
|
||||
|
||||
:return: The line items.
|
||||
"""
|
||||
conditions: list[sa.BinaryExpression] = []
|
||||
conditions: list[sa.ColumnElement[bool]] = []
|
||||
if self.__period.start is not None:
|
||||
conditions.append(JournalEntry.date >= self.__period.start)
|
||||
if self.__period.end is not None:
|
||||
|
||||
@@ -139,7 +139,7 @@ class LineItemCollector:
|
||||
|
||||
:return: The line items.
|
||||
"""
|
||||
conditions: list[sa.BinaryExpression] \
|
||||
conditions: list[sa.ColumnElement[bool]] \
|
||||
= [JournalEntryLineItem.currency_code == self.__currency.code,
|
||||
JournalEntryLineItem.account_id == self.__account.id]
|
||||
if self.__period.start is not None:
|
||||
|
||||
@@ -53,9 +53,9 @@ class LineItemCollector:
|
||||
keywords: list[str] = parse_query_keywords(request.args.get("q"))
|
||||
if len(keywords) == 0:
|
||||
return []
|
||||
conditions: list[sa.BinaryExpression] = []
|
||||
conditions: list[sa.ColumnElement[bool]] = []
|
||||
for k in keywords:
|
||||
sub_conditions: list[sa.BinaryExpression] \
|
||||
sub_conditions: list[sa.ColumnElement[bool]] \
|
||||
= [JournalEntryLineItem.description.icontains(k),
|
||||
JournalEntryLineItem.account_id.in_(
|
||||
self.__get_account_condition(k)),
|
||||
@@ -86,13 +86,13 @@ class LineItemCollector:
|
||||
:param k: The keyword.
|
||||
:return: The condition to filter the account.
|
||||
"""
|
||||
code: sa.BinaryExpression = Account.base_code + "-" \
|
||||
code: sa.ColumnElement[str] = Account.base_code + "-" \
|
||||
+ sa.func.substr("000" + sa.cast(Account.no, sa.String),
|
||||
sa.func.char_length(sa.cast(Account.no,
|
||||
sa.String)) + 1)
|
||||
select_l10n: sa.Select = sa.select(AccountL10n.account_id)\
|
||||
.filter(AccountL10n.title.icontains(k))
|
||||
conditions: list[sa.BinaryExpression] \
|
||||
conditions: list[sa.ColumnElement[bool]] \
|
||||
= [Account.base_code.contains(k),
|
||||
Account.title_l10n.icontains(k),
|
||||
code.contains(k),
|
||||
@@ -122,7 +122,7 @@ class LineItemCollector:
|
||||
:param k: The keyword.
|
||||
:return: The condition to filter the journal entry.
|
||||
"""
|
||||
conditions: list[sa.BinaryExpression] \
|
||||
conditions: list[sa.ColumnElement[bool]] \
|
||||
= [JournalEntry.note.icontains(k)]
|
||||
date: dt.datetime
|
||||
try:
|
||||
|
||||
@@ -178,7 +178,7 @@ class TrialBalance(BaseReport):
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
conditions: list[sa.BinaryExpression] \
|
||||
conditions: list[sa.ColumnElement[bool]] \
|
||||
= [JournalEntryLineItem.currency_code == self.__currency.code]
|
||||
if self.__period.start is not None:
|
||||
conditions.append(JournalEntry.date >= self.__period.start)
|
||||
|
||||
@@ -66,15 +66,19 @@ def period_spec(period: Period) -> str:
|
||||
"""
|
||||
start: str | None = __get_start_str(period.start)
|
||||
end: str | None = __get_end_str(period.end)
|
||||
if period.start is None and period.end is None:
|
||||
return "all-time"
|
||||
if start == end:
|
||||
return start
|
||||
if period.start is None:
|
||||
return f"until-{end}"
|
||||
if period.end is None:
|
||||
return f"since-{start}"
|
||||
return f"{start}-{end}"
|
||||
if start is None:
|
||||
return "all-time" if end is None else f"until-{end}"
|
||||
return f"since-{start}" if end is None else __get_spec(start, end)
|
||||
|
||||
|
||||
def __get_spec(start: str, end: str) -> str:
|
||||
"""Constructs the period specification with both start and end
|
||||
|
||||
:param start: The start date.
|
||||
:param end: The end date.
|
||||
:return: The period specification.
|
||||
"""
|
||||
return start if start == end else f"{start}-{end}"
|
||||
|
||||
|
||||
def __get_start_str(start: dt.date | None) -> str | None:
|
||||
|
||||
@@ -54,7 +54,7 @@ class OffsetMatcher:
|
||||
:param currency: The currency.
|
||||
:param account: The account.
|
||||
"""
|
||||
self.__currency: Account = currency
|
||||
self.__currency: Currency = currency
|
||||
"""The currency."""
|
||||
self.__account: Account = account
|
||||
"""The account."""
|
||||
@@ -105,7 +105,7 @@ class OffsetMatcher:
|
||||
"""
|
||||
net_balances: dict[int, Decimal | None] \
|
||||
= get_net_balances(self.__currency, self.__account)
|
||||
unmatched_offset_condition: sa.BinaryExpression \
|
||||
unmatched_offset_condition: sa.ColumnElement[bool] \
|
||||
= sa.and_(Account.id == self.__account.id,
|
||||
JournalEntryLineItem.currency_code
|
||||
== self.__currency.code,
|
||||
|
||||
@@ -22,21 +22,21 @@ from enum import Enum
|
||||
|
||||
class ReportType(Enum):
|
||||
"""The report types."""
|
||||
JOURNAL: str = "journal"
|
||||
JOURNAL = "journal"
|
||||
"""The journal."""
|
||||
LEDGER: str = "ledger"
|
||||
LEDGER = "ledger"
|
||||
"""The ledger."""
|
||||
INCOME_EXPENSES: str = "income-expenses"
|
||||
INCOME_EXPENSES = "income-expenses"
|
||||
"""The income and expenses log."""
|
||||
TRIAL_BALANCE: str = "trial-balance"
|
||||
TRIAL_BALANCE = "trial-balance"
|
||||
"""The trial balance."""
|
||||
INCOME_STATEMENT: str = "income-statement"
|
||||
INCOME_STATEMENT = "income-statement"
|
||||
"""The income statement."""
|
||||
BALANCE_SHEET: str = "balance-sheet"
|
||||
BALANCE_SHEET = "balance-sheet"
|
||||
"""The balance sheet."""
|
||||
UNAPPLIED: str = "unapplied"
|
||||
UNAPPLIED = "unapplied"
|
||||
"""The unapplied original line items."""
|
||||
UNMATCHED: str = "unmatched"
|
||||
UNMATCHED = "unmatched"
|
||||
"""The unmatched offsets."""
|
||||
SEARCH: str = "search"
|
||||
SEARCH = "search"
|
||||
"""The search."""
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# The Mia! Accounting Project.
|
||||
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/3/3
|
||||
|
||||
# Copyright (c) 2023 imacat.
|
||||
# Copyright (c) 2023-2026 imacat.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
@@ -391,7 +391,7 @@ def get_unmatched(currency: Currency, account: Account) -> str | Response:
|
||||
@bp.post("match-offsets/<currency:currency>/<needOffsetAccount:account>",
|
||||
endpoint="match-offsets")
|
||||
@has_permission(can_edit)
|
||||
def match_offsets(currency: Currency, account: Account) -> redirect:
|
||||
def match_offsets(currency: Currency, account: Account) -> Response:
|
||||
"""Matches the original line items with their offsets.
|
||||
|
||||
:return: Redirection to the view of the unmatched offsets.
|
||||
|
||||
Reference in New Issue
Block a user