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



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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/oddb2xml/extractor.rb', line 37

def to_hash
  data = {}
  result = PreparationsEntry.parse(@xml.sub(Strip_For_Sax_Machine, ''), :lazy => true)
  result.Preparations.Preparation.each do |seq|
    if seq.SwissmedicNo5.eql?('0')
      puts "BagXmlExtractor Skipping SwissmedicNo5 0 for #{seq.NameDe} #{seq.DescriptionDe} #{seq.CommentDe}"
      next
    end
    item = {}
    item[:data_origin]  = 'bag_xml'
    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|
      if pac.SwissmedicNo8 && pac.SwissmedicNo8.length < 8
        puts "BagXmlExtractor: Adding leading zeros for SwissmedicNo8 #{pac.SwissmedicNo8}  BagDossierNo #{pac.BagDossierNo} PackId #{pac.PackId} #{item[:name_de]}" if $VERBOSE
        pac.SwissmedicNo8  = pac.SwissmedicNo8.rjust(8, '0')
      end
      unless pac.GTIN
        unless pac.SwissmedicNo8
          puts "BagXmlExtractor: Missing GTIN and SwissmedicNo8 in SwissmedicNo8 #{pac.SwissmedicNo8}  BagDossierNo #{pac.BagDossierNo} PackId #{pac.PackId} #{item[:name_de]}"
          next
        else
          ean12 = '7680' + pac.SwissmedicNo8
          pac.GTIN  = (ean12 + Oddb2xml.calc_checksum(ean12)) unless @artikelstamm
          # puts "BagXmlExtractor: Missing GTIN in SwissmedicNo8 #{pac.SwissmedicNo8}  BagDossierNo #{pac.BagDossierNo} PackId #{pac.PackId} #{item[:name_de]}."
        end
      end
      ean13 = pac.GTIN.to_s
      Oddb2xml.setEan13forNo8(pac.SwissmedicNo8, ean13) if pac.SwissmedicNo8
      # 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][ean13] = {
        :ean13               => ean13,
        :name_de             => (desc = seq.NameDe) ? desc : '',
        :name_fr             => (desc = seq.NameFr) ? desc : '',
        :desc_de             => (desc = pac.DescriptionDe) ? desc : '',
        :desc_fr             => (desc = pac.DescriptionFr) ? desc : '',
        :sl_entry            => true,
        :swissmedic_category => (cat = pac.SwissmedicCategory) ? cat : '',
        :swissmedic_number8  => (num = pac.SwissmedicNo8)      ? num : '',
        :prices              => { :exf_price => exf, :pub_price => pub },
      }
      # related all limitations
      item[:packages][ean13][: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][ean13][key].to_s
        end
        if id.empty? && item[:packages][ean13][ :swissmedic_number8]
          key = :swissmedic_number8
          id  = item[:packages][ean13][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][ean13][:limitations] << limitation
        end if lims
      end
      # limitation points
      pts = pac.PointLimitations.PointLimitation.first # only first points
      item[:packages][ean13][:limitation_points] = pts ? pts.Points : ''
      if pac.SwissmedicNo8
          ean12 = '7680' + pac.SwissmedicNo8
          correct_ean13 = ean12+ Oddb2xml.calc_checksum(ean12)
          unless pac.GTIN.eql?(correct_ean13)
            puts "pac.GTIN #{pac.GTIN} should be #{correct_ean13}"
            item[:packages][ean13][:CORRECT_EAN13] = correct_ean13
          end
      end
      data[ean13] = item
    end
  end
  data
end