Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20359 -business summary updates #2593

Merged
merged 6 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 52 additions & 42 deletions legal-api/src/legal_api/reports/business_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BusinessDocument:

def __init__(self, business, document_key):
"""Create the Report instance."""
self._legal_entity = business
self._business = business
self._document_key = document_key
self._report_date_time = LegislationDatetime.now()
self._epoch_filing_date = None
Expand All @@ -63,7 +63,7 @@ def get_json(self):
def _get_report_filename(self):
report_date = str(self._report_date_time)[:19]
return "{}_{}_{}.pdf".format(
self._legal_entity.identifier, report_date, ReportMeta.reports[self._document_key]["reportName"]
self._business.identifier, report_date, ReportMeta.reports[self._document_key]["reportName"]
).replace(" ", "_")

def _get_template(self):
Expand Down Expand Up @@ -120,8 +120,11 @@ def _get_template_data(self, get_json=False):
try:
# get document data
business_json["reportType"] = self._document_key
business_json["business"] = self._legal_entity.json()
business_json["business"]["businessName"] = self._legal_entity.business_name
argush3 marked this conversation as resolved.
Show resolved Hide resolved
business_json["business"] = self._business.json()
if self._business.alternate_names:
business_json["business"]["businessName"] = self._business.alternate_names[0].name
else:
business_json["business"]["businessName"] = self._business.business_name
business_json["registrarInfo"] = {**RegistrarInfo.get_registrar_info(self._report_date_time)}
self._set_description(business_json)
self._set_epoch_date(business_json)
Expand All @@ -138,8 +141,8 @@ def _get_template_data(self, get_json=False):
self._set_amalgamating_details(business_json)
self._set_liquidation_details(business_json)

if self._legal_entity.entity_type in ["SP", "GP"]:
registration_filing = Filing.get_filings_by_types(self._legal_entity, ["registration"])
if self._business.entity_type in ["SP", "GP"]:
registration_filing = Filing.get_filings_by_types(self._business, ["registration"])
if registration_filing:
business_json["business"]["registrationDateTime"] = registration_filing[
0
Expand Down Expand Up @@ -176,14 +179,14 @@ def _get_template_data(self, get_json=False):

def _set_epoch_date(self, legal_entity: dict):
"""Set the epoch filing date (date it was imported from COLIN)."""
epoch_filing = Filing.get_filings_by_status(self._legal_entity, [Filing.Status.EPOCH])
epoch_filing = Filing.get_filings_by_status(self._business, [Filing.Status.EPOCH])
if epoch_filing:
self._epoch_filing_date = epoch_filing[0].effective_date
legal_entity["business"]["epochFilingDate"] = self._epoch_filing_date.isoformat()

def _set_description(self, legal_entity: dict):
"""Set business descriptors used by json and pdf template."""
legal_type = self._legal_entity.entity_type
legal_type = self._business.entity_type
corp_type = CorpType.find_by_id(legal_type)
legal_entity["entityDescription"] = corp_type.full_desc
act = {
Expand All @@ -194,8 +197,8 @@ def _set_description(self, legal_entity: dict):
legal_entity["entityAct"] = act.get(legal_type, "Business Corporations Act")

legal_entity["business"]["coopType"] = (
BusinessDocument.CP_TYPE_DESCRIPTION[self._legal_entity.association_type]
if self._legal_entity.association_type
BusinessDocument.CP_TYPE_DESCRIPTION[self._business.association_type]
if self._business.association_type
else NOT_AVAILABLE
)

Expand All @@ -207,29 +210,29 @@ def _set_description_xtra(self, legal_entity: dict):
LegalEntity.EntityTypes.PARTNERSHIP.value: "General Partnership",
LegalEntity.EntityTypes.SOLE_PROP.value: "Sole Proprietorship",
}
legal_entity["entityShortDescription"] = description.get(self._legal_entity.entity_type, "Corporation")
legal_entity["entityShortDescription"] = description.get(self._business.entity_type, "Corporation")
legal_entity["entityInformalDescription"] = legal_entity["entityShortDescription"].lower()

def _set_dates(self, legal_entity: dict): # pylint: disable=too-many-branches
"""Set the business json with formatted dates."""
# business dates
if self._legal_entity.last_ar_date:
last_ar_date = self._legal_entity.last_ar_date.strftime(OUTPUT_DATE_FORMAT)
if self._business.last_ar_date:
last_ar_date = self._business.last_ar_date.strftime(OUTPUT_DATE_FORMAT)
else:
last_ar_date = NOT_AVAILABLE
legal_entity["business"]["last_ar_date"] = last_ar_date
if self._legal_entity.last_agm_date:
last_agm_date = self._legal_entity.last_agm_date.strftime(OUTPUT_DATE_FORMAT)
if self._business.last_agm_date:
last_agm_date = self._business.last_agm_date.strftime(OUTPUT_DATE_FORMAT)
else:
last_agm_date = NOT_AVAILABLE
legal_entity["business"]["last_agm_date"] = last_agm_date
if epoch_date := legal_entity["business"].get("epochFilingDate"):
legal_entity["business"]["epochFilingDate"] = LegislationDatetime.as_legislation_timezone(
datetime.fromisoformat(epoch_date)
).strftime(OUTPUT_DATE_FORMAT)
if self._legal_entity.restoration_expiry_date:
if self._business.restoration_expiry_date:
legal_entity["business"]["restorationExpiryDate"] = LegislationDatetime.format_as_report_string(
self._legal_entity.restoration_expiry_date
self._business.restoration_expiry_date
)
# state change dates
for filing in legal_entity.get("stateFilings", []):
Expand Down Expand Up @@ -258,27 +261,27 @@ def _set_dates(self, legal_entity: dict): # pylint: disable=too-many-branches
datetime.fromisoformat(registration_datetime_str)
)
# founding dates
founding_datetime = LegislationDatetime.as_legislation_timezone(self._legal_entity.founding_date)
founding_datetime = LegislationDatetime.as_legislation_timezone(self._business.founding_date)
legal_entity["formatted_founding_date_time"] = LegislationDatetime.format_as_report_string(founding_datetime)
legal_entity["formatted_founding_date"] = founding_datetime.strftime(OUTPUT_DATE_FORMAT)
# dissolution dates
if self._legal_entity.dissolution_date:
dissolution_datetime = LegislationDatetime.as_legislation_timezone(self._legal_entity.dissolution_date)
if self._business.dissolution_date:
dissolution_datetime = LegislationDatetime.as_legislation_timezone(self._business.dissolution_date)
legal_entity["formatted_dissolution_date"] = dissolution_datetime.strftime(OUTPUT_DATE_FORMAT)
# report dates
legal_entity["report_date_time"] = LegislationDatetime.format_as_report_string(self._report_date_time)
legal_entity["report_date"] = self._report_date_time.strftime(OUTPUT_DATE_FORMAT)
if self._legal_entity.start_date:
legal_entity["start_date_utc"] = self._legal_entity.start_date.strftime(OUTPUT_DATE_FORMAT)
if self._legal_entity.restoration_expiry_date:
if self._business.start_date:
legal_entity["start_date_utc"] = self._business.start_date.strftime(OUTPUT_DATE_FORMAT)
if self._business.restoration_expiry_date:
formatted_restoration_expiry_date = LegislationDatetime.format_as_report_expiry_string(
self._legal_entity.restoration_expiry_date
self._business.restoration_expiry_date
)
legal_entity["formatted_restoration_expiry_date"] = formatted_restoration_expiry_date

def _set_addresses(self, legal_entity: dict):
"""Set business addresses."""
address_json = get_addresses(self._legal_entity.identifier).json
address_json = get_addresses(self._business.identifier).json
for office_type in ["registeredOffice", "recordsOffice", "businessOffice"]:
if office_type in address_json:
for key, value in address_json[office_type].items():
Expand All @@ -287,7 +290,7 @@ def _set_addresses(self, legal_entity: dict):

def _set_directors(self, legal_entity: dict):
"""Set directors (these have a different schema than parties)."""
directors_json = get_directors(self._legal_entity.identifier).json["directors"]
directors_json = get_directors(self._business.identifier).json["directors"]
for director in directors_json:
if director.get("mailingAddress"):
director["mailingAddress"] = BusinessDocument._format_address(director["mailingAddress"])
Expand All @@ -297,25 +300,27 @@ def _set_directors(self, legal_entity: dict):

def _set_parties(self, legal_entity: dict):
"""Set the parties of the business (all parties)."""
party_json = get_parties(self._legal_entity.identifier).json["parties"]
party_json = get_parties(self._business.identifier).json["parties"]
for party in party_json:
if party.get("mailingAddress"):
party["mailingAddress"] = BusinessDocument._format_address(party["mailingAddress"])
if party.get("deliveryAddress"):
party["deliveryAddress"] = BusinessDocument._format_address(party["deliveryAddress"])
if not party.get("officer", {}).get("partyType") and not party.get("officer", {}).get("lastName"):
party["officer"]["partyType"] = "organization"
argush3 marked this conversation as resolved.
Show resolved Hide resolved
legal_entity["parties"] = party_json

def _set_name_translations(self, legal_entity: dict):
"""Set the aliases."""
aliases = AlternateName.find_by_name_type(self._legal_entity.id, "TRANSLATION")
aliases = AlternateName.find_by_name_type(self._business.id, "TRANSLATION")
legal_entity["listOfTranslations"] = [alias.json for alias in aliases]

def _set_business_state_changes(self, legal_entity: dict):
"""Set list of partial state change filing data."""
state_filings = []
# Any filings like restoration, liquidation etc. that changes the state must be included here
for filing in Filing.get_filings_by_types(
self._legal_entity,
self._business,
[
"dissolution",
"restorationApplication",
Expand All @@ -331,19 +336,19 @@ def _set_business_state_changes(self, legal_entity: dict):
state_filings.append(self._format_state_filing(filing))
# If it is amalgamating business
if legal_entity.get("business").get("amalgamatedInto"):
amalgamating_business = self._legal_entity.amalgamating_businesses.one_or_none()
amalgamating_business = self._business.amalgamating_businesses.one_or_none()
amalgamation = Amalgamation.find_by_id(amalgamating_business.amalgamation_id)
filing = Filing.find_by_id(amalgamation.filing_id)
state_filings.insert(0, self._format_state_filing(filing))
legal_entity["stateFilings"] = state_filings

def _set_record_keepers(self, legal_entity: dict):
"""Set the custodians of the business (parties with custodian role)."""
if self._legal_entity.state.name == "HISTORICAL":
if self._business.state.name == "HISTORICAL":
custodian_json = [
party_role.json
for party_role in self._legal_entity.entity_roles.all()
if party_role.role.lower() == "custodian"
for party_role in self._business.entity_roles.all()
if party_role.role_type.lower() == "custodian"
]
for custodian in custodian_json:
custodian["mailingAddress"] = BusinessDocument._format_address(custodian["mailingAddress"])
Expand All @@ -356,7 +361,7 @@ def _set_business_changes(self, legal_entity: dict):
alterations = []
# Any future filings that includes a company name/type change must be added here
for filing in Filing.get_filings_by_types(
self._legal_entity,
self._business,
["alteration", "correction", "changeOfName", "changeOfRegistration", "specialResolution"],
):
filing_meta = filing.meta_data
Expand Down Expand Up @@ -415,18 +420,18 @@ def _format_state_filing(self, filing: Filing) -> dict:
filing_meta = filing.meta_data
if filing.filing_type == "dissolution":
filing_info["filingName"] = BusinessDocument._get_summary_display_name(
filing.filing_type, filing_meta["dissolution"]["dissolutionType"], self._legal_entity.entity_type
filing.filing_type, filing_meta["dissolution"]["dissolutionType"], self._business.entity_type
)
if (
self._legal_entity.entity_type in ["SP", "GP"]
self._business.entity_type in ["SP", "GP"]
and filing_meta["dissolution"]["dissolutionType"] == "voluntary"
):
filing_info["dissolution_date_str"] = LegislationDatetime.as_legislation_timezone_from_date_str(
filing.filing_json["filing"]["dissolution"]["dissolutionDate"]
).strftime(OUTPUT_DATE_FORMAT)
elif filing.filing_type == "restoration":
filing_info["filingName"] = BusinessDocument._get_summary_display_name(
filing.filing_type, filing.filing_sub_type, self._legal_entity.entity_type
filing.filing_type, filing.filing_sub_type, self._business.entity_type
)
if filing.filing_sub_type in ["limitedRestoration", "limitedRestorationExtension"]:
expiry_date = filing_meta["restoration"]["expiry"]
Expand Down Expand Up @@ -456,7 +461,7 @@ def _format_state_filing(self, filing: Filing) -> dict:
def _set_amalgamation_details(self, legal_entity: dict):
"""Set amalgamation filing data."""
amalgamated_businesses = []
filings = Filing.get_filings_by_types(self._legal_entity.id, ["amalgamationApplication"])
filings = Filing.get_filings_by_types(self._business, ["amalgamationApplication"])
if filings:
amalgamation_application = filings[0]
legal_entity["business"]["amalgamatedEntity"] = True
Expand All @@ -465,7 +470,7 @@ def _set_amalgamation_details(self, legal_entity: dict):
amalgamated_businesses_info = {"legalName": "Not Available", "identifier": "Not Available"}
amalgamated_businesses.append(amalgamated_businesses_info)
else:
amalgamation = self._legal_entity.amalgamation.one_or_none()
amalgamation = self._business.amalgamation.one_or_none()
amalgamating_businesses = amalgamation.amalgamating_businesses.all()
for amalgamating_business in amalgamating_businesses:
if ting_business := LegalEntity.find_by_internal_id(amalgamating_business.legal_entity_id):
Expand All @@ -491,7 +496,7 @@ def _set_amalgamating_details(self, legal_entity: dict):
def _set_liquidation_details(self, legal_entity: dict):
"""Set partial liquidation filing data."""
liquidation_info = {}
liquidation = Filing.get_filings_by_types(self._legal_entity, ["voluntaryLiquidation"])
liquidation = Filing.get_filings_by_types(self._business, ["voluntaryLiquidation"])
if liquidation:
liquidation_info["filingDateTime"] = liquidation[0].filing_date.isoformat()
legal_entity["business"]["state"] = LegalEntity.State.LIQUIDATION.name
Expand All @@ -516,9 +521,14 @@ def _format_address(address):
return address

def _set_meta_info(self, legal_entity: dict):
legal_entity["environment"] = f"{self._get_environment()} BUSINESS #{self._legal_entity.identifier}".lstrip()
legal_entity["environment"] = f"{self._get_environment()} BUSINESS #{self._business.identifier}".lstrip()
legal_entity["meta_title"] = "Business Summary on {}".format(legal_entity["report_date_time"])
legal_entity["meta_subject"] = "{} ({})".format(self._legal_entity.legal_name, self._legal_entity.identifier)
argush3 marked this conversation as resolved.
Show resolved Hide resolved
if self._business.alternate_names and self._business.legal_name is None:
legal_entity["meta_subject"] = "{} ({})".format(
self._business.alternate_names[0].name, self._business.identifier
)
else:
legal_entity["meta_subject"] = "{} ({})".format(self._business.legal_name, self._business.identifier)

@staticmethod
def _get_environment():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def get_name_translations_revision(filing, legal_entity_id) -> dict:
)

name_translations_version = history_cls(AlternateName)
columns_to_select = [col for col in name_translations_version.__table__.columns if col.name != 'changed']
columns_to_select = [col for col in name_translations_version.__table__.columns if col.name != "changed"]
name_translations_history = (
db.session.query(*columns_to_select)
.filter(name_translations_version.change_filing_id == filing.id)
Expand Down
Loading