2023-04-08 09:34:24 +08:00
|
|
|
# The Mia! Accounting Project.
|
|
|
|
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/4/7
|
|
|
|
|
|
|
|
# Copyright (c) 2023 imacat.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
"""The unapplied original line items.
|
|
|
|
|
|
|
|
"""
|
|
|
|
from datetime import date
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
from flask import render_template, Response
|
|
|
|
|
|
|
|
from accounting.locale import gettext
|
2023-04-18 01:12:04 +08:00
|
|
|
from accounting.models import Currency, Account, JournalEntryLineItem
|
|
|
|
from accounting.report.period import Period, PeriodChooser
|
2023-04-08 09:34:24 +08:00
|
|
|
from accounting.report.utils.base_page_params import BasePageParams
|
|
|
|
from accounting.report.utils.base_report import BaseReport
|
2023-04-18 01:12:04 +08:00
|
|
|
from accounting.report.utils.csv_export import BaseCSVRow, csv_download, \
|
|
|
|
period_spec
|
|
|
|
from accounting.report.utils.offset_matcher import OffsetMatcher
|
2023-04-08 09:34:24 +08:00
|
|
|
from accounting.report.utils.option_link import OptionLink
|
|
|
|
from accounting.report.utils.report_chooser import ReportChooser
|
|
|
|
from accounting.report.utils.report_type import ReportType
|
|
|
|
from accounting.report.utils.unapplied import get_accounts_with_unapplied
|
|
|
|
from accounting.report.utils.urls import unapplied_url
|
|
|
|
from accounting.utils.pagination import Pagination
|
|
|
|
|
|
|
|
|
|
|
|
class CSVRow(BaseCSVRow):
|
|
|
|
"""A row in the CSV."""
|
|
|
|
|
|
|
|
def __init__(self, journal_entry_date: str | date, currency: str,
|
|
|
|
description: str | None, amount: str | Decimal,
|
|
|
|
net_balance: str | Decimal):
|
|
|
|
"""Constructs a row in the CSV.
|
|
|
|
|
|
|
|
:param journal_entry_date: The journal entry date.
|
|
|
|
:param currency: The currency.
|
|
|
|
:param description: The description.
|
|
|
|
:param amount: The amount.
|
|
|
|
:param net_balance: The net balance.
|
|
|
|
"""
|
|
|
|
self.date: str | date = journal_entry_date
|
|
|
|
"""The date."""
|
|
|
|
self.currency: str = currency
|
|
|
|
"""The currency."""
|
|
|
|
self.description: str | None = description
|
|
|
|
"""The description."""
|
|
|
|
self.amount: str | Decimal = amount
|
|
|
|
"""The amount."""
|
|
|
|
self.net_balance: str | Decimal = net_balance
|
|
|
|
"""The net balance."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def values(self) -> list[str | date | Decimal | None]:
|
|
|
|
"""Returns the values of the row.
|
|
|
|
|
|
|
|
:return: The values of the row.
|
|
|
|
"""
|
|
|
|
return [self.date, self.currency, self.description, self.amount,
|
|
|
|
self.net_balance]
|
|
|
|
|
|
|
|
|
|
|
|
class PageParams(BasePageParams):
|
|
|
|
"""The HTML page parameters."""
|
|
|
|
|
2023-04-18 01:12:04 +08:00
|
|
|
def __init__(self, currency: Currency,
|
|
|
|
account: Account,
|
|
|
|
period: Period,
|
2023-04-08 09:34:24 +08:00
|
|
|
pagination: Pagination[JournalEntryLineItem],
|
|
|
|
line_items: list[JournalEntryLineItem]):
|
|
|
|
"""Constructs the HTML page parameters.
|
|
|
|
|
2023-04-18 01:12:04 +08:00
|
|
|
:param currency: The currency.
|
2023-04-08 09:34:24 +08:00
|
|
|
:param account: The account.
|
2023-04-18 01:12:04 +08:00
|
|
|
:param period: The period.
|
2023-04-08 09:34:24 +08:00
|
|
|
:param pagination: The pagination.
|
|
|
|
:param line_items: The line items.
|
|
|
|
"""
|
2023-04-18 01:12:04 +08:00
|
|
|
self.currency: Currency = currency
|
|
|
|
"""The currency."""
|
2023-04-08 09:34:24 +08:00
|
|
|
self.account: Account = account
|
|
|
|
"""The account."""
|
2023-04-18 01:12:04 +08:00
|
|
|
self.period: Period = period
|
|
|
|
"""The period."""
|
2023-04-08 09:34:24 +08:00
|
|
|
self.pagination: Pagination[JournalEntryLineItem] = pagination
|
|
|
|
"""The pagination."""
|
|
|
|
self.line_items: list[JournalEntryLineItem] = line_items
|
|
|
|
"""The line items."""
|
2023-04-18 01:12:04 +08:00
|
|
|
self.period_chooser: PeriodChooser = PeriodChooser(
|
|
|
|
lambda x: unapplied_url(currency, account, x))
|
|
|
|
"""The period chooser."""
|
2023-04-08 09:34:24 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def has_data(self) -> bool:
|
|
|
|
"""Returns whether there is any data on the page.
|
|
|
|
|
|
|
|
:return: True if there is any data, or False otherwise.
|
|
|
|
"""
|
|
|
|
return len(self.line_items) > 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def report_chooser(self) -> ReportChooser:
|
|
|
|
"""Returns the report chooser.
|
|
|
|
|
|
|
|
:return: The report chooser.
|
|
|
|
"""
|
2023-04-18 01:12:04 +08:00
|
|
|
return ReportChooser(ReportType.UNAPPLIED, currency=self.currency,
|
|
|
|
account=self.account, period=self.period)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def currency_options(self) -> list[OptionLink]:
|
|
|
|
"""Returns the currency options.
|
|
|
|
|
|
|
|
:return: The currency options.
|
|
|
|
"""
|
|
|
|
return self._get_currency_options(
|
|
|
|
lambda x: unapplied_url(x, self.account, self.period),
|
|
|
|
self.currency)
|
2023-04-08 09:34:24 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def account_options(self) -> list[OptionLink]:
|
|
|
|
"""Returns the account options.
|
|
|
|
|
|
|
|
:return: The account options.
|
|
|
|
"""
|
2023-04-18 01:12:04 +08:00
|
|
|
options: list[OptionLink] \
|
|
|
|
= [OptionLink(gettext("Accounts"),
|
|
|
|
unapplied_url(self.currency, None, self.period),
|
|
|
|
False)]
|
|
|
|
options.extend(
|
|
|
|
[OptionLink(str(x),
|
|
|
|
unapplied_url(self.currency, x, self.period),
|
|
|
|
x.id == self.account.id)
|
|
|
|
for x in get_accounts_with_unapplied(self.currency, self.period)])
|
2023-04-08 10:01:03 +08:00
|
|
|
return options
|
2023-04-08 09:34:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_csv_rows(line_items: list[JournalEntryLineItem]) -> list[CSVRow]:
|
|
|
|
"""Composes and returns the CSV rows from the line items.
|
|
|
|
|
|
|
|
:param line_items: The line items.
|
|
|
|
:return: The CSV rows.
|
|
|
|
"""
|
|
|
|
rows: list[CSVRow] = [CSVRow(gettext("Date"), gettext("Currency"),
|
|
|
|
gettext("Description"), gettext("Amount"),
|
|
|
|
gettext("Net Balance"))]
|
|
|
|
rows.extend([CSVRow(x.journal_entry.date, x.currency.code,
|
|
|
|
x.description, x.amount, x.net_balance)
|
|
|
|
for x in line_items])
|
|
|
|
return rows
|
|
|
|
|
|
|
|
|
|
|
|
class UnappliedOriginalLineItems(BaseReport):
|
|
|
|
"""The unapplied original line items."""
|
|
|
|
|
2023-04-18 01:12:04 +08:00
|
|
|
def __init__(self, currency: Currency, account: Account, period: Period):
|
2023-04-08 09:34:24 +08:00
|
|
|
"""Constructs the unapplied original line items.
|
|
|
|
|
2023-04-18 01:12:04 +08:00
|
|
|
:param currency: The currency.
|
2023-04-08 09:34:24 +08:00
|
|
|
:param account: The account.
|
2023-04-18 01:12:04 +08:00
|
|
|
:param period: The period.
|
2023-04-08 09:34:24 +08:00
|
|
|
"""
|
2023-04-18 01:12:04 +08:00
|
|
|
self.__currency: Currency = currency
|
|
|
|
"""The currency."""
|
2023-04-08 09:34:24 +08:00
|
|
|
self.__account: Account = account
|
|
|
|
"""The account."""
|
2023-04-18 01:12:04 +08:00
|
|
|
self.__period: Period = period
|
|
|
|
"""The period."""
|
|
|
|
offset_matcher: OffsetMatcher \
|
|
|
|
= OffsetMatcher(self.__currency, self.__account, self.__period)
|
2023-04-08 09:34:24 +08:00
|
|
|
self.__line_items: list[JournalEntryLineItem] \
|
2023-04-08 18:52:41 +08:00
|
|
|
= offset_matcher.unapplied
|
2023-04-08 09:34:24 +08:00
|
|
|
"""The line items."""
|
|
|
|
|
|
|
|
def csv(self) -> Response:
|
|
|
|
"""Returns the report as CSV for download.
|
|
|
|
|
|
|
|
:return: The response of the report for download.
|
|
|
|
"""
|
2023-04-18 01:12:04 +08:00
|
|
|
filename: str = "unapplied-{currency}-{account}-{period}.csv"\
|
|
|
|
.format(currency=self.__currency.code, account=self.__account.code,
|
|
|
|
period=period_spec(self.__period))
|
2023-04-08 09:34:24 +08:00
|
|
|
return csv_download(filename, get_csv_rows(self.__line_items))
|
|
|
|
|
|
|
|
def html(self) -> str:
|
|
|
|
"""Composes and returns the report as HTML.
|
|
|
|
|
|
|
|
:return: The report as HTML.
|
|
|
|
"""
|
|
|
|
pagination: Pagination[JournalEntryLineItem] \
|
|
|
|
= Pagination[JournalEntryLineItem](self.__line_items,
|
|
|
|
is_reversed=True)
|
2023-04-18 01:12:04 +08:00
|
|
|
params: PageParams = PageParams(currency=self.__currency,
|
|
|
|
account=self.__account,
|
|
|
|
period=self.__period,
|
2023-04-08 09:34:24 +08:00
|
|
|
pagination=pagination,
|
|
|
|
line_items=pagination.list)
|
|
|
|
return render_template("accounting/report/unapplied.html",
|
|
|
|
report=params)
|