diff --git a/app/jobs/send_e_invoice_job.rb b/app/jobs/send_e_invoice_job.rb index e281db14d..91e068b9f 100644 --- a/app/jobs/send_e_invoice_job.rb +++ b/app/jobs/send_e_invoice_job.rb @@ -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 diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 8122e46dd..87f27af36 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -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 diff --git a/app/models/invoice/e_invoice_generator.rb b/app/models/invoice/e_invoice_generator.rb index 9a2ab2e01..d2963b93e 100644 --- a/app/models/invoice/e_invoice_generator.rb +++ b/app/models/invoice/e_invoice_generator.rb @@ -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 \ No newline at end of file +end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 25380b4ef..86ff5ef4d 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -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