<commit_before>module Parsers
  module Edi
    class TransmissionFile
      attr_reader :result
      attr_accessor :transmission_kind
      
      def initialize(path, t_kind, r_data, blist = [], i_cache, pb)
        @raw = r_data
        @progress_bar = pb
        @result = Oj.load(r_data)
        @progress_bar.refresh
        @file_name = File.basename(path)
        @transmission_kind = t_kind
        @inbound = (t_kind == "effectuation")
        @bgn_blacklist = blist
        @import_cache = i_cache
      end

      def transaction_set_kind(etf)
        if(@transmission_kind == "effectuation" && etf.cancellation_or_termination?)
          return "maintenance"
        end
        @transmission_kind
      end

      def persist_edi_transactions(
        l834,
        policy_id,
        carrier_id,
        employer_id,
        edi_transmission,
        error_list = [])
        st = l834["ST"]
        bgn = l834["BGN"]
        fs = FileString.new(bgn[2] + "_" + @file_name, l834["RAW_CONTENT"])
        etf = Etf::EtfLoop.new(l834)
        @progress_bar.refresh
        Protocols::X12::TransactionSetEnrollment.create!(
          :st01 => st[1],
          :st02 => st[2],
          :st03 => st[3],
          :bgn01 => bgn[1],
          :bgn02 => bgn[2],
          :bgn03 => bgn[3],
          :bgn04 => bgn[4],
          :bgn05 => bgn[5],
          :bgn06 => bgn[6],
          :bgn08 => bgn[8],
          :carrier_id => carrier_id,
          :receiver_id => edi_transmission.isa08,
          :employer_id => employer_id,
          :policy_id => policy_id,
          :error_list => error_list,
          :transmission => edi_transmission,
          :transaction_kind => transaction_set_kind(etf),
          :body => fs
        )
      end

      def parse_edi_transmission(top_doc)
        isa = top_doc["ISA"]
        gs = top_doc["GS"]
        sender_id = isa[6].strip
        @carrier = @import_cache.lookup_carrier_fein(sender_id)
        Protocols::X12::Transmission.create!({
          :isa06 => sender_id,
          :isa08 => isa[8].strip,
          :isa09 => isa[9].strip,
          :isa10 => isa[10].strip,
          :isa12 => isa[12].strip,
          :isa13 => isa[13].strip,
          :isa15 => isa[15].strip,
          :gs01 => gs[1],
          :gs02 => gs[2],
          :gs03 => gs[3],
          :gs04 => gs[4],
          :gs05 => gs[5],
          :gs06 => gs[6],
          :gs07 => gs[7],
          :gs08 => gs[8],
          :file_name => @file_name
        })
      end

      def persist_834(etf_loop, edi_transmission)
        etf = Etf::EtfLoop.new(etf_loop)

        # Carrier
        carrier = @import_cache.lookup_carrier_fein(etf.carrier_fein)
        carrier ||= @carrier

        # Employer
        employer = nil
        if(etf.is_shop?)
          employer_loop = Etf::EmployerLoop.new(etf.employer_loop)
          employer = persist_employer(employer_loop, carrier._id)

          if(employer.nil?)
            raise("Unknown employer ID: #{employer_loop.id_qualifier} #{employer_loop.fein}")
          end
        end
        employer_id = Maybe.new(employer)._id.value

        #Policy
        policy_loop = etf.subscriber_loop.policy_loops.first
        plan_year = nil
        plan = nil
        coverage_start = Maybe.new(policy_loop.coverage_start).fmap { |cs| Date.parse(cs) }.value
        if !coverage_start.nil?
          if(etf.is_shop?)
            plan_year = PlanYear.where({
              :employer_id => employer.id,
              :start_date => { "$lte" => coverage_start }
            }).order_by(&:start_date).last.start_date.year
          else
            plan_year = coverage_start.year
          end
          plan = @import_cache.lookup_plan(policy_loop.hios_id, plan_year)
        else
          eg_id = policy_loop.eg_id
          hios = policy_loop.hios_id
          plan = Maybe.new(Policy.find_for_group_and_hios(eg_id, hios)).plan.value
        end

        policy = nil

        if etf.is_shop? && is_carrier_maintenance?(etf, edi_transmission)
          policy = Policy.find_by_subkeys(policy_loop.eg_id, carrier._id, policy_loop.hios_id)

          if policy
            edi_transmission.save!
          end
        else
          responsible_party_id = persist_responsible_party_get_id(etf_loop)
          broker_id = persist_broker_get_id(etf_loop)

          if(plan)
            policy = persist_policy(etf, carrier._id, plan._id, policy_loop.eg_id, employer_id, responsible_party_id, broker_id)

            if policy
              edi_transmission.save!

              persist_people(etf_loop, employer_id)
              #              Etf::ApplicationGroupParser.new(etf.people).persist!
            end
          end
        end

        #persist transaction
        policy_id = (policy.nil?) ? nil : policy._id
        persist_edi_transactions(etf_loop, policy_id, carrier._id, employer_id, edi_transmission)
      end

      # FIXME: pull sep reason
      def persist_policy(etf, carrier_id, plan_id, eg_id, employer_id, rp_id, broker_id)
        reporting_categories = etf.subscriber_loop.reporting_catergories

        new_policy = Policy.new(
          :plan_id => plan_id,
          :enrollment_group_id => eg_id,
          :carrier_id => carrier_id,
          :tot_res_amt => reporting_categories.tot_res_amt,
          :pre_amt_tot => reporting_categories.pre_amt_tot,
          :applied_aptc => reporting_categories.applied_aptc,
          :tot_emp_res_amt => reporting_categories.tot_emp_res_amt,
          :carrier_to_bill => reporting_categories.carrier_to_bill?,
          :employer_id => employer_id,
          :broker_id => broker_id,
          :responsible_party_id => rp_id,
          :enrollees => []
        )
        policy = Policy.find_or_update_policy(new_policy)
        if transaction_set_kind(etf) == "effectuation"
          policy.aasm_state = 'effectuated'
        end

        etf.people.each do |person_loop|
          policy_loop = person_loop.policy_loops.first
          enrollee = build_enrollee(person_loop, policy_loop)
          policy.merge_enrollee(enrollee, policy_loop.action)
        end
        policy.unsafe_save!

        policy
      end
<commit_msg>Trying to figure out why cancels aren't cancelling.<commit_after>module Parsers
  module Edi
    class TransmissionFile
      attr_reader :result
      attr_accessor :transmission_kind
      
      def initialize(path, t_kind, r_data, blist = [], i_cache, pb)
        @raw = r_data
        @progress_bar = pb
        @result = Oj.load(r_data)
        @progress_bar.refresh
        @file_name = File.basename(path)
        @transmission_kind = t_kind
        @inbound = (t_kind == "effectuation")
        @bgn_blacklist = blist
        @import_cache = i_cache
      end

      def transaction_set_kind(etf)
        if(@transmission_kind == "effectuation" && etf.cancellation_or_termination?)
          return "maintenance"
        end
        @transmission_kind
      end

      def persist_edi_transactions(
        l834,
        policy_id,
        carrier_id,
        employer_id,
        edi_transmission,
        error_list = [])
        st = l834["ST"]
        bgn = l834["BGN"]
        fs = FileString.new(bgn[2] + "_" + @file_name, l834["RAW_CONTENT"])
        etf = Etf::EtfLoop.new(l834)
        @progress_bar.refresh
        Protocols::X12::TransactionSetEnrollment.create!(
          :st01 => st[1],
          :st02 => st[2],
          :st03 => st[3],
          :bgn01 => bgn[1],
          :bgn02 => bgn[2],
          :bgn03 => bgn[3],
          :bgn04 => bgn[4],
          :bgn05 => bgn[5],
          :bgn06 => bgn[6],
          :bgn08 => bgn[8],
          :carrier_id => carrier_id,
          :receiver_id => edi_transmission.isa08,
          :employer_id => employer_id,
          :policy_id => policy_id,
          :error_list => error_list,
          :transmission => edi_transmission,
          :transaction_kind => transaction_set_kind(etf),
          :body => fs
        )
      end

      def parse_edi_transmission(top_doc)
        isa = top_doc["ISA"]
        gs = top_doc["GS"]
        sender_id = isa[6].strip
        @carrier = @import_cache.lookup_carrier_fein(sender_id)
        Protocols::X12::Transmission.create!({
          :isa06 => sender_id,
          :isa08 => isa[8].strip,
          :isa09 => isa[9].strip,
          :isa10 => isa[10].strip,
          :isa12 => isa[12].strip,
          :isa13 => isa[13].strip,
          :isa15 => isa[15].strip,
          :gs01 => gs[1],
          :gs02 => gs[2],
          :gs03 => gs[3],
          :gs04 => gs[4],
          :gs05 => gs[5],
          :gs06 => gs[6],
          :gs07 => gs[7],
          :gs08 => gs[8],
          :file_name => @file_name
        })
      end

      def persist_834(etf_loop, edi_transmission)
        etf = Etf::EtfLoop.new(etf_loop)

        # Carrier
        carrier = @import_cache.lookup_carrier_fein(etf.carrier_fein)
        carrier ||= @carrier

        # Employer
        employer = nil
        if(etf.is_shop?)
          employer_loop = Etf::EmployerLoop.new(etf.employer_loop)
          employer = persist_employer(employer_loop, carrier._id)

          if(employer.nil?)
            raise("Unknown employer ID: #{employer_loop.id_qualifier} #{employer_loop.fein}")
          end
        end
        employer_id = Maybe.new(employer)._id.value

        #Policy
        policy_loop = etf.subscriber_loop.policy_loops.first
        plan_year = nil
        plan = nil
        coverage_start = Maybe.new(policy_loop.coverage_start).fmap { |cs| Date.parse(cs) }.value
        if !coverage_start.nil?
          if(etf.is_shop?)
            plan_year = PlanYear.where({
              :employer_id => employer.id,
              :start_date => { "$lte" => coverage_start }
            }).order_by(&:start_date).last.start_date.year
          else
            plan_year = coverage_start.year
          end
          plan = @import_cache.lookup_plan(policy_loop.hios_id, plan_year)
        else
          eg_id = policy_loop.eg_id
          hios = policy_loop.hios_id
          plan = Maybe.new(Policy.find_for_group_and_hios(eg_id, hios)).plan.value
        end

        policy = nil

        if etf.is_shop? && is_carrier_maintenance?(etf, edi_transmission)
          policy = Policy.find_by_subkeys(policy_loop.eg_id, carrier._id, policy_loop.hios_id)

          if policy
            edi_transmission.save!
          end
        else
          responsible_party_id = persist_responsible_party_get_id(etf_loop)
          broker_id = persist_broker_get_id(etf_loop)

          if(plan)
            policy = persist_policy(etf, carrier._id, plan._id, policy_loop.eg_id, employer_id, responsible_party_id, broker_id)

            if policy
              edi_transmission.save!

              persist_people(etf_loop, employer_id)
              #              Etf::ApplicationGroupParser.new(etf.people).persist!
            end
          end
        end

        #persist transaction
        policy_id = (policy.nil?) ? nil : policy._id
        persist_edi_transactions(etf_loop, policy_id, carrier._id, employer_id, edi_transmission)
      end

      # FIXME: pull sep reason
      def persist_policy(etf, carrier_id, plan_id, eg_id, employer_id, rp_id, broker_id)
        reporting_categories = etf.subscriber_loop.reporting_catergories

        new_policy = Policy.new(
          :plan_id => plan_id,
          :enrollment_group_id => eg_id,
          :carrier_id => carrier_id,
          :tot_res_amt => reporting_categories.tot_res_amt,
          :pre_amt_tot => reporting_categories.pre_amt_tot,
          :applied_aptc => reporting_categories.applied_aptc,
          :tot_emp_res_amt => reporting_categories.tot_emp_res_amt,
          :carrier_to_bill => reporting_categories.carrier_to_bill?,
          :employer_id => employer_id,
          :broker_id => broker_id,
          :responsible_party_id => rp_id,
          :enrollees => []
        )
        policy = Policy.find_or_update_policy(new_policy)
        if transaction_set_kind(etf) == "effectuation"
          policy.aasm_state = 'effectuated'
        end

        etf.people.each do |person_loop|
          policy_loop = person_loop.policy_loops.first
          enrollee = build_enrollee(person_loop, policy_loop)
          policy.merge_enrollee(enrollee, policy_loop.action)
        end
        policy.save!

        policy
      end

{'fraction_non_alphanumeric': 0.1054671727368166, 'fraction_numerical': 0.016797221997900347, 'mean_word_length': 2.0226995362460336, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3214635', 'n_tokens_mistral': 4216, 'n_tokens_neox': 4050, 'n_words': 657}