Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jl services update script #76

Merged
merged 8 commits into from
Feb 15, 2017
117 changes: 85 additions & 32 deletions lib/tasks/update_hb_services.rake
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR~
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.~

desc "Adding eap_id to services and updating columns"
task :add_eap_id => :environment do
desc "Updating service columns as needed"
task :update_hb_services => :environment do

def prompt(*args)
print(*args)
Expand All @@ -39,52 +39,96 @@ task :add_eap_id => :environment do

def header
[
'service_id',
'name',
'cpt_code',
'eap_id',
'revenue_code',
'is_available',
'organization_id',
'line_items_count',
'description'
'Service ID',
'EAP ID',
'CPT Code',
'Revenue Code',
'Procedure Name',
'Service Rate',
'Corporate Rate',
'Federal Rate',
'Member Rate',
'Other Rate',
'Is One Time Fee?',
'Clinical Qty Type',
'Unit Factor',
'Qty Min',
'Display Date',
'Effective Date'
]
end

def update_service_pricing(service, row)
pricing_map = service.pricing_maps.build( full_rate: Service.dollars_to_cents(row['Service Rate'].to_s),
corporate_rate: Service.dollars_to_cents(row['Corporate Rate'].to_s),
federal_rate: Service.dollars_to_cents(row['Federal Rate'].to_s),
member_rate: Service.dollars_to_cents(row['Member Rate'].to_s),
other_rate: Service.dollars_to_cents(row['Other Rate'].to_s),
unit_factor: row['Unit Factor'],
unit_type: row['Clinical Qty Type'],
unit_minimum: row['Qty Min'],
units_per_qty_max: service.current_effective_pricing_map.units_per_qty_max,
quantity_type: service.current_effective_pricing_map.quantity_type,
otf_unit_type: service.current_effective_pricing_map.otf_unit_type,
quantity_minimum: service.current_effective_pricing_map.quantity_minimum,
display_date: Date.strptime(row['Display Date'], "%m/%d/%y"),
effective_date: Date.strptime(row['Effective Date'], "%m/%d/%y")
)
if pricing_map.valid?
pricing_map.save
else
puts "#"*50
puts "Error importing pricing map"
puts service.inspect
puts pricing_map.inspect
puts service.errors.inspect
puts pricing_map.errors.inspect
end
end

revenue_codes = []
cpt_codes = []
service_names = []
pricing_maps = []
puts ""
puts "Reading in file..."
input_file = Rails.root.join("db", "imports", get_file)
continue = prompt('Preparing to modify the services. Are you sure you want to continue? (y/n): ')

if continue == 'y'
if (continue == 'y') || (continue == 'Y')
ActiveRecord::Base.transaction do
CSV.foreach(input_file, :headers => true) do |row|
service = Service.find(row['service_id'].to_i)
service = Service.where(id: row['Service ID'].to_i).first

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong with #find here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, then you could leave off the #first

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is with the import file. There are a bunch of null values after the last service row. The 'where' handles this.

puts ""
puts ""
puts "Adding eap id to service #{service.name}"

service.eap_id = row['eap_id']

unless service.revenue_code == row['revenue_code'].rjust(4, '0')
revenue_codes << [service.id, service.revenue_code]
service.revenue_code = row['revenue_code'].rjust(4, '0')
end

unless service.cpt_code == row['cpt_code']
cpt_codes << [service.id, service.cpt_code]
service.cpt_code = row['cpt_code'] == 'NULL' ? nil : row['cpt_code']
if service
unless service.revenue_code == row['Revenue Code'].rjust(4, '0')
revenue_codes << [service.id, service.revenue_code]
puts "Altering the revenue code of service with an id of #{service.id} from #{service.revenue_code} to #{row['Revenue Code']}"
service.revenue_code = row['Revenue Code'].rjust(4, '0')
end

unless service.cpt_code == row['CPT Code']
cpt_codes << [service.id, service.cpt_code]
puts "Altering the CPT code of service with an id of #{service.id} from #{service.cpt_code} to #{row['CPT Code']}"
service.cpt_code = row['CPT Code'] == 'NULL' ? nil : row['CPT Code']
end

unless service.name == row['Procedure Name']
service_names << [service.id, service.name]
puts "Altering the name of service with an id of #{service.id} from #{service.name} to #{row['Procedure Name']}"
service.name = row['Procedure Name']
end

unless service.current_effective_pricing_map.full_rate == (row['Service Rate'].to_i * 100)
pricing_maps << [service.id, service.current_effective_pricing_map.full_rate]
puts "Altering service #{service.id} cost from a rate of #{service.current_effective_pricing_map.full_rate} to #{row['Service Rate'].to_i * 100}"
update_service_pricing(service, row)
end

service.save
end

unless service.name == row['name']
service_names << [service.id, service.name]
service.name = row['name']
end

service.save
end
end

Expand All @@ -107,9 +151,18 @@ task :add_eap_id => :environment do
unless service_names.empty?
service_names.each do |id_and_name|
service = Service.find(id_and_name[0])
csv << [service.name, id_and_name[0], 'Service Name', id_and_name[1]]
csv << [service.name, id_and_name[0], 'Procedure Name', service.name, id_and_name[1]]
end
end

unless pricing_maps.empty?
pricing_maps.each do |id_and_rate|
service = Service.find(id_and_rate[0])
csv << [service.name, id_and_rate[0], 'Pricing Map', service.current_effective_pricing_map.full_rate, id_and_rate[1]]
end
end
end
else
puts 'Exiting rake task...'
end
end