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

View file

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

View file

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

View file

@ -54,7 +54,7 @@ class Registrar < ApplicationRecord
end end
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 vat_rate = ::Invoice::VatRateCalculator.new(registrar: self).calculate
invoice = invoices.create!( invoice = invoices.create!(
@ -99,7 +99,7 @@ class Registrar < ApplicationRecord
} }
] ]
) )
SendEInvoiceJob.enqueue(invoice.id) unless paid SendEInvoiceJob.enqueue(invoice.id, payable: payable)
invoice invoice
end end