module Billing class IssueInvoice class Error < StandardError; end MAILER_ACTIONS = %i[invoice_pdf plan_activated_with_invoice].freeze def self.call(invoice:, pdf: nil, mailer_action: :invoice_pdf) new(invoice: invoice, pdf: pdf, mailer_action: mailer_action).call end def initialize(invoice:, pdf: nil, mailer_action: :invoice_pdf) @invoice = invoice @pdf = pdf @mailer_action = mailer_action.to_sym raise Error, "Azione email non valida" unless MAILER_ACTIONS.include?(@mailer_action) end def call raise Error, "Carica il file PDF della fattura" if @pdf.blank? && !@invoice.pdf.attached? attach_pdf!(@pdf) if @pdf.present? raise Error, "PDF mancante" unless @invoice.pdf.attached? recipient = @invoice.club.billing_email.presence raise Error, "Email di fatturazione non configurata per la società" if recipient.blank? @invoice.update!(status: "issued") mail = Billing::InvoiceMailer.with(invoice: @invoice).public_send(@mailer_action) if MatchLiveTv.deliver_mail(mail) @invoice.update!(status: "sent", emailed_at: Time.current) end @invoice end def attach_pdf!(upload) if upload.respond_to?(:tempfile) @invoice.pdf.attach( io: upload.tempfile, filename: upload.original_filename, content_type: upload.content_type.presence || "application/pdf" ) elsif upload.is_a?(Hash) @invoice.pdf.attach(**upload) elsif upload.is_a?(StringIO) @invoice.pdf.attach(io: upload, filename: "fattura.pdf", content_type: "application/pdf") else @invoice.pdf.attach(upload) end end end end