mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 13:36:15 +02:00
51 lines
1.5 KiB
Ruby
51 lines
1.5 KiB
Ruby
class SendEInvoiceLegacyJob < ApplicationJob
|
|
discard_on HTTPClient::TimeoutError
|
|
|
|
def perform(invoice_id, payable: true)
|
|
logger.info "Started to process e-invoice for invoice_id #{invoice_id}"
|
|
invoice = Invoice.find_by(id: invoice_id)
|
|
return unless need_to_process_invoice?(invoice: invoice, payable: payable)
|
|
|
|
process(invoice: invoice, payable: payable)
|
|
rescue StandardError => e
|
|
log_error(invoice: invoice, error: e)
|
|
raise e
|
|
end
|
|
|
|
private
|
|
|
|
def need_to_process_invoice?(invoice:, payable:)
|
|
logger.info "Checking if need to process e-invoice #{invoice}, payable: #{payable}"
|
|
unprocessable = invoice.do_not_send_e_invoice? && (invoice.monthly_invoice ? true : payable)
|
|
return false if invoice.blank?
|
|
return false if unprocessable
|
|
|
|
true
|
|
end
|
|
|
|
def process(invoice:, payable:)
|
|
invoice.to_e_invoice(payable: payable).deliver unless Rails.env.development?
|
|
invoice.update(e_invoice_sent_at: Time.zone.now)
|
|
log_success(invoice)
|
|
end
|
|
|
|
def log_success(invoice)
|
|
id = invoice.try(:id) || invoice
|
|
message = "E-Invoice for an invoice with ID # #{id} was sent successfully"
|
|
logger.info message
|
|
end
|
|
|
|
def log_error(invoice:, error:)
|
|
id = invoice.try(:id) || invoice
|
|
message = <<~TEXT.squish
|
|
There was an error sending e-invoice for invoice with ID # #{id}.
|
|
The error message was the following: #{error}
|
|
This job will retry.
|
|
TEXT
|
|
logger.error message
|
|
end
|
|
|
|
def logger
|
|
Rails.logger
|
|
end
|
|
end
|