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