def _get_or_create_group(self, group_name, company_id): comp_group = CompanyGroup.objects.filter(company=company_id, name=group_name) if not comp_group: # Let's create a new company_group new_comp_group = {'company': company_id, 'name': group_name} group_serializer = CompanyGroupPostSerializer(data=new_comp_group) if group_serializer.is_valid(): group_serializer.save() hash_key_service = HashKeyService() return hash_key_service.decode_key(group_serializer.data['id']) else: raise Exception( "Group cannot be created with the name {}".format( group_name)) else: return comp_group[0].id
def _decode_value(self, value): hash_key_service = HashKeyService() return hash_key_service.decode_key(value)
def execute_creation(self, account_info, do_validation=True): result = OperationResult(account_info) account_result = None # Do validation first, and short circuit if failed if (do_validation): account_result = self.validate(account_info) else: # directly construct the result, skipping validation account_result = OperationResult(account_info) # If the account creation info is not valid to begin with # simply short circuit and return it if (account_result.has_issue()): return account_result userManager = AuthUserManager() # Create the actual user data password = settings.DEFAULT_USER_PW if account_info.password: password = account_info.password user = User.objects.create_user(account_info.email, password) user.first_name = account_info.first_name user.last_name = account_info.last_name user.save() # Create the company_user data company_user = CompanyUser( company_id=account_info.company_id, user=user, company_user_type=account_info.company_user_type) if account_info.new_employee is not None: company_user.new_employee = account_info.new_employee company_user.save() # Now create the person object person_data = { 'first_name': account_info.first_name, 'last_name': account_info.last_name, 'user': user.id, 'relationship': SELF, 'person_type': 'primary_contact', 'email': user.email } person_serializer = PersonSimpleSerializer(data=person_data) if person_serializer.is_valid(): person_serializer.save() else: raise Exception("Failed to create person record") # Create the employee profile key_service = HashKeyService() person_id = key_service.decode_key(person_serializer.data['id']) pin = account_info.pin pinService = EmployeePinService() if not pin: pin = pinService.get_company_wide_unique_pin( account_info.company_id, user.id) profile_data = { 'person': person_id, 'company': account_info.company_id, 'start_date': account_info.start_date, 'benefit_start_date': account_info.benefit_start_date, 'pin': pin } if (account_info.compensation_info.annual_base_salary is not None): profile_data[ 'annual_base_salary'] = account_info.compensation_info.annual_base_salary if (account_info.employment_type): profile_data['employment_type'] = account_info.employment_type if (account_info.manager_id): profile_data['manager'] = account_info.manager_id if (account_info.employee_number): profile_data['employee_number'] = account_info.employee_number profile_serializer = EmployeeProfilePostSerializer(data=profile_data) if profile_serializer.is_valid(): profile_serializer.save() else: raise Exception("Failed to create employee profile record") # Now check to see send email and create documents if company_user.company_user_type == 'employee': if account_info.send_email: # now try to create the onboard email for this user. try: onboard_email( "%s %s" % (user.first_name, user.last_name), account_info.company_id, [account_info.email, settings.SUPPORT_EMAIL_ADDRESS], user.id) except StandardError: raise Exception("Failed to send email to employee") if (account_info.create_docs): # Let's create the documents for this new user try: doc_gen = UserDocumentGenerator(company_user.company, user) doc_gen.generate_all_document(account_info.doc_fields) except Exception: raise Exception( "Failed to generate documents for employee") # Create the initial compensation record compensation_data = { 'person': person_id, 'company': account_info.company_id, 'annual_base_salary': account_info.compensation_info.annual_base_salary, 'projected_hour_per_month': account_info.compensation_info.projected_hour_per_month, 'hourly_rate': account_info.compensation_info.hourly_rate, 'effective_date': account_info.compensation_info.effective_date, 'increase_percentage': None } compensation_serializer = EmployeeCompensationPostSerializer( data=compensation_data) if (compensation_serializer.is_valid()): compensation_serializer.save() else: raise Exception("Failed to create compensation record") if account_info.group_id: self._add_to_group(account_info.group_id, user.id) elif account_info.group_name: group_id = self._get_or_create_group(account_info.group_name, account_info.company_id) if group_id: self._add_to_group(group_id, user.id) else: raise Exception( "Cannot get group_id from group name {}".format( account_info.group_name)) account_info.user_id = user.id account_result.set_output_data(account_info) # Now for the new employee being created, setup any information # required for external parties (such as payroll and benefit service) # providers. self.company_integration_provider_data_service.generate_and_record_external_employee_number( user.id) return account_result
def from_native(self, data): hash_key_service = HashKeyService() return hash_key_service.decode_key(data)
class PersonEnrollmentSummaryView(APIView): """ Benefit enrollment status for a person """ def __init__(self): self.hash_service = HashKeyService() def get_person_info(self, person_id): try: person = Person.objects.get(pk=person_id) serializer = PersonSerializer(person) return serializer.data except Person.DoesNotExist: raise Http404 def get_health_benefit_enrollment(self, user_id): enrollment = UserCompanyBenefitPlanOption.objects.filter(user=user_id) serializer = UserCompanyBenefitPlanOptionSerializer(enrollment, many=True, required=False) return serializer.data def get_health_benefit_waive(self, user_id): waived = UserCompanyWaivedBenefit.objects.filter(user=user_id) serializer = UserCompanyWaivedBenefitSerializer(waived, required=False, many=True) return serializer.data def get_hra_plan(self, person_id): hra_plan = PersonCompanyHraPlan.objects.filter(person=person_id) serializer = PersonCompanyHraPlanSerializer(hra_plan, required=False, many=True) return serializer.data def get_fsa_plan(self, user_id): fsa_plan = FSA.objects.filter(user=user_id) serializer = FsaSerializer(fsa_plan, required=False, many=True) return serializer.data def get_hsa_plan(self, person_id): hsa_plan = PersonCompanyGroupHsaPlan.objects.filter(person=person_id) serializer = PersonCompanyGroupHsaPlanSerializer(hsa_plan, required=False, many=True) return serializer.data def get_life_insurance(self, user_id): life_insurance = UserCompanyLifeInsurancePlan.objects.filter( user=user_id) serializer = UserCompanyLifeInsuranceSerializer(life_insurance, required=False, many=True) return serializer.data def get_supplimental_life_insurance(self, person_id): supplimental_life = PersonCompSupplLifeInsurancePlan.objects.filter( person=person_id) serializer = PersonCompanySupplementalLifeInsurancePlanSerializer( supplimental_life, required=False, many=True) return serializer.data def get_std_insurance(self, user_id): std_insurance = UserCompanyStdInsurancePlan.objects.filter( user=user_id) serializer = UserCompanyStdInsuranceSerializer(std_insurance, required=False, many=True) return serializer.data def get_ltd_insurance(self, user_id): ltd_insurance = UserCompanyLtdInsurancePlan.objects.filter( user=user_id) serializer = UserCompanyLtdInsuranceSerializer(ltd_insurance, required=False, many=True) return serializer.data def get(self, request, person_id, format=None): person_info = self.get_person_info(person_id) user_id = self.hash_service.decode_key(person_info['user']) health_benefit = self.get_health_benefit_enrollment(user_id) health_benefit_waived = self.get_health_benefit_waive(user_id) hra_plan = self.get_hra_plan(person_id) fsa_plan = self.get_fsa_plan(user_id) hsa_plan = self.get_hsa_plan(person_id) life_insurance = self.get_life_insurance(user_id) supplemental_life = self.get_supplimental_life_insurance(person_id) std_insurance = self.get_std_insurance(user_id) ltd_insurance = self.get_ltd_insurance(user_id) response = { "person": person_info, "health_benefit_enrolled": health_benefit, "health_benefit_waived": health_benefit_waived, "hra": hra_plan, "fsa": fsa_plan, "hsa": hsa_plan, "basic_life": life_insurance, "supplemental_life": supplemental_life, "std": std_insurance, "ltd": ltd_insurance } return Response(response)