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

20328 Filer - Update change of registration filing processor to work with alternate name changes #2580

Merged
merged 10 commits into from
Apr 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Address(Versioned, db.Model): # pylint: disable=too-many-instance-attribu
"id",
"address_type",
"legal_entity_id",
"alternate_name_id",
"city",
"change_filing_id",
"country",
Expand All @@ -69,6 +70,7 @@ class Address(Versioned, db.Model): # pylint: disable=too-many-instance-attribu
# parent keys
legal_entity_id = db.Column("legal_entity_id", db.Integer, db.ForeignKey("legal_entities.id"), index=True)
change_filing_id = db.Column("change_filing_id", db.Integer, db.ForeignKey("filings.id"), index=True)
alternate_name_id = db.Column("alternate_name_id", db.Integer, db.ForeignKey("alternate_names.id"), nullable=True)
office_id = db.Column(
"office_id",
db.Integer,
Expand Down
16 changes: 8 additions & 8 deletions queue_services/entity-filer/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Dict

import dpath
from business_model import Address, AlternateName, Filing, LegalEntity, db
from business_model import Address, AlternateName, BusinessCommon, Filing, LegalEntity, db

from entity_filer.exceptions.default_exception import DefaultException
from entity_filer.filing_meta import FilingMeta
Expand All @@ -26,54 +26,60 @@
update_partner_change,
update_proprietor_change,
)
from entity_filer.filing_processors.filing_components.offices import update_offices
from entity_filer.filing_processors.filing_components.parties import get_or_create_party, merge_all_parties
from entity_filer.filing_processors.registration import get_partnership_name


def process(
legal_entity: LegalEntity,
business: any,
change_filing_rec: Filing,
change_filing: Dict,
filing_meta: FilingMeta,
):
"""Render the change of registration filing onto the business model objects."""
filing_meta.change_of_registration = {}
match legal_entity.entity_type:
case LegalEntity.EntityTypes.PARTNERSHIP:
update_partner_change(
legal_entity=legal_entity,
entity_type = business.entity_type
match entity_type:
case BusinessCommon.EntityTypes.PARTNERSHIP:
business, alternate_name = update_partner_change(
legal_entity=business,
filing_type="changeOfRegistration",
change_filing_rec=change_filing_rec,
change_filing=change_filing,
filing_meta=filing_meta.change_of_registration,
)
case _: # LegalEntity.EntityTypes.SOLE_PROP: # legal_entity might be a proprietor?
update_proprietor_change(
case BusinessCommon.EntityTypes.SOLE_PROP:
business, alternate_name = update_proprietor_change(
filing_type="changeOfRegistration",
change_filing_rec=change_filing_rec,
change_filing=change_filing,
filing_meta=filing_meta.change_of_registration,
)
case _:
# Default and failed
raise DefaultException(f"change of registration {change_filing_rec.id} had no valid Firm type.")

# Update business office if present
with suppress(IndexError, KeyError, TypeError):
business_office_json = dpath.util.get(change_filing, "/changeOfRegistration/offices/businessOffice")
for updated_address in business_office_json.values():
if updated_address.get("id", None):
address = Address.find_by_id(updated_address.get("id"))
if address:
update_address(address, updated_address)
offices = dpath.util.get(change_filing, "/changeOfRegistration/offices")
if entity_type == BusinessCommon.EntityTypes.PARTNERSHIP:
update_offices(business, offices)
else:
update_offices(alternate_name, offices)

# Update parties
with suppress(IndexError, KeyError, TypeError):
parties = dpath.util.get(change_filing, "/changeOfRegistration/parties")
merge_all_parties(legal_entity, change_filing_rec, {"parties": parties})
merge_all_parties(business, change_filing_rec, {"parties": parties})

# update court order, if any is present
with suppress(IndexError, KeyError, TypeError):
court_order_json = dpath.util.get(change_filing, "/changeOfRegistration/courtOrder")
filings.update_filing_court_order(change_filing_rec, court_order_json)

return business, alternate_name


def post_process(business: LegalEntity, filing: Filing):
"""Post processing activities for change of registration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
from typing import Dict, List, Optional, Tuple

import dpath
from business_model import AlternateName, Filing, LegalEntity
from business_model import AlternateName, BusinessCommon, ColinEntity, Filing, LegalEntity

from entity_filer import db
from entity_filer.exceptions import DefaultException
from entity_filer.filing_meta import FilingMeta
from entity_filer.filing_processors.filing_components import legal_entity_info
from entity_filer.filing_processors.filing_components.offices import delete_existing_offices
from entity_filer.filing_processors.filing_components.parties import get_or_create_party
from entity_filer.utils.legislation_datetime import LegislationDatetime

Expand All @@ -47,9 +48,11 @@ def update_partner_change(
alternate_name.change_filing_id = change_filing_rec.id

if start := change_filing.get("filing", {}).get(f"{filing_type}", {}).get("startDate"):
start_date = LegislationDatetime.as_utc_timezone_from_legislation_date_str(start)
business_start_date = LegislationDatetime.as_utc_timezone_from_legislation_date_str(start)
legal_entity.start_date = business_start_date
else:
start_date = alternate_name.start_date
business_start_date = alternate_name.business_start_date

# alternate_name.delete()
db.session.add(alternate_name)
db.session.commit()
Expand All @@ -62,48 +65,49 @@ def update_partner_change(
end_date=None,
identifier=legal_entity.identifier,
name=to_legal_name,
name_type=AlternateName.NameType.OPERATING,
start_date=start_date,
registration_date=change_filing_rec.effective_date,
name_type=AlternateName.NameType.DBA,
start_date=alternate_name.start_date,
business_start_date=business_start_date,
entity_type=BusinessCommon.EntityTypes.PARTNERSHIP,
)
legal_entity.alternate_names.append(new_alternate_name)

filing_meta = {
**filing_meta,
"fromLegalName": alternate_name.name,
"toLegalName": to_legal_name,
}
filing_meta.update(
{
"fromLegalName": alternate_name.name,
"toLegalName": to_legal_name,
}
)

# Update Nature of LegalEntity
# Update nature of business for LegalEntity
current_alternate_name = AlternateName.find_by_identifier(legal_entity.identifier)
if (naics := change_filing.get(f"{filing_type}", {}).get("business", {}).get("naics")) and (
naics_code := naics.get("naicsCode")
):
if legal_entity.naics_code != naics_code:
filing_meta = {
**filing_meta,
**{
filing_meta.update(
{
"fromNaicsCode": legal_entity.naics_code,
"toNaicsCode": naics_code,
"naicsDescription": naics.get("naicsDescription"),
},
}
}
)
legal_entity_info.update_naics_info(legal_entity, naics)

return legal_entity, current_alternate_name


def update_proprietor_change(
filing_type: str,
change_filing_rec: Filing,
change_filing: Dict,
filing_meta: Dict,
):
name_request = dpath.util.get(change_filing, f"/{filing_type}/nameRequest", default=None)
identifier = dpath.util.get(change_filing_rec.filing_json, "filing/business/identifier")
if name_request and (to_legal_name := name_request.get("legalName")):
alternate_name = AlternateName.find_by_identifier(identifier)
parties_dict = dpath.util.get(change_filing, f"/{filing_type}/parties")

# Update proprietor information
if parties_dict := dpath.util.get(change_filing, f"/{filing_type}/parties", default=None):
# Find the Proprietor
proprietor = None
proprietor_dict = None
for party in parties_dict:
for role in party.get("roles"):
if role.get("roleType") == "Proprietor":
Expand All @@ -116,16 +120,44 @@ def update_proprietor_change(
raise DefaultException(f"No Proprietor in the SP {filing_type} for filing:{change_filing_rec.id}")

proprietor, delivery_address, mailing_address = get_or_create_party(proprietor_dict, change_filing_rec)

if not proprietor:
raise DefaultException(f"No Proprietor in the SP {filing_type} for filing:{change_filing_rec.id}")

# Update operating name
name_request = dpath.util.get(change_filing, f"/{filing_type}/nameRequest", default=None)
identifier = dpath.util.get(change_filing_rec.filing_json, "filing/business/identifier")
alternate_name = AlternateName.find_by_identifier(identifier)
proprietor = (
LegalEntity.find_by_id(alternate_name.legal_entity_id)
if alternate_name.legal_entity_id
else ColinEntity.find_by_id(alternate_name.colin_entity_id)
)
if name_request and (to_legal_name := name_request.get("legalName")):
if start := change_filing.get("filing", {}).get(f"{filing_type}", {}).get("startDate"):
start_date = LegislationDatetime.as_utc_timezone_from_legislation_date_str(start)
business_start_date = LegislationDatetime.as_utc_timezone_from_legislation_date_str(start)
else:
business_start_date = alternate_name.business_start_date

if isinstance(proprietor, ColinEntity):
delivery_address = proprietor.delivery_address
mailing_address = proprietor.mailing_address
else:
delivery_address = proprietor.entity_delivery_address
mailing_address = proprietor.entity_mailing_address

if isinstance(proprietor, LegalEntity) and proprietor.entity_type == BusinessCommon.EntityTypes.PERSON:
delivery_address_id = None
mailing_address_id = None
else:
start_date = alternate_name.start_date
delivery_address_id = delivery_address.id if delivery_address else None
mailing_address_id = mailing_address.id if mailing_address else None

alternate_name.end_date = change_filing_rec.effective_date
alternate_name.change_filing_id = change_filing_rec.id

existing_offices = delete_existing_offices(alternate_name)

# alternate_name.delete()
db.session.add(alternate_name)
db.session.commit()
Expand All @@ -134,20 +166,50 @@ def update_proprietor_change(

new_alternate_name = AlternateName(
identifier=identifier,
name_type=AlternateName.NameType.OPERATING,
name_type=AlternateName.NameType.DBA,
change_filing_id=change_filing_rec.id,
end_date=None,
name=to_legal_name,
start_date=start_date,
registration_date=change_filing_rec.effective_date,
start_date=alternate_name.start_date,
business_start_date=business_start_date,
delivery_address_id=delivery_address_id,
mailing_address_id=mailing_address_id,
bn15=alternate_name.bn15,
email=proprietor.email,
state=BusinessCommon.State.ACTIVE,
entity_type=BusinessCommon.EntityTypes.SOLE_PROP,
naics_key=alternate_name.naics_key,
naics_code=alternate_name.naics_code,
naics_description=alternate_name.naics_description,
)
proprietor.alternate_names.append(new_alternate_name)
for office in existing_offices:
new_alternate_name.append(office)

filing_meta.update(
{
"fromLegalName": alternate_name.name,
"toLegalName": to_legal_name,
}
)

filing_meta = {
**filing_meta,
"fromLegalName": alternate_name.name,
"toLegalName": to_legal_name,
}
# Update nature of business for AlternateName
if (naics := change_filing.get(f"{filing_type}", {}).get("business", {}).get("naics")) and (
naics_code := naics.get("naicsCode")
):
if new_alternate_name.naics_code != naics_code:
filing_meta.update(
{
"fromNaicsCode": new_alternate_name.naics_code,
"toNaicsCode": naics_code,
"naicsDescription": naics.get("naicsDescription"),
}
)
legal_entity_info.update_naics_info(new_alternate_name, naics)

return proprietor, new_alternate_name

return proprietor, alternate_name


def get_partnership_name(parties_dict: dict):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@
from flask import current_app
from flask_babel import _ as babel # noqa: N813

# from legal_api.services import NaicsService


class NaicsService:
@staticmethod
def find_by_code(naics_code: str):
return None
from entity_filer.services.naics import NaicsService


def set_corp_type(legal_entity: LegalEntity, legal_entity_info: Dict) -> Dict:
Expand Down Expand Up @@ -83,19 +77,16 @@ def update_legal_entity_info(corp_num: str, legal_entity: LegalEntity, legal_ent
return None


def update_naics_info(legal_entity: LegalEntity, naics: Dict):
def update_naics_info(business: any, naics: Dict):
"""Update naics info."""
# TODO update NAICS info
legal_entity.naics_code = naics.get("naicsCode")
if legal_entity.naics_code:
# TODO: Uncomment next 2 lines when find_by_code implemented and delete "pass"
# naics_structure = NaicsService.find_by_code(legal_entity.naics_code)
# legal_entity.naics_key = naics_structure["naicsKey"]
pass
business.naics_code = naics.get("naicsCode")
if business.naics_code and (naics_structure := NaicsService.find_by_code(business.naics_code)):
business.naics_key = naics_structure["naicsKey"]
else:
legal_entity.naics_code = None
legal_entity.naics_key = None
legal_entity.naics_description = naics.get("naicsDescription")
business.naics_code = None
business.naics_key = None
business.naics_description = naics.get("naicsDescription")


def get_next_corp_num(entity_type: str):
Expand Down
Loading
Loading