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 - merge srs refactor #939

Merged
merged 3 commits into from Jul 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/tasks/merge_srs.rake
Expand Up @@ -4,17 +4,18 @@ AUDIT_COMMENT = "merge_srs"

task :merge_srs => :environment do
ServiceRequest.skip_callback(:save, :after, :set_original_submitted_date)
organizations = Organization.all

multiple_ssrs_fp = CSV.open("tmp/multiple_ssrs.csv", "w")

multiple_ssrs_fp << ['Protocol ID', 'short title', 'ssr_id', 'Organization ID', 'Organization Name', 'SSR status', 'SSR pushed to Fulfillment?', 'SR ID', 'SR Submission date']

protocols = Protocol.joins(:sub_service_requests).where(sub_service_requests: { organization_id: organizations }).distinct
protocols = Protocol.joins(:service_requests).group('protocols.id').having('count(protocol_id) >= 2').to_a

bar1 = ProgressBar.new(protocols.count)
protocols.each do |protocol|

# Grab SSR's
sub_service_requests = protocol.sub_service_requests.where(organization_id: organizations)
sub_service_requests = protocol.sub_service_requests

# If we have multiple SSR's
if sub_service_requests.count > 1
Expand All @@ -35,7 +36,7 @@ task :merge_srs => :environment do

multiple_ssrs_fp.close

protocols = Protocol.joins(:sub_service_requests).where(sub_service_requests: { organization_id: organizations }).distinct
protocols = Protocol.joins(:service_requests).group('protocols.id').having('count(protocol_id) >= 2').to_a
bar2 = ProgressBar.new(protocols.count)
protocols.each do |protocol|

Expand Down Expand Up @@ -87,7 +88,7 @@ task :merge_srs => :environment do
protocol.sub_service_requests.where.not(service_request_id: recent_service_request.id).each do |ssr|
ssr.service_request_id = recent_service_request.id
ssr.audit_comment = AUDIT_COMMENT
ssr.save
ssr.save(validate: false)
end
line_items = LineItem.joins(:service_request).where(service_requests: { protocol_id: protocol.id }).where.not(service_request_id: recent_service_request.id)
line_items.each do |li|
Expand All @@ -108,6 +109,8 @@ task :merge_srs => :environment do

bar2.increment!
end

record_odd_balls
end

def delete_empty_srs(protocol)
Expand All @@ -126,4 +129,16 @@ def delete_empty_srs(protocol)
sr.audits.last.update(comment: AUDIT_COMMENT)
end
end

# If we still have multiple service request we have a problem
def record_odd_balls
protocols = Protocol.joins(:service_requests).group('protocols.id').having('count(protocol_id) >= 2').to_a

if protocols.count != 0
puts 'Oops, found some oddballs.'
protocols.each do |protocol|
puts "Protocol ID: #{protocol.id}"
end
end
end
end