- Add Payout model with associations to User and Event - Create payout requests for completed events with proper earnings calculation - Exclude refunded tickets from payout calculations - Add promoter dashboard views for managing payouts - Implement admin interface for processing payouts - Integrate with Stripe for actual payment processing - Add comprehensive tests for payout functionality Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
66 lines
2.0 KiB
Ruby
66 lines
2.0 KiB
Ruby
class Promoter::PayoutsController < ApplicationController
|
|
before_action :authenticate_user!
|
|
before_action :ensure_promoter!
|
|
before_action :set_event, only: [:show, :create]
|
|
|
|
# List all payouts for the current promoter
|
|
def index
|
|
@payouts = current_user.payouts
|
|
.includes(:event)
|
|
.order(created_at: :desc)
|
|
.page(params[:page])
|
|
end
|
|
|
|
# Show payout details
|
|
def show
|
|
@payout = @event.payouts.find(params[:id])
|
|
end
|
|
|
|
# Create a new payout request
|
|
def create
|
|
# Check if event can request payout
|
|
unless @event.can_request_payout?
|
|
redirect_to promoter_event_path(@event), alert: "Payout cannot be requested for this event."
|
|
return
|
|
end
|
|
|
|
# Calculate payout amount
|
|
total_earnings_cents = @event.total_earnings_cents
|
|
total_fees_cents = @event.total_fees_cents
|
|
net_earnings_cents = @event.net_earnings_cents
|
|
|
|
# Count orders
|
|
total_orders_count = @event.orders.where(status: ['paid', 'completed']).count
|
|
refunded_orders_count = @event.tickets.where(status: 'refunded').joins(:order).where(orders: {status: ['paid', 'completed']}).count
|
|
|
|
# Create payout record
|
|
@payout = @event.payouts.build(
|
|
user: current_user,
|
|
amount_cents: total_earnings_cents,
|
|
fee_cents: total_fees_cents,
|
|
total_orders_count: total_orders_count,
|
|
refunded_orders_count: refunded_orders_count
|
|
)
|
|
|
|
if @payout.save
|
|
# Update event payout status
|
|
@event.update!(payout_status: :requested, payout_requested_at: Time.current)
|
|
|
|
redirect_to promoter_payout_path(@payout), notice: "Payout request submitted successfully."
|
|
else
|
|
redirect_to promoter_event_path(@event), alert: "Failed to submit payout request: #{@payout.errors.full_messages.join(', ')}"
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def ensure_promoter!
|
|
unless current_user.promoter?
|
|
redirect_to dashboard_path, alert: "Access denied."
|
|
end
|
|
end
|
|
|
|
def set_event
|
|
@event = current_user.events.find(params[:event_id])
|
|
end
|
|
end |