class UpdateMemberJob < ApplicationJob queue_as :default def perform(pb_entity_key, employer_id, has_divisions = false, has_dental = false, add_inactive = false, vw_mb_member = {}) unless vw_mb_member.present? vw_mb_member = Vhcs::VwmbMember.find_by(enrollee_type_value_id: 1, pb_entity_key: pb_entity_key).attributes.with_indifferent_access.slice(:mb_member_key, :pb_entity_key, :pl_plan_key, :family_id, :full_name_last_name_first, :social_security_number) end pb_products = Vhcs::PbProduct.joins(' INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductKey" = "PBProduct"."PBProductKey" INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBProductAvailabilityKey" = "PBProductAvailability"."PBProductAvailabilityKey" INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey" ').select(' "PBProduct".*, "PBProductParticipation"."InEffect" AS InEffectDate, "PBProductParticipation"."OutOfEffect" AS OutOfEffectDate ').where(' "PBCoveredEntities"."PBEntityKey" = ? AND "PBProductParticipation"."InEffect" <= ? AND "PBProductParticipation"."OutOfEffect" > ?', vw_mb_member[:pb_entity_key], 3.month.from_now , 1.day.ago ) pb_products, max_in_effect, max_out_effect = get_products_with_max_dates(vw_mb_member[:pb_entity_key]) if add_inactive || (pb_products.exists? && max_out_effect > 1.year.ago) # && vw_mb_member[:social_security_number].present? member = Member.find_or_create_by(pb_entity_key: vw_mb_member[:pb_entity_key], employer_id: employer_id) member.name = vw_mb_member[:full_name_last_name_first].titleize member.mb_member_key = vw_mb_member[:mb_member_key] member.family_id = vw_mb_member[:family_id] member.pl_plan_key = vw_mb_member[:pl_plan_key] card_display_name = Vhcs::PbEntity.find_by(pb_entity_key: member.pb_entity_key).full_name member.id_card_display_name ||= card_display_name if max_in_effect <= 3.months.from_now && max_out_effect > 1.day.ago if has_divisions division = Vhcs::PbEntity.joins(' INNER JOIN "PBAffiliation" ON "PBAffiliation"."ParentPBEntityKey" = "PBEntity"."PBEntityKey" INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBAffiliationKey" = "PBAffiliation"."PBAffiliationKey" INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey" ').find_by(' "PBCoveredEntities"."PBEntityKey" = ? AND "PBProductParticipation"."OutOfEffect" > ?', member.pb_entity_key, 1.day.ago ).last_name member.division = division end if has_dental medical_pb_product_key = pb_products.where("ShortDescription LIKE ?", "%Medical%")&.first&.pb_product_key dental_pb_product_key = pb_products.where("ShortDescription LIKE ?", "%Dental%")&.first&.pb_product_key # dental_pb_product_key = pb_products.where(short_description: "Dental")&.first&.pb_product_key if dental_pb_product_key member.dental_plan_key = dental_pb_product_key coverage_class = Vhcs::GenLookupTables.joins(' INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."CoverageTypeCode" = "GEN_LookupTables"."RecordID" INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey" INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductAvailabilityKey" = "PBProductParticipation"."PBProductAvailabilityKey" INNER JOIN "PBProduct" ON "PBProduct"."PBProductKey" = "PBProductAvailability"."PBProductKey" ').select(' "GEN_LookupTables".*, "PBProductParticipation"."InEffect" AS EffDate ').where(' "PBCoveredEntities"."PBEntityKey" = ? AND "PBProductParticipation"."InEffect" <= ? AND "PBProductParticipation"."OutOfEffect" > ? AND "PBProduct"."PBProductKey" = ?', member.pb_entity_key, 1.month.from_now, 1.day.ago, dental_pb_product_key ).order('EffDate DESC').first.short_desc member.coverage_class = coverage_class else member.coverage_class = "NONE" end else medical_pb_product_key = pb_products.first.pb_product_key end if medical_pb_product_key && plan = IdCard::Plan.find_by(pb_product_key: medical_pb_product_key) member.id_card_plan = plan end member.dependents = get_dependent_names_by_sequence(member) member.active = true else member.active = false end puts "---- #{member.name}" member.save! end member.presence end private def get_products_with_max_dates(pb_entity_key) pb_product_with_dates = Vhcs::PbProduct.joins(' INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductKey" = "PBProduct"."PBProductKey" INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBProductAvailabilityKey" = "PBProductAvailability"."PBProductAvailabilityKey" INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey" ').select(' "PBProduct".*, "PBProductParticipation"."InEffect" AS InEffectDate, "PBProductParticipation"."OutOfEffect" AS OutOfEffectDate ').where(' "PBCoveredEntities"."PBEntityKey" = ?', pb_entity_key ) [pb_product_with_dates, pb_product_with_dates.map { |products| products["InEffectDate"]}.max, pb_product_with_dates.map { |products| products["OutOfEffectDate"]}.max] end def get_dependent_names_by_sequence(member) dependents = Vhcs::VwmbMember.joins(' INNER JOIN "PBCoveredEntities" ON "PBCoveredEntities"."PBEntityKey" = "vwMBMember"."PBEntityKey" INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey" INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductAvailabilityKey" = "PBProductParticipation"."PBProductAvailabilityKey" INNER JOIN "PBProduct" ON "PBProduct"."PBProductKey" = "PBProductAvailability"."PBProductKey" ').where(' "EnrolleeTypeKey" != 1044 AND "PLPlanKey" = ? AND "PBProductParticipation"."OutOfEffect" >= ? AND "FamilyID" = ?', member.pl_plan_key, Date.today.strftime("%m/%d/%Y"), member.family_id ) if member.pl_plan_key == 3 dependents = dependents.where('"PBProduct"."PBProductKey" != 1024') elsif member.pl_plan_key == 2 dependents = dependents.where('"PBProduct"."PBProductKey" != 1019') end dependent_names = dependents.order(:sequence_number).map { |dep| dep.first_name + ' ' + dep.last_name}.uniq dependent_names end end