Merge pull request #2011 from internetee/2009-mark-internal-invoice-as-paid

Mark internal invoices as paid prior to sending
This commit is contained in:
Timo Võhmar 2021-06-01 13:08:13 +03:00 committed by GitHub
commit e8c19d16e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 60 additions and 16 deletions

View file

@ -21,7 +21,7 @@ class SendEInvoiceJob < ApplicationJob
end
def process(invoice:, payable:)
invoice.to_e_invoice(payable: payable).deliver
invoice.to_e_invoice(payable: payable).deliver unless Rails.env.development?
invoice.update(e_invoice_sent_at: Time.zone.now)
log_success(invoice)
end

View file

@ -1,9 +1,10 @@
class InvoiceMailer < ApplicationMailer
def invoice_email(invoice:, recipient:)
def invoice_email(invoice:, recipient:, paid: false)
@invoice = invoice
subject = default_i18n_subject(invoice_number: invoice.number)
subject << I18n.t('invoice.already_paid') if paid
attachments["invoice-#{invoice.number}.pdf"] = invoice.as_pdf
mail(to: recipient, subject: subject)
end
end
end

View file

@ -15,7 +15,7 @@ module Invoice::Cancellable
end
def cancelled?
cancelled_at
cancelled_at.present?
end
def not_cancelled?

View file

@ -105,7 +105,7 @@ class Invoice < ApplicationRecord
end
def do_not_send_e_invoice?
e_invoice_sent? || cancelled? || paid?
e_invoice_sent? || cancelled?
end
def e_invoice_sent?

View file

@ -101,10 +101,11 @@ class Registrar < ApplicationRecord
)
unless payable
InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email).deliver_now
InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email, paid: !payable)
.deliver_later(wait: 1.minute)
end
SendEInvoiceJob.perform_later(invoice.id, payable)
SendEInvoiceJob.perform_now(invoice.id, payable)
invoice
end

View file

@ -470,7 +470,8 @@ en:
price: 'Price'
total: 'Total'
paid_at: 'Paid at'
invoice: 'Invoice'
invoice:
already_paid: " (already paid)"
bank_statements: 'Bank statements'
back_to_bank_statements: 'Back to bank statements'
back_to_bank_statement: 'Back to bank statement'

View file

@ -6,3 +6,5 @@ et:
# Don't forget the nil at the beginning; there's no such thing as a 0th month
month_names: [~, Jaanuar, Veebruar, Märts, Aprill, Mai, Juuni, Juuli, August, September, Oktoober, November, Detsember]
emails: "Meillaadressid"
invoice:
already_paid: " (juba makstud)"

View file

@ -22,6 +22,30 @@ namespace :invoices do
end
end
process_transactions(incoming_transactions)
puts "Transactions processed: #{incoming_transactions.size}"
end
task test_payments: :environment do
registrar = Invoice.last.buyer
transactions = [OpenStruct.new(amount: 0.1,
currency: 'EUR',
date: Time.zone.today,
payment_reference_number: registrar.reference_no,
payment_description: "description #{registrar.reference_no}")]
process_transactions(transactions)
puts 'Last registrar invoice is'
pp registrar.invoices.last
puts "Last invoice paid at #{registrar.invoices.last.receipt_date}"
end
def log(msg)
@log ||= Logger.new(STDOUT)
@log.info(msg)
end
def process_transactions(incoming_transactions)
if incoming_transactions.any?
log 'Got incoming transactions'
log incoming_transactions
@ -51,12 +75,5 @@ namespace :invoices do
else
log 'Got no incoming transactions parsed, aborting'
end
puts "Transactions processed: #{incoming_transactions.size}"
end
def log(msg)
@log ||= Logger.new(STDOUT)
@log.info(msg)
end
end

View file

@ -1,6 +1,6 @@
require 'test_helper'
class ProcessPaymentsTaskTest < ActiveSupport::TestCase
class ProcessPaymentsTaskTest < ActiveJob::TestCase
setup do
@payment_amount = payment_amount = 0.1
@payment_currency = payment_currency = 'EUR'
@ -162,6 +162,28 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
assert_equal 0.1, registrar.invoices.last.total
end
def test_topup_creates_invoice_and_send_it_as_paid
registrar = registrars(:bestnames)
@invoice.payment_orders.destroy_all
@invoice.destroy
perform_enqueued_jobs do
run_task
end
invoice = Invoice.last
assert invoice.paid?
assert_not invoice.e_invoice_sent_at.blank?
pdf_source = Invoice::PdfGenerator.new(invoice)
pdf_source.send(:invoice_html).include?('Receipt date')
email= ActionMailer::Base.deliveries.last
assert email.subject.include?('already paid')
assert_equal 0.1, registrar.invoices.last.total
end
def test_output
assert_output "Transactions processed: 1\n" do
run_task