Admin fatturazione: coda pagamenti senza PDF e upload diretto.
Lista globale dei pagamenti da fatturare con dati intestazione, caricamento PDF per riga che crea la fattura e invia email; rimuove il flusso bozza manuale. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
37
backend/app/services/billing/attach_payment_invoice.rb
Normal file
37
backend/app/services/billing/attach_payment_invoice.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
module Billing
|
||||
class AttachPaymentInvoice
|
||||
class Error < StandardError; end
|
||||
|
||||
def self.call(payment:, pdf:)
|
||||
new(payment: payment, pdf: pdf).call
|
||||
end
|
||||
|
||||
def initialize(payment:, pdf:)
|
||||
@payment = payment
|
||||
@pdf = pdf
|
||||
end
|
||||
|
||||
def call
|
||||
raise Error, "Seleziona un file PDF" if @pdf.blank?
|
||||
raise Error, "Pagamento non valido" unless @payment.status == "paid"
|
||||
|
||||
club = @payment.club
|
||||
invoice = @payment.invoice || build_invoice!(club)
|
||||
IssueInvoice.call(invoice: invoice, pdf: @pdf)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_invoice!(club)
|
||||
club.billing_invoices.create!(
|
||||
number: InvoiceNumber.next_for(club),
|
||||
issued_on: @payment.paid_at&.to_date || Date.current,
|
||||
amount_cents: @payment.amount_cents,
|
||||
currency: @payment.currency.presence || "eur",
|
||||
status: "draft",
|
||||
source: "manual",
|
||||
billing_payment: @payment
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user