module Admin class BillingController < BaseController def index @pending_payments = pending_scope.recent @pending_transfers = transfer_scope @completed_payments = Billing::Payment.with_invoice_pdf.includes(:club, :invoice).recent.limit(40) @filter_club = Club.find_by(id: params[:club_id]) if params[:club_id].present? @clubs = Club.order(:name) end def attach_pdf payment = Billing::Payment.find(params[:payment_id]) Billing::AttachPaymentInvoice.call(payment: payment, pdf: params[:pdf]) redirect_to admin_billing_path(billing_redirect_params(payment)), notice: t("admin.flash.invoice_uploaded", email: payment.club.billing_email) rescue Billing::AttachPaymentInvoice::Error, Billing::IssueInvoice::Error => e payment ||= Billing::Payment.find_by(id: params[:payment_id]) redirect_to admin_billing_path(payment ? billing_redirect_params(payment) : {}), alert: e.message end def confirm_transfer order = Billing::TransferOrder.find(params[:id]) Billing::ConfirmBankTransfer.call( order: order, admin: current_admin_account, pdf: params[:pdf] ) redirect_to admin_billing_path(club_id: order.club_id), notice: t("admin.flash.transfer_confirmed", plan: order.plan.name, club: order.club.name) rescue Billing::ConfirmBankTransfer::Error, Billing::AttachPaymentInvoice::Error, Billing::IssueInvoice::Error => e redirect_to admin_billing_path, alert: e.message end def cancel_transfer order = Billing::TransferOrder.find(params[:id]) order.cancel! redirect_to admin_billing_path(club_id: order.club_id), notice: t("admin.flash.transfer_cancelled") end private def pending_scope scope = Billing::Payment.awaiting_invoice_pdf.includes(:club, invoice: { pdf_attachment: :blob }) scope = scope.where(club_id: params[:club_id]) if params[:club_id].present? scope end def transfer_scope scope = Billing::TransferOrder.awaiting_payment.includes(:club, :requested_by_user) scope = scope.where(club_id: params[:club_id]) if params[:club_id].present? scope end def billing_redirect_params(payment) { club_id: payment.club_id, anchor: "payment-#{payment.id}" }.compact end end end