Class: ODDB::Import::Dimdi::Product

Inherits:
ODDB::Import::DatedExcel show all
Defined in:
lib/oddb/import/dimdi.rb

Instance Method Summary collapse

Methods inherited from Excel

#cell, #import, #import_worksheet, #parse

Methods inherited from Importer

#capitalize_all, #company_name, #utf8

Constructor Details

#initialize(date = Date.today) ⇒ Product

Returns a new instance of Product.



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/oddb/import/dimdi.rb', line 146

def initialize(date = Date.today)
  super
  @count = 0
  @created = 0
  @created_sequences = 0
  @created_substances = 0
  @deleted_sequences = 0
  @deleted_products = 0
  @existing = 0
  @existing_sequences = 0
  @reassigned_pzns = 0
  @renamed_products = 0
end

Instance Method Details

#assign_substance_group(sub, groupname) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/oddb/import/dimdi.rb', line 159

def assign_substance_group(sub, groupname)
  if(sub.group.nil? \
     && (group = import_substance_group(groupname)))
    sub.group = group
    sub.save
  end
end

#create_product(name) ⇒ Object



166
167
168
169
170
# File 'lib/oddb/import/dimdi.rb', line 166

def create_product name
  product = Drugs::Product.new
  product.name.de = name
  product.save
end

#delete_sequence(sequence) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/oddb/import/dimdi.rb', line 171

def delete_sequence(sequence)
  product = sequence.product
  if(product.sequences.size == 1)
    @deleted_products += 1
    product.delete
  else
    @deleted_sequences += 1
    sequence.delete
  end
  sequence
end

#import_atc(row, sequence) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/oddb/import/dimdi.rb', line 182

def import_atc(row, sequence)
  atc_name = cell(row, 10)
  substances = sequence.substances.uniq
  candidates = []
  if(substances.size == 1)
    substance = substances.first
    candidates = Drugs::Atc.search_by_exact_name(substance.to_s)
    if(candidates.empty?)
      candidates = Drugs::Atc.search_by_name(substance.to_s)
    end
  end
  if(candidates.size != 1)
    candidates = Drugs::Atc.search_by_exact_name(atc_name)
  end
  if(candidates.empty?)
    candidates = Drugs::Atc.search_by_name(atc_name)
  end
  if(candidates.size == 1)
    atc = candidates.first
    if(sequence.atc.nil? \
       || (sequence.atc != atc && atc.level >= sequence.atc.level))
      sequence.atc = atc
      sequence.save
    end
  end
end

#import_galenic_form(row) ⇒ Object



339
340
341
342
343
# File 'lib/oddb/import/dimdi.rb', line 339

def import_galenic_form(row)
  Drugs::GalenicForm.find_by_code(:value   => cell(row, 6),
                                  :type    => "galenic_form",
                                  :country => 'DE')
end

#import_package(row, sequence, unitname) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/oddb/import/dimdi.rb', line 237

def import_package(row, sequence, unitname)
  ## we don't expect any multipart packages here
  psize = cell(row, 2)
  package = Drugs::Package.new
  pzn = u(cell(row, 0).to_i.to_s)
  package.add_code(Util::Code.new(:cid, pzn, 'DE', @package_date))
  part = Drugs::Part.new
  part.size = psize
  if(unitname)
    unit = Drugs::Unit.find_by_name(unitname)
    if(unit.nil?)
      unit = Drugs::Unit.new
      unit.name.de = unitname
      unit.save
    end
    part.unit = unit
  end
  part.composition = sequence.compositions.first
  part.package = package
  part.save
  package.sequence = sequence
  package.save
  update_package(row, package)
end

#import_price(package, type, amount) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/oddb/import/dimdi.rb', line 270

def import_price(package, type, amount)
  dotype = :"price_#{type}"
  # if this price has been edited manually we won't overwrite
  unless((data_origin = package.data_origin(dotype)) \
     && data_origin.to_s.include?('@'))
    if(price = package.price(type, 'DE'))
      if(price != amount)
        price.amount = amount
      end
    else
      price = Util::Money.new(amount, type, 'DE')
      package.add_price(price)
    end
    package.data_origins.store dotype, :dimdi
  end
end

#import_product(row, product, name) ⇒ Object



261
262
263
264
265
266
267
268
269
# File 'lib/oddb/import/dimdi.rb', line 261

def import_product(row, product, name)
  if(product)
    @existing += 1
  else
    @created += 1
    product = create_product name
  end
  import_sequence(row, product)
end

#import_row(row) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/oddb/import/dimdi.rb', line 208

def import_row(row)
  if(value = cell(row, 0))
    @package_date = cell(row, 13) || @date
    @count += 1
    pzn = u(value.to_i.to_s)
    package = Drugs::Package.find_by_code(:type    => 'cid',
                                        :value   => pzn,
                                        :country => 'DE')
    name = product_name(row)
    product = Drugs::Product.find_by_name(name)
    if !package
      ## new package and possibly new product
      import_product row, product, name
    elsif !package.product
      product ||= create_product name
      if package.sequence.nil?
        import_sequence row, product, package
      else
        seq = package.sequence
        seq.product = product
        seq.save
        update_package row, package
      end
    else
      ## update package-data
      update_package row, package
    end
  end
end

#import_sequence(row, product, package = nil) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/oddb/import/dimdi.rb', line 286

def import_sequence(row, product, package=nil)
  ## be simplistic here - the input file can not describe
  #  multiple compositions or active agents. Simply identify
  #  existing active agents by their substance and dose.
  sequence, composition, substance, dose = nil
  substances = import_substances(row)
  galform = import_galenic_form(row)
  if(dose = cell(row, 8))
    dose = Drugs::Dose.new(dose, 'mg')
  end
  if product
    sequence = product.sequences.find { |seq|
      doses = seq.doses
      seq.galenic_forms == [galform] \
        && seq.substances == substances \
        && (doses.empty? || doses.inject { |a, b| a + b } == dose)
    } 
  end
  if(sequence)
    @existing_sequences += 1
  else
    @created_sequences += 1
    sequence = Drugs::Sequence.new
    composition = Drugs::Composition.new
    if(substances.size > 1)
      dose = nil
    end
    substances.each { |substance|
      active_agent = Drugs::ActiveAgent.new(substance, dose)
      active_agent.composition = composition
    }
    composition.galenic_form = galform
    if(factor = cell(row, 9))
      composition.equivalence_factor = factor
    end
    composition.sequence = sequence
    composition.save
    sequence.product = product
    sequence.save
  end
  import_atc(row, sequence)
  if(package)
    package.sequence = sequence
    package.save
    update_package(row, package)
  else
    unitname = nil
    if(galform)
      unitname = galform.description.de
    end
    import_package(row, sequence, unitname)
  end
end

#import_substance_group(groupname) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/oddb/import/dimdi.rb', line 378

def import_substance_group(groupname)
  groupname = groupname.to_s.strip
  if(groupname.length > 3)
    group = Drugs::SubstanceGroup.find_by_name(groupname)
    if(group.nil?)
      group = Drugs::SubstanceGroup.new
      group.name.de = groupname
      group.save
    end
    group
  end
end

#import_substances(row) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/oddb/import/dimdi.rb', line 344

def import_substances(row)
  subs = []
  groupname = cell(row, 10)
  if(abbr = cell(row, 7))
    if(sub = Drugs::Substance.find_by_code(:value => abbr, 
               :type => "substance", :country => "DE"))
      assign_substance_group(sub, groupname)
      subs.push(sub)
    else
      subs = Drugs::Substance.search_by_code(:value => abbr,
               :type => "substance-combination", :country => 'DE')
    end
  end
  if(subs.empty?)
    names = groupname.split('+')
    subs = names.collect { |name| 
      assumed_name = name.strip[/^\S+/]
      unless(assumed_name.empty?)
        sub = Drugs::Substance.find_by_name(assumed_name)
        if(sub.nil?)
          @created_substances += 1
          sub = Drugs::Substance.new
          sub.name.de = assumed_name
          sub.save
        end
        if(names.size == 1)
          assign_substance_group(sub, groupname)
        end
        sub
      end
    }
  end
  subs.compact
end

#move_package(row, product, package, name) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/oddb/import/dimdi.rb', line 390

def move_package(row, product, package, name)
  @existing += 1
  move_from = package.sequence
  move_to = product.sequences.find { |sequence|
    sequence.comparable?(move_from)
  }
  if(move_to)
    @existing_sequences += 1
    package.sequence = move_to
    package.save
    update_package(row, package)
    if(move_from.packages.empty?)
      delete_sequence(move_from)
    end
  elsif(move_from.packages.size == 1)
    @existing_sequences += 1
    move_sequence(product, move_from)
    update_package(row, package)
  else
    import_sequence(row, product, package)
  end
end

#move_sequence(product, sequence) ⇒ Object



412
413
414
415
416
417
418
419
420
# File 'lib/oddb/import/dimdi.rb', line 412

def move_sequence(product, sequence)
  move_from = sequence.product
  sequence.product = product
  sequence.save
  if(move_from.sequences.empty?)
    @deleted_products += 1
    move_from.delete
  end
end

#postprocessObject



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/oddb/import/dimdi.rb', line 421

def postprocess
  Drugs::Product.all { |product|
    sequences = product.sequences.dup
    sequences.each { |sequence|
      sequences.dup.each { |other|
        if(sequence != other && other.identical?(sequence))
          other.packages.each { |package|
            package.sequence = sequence
            package.save
          }
          sequences.delete other # should be safe because Array is ordered
          other.delete
        end
      }
    }
  }
end

#product_name(row) ⇒ Object



438
439
440
441
442
# File 'lib/oddb/import/dimdi.rb', line 438

def product_name(row)
  if data = cell(row, 1)
    capitalize_all(data.gsub(/[^A-Z\s]/, '').gsub(/\s+/, ' ')).strip
  end
end

#rename_product(row, package, name) ⇒ Object



443
444
445
446
447
448
449
# File 'lib/oddb/import/dimdi.rb', line 443

def rename_product(row, package, name)
  @renamed_products += 1
  product = package.product
  product.name.de = name
  product.save
  update_package(row, package)
end

#reportObject



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/oddb/import/dimdi.rb', line 450

def report
  [
    sprintf("Imported   %5i Products per %s:", 
            @count, @date.strftime("%d.%m.%Y")),
    sprintf("Visited    %5i existing Products", @existing),
    sprintf("Visited    %5i existing Sequences", 
            @existing_sequences),
    sprintf("Ignored    %5i unknown Products", @created),
    sprintf("Created    %5i new Sequences", @created_sequences),
    sprintf("Created    %5i new Substances from Combinations",
            @created_substances),
    sprintf("Renamed    %5i Products", @renamed_products),
    sprintf("Reassigned %5i PZNs", @reassigned_pzns),
    sprintf("Deleted    %5i Products", @deleted_products),
    sprintf("Deleted    %5i Sequences", @deleted_sequences),
  ]
end

#update_package(row, package) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/oddb/import/dimdi.rb', line 467

def update_package(row, package)
  modified = false
  fpgroup = cell(row, 11)
  unless(fpgroup.is_a?(String))
    fpgroup = u(fpgroup.to_i.to_s)
  end
  if(code = package.code(:festbetragsgruppe))
    if(code.value != fpgroup)
      modified = true
      code.value = fpgroup, @package_date
    end
  else
    modified = true
    package.add_code(Util::Code.new(:festbetragsgruppe, 
                                    fpgroup, 'DE', @package_date))
  end
  import_price(package, :public, cell(row, 3)) && modified = true
  if(efp = package._price_exfactory)
    import_price(package, :exfactory, efp.to_f) && modified = true
  end
  import_price(package, :festbetrag, cell(row, 4)) && modified = true
  if(level = cell(row, 12))
    if(code = package.code(:festbetragsstufe))
      if(code.value != level.to_i)
        modified = true
        code.value = level.to_i, @package_date
      end
    else
      modified = true
      package.add_code(Util::Code.new(:festbetragsstufe, level.to_i,
                                      'DE', @package_date))
    end
  end
  package.save if(modified)
end