Files
aperonight/app/controllers/admin/payouts_controller.rb
2025-09-18 00:18:02 +02:00

93 lines
3.2 KiB
Ruby

class Admin::PayoutsController < ApplicationController
before_action :authenticate_user!
before_action :ensure_admin!
before_action :set_payout, only: [ :show, :approve, :reject, :mark_processing, :mark_completed, :mark_failed ]
def index
@pending_payouts = Payout.pending.includes(:user, :event).order(created_at: :asc)
@approved_payouts = Payout.approved.includes(:user, :event).order(created_at: :asc)
@processing_payouts = Payout.processing.includes(:user, :event).order(created_at: :asc)
@completed_payouts = Payout.completed.includes(:user, :event).order(created_at: :desc).limit(10)
end
def show
@service = PayoutService.new(@payout)
@transfer_summary = @service.generate_transfer_summary
@banking_errors = @service.validate_banking_info
end
def approve
if @payout.approve!(current_user)
redirect_to admin_payout_path(@payout), notice: "Payout approved successfully."
else
redirect_to admin_payout_path(@payout), alert: "Cannot approve this payout."
end
end
def reject
reason = params[:rejection_reason].presence || "No reason provided"
if @payout.reject!(current_user, reason)
redirect_to admin_payouts_path, notice: "Payout rejected."
else
redirect_to admin_payout_path(@payout), alert: "Cannot reject this payout."
end
end
def mark_processing
transfer_reference = params[:bank_transfer_reference]
if @payout.mark_processing!(current_user, transfer_reference)
redirect_to admin_payout_path(@payout), notice: "Payout marked as processing."
else
redirect_to admin_payout_path(@payout), alert: "Cannot mark payout as processing."
end
end
def mark_completed
transfer_reference = params[:bank_transfer_reference]
if @payout.mark_completed!(current_user, transfer_reference)
redirect_to admin_payouts_path, notice: "Payout completed successfully."
else
redirect_to admin_payout_path(@payout), alert: "Cannot mark payout as completed."
end
end
def mark_failed
reason = params[:failure_reason].presence || "Transfer failed"
if @payout.mark_failed!(current_user, reason)
redirect_to admin_payouts_path, notice: "Payout marked as failed."
else
redirect_to admin_payout_path(@payout), alert: "Cannot mark payout as failed."
end
end
# Mark a payout as manually processed (for SEPA transfers, etc.)
def mark_as_manually_processed
@payout = Payout.find(params[:id])
if @payout.pending? || @payout.processing?
begin
@payout.mark_as_manually_processed!
redirect_to admin_payouts_path, notice: "Payout marked as manually processed. Please complete the bank transfer."
rescue => e
redirect_to admin_payouts_path, alert: "Failed to mark payout as manually processed: #{e.message}"
end
else
redirect_to admin_payouts_path, alert: "Cannot mark this payout as manually processed."
end
end
private
def set_payout
@payout = Payout.find(params[:id])
end
def ensure_admin!
# For now, we'll just check if the user is a professional user
# In a real app, you'd have an admin role check
unless current_user.promoter?
redirect_to dashboard_path, alert: "Access denied."
end
end
end