Billing: fatture manuali, profilo obbligatorio e piani Stripe mensile/annuale.
Rimuove link al portale Stripe in area cliente, aggiunge flusso admin per PDF fattura e email, blocca checkout senza dati di fatturazione, allinea prezzi a €4,90/€39,90 e €9,90/€69,90, locale italiano e documentazione deploy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,33 +1,90 @@
|
||||
module Admin
|
||||
class BillingInvoicesController < BaseController
|
||||
before_action :set_club
|
||||
before_action :set_invoice, only: %i[edit update]
|
||||
|
||||
def index
|
||||
@invoices = @club.billing_invoices.recent
|
||||
@payments = @club.billing_payments.recent.includes(:invoice)
|
||||
@invoices = @club.billing_invoices.recent.includes(:billing_payment, pdf_attachment: :blob)
|
||||
end
|
||||
|
||||
def new
|
||||
@invoice = @club.billing_invoices.build(issued_on: Date.current, currency: "eur", source: "aruba")
|
||||
@payment = @club.billing_payments.find_by(id: params[:billing_payment_id]) if params[:billing_payment_id].present?
|
||||
if @payment&.invoice.present?
|
||||
redirect_to edit_admin_club_billing_invoice_path(@club, @payment.invoice), alert: "Esiste già una fattura per questo pagamento."
|
||||
return
|
||||
end
|
||||
|
||||
@invoice = @club.billing_invoices.build(
|
||||
issued_on: Date.current,
|
||||
currency: "eur",
|
||||
source: "manual",
|
||||
status: "draft",
|
||||
billing_payment: @payment,
|
||||
amount_cents: @payment&.amount_cents,
|
||||
number: suggested_invoice_number
|
||||
)
|
||||
end
|
||||
|
||||
def create
|
||||
@invoice = @club.billing_invoices.build(invoice_params)
|
||||
@invoice.source = "aruba"
|
||||
@invoice.source = "manual"
|
||||
@invoice.status = "draft"
|
||||
|
||||
if @invoice.save
|
||||
redirect_to admin_club_billing_invoices_path(@club), notice: "Fattura #{@invoice.number} registrata."
|
||||
redirect_to edit_admin_club_billing_invoice_path(@club, @invoice),
|
||||
notice: "Bozza fattura #{@invoice.number} creata. Carica il PDF e invia al cliente."
|
||||
else
|
||||
@payment = @invoice.billing_payment
|
||||
flash.now[:alert] = @invoice.errors.full_messages.join(", ")
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
@payment = @invoice.billing_payment
|
||||
end
|
||||
|
||||
def update
|
||||
@invoice.assign_attributes(invoice_params.except(:pdf))
|
||||
|
||||
if issuing?
|
||||
Billing::IssueInvoice.call(invoice: @invoice, pdf: params.dig(:billing_invoice, :pdf))
|
||||
redirect_to admin_club_billing_invoices_path(@club),
|
||||
notice: "Fattura #{@invoice.number} emessa e inviata a #{@club.billing_email}."
|
||||
elsif @invoice.save
|
||||
redirect_to admin_club_billing_invoices_path(@club), notice: "Fattura #{@invoice.number} aggiornata."
|
||||
else
|
||||
@payment = @invoice.billing_payment
|
||||
flash.now[:alert] = @invoice.errors.full_messages.join(", ")
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
rescue Billing::IssueInvoice::Error => e
|
||||
@payment = @invoice.billing_payment
|
||||
flash.now[:alert] = e.message
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_club
|
||||
@club = Club.find(params[:club_id])
|
||||
end
|
||||
|
||||
def set_invoice
|
||||
@invoice = @club.billing_invoices.find(params[:id])
|
||||
end
|
||||
|
||||
def issuing?
|
||||
params[:commit].to_s == "Emetti e invia via email"
|
||||
end
|
||||
|
||||
def suggested_invoice_number
|
||||
year = Date.current.year
|
||||
count = @club.billing_invoices.where("number LIKE ?", "%#{year}%").count + 1
|
||||
"#{year}/#{count}"
|
||||
end
|
||||
|
||||
def invoice_params
|
||||
params.require(:billing_invoice).permit(
|
||||
:number,
|
||||
@@ -35,8 +92,6 @@ module Admin
|
||||
:amount_cents,
|
||||
:amount_euros,
|
||||
:currency,
|
||||
:status,
|
||||
:aruba_document_id,
|
||||
:billing_payment_id,
|
||||
:notes,
|
||||
:pdf
|
||||
|
||||
Reference in New Issue
Block a user