2024-01-17 01:21:20 -06:00
|
|
|
class EmployersController < ApplicationController
|
2024-01-19 18:25:59 -06:00
|
|
|
before_action :set_employer, only: [:show, :edit, :update, :destroy]
|
|
|
|
|
|
|
|
# GET /employers
|
|
|
|
def index
|
2024-01-30 00:04:20 -06:00
|
|
|
@employers = Employer.order(:last_name).page(params[:page]).per(5) # Adjust the number per page as needed
|
2024-01-19 18:25:59 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /employers/:id
|
|
|
|
def show
|
2024-02-07 05:13:12 -06:00
|
|
|
Rails.logger.debug "Employer ID: #{params[:id]}"
|
|
|
|
@employer = Employer.includes(employer_records: :participant).find(params[:id])
|
|
|
|
Rails.logger.debug "Employer: #{@employer.inspect}"
|
2024-01-19 18:25:59 -06:00
|
|
|
end
|
2024-02-07 05:13:12 -06:00
|
|
|
|
2024-02-02 23:12:36 -06:00
|
|
|
|
2024-01-19 18:25:59 -06:00
|
|
|
def new
|
|
|
|
@employer = Employer.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@employer = Employer.new(employer_params)
|
|
|
|
if @employer.save
|
2024-04-02 18:19:58 -05:00
|
|
|
assign_role_specific_forms_to(@employer)
|
2024-01-19 18:25:59 -06:00
|
|
|
redirect_to @employer, notice: 'Employer was successfully created.'
|
|
|
|
else
|
|
|
|
render :new
|
2024-01-17 01:21:20 -06:00
|
|
|
end
|
2024-01-19 18:25:59 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @employer.update(employer_params)
|
|
|
|
redirect_to @employer, notice: 'Employer was successfully updated.'
|
|
|
|
else
|
|
|
|
render :edit
|
2024-01-17 01:21:20 -06:00
|
|
|
end
|
2024-01-19 18:25:59 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2024-02-07 05:13:12 -06:00
|
|
|
@employer = Employer.find_by(id: params[:id])
|
|
|
|
if @employer
|
|
|
|
@employer.destroy
|
|
|
|
redirect_to employers_url, notice: 'Employer was successfully deleted.'
|
2024-02-06 00:24:38 -06:00
|
|
|
else
|
|
|
|
redirect_to employers_url, alert: 'Employer record not found.'
|
|
|
|
end
|
2024-01-19 18:25:59 -06:00
|
|
|
end
|
2024-02-06 00:24:38 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
def search
|
|
|
|
if params[:term].present?
|
2024-01-30 16:34:18 -06:00
|
|
|
@employers = Employer.where(
|
|
|
|
"first_name LIKE :term OR last_name LIKE :term",
|
|
|
|
term: "%#{params[:term]}%"
|
|
|
|
)
|
2024-01-19 23:38:15 -06:00
|
|
|
else
|
|
|
|
@employers = Employer.none
|
|
|
|
end
|
2024-01-30 16:34:18 -06:00
|
|
|
|
|
|
|
render json: @employers.map { |e| { label: e.full_name, value: e.id } }
|
2024-01-19 23:38:15 -06:00
|
|
|
end
|
2024-02-05 16:48:36 -06:00
|
|
|
|
|
|
|
def link_employer
|
|
|
|
@worker = Worker.find(params[:id])
|
|
|
|
employer_record = @worker.employer_records.new(employer_record_params)
|
|
|
|
# Handle the logic for creating the employer_record
|
|
|
|
# ...
|
|
|
|
end
|
2024-01-19 23:38:15 -06:00
|
|
|
|
2024-02-05 18:49:36 -06:00
|
|
|
def link_worker
|
|
|
|
@employer = Employer.find(params[:id])
|
|
|
|
participant_id = params[:employer_record][:participant_id] # Assuming this is passed from the form
|
|
|
|
start_date = params[:employer_record][:start_date]
|
|
|
|
end_date = params[:employer_record][:end_date]
|
|
|
|
|
|
|
|
employer_record = EmployerRecord.new(
|
|
|
|
employer: @employer,
|
|
|
|
participant_id: participant_id,
|
|
|
|
start_date: start_date,
|
|
|
|
end_date: end_date
|
|
|
|
)
|
|
|
|
|
|
|
|
if employer_record.save
|
|
|
|
redirect_to @employer, notice: 'Worker was successfully linked.'
|
|
|
|
else
|
|
|
|
flash.now[:alert] = employer_record.errors.full_messages.to_sentence
|
|
|
|
render :show
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
# Handle case where Employer is not found
|
|
|
|
redirect_to some_path, alert: 'Employer not found.'
|
|
|
|
end
|
2024-02-17 02:42:12 -06:00
|
|
|
|
|
|
|
def onboarding
|
|
|
|
@employer = employer.find(params[:id])
|
|
|
|
@onboarding_items = @employer.onboarding_items.includes(:form)
|
|
|
|
end
|
|
|
|
|
2024-02-05 18:49:36 -06:00
|
|
|
|
2024-01-19 18:25:59 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_employer
|
2024-01-17 01:21:20 -06:00
|
|
|
@employer = Employer.find(params[:id])
|
|
|
|
end
|
2024-01-19 18:25:59 -06:00
|
|
|
|
|
|
|
def employer_params
|
2024-01-30 16:34:18 -06:00
|
|
|
params.require(:employer).permit(
|
|
|
|
:first_name, :last_name,
|
|
|
|
:address_line_1, :address_line_2,
|
|
|
|
:city, :state, :zip,
|
|
|
|
:phone, :email, :tin, :dob, :ssn, :gender
|
|
|
|
)
|
2024-01-19 18:25:59 -06:00
|
|
|
end
|
2024-02-05 16:48:36 -06:00
|
|
|
|
|
|
|
def employer_record_params
|
|
|
|
params.require(:employer_record).permit(:employer_id, :start_date, :end_date)
|
|
|
|
end
|
2024-01-30 16:34:18 -06:00
|
|
|
|
2024-04-02 18:19:58 -05:00
|
|
|
def assign_role_specific_forms_to(employer)
|
|
|
|
employer_role = FormRole.find_by(name: 'Employer')
|
|
|
|
|
|
|
|
# Fetch forms associated with the "Employer" role
|
|
|
|
forms_for_employer = employer_role.forms
|
|
|
|
|
|
|
|
forms_for_employer.each do |form|
|
|
|
|
# Create an onboarding item for each form for the newly created employer
|
|
|
|
employer.onboarding_items.create(form: form)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-19 18:25:59 -06:00
|
|
|
end
|