Skip to content

Commit

Permalink
Merge branch 'feature-legal-name' of https://github.com/bcgov/lear in…
Browse files Browse the repository at this point in the history
…to feature/18452
  • Loading branch information
vysakh-menon-aot committed Jan 20, 2024
2 parents c552d3d + 50cf58e commit 051c824
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 190 deletions.
12 changes: 12 additions & 0 deletions legal-api/src/legal_api/models/alternate_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class NameType(BaseEnum):
"name",
"name_type",
"start_date",
"naics_key",
"naics_code",
"naics_description"
]
}

Expand All @@ -50,6 +53,9 @@ class NameType(BaseEnum):
bn15 = db.Column("bn15", db.String(20), nullable=True)
start_date = db.Column("start_date", db.DateTime(timezone=True), nullable=False)
end_date = db.Column("end_date", db.DateTime(timezone=True), nullable=True)
naics_key = db.Column("naics_key", db.String(50), nullable=True)
naics_code = db.Column("naics_code", db.String(10), nullable=True)
naics_description = db.Column("naics_description", db.String(300), nullable=True)

# parent keys
legal_entity_id = db.Column("legal_entity_id", db.Integer, db.ForeignKey("legal_entities.id"))
Expand All @@ -62,3 +68,9 @@ def save(self):
"""Save the object to the database immediately."""
db.session.add(self)
db.session.commit()

@classmethod
def find_by_identifier(cls, identifier: str) -> AlternateName | None:
"""Return None or the AlternateName found by its registration number."""
alternate_name = cls.query.filter_by(identifier=identifier).one_or_none()
return alternate_name
17 changes: 15 additions & 2 deletions legal-api/src/legal_api/models/legal_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,22 @@ def find_by_legal_name(cls, legal_name: str = None):
@classmethod
def find_by_identifier(cls, identifier: str = None):
"""Return a Business by the id assigned by the Registrar."""
if not identifier or not cls.validate_identifier(
entity_type=None, identifier=identifier
):
return None

legal_entity = None
if identifier:
non_entity_types = [LegalEntity.EntityTypes.PERSON.value, LegalEntity.EntityTypes.ORGANIZATION.value]

if identifier.startswith("FM"):
if alt_name := AlternateName.find_by_identifier(identifier):
legal_entity = cls.find_by_id(alt_name.legal_entity_id)
else:
non_entity_types = [
LegalEntity.EntityTypes.PERSON.value,
LegalEntity.EntityTypes.ORGANIZATION.value,
]

legal_entity = (
cls.query.filter(~LegalEntity.entity_type.in_(non_entity_types))
.filter_by(identifier=identifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
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
from entity_filer.filing_processors.filing_components.alternate_name import update_partner_change, update_proprietor_change


def process(
Expand All @@ -41,18 +42,20 @@ def process(
filing_meta.change_of_registration = {}
match legal_entity.entity_type:
case LegalEntity.EntityTypes.PARTNERSHIP:
_update_partner_change(
legal_entity,
change_filing_rec,
change_filing,
filing_meta
update_partner_change(
legal_entity=legal_entity,
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_sp_change(
legal_entity,
change_filing_rec,
change_filing,
filing_meta
update_proprietor_change(
legal_entity=legal_entity,
filing_type="changeOfRegistration",
change_filing_rec=change_filing_rec,
change_filing=change_filing,
filing_meta=filing_meta.change_of_registration
)

# Update business office if present
Expand Down Expand Up @@ -85,122 +88,3 @@ def post_process(business: LegalEntity, filing: Filing):
THIS SHOULD NOT ALTER THE MODEL
"""
name_request.consume_nr(business, filing, "changeOfRegistration")

def _update_partner_change(
legal_entity: LegalEntity,
change_filing_rec: Filing,
change_filing: Dict,
filing_meta: FilingMeta,
):
name_request = dpath.util.get(change_filing, "/changeOfRegistration/nameRequest", default=None)
if name_request and (to_legal_name := name_request.get("legalName")):
alternate_name = AlternateName.find_by_identifier(legal_entity.identifier)
parties_dict = dpath.util.get(change_filing, "/changeOfRegistration/parties")

legal_entity.legal_name = get_partnership_name(parties_dict)

legal_entity.alternate_names.remove(alternate_name)
alternate_name.end_date = change_filing_rec.effective_date
alternate_name.change_filing_id = change_filing_rec.id
# alternate_name.delete()
db.session.add(alternate_name)
db.session.commit()
db.session.delete(alternate_name)
db.session.commit()


new_alternate_name = AlternateName(
bn15=alternate_name.bn15,
change_filing_id=change_filing_rec.id,
end_date=None,
identifier=legal_entity.identifier,
name=to_legal_name,
name_type=AlternateName.NameType.OPERATING,
start_date=alternate_name.start_date,
registration_date=change_filing_rec.effective_date,
)
legal_entity.alternate_names.append(new_alternate_name)

filing_meta.change_of_registration = {
**filing_meta.change_of_registration,
"fromLegalName": alternate_name.name,
"toLegalName": to_legal_name,
}

# Update Nature of LegalEntity
if (
naics := change_filing.get("changeOfRegistration", {})
.get("business", {})
.get("naics")
) and (naics_code := naics.get("naicsCode")):
if legal_entity.naics_code != naics_code:
filing_meta.change_of_registration = {
**filing_meta.change_of_registration,
**{
"fromNaicsCode": legal_entity.naics_code,
"toNaicsCode": naics_code,
"naicsDescription": naics.get("naicsDescription"),
},
}
legal_entity_info.update_naics_info(legal_entity, naics)


def _update_sp_change(
legal_entity: LegalEntity,
change_filing_rec: Filing,
change_filing: Dict,
filing_meta: FilingMeta,
):
name_request = dpath.util.get(change_filing, "/changeOfRegistration/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, "/changeOfRegistration/parties")

# Find the Proprietor
proprietor = None
for party in parties_dict:
for role in party.get("roles"):
if role.get("roleType") == "Proprietor":
proprietor_dict = party
break
if proprietor_dict:
break

if not proprietor_dict:
raise DefaultException(
f"No Proprietor in the SP registration 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 registration for filing:{change_filing_rec.id}"
)

alternate_name.end_date = change_filing_rec.effective_date
alternate_name.change_filing_id = change_filing_rec.id
# alternate_name.delete()
db.session.add(alternate_name)
db.session.commit()
db.session.delete(alternate_name)
db.session.commit()

new_alternate_name = AlternateName(
identifier=identifier,
name_type=AlternateName.NameType.OPERATING,
change_filing_id=change_filing_rec.id,
end_date=None,
name=to_legal_name,
start_date=alternate_name.start_date,
registration_date=change_filing_rec.effective_date,
)
proprietor.alternate_names.append(new_alternate_name)

filing_meta.change_of_registration = {
**filing_meta.change_of_registration,
"fromLegalName": alternate_name.name,
"toLegalName": to_legal_name,
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
)
from entity_filer.filing_processors.filing_components.offices import update_offices
from entity_filer.filing_processors.filing_components.parties import merge_all_parties
from entity_filer.filing_processors.filing_components.alternate_name import update_partner_change, update_proprietor_change


def process(
Expand Down Expand Up @@ -111,36 +112,22 @@ def _process_firms_conversion(
filing_rec: Filing,
filing_meta: FilingMeta,
):
# Name change if present
with suppress(IndexError, KeyError, TypeError):
name_request_json = dpath.util.get(
conversion_filing, "/filing/conversion/nameRequest"
)
if name_request_json.get("legalName"):
from_legal_name = legal_entity.legal_name
legal_entity_info.set_legal_name(
legal_entity.identifier, legal_entity, name_request_json
match legal_entity.entity_type:
case LegalEntity.EntityTypes.PARTNERSHIP:
update_partner_change(
legal_entity=legal_entity,
filing_type="conversion",
change_filing_rec=filing_rec,
change_filing=conversion_filing,
filing_meta=filing_meta.conversion
)
case _: # LegalEntity.EntityTypes.PERSON: # legal_entity might be a proprietor?
update_proprietor_change(
filing_type="conversion",
change_filing_rec=filing_rec,
change_filing=conversion_filing,
filing_meta=filing_meta.conversion
)
if from_legal_name != legal_entity.legal_name:
filing_meta.conversion = {
**filing_meta.conversion,
**{
"fromLegalName": from_legal_name,
"toLegalName": legal_entity.legal_name,
},
}
# Update Nature of Business
if (
naics := conversion_filing.get("filing", {})
.get("conversion", {})
.get("legal_entity", {})
.get("naics")
) and naics.get("naicsDescription"):
legal_entity_info.update_naics_info(legal_entity, naics)
filing_meta.conversion = {
**filing_meta.conversion,
**{"naicsDescription": naics.get("naicsDescription")},
}

# Update legal_entity office if present
with suppress(IndexError, KeyError, TypeError):
Expand Down
Loading

0 comments on commit 051c824

Please sign in to comment.