Beispiel #1
0
 def save_detail_company_to_db(self, records):
     for record in records:
         name = record.xpath('NAME')[0].text.lower()
         short_name = record.xpath('SHORT_NAME')[0].text
         if short_name:
             short_name = short_name.lower()
         company_type = record.xpath('OPF')[0].text
         if company_type:
             company_type = self.save_or_get_company_type(
                 company_type, 'uk')
         edrpou = record.xpath('EDRPOU')[0].text
         if not edrpou:
             self.report.invalid_data += 1
             continue
         code = name + edrpou
         address = record.xpath('ADDRESS')[0].text
         status = self.save_or_get_status(record.xpath('STAN')[0].text)
         founding_document_number = record.xpath(
             'FOUNDING_DOCUMENT_NUM')[0].text
         executive_power = record.xpath('EXECUTIVE_POWER')[0].text
         if executive_power:
             executive_power = executive_power.lower()
         # if len(record.xpath('ACTIVITY_KINDS')[0]):
         #     self.add_company_to_kved(record.xpath('ACTIVITY_KINDS')[0], code)
         superior_management = record.xpath('SUPERIOR_MANAGEMENT')[0].text
         if superior_management:
             superior_management = superior_management.lower()
         # if len(record.xpath('SIGNERS')[0]):
         #     self.add_signers(record.xpath('SIGNERS')[0], code)
         authorized_capital = record.xpath('AUTHORIZED_CAPITAL')[0].text
         if authorized_capital:
             authorized_capital = authorized_capital.replace(',', '.')
             authorized_capital = float(authorized_capital)
         bylaw = self.save_or_get_bylaw(record.xpath('STATUTE')[0].text)
         registration_date = None
         registration_info = None
         registration = record.xpath('REGISTRATION')[0].text
         if registration:
             registration_date = format_date_to_yymmdd(
                 get_first_word(registration))
             registration_info = cut_first_word(registration)
         managing_paper = record.xpath('MANAGING_PAPER')[0].text
         if managing_paper:
             managing_paper = managing_paper.lower()
         # TODO: refactor branches storing
         # if len(record.xpath('BRANCHES')[0]):
         #     self.add_branches(record.xpath('BRANCHES')[0], code)
         # if record.xpath('TERMINATION_STARTED_INFO/OP_DATE'):
         #     self.add_termination_started(record, code)
         # if record.xpath('BANKRUPTCY_READJUSTMENT_INFO/OP_DATE'):
         #     self.add_bancruptcy_readjustment(record, code)
         # if len(record.xpath('PREDECESSORS')[0]):
         #     self.add_company_to_predecessors(record.xpath('PREDECESSORS')[0], code)
         # if len(record.xpath('ASSIGNEES')[0]):
         #     self.add_assignees(record.xpath('ASSIGNEES')[0], code)
         terminated_info = record.xpath('TERMINATED_INFO')[0].text
         if terminated_info:
             terminated_info = terminated_info.lower()
         termination_cancel_info = record.xpath(
             'TERMINATION_CANCEL_INFO')[0].text
         if termination_cancel_info:
             termination_cancel_info = termination_cancel_info.lower()
         contact_info = record.xpath('CONTACTS')[0].text
         # if record.xpath('EXCHANGE_DATA')[0]:
         #     self.add_exchange_data(record.xpath('EXCHANGE_DATA')[0], code)
         vp_dates = record.xpath('VP_DATES')[0].text
         authority = self.save_or_get_authority(
             record.xpath('CURRENT_AUTHORITY')[0].text)
         # self.add_company_detail(founding_document_number, executive_power, superior_management, managing_paper,
         #                         terminated_info, termination_cancel_info, vp_dates, code)
         # ToDo: resolve the problem of having records with the same company name amd edrpou
         # that results in the same code
         company = Company.objects.filter(code=code).first()
         if not company:
             company = Company(name=name,
                               short_name=short_name,
                               company_type=company_type,
                               edrpou=edrpou,
                               authorized_capital=authorized_capital,
                               status=status,
                               bylaw=bylaw,
                               registration_date=registration_date,
                               registration_info=registration_info,
                               contact_info=contact_info,
                               authority=authority,
                               code=code)
             company.save()
             # self.bulk_manager.add_create(company)
         else:
             update_fields = []
             if company.name != name:
                 company.name = name
                 update_fields.append('name')
             if company.short_name != short_name:
                 company.short_name = short_name
                 update_fields.append('short_name')
             if company.company_type != company_type:
                 company.company_type = company_type
                 update_fields.append('company_type')
             if company.authorized_capital != authorized_capital:
                 company.authorized_capital = authorized_capital
                 update_fields.append('authorized_capital')
             if company.address != address:
                 company.address = address
                 update_fields.append('address')
             if company.status != status:
                 company.status = status
                 update_fields.append('status')
             if company.bylaw != bylaw:
                 company.bylaw = bylaw
                 update_fields.append('bylaw')
             if to_lower_string_if_exists(
                     company.registration_date) != registration_date:
                 company.registration_date = registration_date
                 update_fields.append('registration_date')
             if company.registration_info != registration_info:
                 company.registration_info = registration_info
                 update_fields.append('registration_info')
             if company.contact_info != contact_info:
                 company.contact_info = contact_info
                 update_fields.append('contact_info')
             if company.authority != authority:
                 company.authority = authority
                 update_fields.append('authority')
             if update_fields:
                 update_fields.append('updated_at')
                 company.save(update_fields=update_fields)
                 # self.bulk_manager.add_update(company)
         if len(record.xpath('FOUNDERS')[0]):
             self.save_or_update_founders(
                 record.xpath('FOUNDERS')[0], company)
     # if len(self.bulk_manager.update_queues['business_register.Company']):
     #     self.bulk_manager.commit_update(Company, ['name', 'short_name', 'company_type',
     #                                               'authorized_capital', 'address', 'status',
     #                                               'bylaw', 'registration_date',
     #                                               'registration_info', 'contact_info',
     #                                               'authority'])
     # if len(self.bulk_manager.create_queues['business_register.Company']):
     #     self.bulk_manager.commit_create(Company)
     if len(self.bulk_manager.queues['business_register.Founder']):
         self.bulk_manager.commit(Founder)
     self.bulk_manager.queues['business_register.Founder'] = []
Beispiel #2
0
 def branch_create(self, item, code):
     branch = Company()
     branch.name = item.xpath('NAME')[0].text
     branch.short_name = code
     branch.address = item.xpath('ADDRESS')[0].text
     if item.xpath('CREATE_DATE')[0].text:
         branch.registration_date = format_date_to_yymmdd(
             item.xpath('CREATE_DATE')[0].text) or None
     branch.contact_info = item.xpath('CONTACTS')[0].text
     branch.authority = self.authority
     branch.bylaw = self.bylaw
     branch.company_type = self.company_type
     branch.status = self.status
     branch.hash_code = self.create_hash_code(branch.name, code)
     return branch
Beispiel #3
0
 def company_create (self, record, edrpou, registration_date, registration_info):
     company = Company()
     company.name = record.xpath('NAME')[0].text
     company.short_name = record.xpath('SHORT_NAME')[0].text
     company.company_type = self.company_type
     company.edrpou = edrpou
     company.address = record.xpath('ADDRESS')[0].text
     company.status = self.status
     company.bylaw = self.bylaw
     company.registration_date = registration_date
     company.registration_info = registration_info
     company.contact_info = record.xpath('CONTACTS')[0].text
     company.authority = self.authority
     company.hash_code = self.create_hash_code(record.xpath('NAME')[0].text, edrpou)
     return company
    def save_to_db(self, file):
        with open(file, newline='') as csvfile:
            index = 0

            for row in DictReader(csvfile):
                name = row['CompanyName'].lower()
                # number is unique identifier in Company House
                number = row[' CompanyNumber']
                code = name + number
                country = self.save_or_get_country(row['CountryOfOrigin'])
                address = (
                    f"{row['RegAddress.Country']} {row['RegAddress.PostCode']} "
                    f"{row['RegAddress.County']} {row['RegAddress.PostTown']} "
                    f"{row[' RegAddress.AddressLine2']} {row['RegAddress.AddressLine1']} "
                    f"{row['RegAddress.POBox']} {row['RegAddress.CareOf']}")
                company_type = self.save_or_get_company_type(
                    row['CompanyCategory'], 'en')
                status = self.save_or_get_status(row['CompanyStatus'])
                if len(row['IncorporationDate']) == 10:
                    registration_date = format_date_to_yymmdd(
                        row['IncorporationDate'])
                else:
                    registration_date = None
                source = Company.GREAT_BRITAIN_REGISTER
                company = Company.objects.filter(
                    edrpou=number,
                    source=Company.GREAT_BRITAIN_REGISTER).first()
                if not company:
                    company = Company(name=name,
                                      company_type=company_type,
                                      edrpou=number,
                                      address=address,
                                      country=country,
                                      status=status,
                                      registration_date=registration_date,
                                      code=code,
                                      source=source)
                    company.save()
                else:
                    update_fields = []
                    if company.name != name:
                        company.name = name
                        update_fields.append('name')
                    if company.company_type_id != company_type.id:
                        company.company_type = company_type
                        update_fields.append('company_type')
                    if company.address != address:
                        company.address = address
                        update_fields.append('address')
                    if company.country_id != country.id:
                        company.country = country
                        update_fields.append('country')
                    if company.status_id != status.id:
                        company.status = status
                        update_fields.append('status')
                    if to_lower_string_if_exists(
                            company.registration_date) != registration_date:
                        company.registration_date = registration_date
                        update_fields.append('registration_date')
                    if company.code != code:
                        company.code = code
                        update_fields.append('code')
                    if company.source != source:
                        company.source = source
                        update_fields.append('source')
                    if update_fields:
                        update_fields.append('updated_at')
                        company.save(update_fields=update_fields)

            print('All companies from UK register were saved')
    def save_to_db(self, file):
        with open(file, newline='') as csvfile:
            for row in DictReader(csvfile):
                name = row['CompanyName'].lower()
                number = row[' CompanyNumber']
                code = name + number
                country = AddressConverter().save_or_get_country(
                    row['CountryOfOrigin'])
                address = (
                    f"{row['RegAddress.Country']} {row['RegAddress.PostCode']} "
                    f"{row['RegAddress.County']} {row['RegAddress.PostTown']} "
                    f"{row[' RegAddress.AddressLine2']} {row['RegAddress.AddressLine1']} "
                    f"{row['RegAddress.POBox']} {row['RegAddress.CareOf']}")
                company_type = self.save_or_get_company_type(
                    row['CompanyCategory'], 'en')
                status = self.save_or_get_status(row['CompanyStatus'])
                registration_date = format_date_to_yymmdd(
                    row['IncorporationDate'])
                company = Company.objects.filter(edrpou=number).first()
                if not company:
                    company = Company(name=name,
                                      company_type=company_type,
                                      edrpou=number,
                                      address=address,
                                      country=country,
                                      status=status,
                                      registration_date=registration_date,
                                      code=code)
                    company.save()
                else:
                    update_fields = []
                    if company.name != name:
                        company.name = name
                        update_fields.append('name')
                    if company.company_type != company_type:
                        company.company_type = company_type
                        update_fields.append('company_type')
                    if company.address != address:
                        company.address = address
                        update_fields.append('address')
                    if company.country != country:
                        company.country = country
                        update_fields.append('country')
                    if company.status != status:
                        company.status = status
                        update_fields.append('status')
                    if to_lower_string_if_exists(
                            company.registration_date) != registration_date:
                        company.registration_date = registration_date
                        update_fields.append('registration_date')
                    if company.code != code:
                        company.code = code
                        update_fields.append('code')

                    if update_fields:
                        company.save(update_fields=update_fields)

            print('All companies from UK register were saved')