mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-05-13 09:58:47 +02:00
Add relations from finanical reports to SQL (#216)
This commit is contained in:
parent
41f590b2e8
commit
36a0bab6ff
@ -32,6 +32,7 @@ class RelationshipRoleEnum(str, MultiValueEnum):
|
|||||||
ZWEIGNIEDERLASSUNG = "Zweigniederlassung"
|
ZWEIGNIEDERLASSUNG = "Zweigniederlassung"
|
||||||
HAUPTNIEDERLASSUNG = "Hauptniederlassung"
|
HAUPTNIEDERLASSUNG = "Hauptniederlassung"
|
||||||
CARE_OF = "c/o"
|
CARE_OF = "c/o"
|
||||||
|
AUDITOR = "Auditor"
|
||||||
|
|
||||||
|
|
||||||
class CompanyTypeEnum(str, MultiValueEnum):
|
class CompanyTypeEnum(str, MultiValueEnum):
|
||||||
|
@ -22,6 +22,7 @@ from aki_prj23_transparenzregister.models.company import (
|
|||||||
CapitalTypeEnum,
|
CapitalTypeEnum,
|
||||||
CompanyRelationshipEnum,
|
CompanyRelationshipEnum,
|
||||||
CurrencyEnum,
|
CurrencyEnum,
|
||||||
|
RelationshipRoleEnum,
|
||||||
)
|
)
|
||||||
from aki_prj23_transparenzregister.utils.logger_config import (
|
from aki_prj23_transparenzregister.utils.logger_config import (
|
||||||
add_logger_options_to_argparse,
|
add_logger_options_to_argparse,
|
||||||
@ -524,7 +525,6 @@ def add_annual_report(company_id: int, year: int, report: dict, db: Session) ->
|
|||||||
"""Ads a annual financial report to the SQL database.
|
"""Ads a annual financial report to the SQL database.
|
||||||
|
|
||||||
The added report is linked with the company.
|
The added report is linked with the company.
|
||||||
# TODO add a link to the accountant.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
company_id: The SQL id of the company.
|
company_id: The SQL id of the company.
|
||||||
@ -538,15 +538,42 @@ def add_annual_report(company_id: int, year: int, report: dict, db: Session) ->
|
|||||||
raise KeyError(f"The company with the id {company_id} could not be found.")
|
raise KeyError(f"The company with the id {company_id} could not be found.")
|
||||||
logger.debug(f"No financial data found for {company.name} in the year {year}.")
|
logger.debug(f"No financial data found for {company.name} in the year {year}.")
|
||||||
return
|
return
|
||||||
db.add(
|
|
||||||
entities.AnnualFinanceStatement(
|
if report.get("financials"):
|
||||||
company_id=company_id,
|
db.add(
|
||||||
date=date(year, 1, 1),
|
entities.AnnualFinanceStatement(
|
||||||
**report.get("financials", {}), # TODO can we have a date?
|
company_id=company_id,
|
||||||
),
|
date=date(year, 1, 1),
|
||||||
)
|
**report.get("financials", {}),
|
||||||
|
),
|
||||||
|
)
|
||||||
for auditor in report.get("auditors", ""):
|
for auditor in report.get("auditors", ""):
|
||||||
_ = auditor
|
try:
|
||||||
|
company_name: str = auditor.get("company")
|
||||||
|
if not company_name:
|
||||||
|
continue
|
||||||
|
relation_to: int = get_company_id(
|
||||||
|
company_name,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
db=db,
|
||||||
|
)
|
||||||
|
except CompanyNotFoundError as err:
|
||||||
|
logger.debug(str(err))
|
||||||
|
company_relation_missing(company_name, None, None, db=db)
|
||||||
|
db.commit()
|
||||||
|
return
|
||||||
|
if company_id == relation_to:
|
||||||
|
logger.warning(f"The {company_name} is auditing itself.")
|
||||||
|
return
|
||||||
|
relation = entities.CompanyRelation(
|
||||||
|
company_id=company_id,
|
||||||
|
relation=RelationshipRoleEnum.AUDITOR,
|
||||||
|
company2_id=relation_to,
|
||||||
|
date_from=date(year, 1, 1),
|
||||||
|
date_to=date(year, 12, 31),
|
||||||
|
)
|
||||||
|
db.add(relation)
|
||||||
|
|
||||||
|
|
||||||
def add_annual_financial_reports(companies: list[dict], db: Session) -> None:
|
def add_annual_financial_reports(companies: list[dict], db: Session) -> None:
|
||||||
@ -576,10 +603,10 @@ def add_annual_financial_reports(companies: list[dict], db: Session) -> None:
|
|||||||
pbar.update(len(yearly_results))
|
pbar.update(len(yearly_results))
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise
|
raise
|
||||||
|
yearly_results = {
|
||||||
|
year: report for year, report in yearly_results.items() if report
|
||||||
|
}
|
||||||
for year, report in yearly_results.items():
|
for year, report in yearly_results.items():
|
||||||
if not report:
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
year_int = int(year)
|
year_int = int(year)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@ -587,7 +614,15 @@ def add_annual_financial_reports(companies: list[dict], db: Session) -> None:
|
|||||||
f"The company {company['name']} has a yearly result with an invalid year of \"{year}\".",
|
f"The company {company['name']} has a yearly result with an invalid year of \"{year}\".",
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
add_annual_report(company_id, year_int, report, db=db)
|
try:
|
||||||
|
add_annual_report(company_id, year_int, report, db=db)
|
||||||
|
except DataInvalidError:
|
||||||
|
logger.exception(
|
||||||
|
f"While adding a financial report to {company['name']} an error has occurred."
|
||||||
|
)
|
||||||
|
db.rollback()
|
||||||
|
else:
|
||||||
|
db.commit()
|
||||||
pbar.update()
|
pbar.update()
|
||||||
db.commit()
|
db.commit()
|
||||||
logger.info("Company connections added.")
|
logger.info("Company connections added.")
|
||||||
|
@ -155,8 +155,8 @@ class Relation(Base):
|
|||||||
id = sa.Column(sa.Integer, primary_key=True)
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
company_id = sa.Column(sa.Integer, sa.ForeignKey("company.id"))
|
company_id = sa.Column(sa.Integer, sa.ForeignKey("company.id"))
|
||||||
|
|
||||||
date_from = sa.Column(sa.DateTime(timezone=True), nullable=True)
|
date_from = sa.Column(sa.Date(), nullable=True)
|
||||||
date_to = sa.Column(sa.DateTime(timezone=True), nullable=True)
|
date_to = sa.Column(sa.Date(), nullable=True)
|
||||||
|
|
||||||
relation = sa.Column(sa.Enum(RelationshipRoleEnum), nullable=False)
|
relation = sa.Column(sa.Enum(RelationshipRoleEnum), nullable=False)
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ from aki_prj23_transparenzregister.models.company import (
|
|||||||
CompanyRelationshipEnum,
|
CompanyRelationshipEnum,
|
||||||
CompanyTypeEnum,
|
CompanyTypeEnum,
|
||||||
CurrencyEnum,
|
CurrencyEnum,
|
||||||
|
RelationshipRoleEnum,
|
||||||
)
|
)
|
||||||
from aki_prj23_transparenzregister.utils import data_transfer
|
from aki_prj23_transparenzregister.utils import data_transfer
|
||||||
from aki_prj23_transparenzregister.utils.data_transfer import CompanyNotFoundError
|
from aki_prj23_transparenzregister.utils.data_transfer import CompanyNotFoundError
|
||||||
@ -1054,6 +1055,94 @@ def test_add_annual_report(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("company_id", [2, 3])
|
||||||
|
@pytest.mark.parametrize("year", [2023, 2025, 2020])
|
||||||
|
def test_add_annual_report_auditors(
|
||||||
|
company_id: int,
|
||||||
|
year: int,
|
||||||
|
finance_statements: list[dict[str, Any]],
|
||||||
|
full_db: Session,
|
||||||
|
) -> None:
|
||||||
|
"""Tests the addition of annual financial records."""
|
||||||
|
data_transfer.add_annual_report(
|
||||||
|
company_id,
|
||||||
|
year,
|
||||||
|
{
|
||||||
|
"auditors": [{"name": "Max Musterauditor", "company": "Some Company GmbH"}],
|
||||||
|
"financials": {},
|
||||||
|
},
|
||||||
|
db=full_db,
|
||||||
|
)
|
||||||
|
full_db.commit()
|
||||||
|
added = full_db.query(entities.CompanyRelation).get(1)
|
||||||
|
assert added
|
||||||
|
assert added.company2_id == 1
|
||||||
|
assert added.company_id == company_id
|
||||||
|
assert added.date_from == date(
|
||||||
|
year,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
assert added.date_to == date(
|
||||||
|
year,
|
||||||
|
12,
|
||||||
|
31,
|
||||||
|
)
|
||||||
|
assert added.relation == RelationshipRoleEnum.AUDITOR
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("company_id", [1])
|
||||||
|
@pytest.mark.parametrize("year", [2023, 2025, 2020])
|
||||||
|
def test_add_annual_report_self_audit(
|
||||||
|
company_id: int,
|
||||||
|
year: int,
|
||||||
|
finance_statements: list[dict[str, Any]],
|
||||||
|
full_db: Session,
|
||||||
|
) -> None:
|
||||||
|
"""Tests the addition of annual financial records."""
|
||||||
|
data_transfer.add_annual_report(
|
||||||
|
company_id,
|
||||||
|
year,
|
||||||
|
{
|
||||||
|
"auditors": [{"name": "Max Musterauditor", "company": "Some Company GmbH"}],
|
||||||
|
"financials": {},
|
||||||
|
},
|
||||||
|
db=full_db,
|
||||||
|
)
|
||||||
|
full_db.commit()
|
||||||
|
assert full_db.query(entities.CompanyRelation).get(1) is None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("company_id", [1, 2, 3])
|
||||||
|
@pytest.mark.parametrize("number_of_years", [1, 4, 7])
|
||||||
|
def test_add_annual_report_unknown_audit(
|
||||||
|
company_id: int,
|
||||||
|
finance_statements: list[dict[str, Any]],
|
||||||
|
number_of_years: int,
|
||||||
|
full_db: Session,
|
||||||
|
) -> None:
|
||||||
|
"""Tests the addition of annual financial records."""
|
||||||
|
for year in range(2020, number_of_years + 2020, 1):
|
||||||
|
data_transfer.add_annual_report(
|
||||||
|
company_id,
|
||||||
|
year,
|
||||||
|
{
|
||||||
|
"auditors": [
|
||||||
|
{"name": "Max Musterauditor", "company": "Unknown Auditor GmbH"}
|
||||||
|
],
|
||||||
|
"financials": {},
|
||||||
|
},
|
||||||
|
db=full_db,
|
||||||
|
)
|
||||||
|
full_db.commit()
|
||||||
|
added = full_db.query(entities.MissingCompany).get("Unknown Auditor GmbH")
|
||||||
|
assert added, added.__dict__
|
||||||
|
assert added.name == "Unknown Auditor GmbH"
|
||||||
|
assert not added.zip_code
|
||||||
|
assert not added.city
|
||||||
|
assert added.number_of_links == number_of_years
|
||||||
|
|
||||||
|
|
||||||
def test_add_annual_report_financial_key_error(full_db: Session) -> None:
|
def test_add_annual_report_financial_key_error(full_db: Session) -> None:
|
||||||
"""Tests if an error is thrown financial data is tried to be added with an unknown financial record type."""
|
"""Tests if an error is thrown financial data is tried to be added with an unknown financial record type."""
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user