Class: ODDB::Export::Xls::ComparisonDeCh

Inherits:
Object
  • Object
show all
Defined in:
lib/oddb/export/xls.rb

Constant Summary collapse

@@iconv =
Iconv.new('latin1//IGNORE//TRANSLIT', 'utf8')

Instance Method Summary collapse

Instance Method Details

#adjust_price(price) ⇒ Object



18
19
20
# File 'lib/oddb/export/xls.rb', line 18

def adjust_price(price)
  price * currency_rate * tax_factor
end

#collect_cell(local, remote, format = "%s (%s)", &block) ⇒ Object



39
40
41
42
43
# File 'lib/oddb/export/xls.rb', line 39

def collect_cell(local, remote, format = "%s (%s)", &block)
  lval = block.call(local) rescue StandardError
  rval = block.call(remote) rescue StandardError
  lval == rval ? lval : sprintf(format, lval, rval)
end

#collect_comparables(drb_uri) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/oddb/export/xls.rb', line 21

def collect_comparables(drb_uri)
  data = []
  DRb::DRbObject.new(nil, drb_uri).remote_each_package { |remote|
    package = Remote::Drugs::Package.new(drb_uri, remote,
                                         1.0 / currency_rate, 
                                         tax_factor)
    if(package.price(:public) \
       && (comparable = package.local_comparables.select { |pac|
          pac.price(:public)
        }.sort_by { |pac|
          pac.price(:public)
        }.first))
      data.push [comparable, package]
    end
    nil # don't return data from the block across drb
  }
  data
end

#currency_rateObject



44
45
46
# File 'lib/oddb/export/xls.rb', line 44

def currency_rate
  @currency_rate ||= Currency.rate('EUR', 'CHF')
end

#export(drb_uri, io) ⇒ Object



15
16
17
# File 'lib/oddb/export/xls.rb', line 15

def export(drb_uri, io)
  write_xls(io, collect_comparables(drb_uri))
end

#tax_factorObject



47
48
49
# File 'lib/oddb/export/xls.rb', line 47

def tax_factor
  1.076 / 1.19
end

#write_row(worksheet, idx, local, remote) ⇒ Object



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
# File 'lib/oddb/export/xls.rb', line 50

def write_row(worksheet, idx, local, remote)
  comparison = Util::Comparison.new(local, remote)
  lexf = local.price(:exfactory)
  rexf = remote.price(:exfactory)
  data = [
    collect_cell(local, remote) { |x| 
      x.name.de || x.product.name.de },
    collect_cell(local, remote) { |x|
      size, _ = x.comparable_size
      size.to_s
    },
    adjust_price(local.price(:public)).to_s,
    collect_cell(local, remote) { |x| x.company.name.de },
    local.code(:cid).to_s,
    remote.code(:ean).to_s,
    collect_cell(local, remote) { |x| 
      x.galenic_forms.first.description.de },
    collect_cell(local, remote) { |x| 
      x.active_agents.first.dose.to_s },
    collect_cell(local, remote) { |x| 
      x.active_agents.first.substance.name.de },
    remote.atc.code,
    remote.ikscat,
    remote.sl_entry ? 'SL' : '',
    sprintf("%+4.2f", adjust_price(comparison.absolute)),
    sprintf("%+4.2f%%", comparison.difference),
    sprintf("%4.2f", comparison.factor),
    (adjust_price(lexf) if lexf).to_s,
    (adjust_price(rexf) if rexf).to_s,
  ]
  if(lexf && rexf)
    data.push sprintf("%+4.2f", adjust_price(comparison.absolute_exfactory)),
      sprintf("%+4.2f%%", comparison.difference_exfactory)
  end
  worksheet.write idx, 0, data
end

#write_xls(io, data) ⇒ Object



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
# File 'lib/oddb/export/xls.rb', line 86

def write_xls(io, data)
  workbook = Spreadsheet::Excel.new(io)
  worksheet = workbook.add_worksheet('Preisvergleich')
  fmt_title = Spreadsheet::Format.new(:bold => true)
  workbook.add_format(fmt_title)
  worksheet.write 0, 0, [
    "Name DE (Name CH)",
    "Pkg Grösse DE (Pkg Grösse CH)",
    "AVP inkl. Mwst in CHF nach Abzug und Zuschlag",
    "Hersteller DE (Hersteller CH)",
    "PZN", "EAN",
    "Gal Form DE (gal. Form CH)",
    "Stärke (Schreibweise CH)",
    "Wirkstoff (Schreibweise CH)",
    "ATC-Code", "Abgabekategorie CH", "SL Schweiz",
    "Preisunterschied inkl. Mwst. in CHF",
    "Preisunterschied in % (normalisiert)",
    "Packungsäquivalenzfaktor",
    "FAP DE exkl. MwSt (errechnet)",
    "FAP CH exkl. MwSt",
    "Preisunterschied FAP in CHF ",
    "Preisunterschied FAP in % (normalisiert)",
  ], fmt_title
  data.sort_by { |local, remote| 
    local.name.de || local.product.name.de
  }.each_with_index { |(local, remote), idx|
    write_row(worksheet, idx.next, local, remote)
  }
  workbook.close
end