Class: Oddb2xml::BagXmlExtractor

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

Instance Attribute Summary

Attributes inherited from Extractor

#xml

Instance Method Summary collapse

Methods inherited from Extractor

#initialize

Constructor Details

This class inherits a constructor from Oddb2xml::Extractor

Instance Method Details

#to_hashObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/oddb2xml/extractor.rb', line 35

def to_hash
  data = {}
  result = PreparationsEntry.parse(@xml.sub(Strip_For_Sax_Machine, ''), :lazy => true)
  result.Preparations.Preparation.each do |seq|
    item = {}
    item[:refdata]      = true
    item[:product_key]  = seq.ProductCommercial
    item[:desc_de]      = (desc = seq.DescriptionDe) ? desc : ''
    item[:desc_fr]      = (desc = seq.DescriptionFr) ? desc : ''
    item[:name_de]      = (name = seq.NameDe)        ? name : ''
    item[:name_fr]      = (name = seq.NameFr)        ? name : ''
    item[:swissmedic_number5] = (num5 = seq.SwissmedicNo5) ? (num5.rjust(5,'0')) : ''
    item[:org_gen_code] = (orgc = seq.OrgGenCode)    ? orgc : ''
    item[:deductible]   = (ddbl = seq.FlagSB20)      ? ddbl : ''
    item[:atc_code]     = (atcc = seq.AtcCode)       ? atcc : ''
    item[:comment_de]   = (info = seq.CommentDe)     ? info : ''
    item[:comment_fr]   = (info = seq.CommentFr)     ? info : ''
    item[:it_code]      = ''
    seq.ItCodes.ItCode.each do |itc|
      if item[:it_code].to_s.empty?
        it_code = itc.Code.to_s
        item[:it_code] = (it_code =~ /(\d+)\.(\d+)\.(\d+)./) ? it_code : ''
      end
    end
    item[:substances] = []
    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
    item[:pharmacodes] = []
    item[:packages]    = {} # pharmacode => package
    seq.Packs.Pack.each do |pac|
      unless pac.GTIN
        puts "BagXmlExtractor: Skipping as missing GTIN in SwissmedicNo8 #{pac.SwissmedicNo8}  BagDossierNo #{pac.BagDossierNo} PackId #{pac.PackId}. Skipping"
        next
      end
      ean = pac.GTIN.to_i
      # packages
      exf = {:price => '', :valid_date => '', :price_code => ''}
      if pac.Prices and pac.Prices.ExFactoryPrice
        exf[:price]      =  pac.Prices.ExFactoryPrice.Price         if pac.Prices.ExFactoryPrice.Price
        exf[:valid_date] =  pac.Prices.ExFactoryPrice.ValidFromDate if pac.Prices.ExFactoryPrice.ValidFromDate
        exf[:price_code] =  pac.Prices.ExFactoryPrice.PriceTypeCode if pac.Prices.ExFactoryPrice.PriceTypeCode
      end
      pub = {:price => '', :valid_date => '', :price_code => ''}
      if pac.Prices and pac.Prices.PublicPrice
        pub[:price]      =  pac.Prices.PublicPrice.Price         if pac.Prices.PublicPrice.Price
        pub[:valid_date] =  pac.Prices.PublicPrice.ValidFromDate if pac.Prices.PublicPrice.ValidFromDate
        pub[:price_code] =  pac.Prices.PublicPrice.PriceTypeCode if pac.Prices.PublicPrice.PriceTypeCode
      end
      item[:packages][ean] = {
        :ean                 => ean,
        :swissmedic_category => (cat = pac.SwissmedicCategory) ? cat : '',
        :swissmedic_number8  => (num = pac.SwissmedicNo8)      ? num.rjust(8, '0') : '',
        :prices              => { :exf_price => exf, :pub_price => pub },
      }
      # related all limitations
      item[:packages][ean][:limitations] = []
      limitations = Hash.new{|h,k| h[k] = [] }
      if seq.Limitations
        limitations[:seq] = seq.Limitations.Limitation.collect { |x| x }
      else
        limitations[:seq] = nil
      end
      # in it-codes
      if seq and seq.ItCodes and seq.ItCodes.ItCode
        limitations[:itc] = []
        seq.ItCodes.ItCode.each { |x|  limitations[:itc] += x.Limitations.Limitation if x.Limitations.Limitation}
      else
        limitations[:itc] =nil
      end
      # in pac
      if pac and pac.Limitations
        limitations[:pac] = (lims = pac.Limitations.Limitation) ? lims.to_a : nil
      else
        limitations[:pac] = nil
      end
      limitations.each_pair do |lim_key, lims|
        key = ''
        id  = ''
        case lim_key
        when :seq, :itc
          key = :swissmedic_number5
          id  = item[key].to_s
        when :pac
          key = :swissmedic_number8
          id  = item[:packages][ean][key].to_s
        end
        lims.each do |lim|
          limitation = {
            :it      => item[:it_code],
            :key     => key,
            :id      => id,
            :code    => (lic = lim.LimitationCode)   ? lic : '',
            :type    => (lit = lim.LimitationType)   ? lit : '',
            :value   => (liv = lim.LimitationValue)  ? liv : '',
            :niv     => (niv = lim.LimitationNiveau) ? niv : '',
            :desc_de => (dsc = lim.DescriptionDe)    ? dsc : '',
            :desc_fr => (dsc = lim.DescriptionFr)    ? dsc : '',
            :vdate   => (dat = lim.ValidFromDate)    ? dat : '',
          }
          deleted = false
          if upto = ((thr = lim.ValidThruDate) ? thr : nil) and
              upto =~ /\d{2}\.\d{2}\.\d{2}/
            begin
              deleted = true if Date.strptime(upto, '%d.%m.%y') >= Date.today
            rescue ArgumentError
            end
          end
          limitation[:del] = deleted
          item[:packages][ean][:limitations] << limitation
        end if lims
      end
      # limitation points
      pts = pac.PointLimitations.PointLimitation.first # only first points
      item[:packages][ean][:limitation_points] = pts ? pts.Points : ''
      data[ean] = item
    end
  end
  data
end