Send unpayable e-invoice when invoice has already been paid via wire transfer

This commit is contained in:
Karl Erik Õunapuu 2020-09-08 13:09:12 +03:00
parent 6b2aaf305f
commit 17188ec635
No known key found for this signature in database
GPG key ID: C9DD647298A34764
4 changed files with 15 additions and 12 deletions

View file

@ -1,8 +1,8 @@
class SendEInvoiceJob < Que::Job
def run(invoice_id)
invoice = run_condition(Invoice.find_by(id: invoice_id))
def run(invoice_id, payable: true)
invoice = run_condition(Invoice.find_by(id: invoice_id), payable: payable)
invoice.to_e_invoice.deliver
invoice.to_e_invoice(payable: payable).deliver
ActiveRecord::Base.transaction do
invoice.update(e_invoice_sent_at: Time.zone.now)
log_success(invoice)
@ -15,9 +15,9 @@ class SendEInvoiceJob < Que::Job
private
def run_condition(invoice)
def run_condition(invoice, payable: true)
destroy unless invoice
destroy if invoice.do_not_send_e_invoice?
destroy if invoice.do_not_send_e_invoice? && payable
invoice
end

View file

@ -99,8 +99,8 @@ class Invoice < ApplicationRecord
generator.as_pdf
end
def to_e_invoice
generator = Invoice::EInvoiceGenerator.new(self)
def to_e_invoice(payable: true)
generator = Invoice::EInvoiceGenerator.new(self, payable: payable)
generator.generate
end
@ -120,7 +120,7 @@ class Invoice < ApplicationRecord
wo_vat = transaction.sum / (1 + (vat / 100))
registrar_user.issue_prepayment_invoice(amount: wo_vat,
description: 'Direct top-up via bank transfer',
paid: true)
payable: false)
end
private

View file

@ -1,9 +1,11 @@
class Invoice
class EInvoiceGenerator
attr_reader :invoice
attr_reader :payable
def initialize(invoice)
def initialize(invoice, payable)
@invoice = invoice
@payable = payable
end
def generate
@ -70,9 +72,10 @@ class Invoice
i.total = invoice.total
i.currency = invoice.currency
i.delivery_channel = %i[internet_bank portal]
i.payable = payable
end
EInvoice::EInvoice.new(date: Time.zone.today, invoice: e_invoice_invoice)
end
end
end
end

View file

@ -54,7 +54,7 @@ class Registrar < ApplicationRecord
end
end
def issue_prepayment_invoice(amount, description = nil, paid: false)
def issue_prepayment_invoice(amount, description = nil, payable: true)
vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate
invoice = invoices.create!(
@ -99,7 +99,7 @@ class Registrar < ApplicationRecord
}
]
)
SendEInvoiceJob.enqueue(invoice.id) unless paid
SendEInvoiceJob.enqueue(invoice.id, payable: payable)
invoice
end