Class: ODDB::OdbaExporter::CompetitionXls

Inherits:
Object
  • Object
show all
Defined in:
ext/export/src/competition_xls.rb

Instance Method Summary (collapse)

Constructor Details

- (CompetitionXls) initialize(path, db_path)

A new instance of CompetitionXls



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'ext/export/src/competition_xls.rb', line 9

def initialize(path, db_path)
  @workbook = Spreadsheet::Excel.new(path)
  @fmt_title = Spreadsheet::Format.new(:bold=>true)
  @fmt_original = Spreadsheet::Format.new(:color => 'red')
  @fmt_original_name = Spreadsheet::Format.new(:bold => true, :color => 'red')
  @fmt_generic = Spreadsheet::Format.new(:color => 'green')
  @fmt_generic_name = Spreadsheet::Format.new(:bold => true, :color => 'green')
  @worksheet = @workbook.add_worksheet("Generikaliste")
  @worksheet.format_column(0, 16.0, @fmt_original_name)
  @worksheet.format_column(1..4, 8.0, @fmt_original)
  @worksheet.format_column(5, 16.0, @fmt_generic_name)
  @worksheet.format_column(6..10, 8.0, @fmt_generic)
  @worksheet.format_column(11, 16.0, @fmt_generic)
  @worksheet.format_column(12..15, 8.0, @fmt_generic)
  columns = [
    'Name Original', 'Dosis Original', 'Packungsgrösse Original',
    'Fabrikabgabe-preis Original', 
    'Publikums-preis Original (inkl. MwSt)',
    'Name Generikum', 'Dosis Generikum', 'Packungsgrösse Generikum',
    'Fabrikabgabe-preis Generikum', 
    'Publikums-preis Generikum (inkl. MwSt)', 
    'Pharmacode Generikum', 'Hersteller Generikum',
    'Differenz in CHF', 'Differenz in %', 'Bemerkung', 'In SL?',
  ]
  @worksheet.write(0, 0, columns, @fmt_title)
  @app = ODBA.cache.fetch_named('oddbapp', nil)
  @rows = 1
  load_price_db(db_path)
end

Instance Method Details

- (Object) _format_generic(orig, gen, remarks)



114
115
116
117
118
119
120
121
122
123
124
125
# File 'ext/export/src/competition_xls.rb', line 114

def _format_generic(orig, gen, remarks)
  ppub = price_public(gen)
  [
    gen.name_base, gen.dose, gen.comparable_size,
    format_price(price_exfactory(gen)),
    format_price(ppub),
    gen.pharmacode, gen.company, 
    (format_price(price_public(orig) - ppub) if(orig)),
    orig && (dff = price_difference(orig, gen)) && sprintf("%1.1f%%", dff),
    remarks, (gen.sl_entry) ? 'Ja' : 'Nein',
  ].collect { |item| item.to_s }
end

- (Object) _format_original(original)



129
130
131
132
133
134
135
# File 'ext/export/src/competition_xls.rb', line 129

def _format_original(original)
  [
    original.name_base, original.dose, original.comparable_size,
    format_price(price_exfactory(original)),
    format_price(price_public(original)),
  ].collect { |item| item.to_s }
end

- (Object) close



38
39
40
# File 'ext/export/src/competition_xls.rb', line 38

def close
  @workbook.close
end

- (Object) export_comparable(package, comp)



96
97
98
# File 'ext/export/src/competition_xls.rb', line 96

def export_comparable(package, comp)
  write_row(format_row(package, comp))
end

- (Object) export_competition(company)



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
# File 'ext/export/src/competition_xls.rb', line 41

def export_competition(company)
  originals = []
  owns = {}
  @app.each_package { |pac|
    pac = pac.odba_instance
    if(pac.sl_entry && pac.public? && pac.registration.active?)
      if(pac.company == company)
        owns.store(pac.ikskey, pac)
      elsif(pac.sl_generic_type == :original && !pac.comparables.empty?)
        originals.push(pac)
      end
    end
  }
  originals.sort!
  last_export = nil
  originals.each_with_index { |package, idx|
    cheapest = nil
    generics = package.comparables.select { |pac|
      pac.sl_entry && (pac.sl_generic_type == :generic \
                       || pac.company.odba_instance == company)
    }.sort_by { |pac| 
      [price_public(pac).to_f, (pac.company == company) ? 1 : 0 ] } 
    if(cheapest = generics.first)
      owns.delete(cheapest.ikskey)
      export_comparable(package, cheapest)
      last_export = package.iksnr
    end
    if((own = generics.find { |pac| 
      pac.company.odba_instance == company }) && own != cheapest)
      owns.delete(own.ikskey)
      export_comparable(package, own)
      last_export = package.iksnr
    end
    unless((nxt = originals.at(idx.next)) && package.iksnr == nxt.iksnr)
      if(last_export == package.iksnr) ## omit phantom exports
        subs = package.substances.sort
        galform = package.galenic_form
        owns.values.select { |pac| 
          pac.galenic_form.equivalent_to?(galform) \
            && pac.substances.sort == subs
        }.sort.each { |pac|
          unless(pac.comparables.any? { |comp| 
            (comp.sl_generic_type == :original) && comp.sl_entry })
            export_generic(pac)
            owns.delete(pac.ikskey)
          end
        }
      end
    end
  }
  owns.values.sort.each { |package|
    export_generic(package)
  }
  @rows
end

- (Object) export_generic(package)



99
100
101
# File 'ext/export/src/competition_xls.rb', line 99

def export_generic(package)
  write_row(format_generic(package))
end

- (Object) export_original(package)



102
103
104
# File 'ext/export/src/competition_xls.rb', line 102

def export_original(package)
  write_row(format_original(package))
end

- (Object) format_generic(generic)



110
111
112
113
# File 'ext/export/src/competition_xls.rb', line 110

def format_generic(generic)
  remarks = 'kein passendes original gefunden'
  Array.new(5, '').concat(_format_generic(nil, generic, remarks))
end

- (Object) format_original(original)



126
127
128
# File 'ext/export/src/competition_xls.rb', line 126

def format_original(original)
  _format_original(original)
end

- (Object) format_price(price)



105
106
107
108
109
# File 'ext/export/src/competition_xls.rb', line 105

def format_price(price)
  if(price && price > 0.0)
    sprintf("%4.2f", price.to_f)
  end
end

- (Object) format_row(original, generic)



136
137
138
139
140
141
142
# File 'ext/export/src/competition_xls.rb', line 136

def format_row(original, generic)
  remarks = if(original.comparable_size != generic.comparable_size)
              'unterschiedliche Packungsgrösse'
            end
  _format_original(original).concat(_format_generic(original, 
                                                    generic, remarks))
end

- (Object) load_price_db(path)



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/export/src/competition_xls.rb', line 143

def load_price_db(path)
  @exf_pcd_prices = {}
  @pbl_pcd_prices = {}
  @exf_iks_prices = {}
  @pbl_iks_prices = {}
  if(path)
    workbook = Spreadsheet.open(path)
    worksheet = workbook.worksheet(0)
    worksheet.each(1) { |row|
      pcode = row.at(8).to_s
      ikskey = row.at(10).to_s
      efp = (row.at(15).to_f * 100.0).to_i
      pbp = (row.at(16).to_f * 100.0).to_i
      @exf_pcd_prices.store(pcode, efp)
      @exf_iks_prices.store(ikskey, efp)
      @pbl_pcd_prices.store(pcode, pbp)
      @pbl_iks_prices.store(ikskey, pbp)
    }
  end
end

- (Object) price_difference(original, generic)



163
164
165
166
167
168
169
170
171
# File 'ext/export/src/competition_xls.rb', line 163

def price_difference(original, generic)
  oprice = price_public(original).to_f
  pprice = price_public(generic).to_f
  osize = original.comparable_size.qty.to_f 
  psize = generic.comparable_size.qty.to_f
  unless ( (oprice <= 0) || (pprice <= 0) || (osize <= 0) || (psize <= 0))
    (( (osize * pprice) / (psize * oprice) ) - 1.0).abs * 100.0
  end
end

- (Object) price_exfactory(package)



172
173
174
175
# File 'ext/export/src/competition_xls.rb', line 172

def price_exfactory(package)
  @exf_pcd_prices[package.pharmacode] \
    || @exf_iks_prices[package.ikskey] || package.price_exfactory
end

- (Object) price_public(package)



176
177
178
179
# File 'ext/export/src/competition_xls.rb', line 176

def price_public(package)
  @pbl_pcd_prices[package.pharmacode] \
    || @pbl_iks_prices[package.ikskey] || package.price_public
end

- (Object) write_row(row)



180
181
182
183
# File 'ext/export/src/competition_xls.rb', line 180

def write_row(row)
  @worksheet.write(@rows, 0, row)
  @rows += 1
end