module Admin class BillingController < BaseController def index @pending_payments = pending_scope.recent @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: "Fattura caricata e inviata a #{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 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 billing_redirect_params(payment) { club_id: payment.club_id, anchor: "payment-#{payment.id}" }.compact end end end