Class: Oddb2xml::FhirExtractor

Inherits:
Extractor show all
Defined in:
lib/oddb2xml/fhir_support.rb

Overview

FHIR Extractor - Compatible with existing BagXmlExtractor

Instance Attribute Summary

Attributes inherited from Extractor

#xml

Instance Method Summary collapse

Constructor Details

#initialize(fhir_files) ⇒ FhirExtractor

Accepts either a single NDJSON file path (back-compat) or a Hash { “de” => path, “fr” => path, “it” => path } of per-language files.



674
675
676
677
678
679
680
681
682
# File 'lib/oddb2xml/fhir_support.rb', line 674

def initialize(fhir_files)
  if fhir_files.is_a?(Hash)
    @fhir_files = fhir_files
    @fhir_file = fhir_files["de"] || fhir_files.values.first
  else
    @fhir_files = {"de" => fhir_files}
    @fhir_file = fhir_files
  end
end

Instance Method Details

#to_hashObject



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/oddb2xml/fhir_support.rb', line 684

def to_hash
  data = {}
  Oddb2xml.log "FhirExtractor: Parsing FHIR file #{@fhir_file}"

  # Parse FHIR NDJSON
  result = FhirPreparationsEntry.parse(@fhir_file)

  result.Preparations.Preparation.each do |seq|
    next unless seq
    next if seq.SwissmedicNo5 && seq.SwissmedicNo5.eql?("0")

    # Build item structure matching BagXmlExtractor
    item = {}
    item[:data_origin] = "fhir"
    item[:refdata] = true
    item[:product_key] = nil  # Not available in FHIR
    item[:desc_de] = ""  # Not in FHIR at product level
    item[:desc_fr] = ""
    item[:desc_it] = ""
    item[:name_de] = (name = seq.NameDe) ? name : ""
    item[:name_fr] = (name = seq.NameFr) ? name : ""
    item[:name_it] = (name = seq.NameIt) ? name : ""
    item[:swissmedic_number5] = (num5 = seq.SwissmedicNo5) ? num5.to_s.rjust(5, "0") : ""
    item[:org_gen_code] = (orgc = seq.OrgGenCode) ? orgc : ""
    item[:deductible] = ""  # Will be set per package based on cost_share
    item[:deductible20] = ""  # Will be set per package based on cost_share  
    item[:atc_code] = (atcc = seq.AtcCode) ? atcc : ""
    item[:comment_de] = ""  # Not available in FHIR
    item[:comment_fr] = ""
    item[:comment_it] = ""
    item[:it_code] = (itc = seq.ItCode) ? itc : ""  # NOW available in FHIR!

    # Indikationscodes (BAG XXXXX.NN, see issue #113). Each entry is a
    # Hash with :code, :cud_id, :text — mandatory on rx/invoices from
    # 2026-07-01.
    item[:indication_codes] = Array(seq.IndicationCodes).map do |ic|
      {code: ic.code, cud_id: ic.cud_id, text: ic.text}
    end

    # Build substances array
    item[:substances] = []
    if seq.Substances && seq.Substances.Substance
      seq.Substances.Substance.each_with_index do |sub, i|
        item[:substances] << {
          index: i.to_s,
          name: (name = sub.DescriptionLa) ? name : "",
          quantity: (qtty = sub.Quantity) ? qtty : "",
          unit: (unit = sub.QuantityUnit) ? unit : ""
        }
      end
    end

    item[:pharmacodes] = []
    item[:packages] = {}

    # Process packages
    if seq.Packs && seq.Packs.Pack
      seq.Packs.Pack.each do |pac|
        next unless pac.GTIN

        ean13 = pac.GTIN.to_s

        # Ensure SwissmedicNo8 has leading zeros
        if pac.SwissmedicNo8 && pac.SwissmedicNo8.length < 8
          pac.SwissmedicNo8 = pac.SwissmedicNo8.rjust(8, "0")
        end

        Oddb2xml.setEan13forNo8(pac.SwissmedicNo8, ean13) if pac.SwissmedicNo8

        # Build price structures
        exf = {price: "", valid_date: "", price_code: ""}
        if pac.Prices && pac.Prices.ExFactoryPrice
          exf[:price] = pac.Prices.ExFactoryPrice.Price.to_s if pac.Prices.ExFactoryPrice.Price
          exf[:valid_date] = pac.Prices.ExFactoryPrice.ValidFromDate if pac.Prices.ExFactoryPrice.ValidFromDate
          exf[:price_code] = "PEXF"
        end

        pub = {price: "", valid_date: "", price_code: ""}
        if pac.Prices && pac.Prices.PublicPrice
          pub[:price] = pac.Prices.PublicPrice.Price.to_s if pac.Prices.PublicPrice.Price
          pub[:valid_date] = pac.Prices.PublicPrice.ValidFromDate if pac.Prices.PublicPrice.ValidFromDate
          pub[:price_code] = "PPUB"
        end

        # Build package entry matching BagXmlExtractor structure
        item[:packages][ean13] = {
          ean13: ean13,
          name_de: (name = seq.NameDe) ? name : "",
          name_fr: (name = seq.NameFr) ? name : "",
          name_it: (name = seq.NameIt) ? name : "",
          desc_de: (desc = pac.DescriptionDe) ? desc : "",
          desc_fr: (desc = pac.DescriptionFr) ? desc : "",
          desc_it: (desc = pac.DescriptionIt) ? desc : "",
          sl_entry: true,
          swissmedic_category: (cat = pac.SwissmedicCategory) ? cat : "",
          swissmedic_number8: (num = pac.SwissmedicNo8) ? num : "",
          prices: {exf_price: exf, pub_price: pub},
          indication_codes: item[:indication_codes]
        }

        # Map limitations from FHIR
        item[:packages][ean13][:limitations] = []
        if pac.Limitations && pac.Limitations.Limitation
          pac.Limitations.Limitation.each do |lim|
            # Calculate is_deleted safely
            is_deleted = false
            if lim.ValidThruDate
              begin
                is_deleted = Date.parse(lim.ValidThruDate) < Date.today
              rescue
                is_deleted = false
              end
            end
            
            item[:packages][ean13][:limitations] << {
              it: item[:it_code],
              key: :swissmedic_number8,
              id: pac.SwissmedicNo8 || "",
              code: lim.LimitationCode || "",
              type: lim.LimitationType || "",
              value: lim.LimitationValue || "",
              niv: lim.LimitationNiveau || "",
              desc_de: lim.DescriptionDe || "",
              desc_fr: lim.DescriptionFr || "",
              desc_it: lim.DescriptionIt || "",
              cud_ref: lim.LimitationCudRef,
              vdate: lim.ValidFromDate || "",
              del: is_deleted
            }
          end
        end
        item[:packages][ean13][:limitation_points] = ""
        
        # Map cost_share to deductible flags
        if pac.CostShare
          case pac.CostShare
          when 10
            item[:deductible] = "Y"
          when 20
            item[:deductible20] = "Y"
          when 40
            # New value - might need new field or special handling
            item[:deductible] = "Y"  # Fallback to standard deductible
          end
        end

        # Store in data hash with ean13 as key
        data[ean13] = item
      end
    end
  end

  # Merge names/descriptions from additional language files
  @fhir_files.each do |lang, file|
    next if file == @fhir_file
    next unless file && File.exist?(file)
    merge_language(data, file, lang)
  end

  Oddb2xml.log "FhirExtractor: Extracted #{data.size} packages"
  data
end