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

KG - ServiceRequestController#remove_service #1030

Merged
merged 10 commits into from
Aug 18, 2017
61 changes: 15 additions & 46 deletions app/controllers/service_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,62 +198,36 @@ def add_service
@duplicate_service = true
else
add_service.generate_new_service_request
@line_items_count = @sub_service_request ? @sub_service_request.line_items.count : @service_request.line_items.count
@line_items_count = (@sub_service_request || @service_request).line_items.count
@sub_service_requests = @service_request.cart_sub_service_requests
end
end

def remove_service
line_item = @service_request.line_items.find( params[:line_item_id] )
line_items = @sub_service_request ? @sub_service_request.line_items : @service_request.line_items
service = line_item.service
ssr = line_item.sub_service_request

line_item_service_ids = @service_request.line_items.map(&:service_id)

# look at related services and set them to optional
# TODO POTENTIAL ISSUE: what if another service has the same related service
service.related_services.each do |rs|
if line_item_service_ids.include? rs.id
@service_request.line_items.find_by_service_id(rs.id).update_attribute(:optional, true)
end
end

line_items.where(service_id: service.id).each do |li|
if li.status != 'complete'
if ssr.can_be_edited? && ssr.status != 'first_draft'
ssr.update_attribute(:status, 'draft')
end
li.destroy
end
end
line_item = @service_request.line_items.find(params[:line_item_id])
ssr = line_item.sub_service_request

line_items.reload
if ssr.can_be_edited?
ssr.line_items.where(service: line_item.service.related_services).update_all(optional: true)

@service_request.reload
@page = previous_page
line_item.destroy

# Have the protocol clean up the arms
@service_request.protocol.arm_cleanup if @service_request.protocol
ssr.update_attribute(:status, 'draft') unless ssr.status == 'first_draft'
@service_request.reload

# clean up sub_service_requests
@service_request.reload
@service_request.previous_submitted_at = @service_request.submitted_at
@protocol = @service_request.protocol

#notify service providers and admin of a destroyed ssr upon deletion of ssr
if ssr.line_items.empty?
notifier_logic = NotifierLogic.new(@service_request, nil, current_user)
notifier_logic.ssr_deletion_emails(deleted_ssr: ssr, ssr_destroyed: true, request_amendment: false, admin_delete_ssr: false)
ssr.destroy
if ssr.line_items.empty?
NotifierLogic.new(@service_request, nil, current_user).ssr_deletion_emails(deleted_ssr: ssr, ssr_destroyed: true, request_amendment: false, admin_delete_ssr: false)
ssr.destroy
end
end

@service_request.reload
@line_items_count = @sub_service_request ? @sub_service_request.line_items.count : @service_request.line_items.count

@line_items_count = (@sub_service_request || @service_request).line_items.count
@sub_service_requests = @service_request.cart_sub_service_requests

respond_to do |format|
format.js {render layout: false}
format.js { render layout: false }
end
end

Expand Down Expand Up @@ -283,11 +257,6 @@ def approve_changes

private

def previous_page
# we need for pages other than the catalog
request.referrer.split('/').last
end

def feedback_params
params.require(:feedback).permit(:email, :message)
end
Expand Down
22 changes: 17 additions & 5 deletions app/models/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class LineItem < ApplicationRecord
has_many :procedures
has_many :admin_rates, dependent: :destroy
has_many :notes, as: :notable, dependent: :destroy

has_one :submission, dependent: :destroy
has_one :protocol, through: :service_request

attr_accessor :pricing_scheme

accepts_nested_attributes_for :fulfillments, allow_destroy: true
Expand All @@ -49,8 +52,9 @@ class LineItem < ApplicationRecord
validate :quantity_must_be_smaller_than_max_and_greater_than_min, on: :update, if: Proc.new { |li| li.service.one_time_fee }
validates :units_per_quantity, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, on: :update, if: Proc.new { |li| li.service.one_time_fee }

after_create :build_line_items_visits, if: Proc.new { |li| li.service.present? && !li.one_time_fee && li.service_request.present? && li.service_request.arms.any? }

after_create :build_line_items_visits_if_pppv
before_destroy :destroy_arms_if_last_pppv_line_item

default_scope { order('line_items.id ASC') }

def displayed_cost_valid?(displayed_cost)
Expand Down Expand Up @@ -338,9 +342,17 @@ def has_incomplete_additional_details?

private

def build_line_items_visits
self.service_request.arms.each do |arm|
arm.line_items_visits.create(line_item: self, subject_count: arm.subject_count)
def build_line_items_visits_if_pppv
if self.service && !self.one_time_fee && self.service_request.try(:arms).try(:any?)
self.service_request.arms.each do |arm|
arm.line_items_visits.create(line_item: self, subject_count: arm.subject_count)
end
end
end

def destroy_arms_if_last_pppv_line_item
if self.try(:protocol).try(:service_requests).try(:none?) { |sr| sr.has_per_patient_per_visit_services? }
self.service_request.try(:arms).try(:destroy_all)
end
end
end
17 changes: 0 additions & 17 deletions app/models/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -529,23 +529,6 @@ def grand_total(service_request)
direct_cost_total(service_request) + indirect_cost_total(service_request)
end

def arm_cleanup
return unless self.arms.count > 0

remove_arms = true

self.service_requests.each do |sr|
if sr.has_per_patient_per_visit_services?
remove_arms = false
break
end
end

if remove_arms
self.arms.destroy_all
end
end

def has_incomplete_additional_details?
line_items.any?(&:has_incomplete_additional_details?)
end
Expand Down
5 changes: 3 additions & 2 deletions app/views/service_requests/remove_service.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
# 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.
$('.shopping-cart').html("<%= escape_javascript(render( 'service_requests/right_navigation/cart', service_request: @service_request, sub_service_request: @sub_service_request, sub_service_requests: @sub_service_requests, line_items_count: @line_items_count, allow_delete: true )) %>")

if "<%= @page %>" == 'protocol'
$('.service-list').html("<%= escape_javascript(render( 'service_requests/protocol/service_list', service_request: @service_request, sub_service_request: @sub_service_request )) %>")
<% if request.referrer.split('/').last == 'protocol' %>
$('.service-list').html("<%= escape_javascript(render( 'service_requests/protocol/service_list', service_request: @service_request, sub_service_request: @sub_service_request )) %>")
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
let!(:before_filters) { find_before_filters }
let!(:logged_in_user) { create(:identity) }

before(:each) do
allow_any_instance_of(ServiceRequestsController).to receive(:previous_page).and_return('http://example.com')
end

describe '#remove_service' do
it 'should call before_filter #initialize_service_request' do
expect(before_filters.include?(:initialize_service_request)).to eq(true)
Expand Down Expand Up @@ -76,6 +72,7 @@
end

it 'should not delete complete line item' do
stub_const('FINISHED_STATUSES', ['complete'])
org = create(:organization, process_ssrs: true)
service = create(:service, organization: org)
protocol = create(:protocol_without_validations, primary_pi: logged_in_user)
Expand Down