diff --git a/app/controllers/admin/payroll_runs_controller.rb b/app/controllers/admin/payroll_runs_controller.rb index 8b0c0524f2..1604cd1a3a 100644 --- a/app/controllers/admin/payroll_runs_controller.rb +++ b/app/controllers/admin/payroll_runs_controller.rb @@ -49,5 +49,16 @@ def show @payroll_run = PayrollRun.where(id: params[:id]).includes({claims: [:eligibility]}, {payments: [{claims: [:eligibility]}]}).first @pagy, @payments = pagy(@payroll_run.payments.ordered.includes(claims: [:eligibility]).includes(:topups)) end + + def destroy + if PayrollRun.allow_destroy? + PayrollRun.find(params[:id]).destroy! + redirect_to admin_payroll_runs_path, notice: "Payroll run deleted" + else + redirect_to( + admin_payroll_runs_path, alert: "Payroll run deletion is not allowed" + ) + end + end end end diff --git a/app/models/payroll_run.rb b/app/models/payroll_run.rb index c65d603c56..2c6adde813 100644 --- a/app/models/payroll_run.rb +++ b/app/models/payroll_run.rb @@ -17,6 +17,12 @@ class PayrollRun < ApplicationRecord scope :this_month, -> { where(created_at: DateTime.now.all_month) } + def self.allow_destroy? + ENV["ENVIRONMENT_NAME"].start_with?("review") || + ENV["ENVIRONMENT_NAME"] == "test" || + Rails.env.development? + end + def total_award_amount payments.sum(:award_amount) end diff --git a/app/views/admin/payroll_runs/show.html.erb b/app/views/admin/payroll_runs/show.html.erb index 9a050dc397..fee1b631b5 100644 --- a/app/views/admin/payroll_runs/show.html.erb +++ b/app/views/admin/payroll_runs/show.html.erb @@ -157,4 +157,15 @@ <%== render partial: 'pagination', locals: { pagy: @pagy } %> + + <% if PayrollRun.allow_destroy? %> +