Skip to content

Commit

Permalink
Company import. Corrected handling of addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed Nov 19, 2014
1 parent 2b30c18 commit fe8c063
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 45 deletions.
2 changes: 2 additions & 0 deletions install_gems.sh
Expand Up @@ -32,6 +32,8 @@ gem install rclconf --version=1.0.0
gem install rdoc --version=4.1.1
gem install rmagick --version=2.13.2
gem install rmail --version=1.0.0
gem install ruby-ole --version=1.2.11.7

gem install rpdf2txt --version=0.8.4
gem install rubyXL --version=3.1.2
gem install rubyzip --version=1.1.6
Expand Down
31 changes: 31 additions & 0 deletions src/model/ba_type.rb
@@ -0,0 +1,31 @@
#!/usr/bin/env ruby
# encoding: utf-8
# Business area for companies
require 'sbsm/validator'

module ODDB
class BA_type
include Enumerable
BA_hospital = 'ba_hospital'
BA_pharma = 'ba_pharma'
BA_public_pharmacy = 'ba_public_pharmacy'
BA_hospital_pharmacy = 'ba_hospital_pharmacy'
BA_research_institute = 'ba_research_institute'
BA_insurance = 'ba_insurance'
BA_doctor = 'ba_doctor'
BA_health = 'ba_health'
BA_info = 'ba_info'
def each
yield nil
yield BA_hospital
yield BA_pharma
yield BA_public_pharmacy
yield BA_hospital_pharmacy
yield BA_research_institute
yield BA_insurance
yield BA_doctor
yield BA_health
yield BA_info
end
end
end
25 changes: 17 additions & 8 deletions src/model/company.rb
@@ -1,25 +1,26 @@
#!/usr/bin/env ruby
# encoding: utf-8
# Company -- oddb -- 28.02.2003 -- hwyss@ywesee.com
# Company -- oddb -- 28.02.2003 -- hwyss@ywesee.com

require 'util/persistence'
require 'util/today'
require 'model/registration_observer'
require 'model/address'
require 'model/user'
require 'model/ba_type'

module ODDB
class Company
include Persistence
include RegistrationObserver
include UserObserver
include AddressObserver
ODBA_SERIALIZABLE = ['@addresses', '@invoice_dates', '@disabled_invoices',
ODBA_SERIALIZABLE = ['@addresses', '@invoice_dates', '@disabled_invoices',
'@prices', '@users']
attr_accessor :address_email, :business_area, :business_unit, :cl_status,
:competition_email, :complementary_type, :contact, :deductible_display,
:disable_patinfo, :ean13, :generic_type,
:invoice_htmlinfos, :logo_filename, :lookandfeel_member_count, :name,
:invoice_htmlinfos, :logo_filename, :lookandfeel_member_count, :name,
:powerlink, :regulatory_email, :swissmedic_email, :swissmedic_salutation,
:url, :ydim_id, :limit_invoice_duration, :force_new_ydim_debitor
attr_reader :invoice_dates, :disabled_invoices
Expand All @@ -35,7 +36,15 @@ def initialize
@disabled_invoices = {}
@prices = {}
super
end
end
def is_pharmacy?
case @business_area
when BA_type::BA_public_pharmacy, BA_type:: BA_hospital_pharmacy
return true
else
false
end
end
def init(app)
@pointer.append(@oid)
end
Expand Down Expand Up @@ -74,7 +83,7 @@ def inactive_registrations
## to be invoiceable, the company needs to have a complete address:
def invoiceable?
addr = address(0)
![ @name, @contact, addr.address, addr.plz,
![ @name, @contact, addr.address, addr.plz,
addr.city, invoice_email, addr.fon ].any? { |datum| datum.nil? }
end
def invoice_date(key)
Expand Down Expand Up @@ -162,9 +171,9 @@ def price_patinfo=(units)
def search_terms
terms = @name.split(/[\s\-()]+/u).select { |str| str.size >= 3 }
terms += [
@name, @ean13,
@name, @ean13,
]
@addresses.each { |addr|
@addresses.each { |addr|
terms += addr.search_terms
}
ODDB.search_terms(terms)
Expand All @@ -186,7 +195,7 @@ def adjust_types(input, app=nil)
if(val.is_a? String)
input[key] = val.intern
end
when :price_lookandfeel, :price_lookandfeel_member,
when :price_lookandfeel, :price_lookandfeel_member,
:price_index, :price_index_package
input[key] = (val.to_f * 100) unless(val.nil?)
end
Expand Down
56 changes: 38 additions & 18 deletions src/plugin/medreg_company.rb
Expand Up @@ -19,7 +19,7 @@

module ODDB
module Companies
DebugImport = false
DebugImport = defined?(MiniTest)
BetriebeURL = 'https://www.medregbm.admin.ch/Betrieb/Search'
BetriebeXLS_URL = "https://www.medregbm.admin.ch/Publikation/CreateExcelListBetriebs"
RegExpBetriebDetail = /\/Betrieb\/Details\//
Expand Down Expand Up @@ -58,7 +58,7 @@ module Companies
class MedregCompanyPlugin < Plugin
RECIPIENTS = []
def log(msg)
$stdout.puts "#{Time.now}: MedregCompanyPlugin #{msg}" unless defined?(Minitest)
$stdout.puts "#{Time.now}: MedregCompanyPlugin #{msg}" # unless defined?(Minitest)
$stdout.flush
LogFile.append('oddb/debug', " MedregCompanyPlugin #{msg}", Time.now)
end
Expand Down Expand Up @@ -122,7 +122,7 @@ def parse_details(doc, gln)
company[:name] = infos[4]
idx_plz = infos.index("PLZ \\ Ort")
idx_canton = infos.index('Bewilligungskanton')
address = [infos[6..idx_plz-1].join(' ')]
address = infos[6..idx_plz-1].join(' ')
company[:plz] = infos[idx_plz+1]
company[:location] = infos[idx_plz+2]
idx_typ = infos.index('Betriebstyp')
Expand All @@ -146,11 +146,6 @@ def get_detail_to_glns(glns)
log "Skipping #{gln}. Waiting for #{r_loop.state_id.inspect}" if DebugImport
next
end
if (@companies_created + @companies_updated) % 100 == 99
log "Start saving @app.companies.odba_store after #{@companies_created} created #{@companies_updated} updated"
@app.companies.odba_store
log "Finished @app.companies.odba_store" if DebugImport
end
nr_tries = 0
success = false
while nr_tries < max_retries and not success
Expand Down Expand Up @@ -180,10 +175,16 @@ def get_detail_to_glns(glns)
end
success = true
end
rescue => e
rescue Timeout => e
nr_tries += max_retries if defined?(MiniTest)
log "rescue #{e} will retry #{max_retries - nr_tries} times"
nr_tries += 1
sleep 60
sleep defined?(MiniTest) ? 0.01 : 60
end
if (@companies_created + @companies_updated) % 100 == 99
log "Start saving @app.companies.odba_store #{gln} after #{@companies_created} created #{@companies_updated} updated"
@app.companies.odba_store
log "Finished @app.companies.odba_store" if DebugImport
end
end
}
Expand Down Expand Up @@ -225,7 +226,9 @@ def report
end
def update_address(data)
addr = Address2.new
addr.name = data[:name ]
addr.address = data[:address]
addr.additional_lines = [data[:address] ]
addr.location = [data[:plz], data[:location]].compact.join(' ')
if(fon = data[:phone])
addr.fon = [fon]
Expand All @@ -240,21 +243,35 @@ def store_company(data)
if(doc = @app.company_by_gln(data[:ean13]))
pointer = doc.pointer
@companies_updated += 1
action = 'create'
action = 'update'
else
@companies_created += 1
ptr = Persistence::Pointer.new(:company)
pointer = ptr.creator
action = 'update'
doc = @app.create_company
log " created #{doc} #{doc.pointer}"
pointer = doc.pointer
action = 'create'
end
update_hash = {}
update_hash[:ean13] = data[:ean13]
update_hash[:name] = data[:name_1]
update_hash[:addresses] = data[:addresses]
ba_type = nil
case data[:type]
when /öffentliche Apotheke/i
ba_type = ODDB::BA_type::BA_public_pharmacy
when /Spitalapotheke/i
ba_type = ODDB::BA_type::BA_hospital_pharmacy
when /wissenschaftliches Institut/i
ba_type = ODDB::BA_type::BA_research_institute
else
end
update_hash[:business_area] = ba_type
update_hash[:addresses] = data[:addresses]
@app.update(pointer, update_hash, :medreg)
log "store_company #{data[:ean13]} #{action} in database. pointer #{pointer.inspect}. Have now #{@app.companies.size} companies. hash #{update_hash}"
doc_copy = @app.company_by_gln(data[:ean13])
log "store_company #{data[:ean13]} #{action} doc_copy is #{doc_copy}"
log "store_company directly #{data[:ean13]} #{action} in database. pointer #{pointer.inspect}. Have now #{@app.companies.size} companies. hash #{update_hash}"
return
company_copy = @app.company_by_gln(data[:ean13])
company_copy.odba_isolated_store
log "store_company copy oid #{company_copy.oid} #{company_copy.business_area} #{company_copy.inspect} "
end
def parse_xls(path)
log "parsing #{path}"
Expand All @@ -276,6 +293,9 @@ def parse_xls(path)
end
@glns_to_import = @info_to_gln.keys.sort.uniq
end
def MedregCompanyPlugin.all_companies
@@all_companies
end
end
end
end
10 changes: 5 additions & 5 deletions src/plugin/medreg_doctor.rb
Expand Up @@ -220,11 +220,6 @@ def get_detail_to_glns(glns)
@doctors_skipped += 1
next
end
if (@doctors_created + @doctors_updated) % 100 == 99
log "Start saving @app.doctors.odba_store after #{@doctors_created} created #{@doctors_updated} updated"
@app.doctors.odba_store
log "Finished @app.doctors.odba_store"
end
@idx += 1
nr_tries = 0
while nr_tries < max_retries
Expand All @@ -245,6 +240,11 @@ def get_detail_to_glns(glns)
end
end
raise "Max retries #{nr_tries} for #{gln.to_s} reached. Aborting import" if nr_tries == max_retries
if (@doctors_created + @doctors_updated) % 100 == 99
log "Start saving @app.doctors.odba_store #{gln} after #{@doctors_created} created #{@doctors_updated} updated"
@app.doctors.odba_store
log "Finished @app.doctors.odba_store"
end
}
r_loop.finished
ensure
Expand Down
2 changes: 1 addition & 1 deletion src/util/oddbapp.rb
Expand Up @@ -380,7 +380,7 @@ def create_division
def create_doctor
doctor = ODDB::Doctor.new
@doctors ||= {}
@doctors.store(doctor.oid, doctor)
@doctors.store(doctor.oid, doctor)
end
def create_experience
experience = ODDB::Experience.new
Expand Down
4 changes: 2 additions & 2 deletions src/util/validator.rb
Expand Up @@ -5,6 +5,7 @@
# ODDB::Validator -- oddb.org -- 18.11.2002 -- hwyss@ywesee.com

require 'sbsm/validator'
require 'model/ba_type'
require 'model/ean13'
require 'iconv'
require 'cgi'
Expand Down Expand Up @@ -59,8 +60,7 @@ class Validator < SBSM::Validator
ENUMS = {
:address_type => [nil, 'at_work', 'at_praxis',
'at_private'],
:business_area=> [nil, 'ba_hospital', 'ba_pharma', 'ba_insurance',
'ba_doctor', 'ba_health', 'ba_info' ],
:business_area=> ODDB::BA_type,
:canton => [nil, 'AG', 'AI', 'AR', 'BE',
'BL', 'BS', 'FR', 'GE', 'GL', 'GR', 'JU', 'LU',
'NE', 'NW', 'OW', 'SG', 'SH', 'SO', 'SZ', 'TG',
Expand Down
20 changes: 16 additions & 4 deletions test/test_model/company.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# encoding: utf-8
# TestCompany -- oddb -- 28.03.2011 -- mhatakeyama@ywesee.com
# TestCompany -- oddb -- 28.02.2003 -- hwyss@ywesee.com
# TestCompany -- oddb -- 28.02.2003 -- hwyss@ywesee.com

$: << File.expand_path('..', File.dirname(__FILE__))
$: << File.expand_path("../../src", File.dirname(__FILE__))
Expand Down Expand Up @@ -223,7 +223,7 @@ def test_listed
def test_merge
reg1 = flexmock :odba_isolated_store => :ignore
reg2 = flexmock :odba_isolated_store => :ignore
other = ODDB::Company.new
other = ODDB::Company.new
other.registrations.push reg1, reg2
reg1.should_receive(:company=).with(@company).times(1).and_return do
other.registrations.delete reg1
Expand Down Expand Up @@ -294,7 +294,7 @@ def test_search_terms
addr.address = 'Street, Number'
addr.location = '1234 City'
expected = [
'Company', 'Name', 'Company', 'CompanyName', 'Company Name',
'Company', 'Name', 'Company', 'CompanyName', 'Company Name',
"7681123456789", "Street Number", "1234 City", "City", "1234"
]
assert_equal expected, @company.search_terms
Expand All @@ -317,7 +317,7 @@ def test_update_values
assert_equal('Intellectual Capital', @company.business_area)
assert_equal('hwyss at ywesee.com', @company.contact)
assert_equal([reg], @company.registrations)
end
end
def test_adjust_types
values = {
:name => 'ywesee.com',
Expand Down Expand Up @@ -364,4 +364,16 @@ def test__yearly_repetition
assert_equal(expected, @company.instance_eval('_yearly_repetition(date)'))
@company.instance_eval('@@today = today_bak')
end
def test_is_pharmacy
@company.business_area = 'invalid'
assert_equal(false, @company.is_pharmacy?)
@company.business_area =nil
assert_equal(false, @company.is_pharmacy?)
@company.business_area = ODDB::BA_type::BA_public_pharmacy
assert_equal(true, @company.is_pharmacy?)
@company.business_area = ODDB::BA_type::BA_hospital_pharmacy
assert_equal(true, @company.is_pharmacy?)
@company.business_area = ODDB::BA_type::BA_research_institute
assert_equal(false, @company.is_pharmacy?)
end
end

0 comments on commit fe8c063

Please sign in to comment.