Files
Emiliano Frascaro a5e781729c 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>
2026-06-02 17:10:12 +02:00

64 lines
1.6 KiB
Ruby

module Billing
class Invoice < ApplicationRecord
self.table_name = "billing_invoices"
STATUSES = %w[draft issued sent cancelled].freeze
SOURCES = %w[manual aruba].freeze
belongs_to :club
belongs_to :billing_payment, class_name: "Billing::Payment", optional: true
has_one_attached :pdf
validates :number, presence: true, uniqueness: { scope: :club_id }
validates :issued_on, presence: true
validates :amount_cents, numericality: { greater_than: 0 }
validates :currency, presence: true
validates :status, inclusion: { in: STATUSES }
validates :source, inclusion: { in: SOURCES }
validates :billing_payment_id, uniqueness: true, allow_nil: true
validate :pdf_present_when_issued, on: :issue
scope :recent, -> { order(issued_on: :desc, created_at: :desc) }
def amount_euros
amount_cents / 100.0
end
def formatted_amount
format("%.2f €", amount_euros)
end
def downloadable?
pdf.attached? && status.in?(%w[issued sent])
end
def draft?
status == "draft"
end
def display_number
n = number.to_s.strip
cleaned = n.gsub(/\baruba[-_\s]*/i, "").strip
cleaned.presence || n
end
def display_status
{
"draft" => "In preparazione",
"issued" => "Emessa",
"sent" => "Inviata",
"cancelled" => "Annullata"
}[status] || status
end
private
def pdf_present_when_issued
return unless validation_context == :issue
return if pdf.attached?
errors.add(:pdf, "è obbligatorio per emettere la fattura")
end
end
end