Files
aperonight/app/models/earning.rb
kbe 3c1e17c2af feat(payouts): implement promoter earnings viewing, request flow, and admin Stripe processing with webhooks
Add model methods for accurate net calculations (€0.50 + 1.5% fees), eligibility, refund handling
Update promoter/payouts controller for index (pending events), create (eligibility checks)
Integrate admin processing via Stripe::Transfer, webhook for status sync
Enhance views: index pending cards, events/show preview/form
Add comprehensive tests (models, controllers, service, integration); run migrations
2025-09-17 02:07:52 +02:00

55 lines
1.6 KiB
Ruby

class Earning < ApplicationRecord
def self.create_from_order(order)
return unless order.paid? || order.completed?
gross_cents = order.tickets.active.sum(:price_cents)
fee_cents = order.tickets.active.sum do |ticket|
50 + (ticket.price_cents * 0.015).to_i
end
amount_cents = gross_cents - fee_cents
create!(
event: order.event,
user: order.event.user,
order: order,
amount_cents: amount_cents,
fee_cents: fee_cents,
status: :pending
)
end
# === Relations ===
belongs_to :event
belongs_to :user
belongs_to :order
# === Enums ===
enum :status, { pending: 0, paid: 1 }
# === Validations ===
validates :amount_cents, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :fee_cents, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :net_amount_cents, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
validates :status, presence: true
validates :stripe_payout_id, allow_blank: true, uniqueness: true
# Recalculate earning based on active tickets in the order
def recalculate!
return unless order.present?
active_tickets = order.tickets.active
if active_tickets.empty?
update!(amount_cents: 0, fee_cents: 0)
else
gross_cents = active_tickets.sum(:price_cents)
fee_cents = active_tickets.sum do |ticket|
50 + (ticket.price_cents * 0.015).to_i
end
update!(amount_cents: gross_cents - fee_cents, fee_cents: fee_cents)
end
end
def recalculate_on_refund(order)
recalculate!
end
end