def test_get_most_recent_filing_by_legal_type_in_json(session): """Assert that the most recent legal filing can be retrieved.""" business = factory_business('CP1234567') uow = versioning_manager.unit_of_work(session) transaction = uow.create_transaction(session) for i in range(1, 5): effective_date = f'200{i}-07-01T00:00:00+00:00' completion_date = datetime.datetime.fromisoformat(effective_date) base_filing = copy.deepcopy(ANNUAL_REPORT) cod = copy.deepcopy(CHANGE_OF_DIRECTORS) base_filing['filing']['changeOfDirectors'] = cod base_filing['filing']['header']['effectiveDate'] = effective_date filing = Filing() filing._filing_date = completion_date filing.business_id = business.id filing.filing_json = base_filing filing.effective_date = datetime.datetime.fromisoformat(effective_date) filing.payment_token = 'token' filing.transaction_id = transaction.id filing.payment_completion_date = completion_date filing.save() f = Filing.get_most_recent_legal_filing(business.id, 'changeOfDirectors') assert f.effective_date == datetime.datetime.fromisoformat(effective_date) assert f.filing_type == 'annualReport' assert f.id == filing.id
def test_get_most_recent_filing_by_legal_type_db_field(session): """Assert that the most recent legal filing can be retrieved. Create 3 filings, find the 2 one by the type only. """ business = factory_business('CP1234567') uow = versioning_manager.unit_of_work(session) transaction = uow.create_transaction(session) # filing 1 effective_date = '2001-07-01T00:00:00+00:00' completion_date = datetime.datetime.fromisoformat(effective_date) base_filing = copy.deepcopy(ANNUAL_REPORT) base_filing['filing']['header']['effectiveDate'] = effective_date filing1 = Filing() filing1._filing_date = completion_date filing1.business_id = business.id filing1.filing_json = base_filing filing1.effective_date = datetime.datetime.fromisoformat(effective_date) filing1.payment_token = 'token' filing1.transaction_id = transaction.id filing1.payment_completion_date = completion_date filing1.save() # filing 2 <- target effective_date = '2002-07-01T00:00:00+00:00' completion_date = datetime.datetime.fromisoformat(effective_date) base_filing = copy.deepcopy(FILING_HEADER) base_filing['filing']['header']['effectiveDate'] = effective_date base_filing['filing']['header']['name'] = 'changeOfDirectors' base_filing['filing']['header']['availableOnPaperOnly'] = True filing2 = Filing() filing2._filing_date = completion_date filing2.business_id = business.id filing2.filing_json = base_filing filing2.effective_date = datetime.datetime.fromisoformat(effective_date) filing2.payment_token = 'token' filing2.transaction_id = transaction.id filing2.payment_completion_date = completion_date filing2.save() # filing 3 effective_date = '2003-07-01T00:00:00+00:00' completion_date = datetime.datetime.fromisoformat(effective_date) base_filing = copy.deepcopy(ANNUAL_REPORT) base_filing['filing']['header']['effectiveDate'] = effective_date filing3 = Filing() filing3._filing_date = completion_date filing3.business_id = business.id filing3.filing_json = base_filing filing3.effective_date = datetime.datetime.fromisoformat(effective_date) filing3.payment_token = 'token' filing3.transaction_id = transaction.id filing3.payment_completion_date = completion_date filing3.save() f = Filing.get_most_recent_legal_filing(business.id, 'changeOfDirectors') assert f.filing_type == 'changeOfDirectors' assert f.id == filing2.id
def validate_effective_date(business: Business, cod: Dict) -> List: """Return an error or warning message based on the effective date validation rules. Rules: - The effective date of change cannot be in the future. - The effective date cannot be a date prior to their Incorporation Date - The effective date of change cannot be a date that is farther in the past as a previous COD filing (Standalone or AR). - The effective date can be the same effective date as another COD filing (standalone OR AR). If this is the case: - COD filing that was filed most recently as the most current director information. """ try: filing_effective_date = cod['filing']['header']['effectiveDate'] except KeyError: try: # we'll assume the filing is at 0 hours UTC filing_effective_date = cod['filing']['header'][ 'date'] + 'T00:00:00+00:00' except KeyError: return { 'error': babel('No effective_date or filing date provided.') } try: effective_date = datetime.fromisoformat(filing_effective_date) except ValueError: return { 'error': babel('Invalid ISO format for effective_date or filing date.') } msg = [] # The effective date of change cannot be in the future if effective_date > datetime.utcnow(): msg.append( {'error': babel('Filing cannot have a future effective date.')}) # The effective date cannot be a date prior to their Incorporation Date if effective_date < business.founding_date: msg.append({ 'error': babel('Filing cannot be before a businesses founding date.') }) last_cod_filing = Filing.get_most_recent_legal_filing( business.id, Filing.FILINGS['changeOfDirectors']['name']) if last_cod_filing: if effective_date < last_cod_filing.effective_date: msg.append({ 'error': babel( "Filing's effective date cannot be before another Change of Director filing." ) }) return msg
def validate_effective_date(business: Business, cod: Dict) -> List: """Return an error or warning message based on the effective date validation rules. Rules: (text from the BA rules document) - The effective date of change cannot be in the future. - The effective date cannot be a date prior to their Incorporation Date. - The effective date of change cannot be a date that is farther in the past than a previous COD filing (standalone or AR). - The effective date can be the same effective date as another COD filing (standalone or AR). If this is the case: - COD filing that was filed most recently is the most current director information. """ msg = [] # get effective datetime string from filing try: effective_datetime_str = cod['filing']['header']['effectiveDate'] except KeyError: return {'error': babel('No effective date provided.')} # convert string to datetime try: effective_datetime_utc = datetime.fromisoformat(effective_datetime_str) except ValueError: return {'error': babel('Invalid ISO format for effective date.')} # check if effective datetime is in the future if effective_datetime_utc > datetime.utcnow(): msg.append({'error': babel('Filing cannot have a future effective date.')}) # convert to legislation timezone and then get date only effective_date_leg = LegislationDatetime.as_legislation_timezone(effective_datetime_utc).date() # check if effective date is before their incorporation date founding_date_leg = LegislationDatetime.as_legislation_timezone(business.founding_date).date() if effective_date_leg < founding_date_leg: msg.append({'error': babel('Effective date cannot be before businesses founding date.')}) # check if effective date is before their most recent COD or AR date last_cod_filing = Filing.get_most_recent_legal_filing(business.id, Filing.FILINGS['changeOfDirectors']['name']) if last_cod_filing: last_cod_date_leg = LegislationDatetime.as_legislation_timezone(last_cod_filing.effective_date).date() if effective_date_leg < last_cod_date_leg: msg.append({'error': babel('Effective date cannot be before another Change of Director filing.')}) return msg