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



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

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)


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

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



53
54
55
56
# File 'app/models/cqm/provider.rb', line 53

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

#ccn=(a_ccn) ⇒ Object



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

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



29
30
31
32
# File 'app/models/cqm/provider.rb', line 29

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

#npi=(an_npi) ⇒ Object



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

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



38
39
40
41
# File 'app/models/cqm/provider.rb', line 38

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



34
35
36
# File 'app/models/cqm/provider.rb', line 34

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