def process(business: Business, filing: Dict, app: Flask = None):  # pylint: disable=too-many-locals; 1 extra
    """Process the incoming incorporation filing."""
    # Extract the filing information for incorporation
    incorp_filing = filing['incorporationApplication']

    if incorp_filing:  # pylint: disable=too-many-nested-blocks; 1 extra and code is still very clear
        # Extract the office, business, addresses, directors etc.
        # these will have to be inserted into the db.
        offices = incorp_filing.get('offices', None)
        parties = incorp_filing.get('parties', None)
        business_info = incorp_filing['nameRequest']
        share_classes = incorp_filing['shareClasses']

        # Sanity check
        if business and business.identifier == business_info['nrNumber']:
            # Reserve the Corp Numper for this entity
            corp_num = get_next_corp_num(business_info['legalType'], app)

            # Initial insert of the business record
            business = update_business_info(corp_num, business, business_info)

            if business:
                for office_type, addresses in offices.items():
                    office = create_office(business, office_type, addresses)
                    business.offices.append(office)

                if parties:
                    for party_info in parties:
                        party = create_party(party_info=party_info)
                        for role_type in party_info.get('roles'):
                            role = {
                                'roleType':
                                role_type.get('roleType'),
                                'appointmentDate':
                                role_type.get('appointmentDate', None),
                                'cessationDate':
                                role_type.get('cessationDate', None)
                            }
                            party_role = create_role(party=party,
                                                     role_info=role)
                            business.party_roles.append(party_role)

                if share_classes:
                    for share_class_info in share_classes:
                        share_class = create_share_class(share_class_info)
                        business.share_classes.append(share_class)
        else:
            logger.error('No business exists for NR number: %s',
                         business_info['nrNUmber'])
Exemple #2
0
async def test_process_combined_filing(app, session):
    """Assert that an AR filling can be applied to the model correctly."""
    # vars
    payment_id = str(random.SystemRandom().getrandbits(0x58))
    identifier = 'CP1234567'
    business = create_business(identifier)
    agm_date = datetime.date.fromisoformat(
        COMBINED_FILING['filing']['annualReport'].get(
            'annualGeneralMeetingDate'))
    ar_date = datetime.date.fromisoformat(
        COMBINED_FILING['filing']['annualReport'].get('annualReportDate'))
    new_delivery_address = COMBINED_FILING['filing']['changeOfAddress'][
        'offices']['registeredOffice'][
            'deliveryAddress']  # noqa: E501; line too long by 1 char
    new_mailing_address = COMBINED_FILING['filing']['changeOfAddress'][
        'offices']['registeredOffice']['mailingAddress']
    end_date = datetime.datetime.utcnow().date()
    # prep director for no change
    filing_data = copy.deepcopy(COMBINED_FILING)
    directors = filing_data['filing']['changeOfDirectors']['directors']
    director_party1 = create_party(business.id, directors[0])
    role1 = {
        'roleType': 'Director',
        'appointmentDate': directors[0].get('appointmentDate'),
        'cessationDate': directors[0].get('cessationDate')
    }
    director1 = create_role(party=director_party1, role_info=role1)
    # prep director for name change
    director_party2_dict = directors[1]
    director_party2_dict['officer']['firstName'] = director_party2_dict[
        'officer']['prevFirstName']
    director_party2_dict['officer']['middleInitial'] = director_party2_dict[
        'officer']['prevMiddleInitial']
    director_party2_dict['officer']['lastName'] = director_party2_dict[
        'officer']['prevLastName']
    director_party2 = create_party(business.id, director_party2_dict)
    role2 = {
        'roleType': 'Director',
        'appointmentDate': director_party2_dict.get('appointmentDate'),
        'cessationDate': director_party2_dict.get('cessationDate')
    }
    director2 = create_role(party=director_party2, role_info=role2)
    # prep director for cease
    director_party3 = create_party(business.id, directors[2])
    role3 = {
        'roleType': 'Director',
        'appointmentDate': directors[3].get('appointmentDate'),
        'cessationDate': directors[3].get('cessationDate')
    }
    director3 = create_role(party=director_party3, role_info=role3)
    # prep director for address change
    director_party4_dict = directors[3]
    director_party4_dict['deliveryAddress'][
        'streetAddress'] = 'should get changed'
    director_party4 = create_party(business.id, director_party4_dict)
    role4 = {
        'roleType': 'Director',
        'appointmentDate': director_party4_dict.get('appointmentDate'),
        'cessationDate': director_party4_dict.get('cessationDate')
    }
    director4 = create_role(party=director_party4, role_info=role4)

    # list of active/ceased directors in test filing
    ceased_directors, active_directors = active_ceased_lists(COMBINED_FILING)

    # setup
    business.party_roles.append(director1)
    business.party_roles.append(director2)
    business.party_roles.append(director3)
    business.party_roles.append(director4)
    business.save()
    director_ceased_id = director3.id
    # check that adding the directors during setup was successful
    directors = PartyRole.get_parties_by_role(
        business.id, PartyRole.RoleTypes.DIRECTOR.value)
    assert len(directors) == 4
    business_id = business.id
    filing_id = (create_filing(payment_id, COMBINED_FILING, business.id)).id
    filing_msg = {'filing': {'id': filing_id}}

    # TEST
    await process_filing(filing_msg, app)

    # Get modified data
    filing = Filing.find_by_id(filing_id)
    business = Business.find_by_internal_id(business_id)

    # check it out
    assert filing.transaction_id
    assert filing.business_id == business_id
    assert filing.status == Filing.Status.COMPLETED.value
    assert datetime.datetime.date(business.last_agm_date) == agm_date
    assert datetime.datetime.date(business.last_ar_date) == ar_date

    # check address filing
    delivery_address = business.delivery_address.one_or_none().json
    compare_addresses(delivery_address, new_delivery_address)
    mailing_address = business.mailing_address.one_or_none().json
    compare_addresses(mailing_address, new_mailing_address)

    # check director filing
    directors = PartyRole.get_active_directors(business.id, end_date)
    check_directors(business, directors, director_ceased_id, ceased_directors,
                    active_directors)
def process(business: Business, filing: Dict):  # pylint: disable=too-many-branches;
    """Render the change_of_directors onto the business model objects."""
    new_directors = filing['changeOfDirectors'].get('directors')
    new_director_names = []

    for new_director in new_directors:  # pylint: disable=too-many-nested-blocks;
        # Applies only for filings coming from colin.
        if filing.get('colinIds'):
            director_found = False
            current_new_director_name = \
                new_director['officer'].get('firstName') + new_director['officer'].get('middleInitial', '') + \
                new_director['officer'].get('lastName')
            new_director_names.append(current_new_director_name.upper())

            for director in PartyRole.get_parties_by_role(
                    business.id, PartyRole.RoleTypes.DIRECTOR.value):
                existing_director_name = \
                    director.party.first_name + director.party.middle_initial + director.party.last_name
                if existing_director_name.upper(
                ) == current_new_director_name.upper():
                    # Creates a new director record in Lear if a matching ceased director exists in Lear
                    # and the colin json contains the same director record with cessation date null.
                    if director.cessation_date is not None and new_director.get(
                            'cessationDate') is None:
                        director_found = False
                    else:
                        director_found = True
                        if new_director.get('cessationDate'):
                            new_director['actions'] = ['ceased']
                        else:
                            # For force updating address always as of now.
                            new_director['actions'] = ['modified']
                    break
            if not director_found:
                new_director['actions'] = ['appointed']

        if 'appointed' in new_director['actions']:

            # add new diretor party role to the business
            party = create_party(party_info=new_director)
            role = {
                'roleType': 'Director',
                'appointmentDate': new_director.get('appointmentDate'),
                'cessationDate': new_director.get('cessationDate')
            }
            new_director_role = create_role(party=party, role_info=role)
            business.party_roles.append(new_director_role)

        if any([action != 'appointed' for action in new_director['actions']]):
            # get name of director in json for comparison *
            new_director_name = \
                new_director['officer'].get('firstName') + new_director['officer'].get('middleInitial', '') + \
                new_director['officer'].get('lastName') \
                if 'nameChanged' not in new_director['actions'] \
                else new_director['officer'].get('prevFirstName') + \
                new_director['officer'].get('prevMiddleInitial') + new_director['officer'].get('prevLastName')
            if not new_director_name:
                logger.error('Could not resolve director name from json %s.',
                             new_director)
                raise QueueException

            for director in PartyRole.get_parties_by_role(
                    business.id, PartyRole.RoleTypes.DIRECTOR.value):
                # get name of director in database for comparison *
                director_name = director.party.first_name + director.party.middle_initial + director.party.last_name
                # Update only an active director
                if director_name.upper() == new_director_name.upper(
                ) and director.cessation_date is None:
                    update_director(director=director, new_info=new_director)
                    break

    if filing.get('colinIds'):
        for director in PartyRole.get_parties_by_role(
                business.id, PartyRole.RoleTypes.DIRECTOR.value):
            # get name of director in database for comparison *
            director_name = director.first_name + director.middle_initial + director.last_name
            if director_name.upper(
            ) not in new_director_names and director.cessation_date is None:
                director.cessation_date = datetime.utcnow()
Exemple #4
0
def process(business: Business, filing: Dict, filing_rec: Filing):
    # pylint: disable=too-many-locals; 1 extra
    """Process the incoming incorporation filing."""
    # Extract the filing information for incorporation
    incorp_filing = filing.get('incorporationApplication')

    if not incorp_filing:
        raise QueueException(
            f'IA legal_filing:incorporationApplication missing from {filing_rec.id}'
        )
    if business:
        raise QueueException(
            f'Business Already Exist: IA legal_filing:incorporationApplication {filing_rec.id}'
        )

    offices = incorp_filing.get('offices', None)
    parties = incorp_filing.get('parties', None)
    business_info = incorp_filing.get('nameRequest')
    share_classes = incorp_filing['shareClasses']

    # Reserve the Corp Numper for this entity
    corp_num = get_next_corp_num(business_info['legalType'])
    if not corp_num:
        raise QueueException(
            f'incorporationApplication {filing_rec.id} unable to get a business registration number.'
        )

    # Initial insert of the business record
    business = Business()
    business = update_business_info(corp_num, business, business_info,
                                    incorp_filing, filing_rec)
    if not business:
        raise QueueException(
            f'IA incorporationApplication {filing_rec.id}, Unable to create business.'
        )

    for office_type, addresses in offices.items():
        office = create_office(business, office_type, addresses)
        business.offices.append(office)

    if parties:
        for party_info in parties:
            party = create_party(business_id=business.id,
                                 party_info=party_info,
                                 create=False)
            for role_type in party_info.get('roles'):
                role = {
                    'roleType': role_type.get('roleType'),
                    'appointmentDate': role_type.get('appointmentDate', None),
                    'cessationDate': role_type.get('cessationDate', None)
                }
                party_role = create_role(party=party, role_info=role)
                business.party_roles.append(party_role)

    if share_classes:
        for share_class_info in share_classes:
            share_class = create_share_class(share_class_info)
            business.share_classes.append(share_class)

    ia_json = copy.deepcopy(filing_rec.filing_json)
    ia_json['filing']['business']['identifier'] = business.identifier
    ia_json['filing']['business'][
        'foundingDate'] = business.founding_date.isoformat()
    filing_rec._filing_json = ia_json  # pylint: disable=protected-access; bypass to update filing data
    return business, filing_rec
Exemple #5
0
def test_process_cod_filing(app, session):
    """Assert that an AR filling can be applied to the model correctly."""
    # vars
    payment_id = str(random.SystemRandom().getrandbits(0x58))
    identifier = 'CP1234567'
    end_date = datetime.datetime.utcnow().date()
    # prep director for no change
    filing_data = copy.deepcopy(COD_FILING)
    directors = filing_data['filing']['changeOfDirectors']['directors']
    director_party1 = create_party(directors[0])
    role1 = {
        'roleType': 'Director',
        'appointmentDate': directors[0].get('appointmentDate'),
        'cessationDate': directors[0].get('cessationDate')
    }
    director1 = create_role(party=director_party1, role_info=role1)
    # prep director for name change
    director_party2_dict = directors[1]
    director_party2_dict['officer']['firstName'] = director_party2_dict['officer']['prevFirstName']
    director_party2_dict['officer']['middleInitial'] = director_party2_dict['officer']['prevMiddleInitial']
    director_party2_dict['officer']['lastName'] = director_party2_dict['officer']['prevLastName']
    director_party2 = create_party(director_party2_dict)
    role2 = {
        'roleType': 'Director',
        'appointmentDate': director_party2_dict.get('appointmentDate'),
        'cessationDate': director_party2_dict.get('cessationDate')
    }
    director2 = create_role(party=director_party2, role_info=role2)
    # prep director for cease
    director_party3 = create_party(directors[2])
    role3 = {
        'roleType': 'Director',
        'appointmentDate': directors[3].get('appointmentDate'),
        'cessationDate': directors[3].get('cessationDate')
    }
    director3 = create_role(party=director_party3, role_info=role3)
    # prep director for address change
    director_party4_dict = directors[3]
    director_party4_dict['deliveryAddress']['streetAddress'] = 'should get changed'
    director_party4 = create_party(director_party4_dict)
    role4 = {
        'roleType': 'Director',
        'appointmentDate': director_party4_dict.get('appointmentDate'),
        'cessationDate': director_party4_dict.get('cessationDate')
    }
    director4 = create_role(party=director_party4, role_info=role4)

    # list of active/ceased directors in test filing
    ceased_directors, active_directors = active_ceased_lists(COD_FILING)

    # setup
    business = create_business(identifier)
    business.party_roles.append(director1)
    business.party_roles.append(director2)
    business.party_roles.append(director3)
    business.party_roles.append(director4)
    business.save()
    director_ceased_id = director3.id
    # check that adding the director during setup was successful
    directors = PartyRole.get_parties_by_role(business.id, PartyRole.RoleTypes.DIRECTOR.value)
    assert len(directors) == 4
    # create filing
    business_id = business.id
    filing_id = (create_filing(payment_id, COD_FILING, business.id)).id
    filing_msg = {'filing': {'id': filing_id}}

    # TEST
    process_filing(filing_msg, app)

    # Get modified data
    filing = Filing.find_by_id(filing_id)
    business = Business.find_by_internal_id(business_id)

    # check it out
    assert filing.transaction_id
    assert filing.business_id == business_id
    assert filing.status == Filing.Status.COMPLETED.value

    directors = PartyRole.get_active_directors(business.id, end_date)
    check_directors(business, directors, director_ceased_id, ceased_directors, active_directors)