class UpdateEmployerJob < ApplicationJob queue_as :default def perform(pl_plan_key, plan_header = {}, full_sync = false) unless plan_header.present? sql_query = "SELECT PLPlanKey, PlanId, ShortDesc FROM PLPlanHeader WHERE PLPlanKey = #{pl_plan_key}" plan_header = VhcsRecord.connection.select_all(sql_query).first end puts "== Updating #{plan_header['ShortDesc'].strip.titleize} ==" plan_code = Vhcs::HlPlanCode.find_by(plan_key: plan_header['PLPlanKey']) pb_company_plan = Vhcs::PbCompanyPlans.find_by(pl_plan_key: plan_header['PLPlanKey']) employer = Employer.find_or_create_by!(pl_plan_key: plan_header['PLPlanKey']) full_sync = employer.previously_new_record? || full_sync # id_card_setup = employer.id_card_setup || employer.create_id_card_setup(pl_plan_key: employer.pl_plan_key) # id_card_setup = employer.id_card_setup.find_or_create_by(pl_plan_key: employer.pl_plan_key) employer_update_attrs = {} setup_update_attrs = {} employer_update_attrs.merge!({ plan_id: plan_header['PlanId'].strip.to_i, company_pb_entity_key: pb_company_plan.company_pb_entity_key }) if plan_code.present? employer_update_attrs.merge!({ group_number: plan_code.group_number, effective_date: plan_code.effect_date.strftime("%m/%d/%Y") }) setup_update_attrs.merge!({ rx_group_number: plan_code.medical_number }) end # id_card_setup = employer.id_card_setup.find_or_create_by(pl_plan_key: employer.pl_plan_key) # employer.name = plan_header['ShortDesc'].strip.titleize # employer.plan_id = plan_header['PlanId'].strip.to_i # id_card_templates = determine_id_card_templates(employer.pl_plan_key) # employer.single_card_template = id_card_templates[:single_card_template] # employer.multiple_card_template = id_card_templates[:multiple_card_template] if full_sync employer_update_attrs.merge!({ name: plan_header['ShortDesc'].strip.titleize }) setup_update_attrs.merge!({ print_name: determine_card_print_name(pb_company_plan.company_pb_entity_key, @pl_plan_key) }) end employer.update(employer_update_attrs) # if full_sync # setup_update_attrs.merge!{ # print_name: card_print_name # } # end id_card_setup = employer.id_card_setup || employer.create_id_card_setup!(pl_plan_key: employer.pl_plan_key) id_card_setup.update(setup_update_attrs) # plan_code = Vhcs::HlPlanCode.find_by(plan_key: employer.pl_plan_key) # employer.group_number = plan_code.group_number # id_card_setup.rx_group_number = plan_code.medical_number # employer.effective_date = plan_code.effect_date.strftime("%m/%d/%Y") # pb_company_plan = Vhcs::PbCompanyPlans.find_by(pl_plan_key: employer.pl_plan_key) # employer.company_pb_entity_key = pb_company_plan.company_pb_entity_key # card_print_name = Vhcs::PbEntity.where(company_pb_entity_key: employer.company_pb_entity_key, entity_type_id: 1007).last.last_name # id_card_setup.print_name = card_print_name # if employer.up_to_date? # employer.active = true # end # employer.default_network_logo = determine_network_logos(employer.pl_plan_key) # plan_codes = Vhcs::HlEgglestonCardBenefit.where(plan_key: employer.pl_plan_key).pluck(:plan_id).uniq employer_plans = employer.id_card_setup.plans plan_titles = employer_plans.pluck(:title) vhcs_plans = Vhcs::PbProduct.where(company_pb_entity_key: pb_company_plan.company_pb_entity_key, is_active: 255) vhcs_plans.each do |vp| if employer_plans.present? && plan_titles.all?(&:present?) && employer_plans.find_by(pb_product_key: vp.pb_product_key).nil? plan_title_matcher = Amatch::JaroWinkler.new(vp.short_description) closest_title = plan_titles.max_by { |title| plan_title_matcher.match(title) } plan = employer_plans.find_or_create_by(title: closest_title) plan.update( title: vp.short_description, pb_product_key: vp.pb_product_key, pl_plan_key: employer.pl_plan_key ) plan_titles.delete(closest_title) elsif employer_plans.blank? employer_plans.create!( pb_product_key: vp.pb_product_key, title: vp.short_description, pl_plan_key: employer.pl_plan_key ) # plan.update( # title: vp.short_description, # pl_plan_key: employer.pl_plan_key # ) end end employer end private def determine_card_print_name(company_pb_entity_key, pl_plan_key) card_print_names = Vhcs::PbEntity.where(company_pb_entity_key: company_pb_entity_key, entity_type_id: 1007) .where.not("LastName LIKE ? OR LastName LIKE ? OR LastName LIKE ?", "%COBRA%", "Active", ".%").pluck(:last_name) if pl_plan_key == "2" "sm ART" elsif card_print_names.count > 2 Employer.employer_trim_name(card_print_names.first) else card_print_names.last end end end