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

View file

@ -102,6 +102,14 @@ class Invoice < ApplicationRecord
generator.generate
end
def do_not_send_e_invoice?
e_invoice_sent? || cancelled? || paid?
end
def e_invoice_sent?
e_invoice_sent_at.present?
end
private
def apply_default_buyer_vat_no
@ -111,4 +119,4 @@ class Invoice < ApplicationRecord
def calculate_total
self.total = subtotal + vat_amount
end
end
end