Class: CQM::Provider

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/cqm/provider.rb

Overview

Provider model that holds non-QDM data for the provider. As well as indentifiers referenced in QDM

Constant Summary collapse

NPI_OID =
'2.16.840.1.113883.4.6'.freeze
TAX_ID_OID =
'2.16.840.1.113883.4.2'.freeze
CCN_OID =
'2.16.840.1.113883.4.336'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.luhn_checksum(num) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/cqm/provider.rb', line 75

def self.luhn_checksum(num)
  double = { '0' => 0, '1' => 2, '2' => 4, '3' => 6, '4' => 8, '5' => 1, '6' => 3, '7' => 5, '8' => 7, '9' => 9 }
  sum = 0
  num.reverse!
  num.split('').each_with_index do |char, i|
    sum += if (i % 2).zero?
             double[char]
           else
             char.to_i
           end
  end
  sum = (9 * sum) % 10

  sum.to_s
end

.valid_npi?(npi) ⇒ Boolean

validate the NPI, should be 10 or 15 digits total with the final digit being a checksum using the Luhn algorithm with additional special handling as described in www.cms.gov/NationalProvIdentStand/Downloads/NPIcheckdigit.pdf

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/cqm/provider.rb', line 63

def self.valid_npi?(npi)
  return false unless npi
  return false if npi.length != 10 && npi.length != 15
  return false if npi.gsub(/\d/, '').length.positive? # npi must be all digits
  return false if npi.length == 15 && (npi =~ /^80840/).nil? # 15 digit npi must start with 80840

  # checksum is always calculated as if 80840 prefix is present
  npi = '80840' + npi if npi.length == 10

  luhn_checksum(npi[0, 14]) == npi[14]
end

Instance Method Details

#ccnObject



55
56
57
58
# File 'app/models/cqm/provider.rb', line 55

def ccn
  cda_id_ccn = ids.where(namingSystem: CCN_OID).first
  cda_id_ccn ? cda_id_ccn.value : nil
end

#ccn=(a_ccn) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'app/models/cqm/provider.rb', line 45

def ccn=(a_ccn)
  cda_id_ccn = ids.where(namingSystem: CCN_OID).first
  if cda_id_ccn
    cda_id_ccn.value = a_ccn
    cda_id_ccn.save!
  else
    ids << QDM::Identifier.new(namingSystem: CCN_OID, value: a_ccn)
  end
end

#npiObject



31
32
33
34
# File 'app/models/cqm/provider.rb', line 31

def npi
  cda_id_npi = ids.where(namingSystem: NPI_OID).first
  cda_id_npi ? cda_id_npi.value : nil
end

#npi=(an_npi) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'app/models/cqm/provider.rb', line 21

def npi=(an_npi)
  cda_id_npi = ids.where(namingSystem: NPI_OID).first
  if cda_id_npi
    cda_id_npi.value = an_npi
    cda_id_npi.save!
  else
    ids << QDM::Identifier.new(namingSystem: NPI_OID, value: an_npi)
  end
end

#tinObject



40
41
42
43
# File 'app/models/cqm/provider.rb', line 40

def tin
  cda_id_tin = ids.where(namingSystem: TAX_ID_OID).first
  cda_id_tin ? cda_id_tin.value : nil
end

#tin=(a_tin) ⇒ Object



36
37
38
# File 'app/models/cqm/provider.rb', line 36

def tin=(a_tin)
  ids << QDM::Identifier.new(namingSystem: TAX_ID_OID, value: a_tin)
end