diff --git a/app/models/prescription_details.rb b/app/models/prescription_details.rb index 71c128d88ca..add7492cfaf 100644 --- a/app/models/prescription_details.rb +++ b/app/models/prescription_details.rb @@ -34,7 +34,8 @@ class PrescriptionDetails < Prescription def sorted_dispensed_date has_refills = try(:rx_rf_records).present? - last_refill_date = Date.new(0) + default_date = Date.new(0) + last_refill_date = default_date if has_refills refills = rx_rf_records[0][1] @@ -48,12 +49,13 @@ def sorted_dispensed_date end end + last_refill_date = nil if last_refill_date == default_date if has_refills && last_refill_date.present? last_refill_date.to_date elsif dispensed_date.present? dispensed_date.to_date else - Date.new(0) + nil end end end diff --git a/modules/my_health/app/helpers/my_health/prescription_helper.rb b/modules/my_health/app/helpers/my_health/prescription_helper.rb index a5f521cad69..06c1157ea84 100644 --- a/modules/my_health/app/helpers/my_health/prescription_helper.rb +++ b/modules/my_health/app/helpers/my_health/prescription_helper.rb @@ -23,8 +23,9 @@ def sort_by(resource, sort_params) resource.data = resource.data.sort do |a, b| comparison = 0 sort_params.each_with_index do |field, index| - field1_a, field1_b = populate_sort_vars(field, a, b) - comparison = compare_fields(field1_a, field1_b, sort_orders[index]) + is_descending = sort_orders[index] + field_a, field_b = populate_sort_vars(field, a, b, is_descending) + comparison = compare_fields(field_a, field_b, is_descending) break if !comparison.zero? || field.nil? end comparison @@ -44,28 +45,34 @@ def get_sort_order(fields) end end - def compare_fields(field_a, field_b, descending) + def compare_fields(field_a, field_b, is_descending) if field_a > field_b - descending ? -1 : 1 + is_descending ? -1 : 1 elsif field_a < field_b - descending ? 1 : -1 + is_descending ? 1 : -1 else 0 end end - def populate_sort_vars(field, a, b) + def populate_sort_vars(field, a, b, is_descending) if field.nil? [nil, nil] else - [get_field_data(field, a), get_field_data(field, b)] + [get_field_data(field, a, is_descending), get_field_data(field, b, is_descending)] end end - def get_field_data(field, data) + def get_field_data(field, data, is_descending) case field when /dispensed_date/ - data[:sorted_dispensed_date] + if data[:sorted_dispensed_date].nil? && is_descending + Date.new(9999, 12, 31) + elsif data[:sorted_dispensed_date].nil? + Date.new(0, 1, 1) + else + data[:sorted_dispensed_date] + end when 'prescription_name' if data[:disp_status] == 'Active: Non-VA' && data[:prescription_name].nil? data[:orderable_item]