コード例 #1
0
ファイル: filing.py プロジェクト: kulpree/lear
    def add_filing(cls, filing):  # pylint: disable=too-many-locals,too-many-statements,too-many-branches;
        """Add new filing to COLIN tables.

        :param filing: Filing dict.
        :returns (int): the filing ID of the new filing.
        """
        try:
            corp_num = filing.get_corp_num()

            # get db connection and start a session, in case we need to roll back
            con = DB.connection
            con.begin()
            cursor = con.cursor()

            # create new event record, return event ID
            event_id = cls._get_event_id(cursor, corp_num, 'FILE')

            # create new filing user
            cls._create_filing_user(cursor, event_id, filing)
            if filing.filing_type == 'annualReport':
                ar_date = filing.body['annualReportDate']
                agm_date = filing.body['annualGeneralMeetingDate']
                filing_type_cd = 'OTANN'

                # create new filing
                cls._create_filing(cursor, event_id, corp_num, ar_date,
                                   agm_date, filing_type_cd)

                # update corporation record
                Business.update_corporation(cursor, corp_num, agm_date)

                # update corp_state TO ACT (active) if it is in good standing. From CRUD:
                # - the current corp_state != 'ACT' and,
                # - they just filed the last outstanding ARs
                if filing.business.business['corpState'] != 'ACT':
                    agm_year = int(ar_date[:4])
                    last_year = datetime.datetime.now().year - 1
                    if agm_year >= last_year:
                        Business.update_corp_state(cursor,
                                                   event_id,
                                                   corp_num,
                                                   state='ACT')

            elif filing.filing_type == 'changeOfAddress':
                # set date to last ar date + 1
                last_ar_date = filing.business.business['lastArDate']
                day = int(last_ar_date[-2:]) + 1
                try:
                    date = str(
                        datetime.datetime.strptime(
                            last_ar_date[:-2] + ('0' + str(day))[1:],
                            '%Y-%m-%d'))[:10]
                except ValueError:
                    try:
                        day = '-01'
                        month = int(last_ar_date[5:7]) + 1
                        date = str(
                            datetime.datetime.strptime(
                                last_ar_date[:5] + ('0' + str(month))[1:] +
                                day, '%Y-%m-%d'))[:10]
                    except ValueError:
                        mm_dd = '-01-01'
                        yyyy = int(last_ar_date[:4]) + 1
                        date = str(
                            datetime.datetime.strptime(
                                str(yyyy) + mm_dd, '%Y-%m-%d'))[:10]

                # create new filing
                filing_type_cd = 'OTADD'
                cls._create_filing(cursor, event_id, corp_num, date, None,
                                   filing_type_cd)

                # create new addresses for delivery + mailing, return address ids
                delivery_addr_id = Address.create_new_address(
                    cursor, filing.body['deliveryAddress'])
                mailing_addr_id = Address.create_new_address(
                    cursor, filing.body['mailingAddress'])

                # update office table to include new addresses
                Office.update_office(cursor, event_id, corp_num,
                                     delivery_addr_id, mailing_addr_id, 'RG')

                # create new ledger text for address change
                cls._add_ledger_text(
                    cursor, event_id,
                    f'Change to the Registered Office, effective on {date}')
                # update corporation record
                Business.update_corporation(cursor, corp_num)

            elif filing.filing_type == 'changeOfDirectors':
                # create new filing
                date = filing.business.business['lastArDate']
                filing_type_cd = 'OTCDR'
                cls._create_filing(cursor, event_id, corp_num, date, None,
                                   filing_type_cd)

                # create, cease, change directors
                changed_dirs = []
                for director in filing.body['directors']:
                    if 'appointed' in director['actions']:
                        Director.create_new_director(
                            cursor=cursor,
                            event_id=event_id,
                            director=director,
                            business=filing.business.as_dict())

                    if 'ceased' in director['actions'] and not any(
                            elem in ['nameChanged', 'addressChanged']
                            for elem in director['actions']):
                        Director.end_by_name(cursor=cursor,
                                             director=director,
                                             event_id=event_id,
                                             corp_num=corp_num)

                    elif 'nameChanged' in director[
                            'actions'] or 'addressChanged' in director[
                                'actions']:
                        if 'appointed' in director['actions']:
                            current_app.logger.error(
                                f'Director appointed with name/address change: {director}'
                            )
                        changed_dirs.append(director)
                        # end tmp copy of director with no cessation date (will be recreated with changes and cessation
                        # date - otherwise end up with two copies of ended director)
                        tmp = director.copy()
                        tmp['cessationDate'] = ''
                        Director.end_by_name(cursor=cursor,
                                             director=tmp,
                                             event_id=event_id,
                                             corp_num=corp_num)

                # add back changed directors as new row - if ceased director with changes this will add them with
                # cessation date + end event id filled
                for director in changed_dirs:
                    Director.create_new_director(
                        cursor=cursor,
                        event_id=event_id,
                        director=director,
                        business=filing.business.as_dict())

                # create new ledger text for address change
                cls._add_ledger_text(cursor=cursor,
                                     event_id=event_id,
                                     text=f'Director change.')
                # update corporation record
                Business.update_corporation(cursor=cursor, corp_num=corp_num)

            else:
                raise InvalidFilingTypeException(
                    filing_type=filing.filing_type)

            # success! commit the db changes
            con.commit()
            return event_id

        except Exception as err:
            # something went wrong, roll it all back
            current_app.logger.error(err.with_traceback(None))
            if con:
                con.rollback()

            raise err
コード例 #2
0
    def add_filing(cls, con, filing):  # pylint: disable=too-many-locals,too-many-statements,too-many-branches;
        """Add new filing to COLIN tables.

        :param con: DB connection
        :param filing: Filing dict.
        :returns (int): the filing ID of the new filing.
        """
        try:
            corp_num = filing.get_corp_num()
            legal_type = corp_num[:2]
            user_id = Filing.USERS[legal_type] if legal_type in (
                'CP', 'BC') else None
            cursor = con.cursor()

            # create new event record, return event ID
            event_id = cls._get_event_id(cursor, corp_num, 'FILE')

            # create new filing user
            cls._create_filing_user(cursor, event_id, filing, user_id)
            if filing.filing_type == 'annualReport':
                ar_date = filing.body['annualReportDate']
                agm_date = filing.body['annualGeneralMeetingDate']
                filing_type_cd = 'OTANN'

                # create new filing
                cls._create_filing(cursor, event_id, corp_num, ar_date,
                                   agm_date, filing_type_cd)

                # update corporation record
                Business.update_corporation(cursor, corp_num, agm_date, True)

                # update corp_state TO ACT (active) if it is in good standing. From CRUD:
                # - the current corp_state != 'ACT' and,
                # - they just filed the last outstanding ARs
                agm_year = int(ar_date[:4])
                if filing.business.business['corpState'] != 'ACT':
                    last_year = datetime.datetime.now().year - 1
                    if agm_year >= last_year:
                        Business.update_corp_state(cursor,
                                                   event_id,
                                                   corp_num,
                                                   state='ACT')

                # create new ledger text for annual report
                text = agm_date if agm_date else f'NO AGM HELD IN {agm_year}'
                cls._add_ledger_text(cursor=cursor,
                                     event_id=event_id,
                                     text=f'ANNUAL REPORT - {text}',
                                     user_id=user_id)

            elif filing.filing_type == 'changeOfAddress':
                # set date to last ar date + 1 -- Bob wants this to be set to null
                # last_ar_date = filing.business.business['lastArDate']
                # day = int(last_ar_date[-2:]) + 1
                # try:
                #     date = str(datetime.datetime.strptime(last_ar_date[:-2] + ('0' + str(day))[1:], '%Y-%m-%d'))[:10]
                # except ValueError:
                #     try:
                #         day = '-01'
                #         month = int(last_ar_date[5:7]) + 1
                #         date = str(datetime.datetime.strptime(last_ar_date[:5] + ('0' + str(month))[1:] + day,
                #                                               '%Y-%m-%d')
                #                    )[:10]
                #     except ValueError:
                #         mm_dd = '-01-01'
                #         yyyy = int(last_ar_date[:4]) + 1
                #         date = str(datetime.datetime.strptime(str(yyyy) + mm_dd, '%Y-%m-%d'))[:10]
                date = None

                # create new filing
                filing_type_cd = 'OTADD'
                cls._create_filing(cursor, event_id, corp_num, date, None,
                                   filing_type_cd)

                # create new addresses for delivery + mailing, return address ids

                cls._add_office_from_filing(cursor, event_id, corp_num,
                                            user_id, filing)
                # update corporation record
                Business.update_corporation(cursor, corp_num)

            elif filing.filing_type == 'changeOfDirectors':
                # create new filing
                # bob wants this to be null
                # date = filing.business.business['lastArDate']
                date = None
                filing_type_cd = 'OTCDR'
                cls._create_filing(cursor, event_id, corp_num, date, None,
                                   filing_type_cd)

                # create, cease, change directors
                changed_dirs = []
                for director in filing.body['directors']:
                    if 'appointed' in director['actions']:
                        Director.create_new_director(
                            cursor=cursor,
                            event_id=event_id,
                            director=director,
                            business=filing.business.as_dict())

                    if 'ceased' in director['actions'] and not any(
                            elem in ['nameChanged', 'addressChanged']
                            for elem in director['actions']):
                        Director.end_by_name(cursor=cursor,
                                             director=director,
                                             event_id=event_id,
                                             corp_num=corp_num)

                    elif 'nameChanged' in director[
                            'actions'] or 'addressChanged' in director[
                                'actions']:
                        if 'appointed' in director['actions']:
                            current_app.logger.error(
                                f'Director appointed with name/address change: {director}'
                            )
                        changed_dirs.append(director)
                        # end tmp copy of director with no cessation date (will be recreated with changes and cessation
                        # date - otherwise end up with two copies of ended director)
                        tmp = director.copy()
                        tmp['cessationDate'] = ''
                        Director.end_by_name(cursor=cursor,
                                             director=tmp,
                                             event_id=event_id,
                                             corp_num=corp_num)

                # add back changed directors as new row - if ceased director with changes this will add them with
                # cessation date + end event id filled
                for director in changed_dirs:
                    Director.create_new_director(
                        cursor=cursor,
                        event_id=event_id,
                        director=director,
                        business=filing.business.as_dict())

                # create new ledger text for address change
                cls._add_ledger_text(cursor=cursor,
                                     event_id=event_id,
                                     text=f'Director change.',
                                     user_id=user_id)
                # update corporation record
                Business.update_corporation(cursor=cursor, corp_num=corp_num)
            elif filing.filing_type == 'incorporationApplication':
                # Add business, update business info
                # Add offices
                date = None
                filing_type_cd = 'OTINC'
                cls._create_filing(cursor, event_id, corp_num, date, None,
                                   filing_type_cd)
                # Do incorporation here
                Business.create_corp_name(cursor, corp_num, 'test name',
                                          event_id)
                Business.create_corp_state(cursor, corp_num, event_id)

                cls._add_office_from_filing(cursor, event_id, corp_num,
                                            user_id, filing)

            else:
                raise InvalidFilingTypeException(
                    filing_type=filing.filing_type)

            return event_id

        except Exception as err:
            # something went wrong, roll it all back
            current_app.logger.error(err.with_traceback(None))

            raise err