Destroy e-invoice sending job if invoice sent, cancelled or paid

This commit is contained in:
Alex Sherman 2020-02-05 14:24:09 +05:00
parent 47e601f3cd
commit 187ce318a2
2 changed files with 15 additions and 5 deletions

View file

@ -1,6 +1,6 @@
class SendEInvoiceJob < Que::Job class SendEInvoiceJob < Que::Job
def run(invoice) def run(invoice)
return if invoice.e_invoice_sent_at destroy if invoice.do_not_send_e_invoice?
invoice.to_e_invoice.deliver invoice.to_e_invoice.deliver
@ -9,7 +9,7 @@ class SendEInvoiceJob < Que::Job
log_success(invoice) log_success(invoice)
destroy destroy
end end
rescue StandardError => e rescue Exception => e
log_error(invoice: invoice, error: e) log_error(invoice: invoice, error: e)
raise e raise e
end end
@ -17,13 +17,15 @@ class SendEInvoiceJob < Que::Job
private private
def log_success(invoice) def log_success(invoice)
message = "E-Invoice for an invoice with ID # #{invoice.id} was sent successfully" id = invoice.try(:id) || invoice
message = "E-Invoice for an invoice with ID # #{id} was sent successfully"
logger.info message logger.info message
end end
def log_error(invoice:, error:) def log_error(invoice:, error:)
id = invoice.try(:id) || invoice
message = <<~TEXT.squish message = <<~TEXT.squish
There was an error sending e-invoice for invoice with ID # #{invoice.id}. There was an error sending e-invoice for invoice with ID # #{id}.
The error message was the following: #{error}. The error message was the following: #{error}.
This job will retry This job will retry
TEXT TEXT

View file

@ -102,6 +102,14 @@ class Invoice < ApplicationRecord
generator.generate generator.generate
end end
def do_not_send_e_invoice?
e_invoice_sent? || cancelled? || paid?
end
def e_invoice_sent?
e_invoice_sent_at.present?
end
private private
def apply_default_buyer_vat_no def apply_default_buyer_vat_no