diff --git a/app/controllers/admin/invoices/delivery_controller.rb b/app/controllers/admin/invoices/delivery_controller.rb
new file mode 100644
index 0000000000..e7693fbd39
--- /dev/null
+++ b/app/controllers/admin/invoices/delivery_controller.rb
@@ -0,0 +1,26 @@
+module Admin
+ module Invoices
+ class DeliveryController < BaseController
+ before_action :find_invoice
+
+ def new
+ authorize! :manage, @invoice
+ @recipient = @invoice.buyer.billing_email
+ end
+
+ def create
+ authorize! :manage, @invoice
+
+ InvoiceMailer.invoice_email(invoice: @invoice, recipient: params[:recipient]).deliver_now
+
+ redirect_to admin_invoice_path(@invoice), notice: t('.delivered')
+ end
+
+ private
+
+ def find_invoice
+ @invoice = Invoice.find(params[:invoice_id])
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb
index 10610983dc..fc06f65853 100644
--- a/app/controllers/admin/invoices_controller.rb
+++ b/app/controllers/admin/invoices_controller.rb
@@ -2,7 +2,7 @@ module Admin
class InvoicesController < BaseController
load_and_authorize_resource
- before_action :set_invoice, only: [:forward, :download_pdf]
+ before_action :set_invoice, only: [:download_pdf]
def new
@deposit = Deposit.new
@@ -37,21 +37,6 @@ def cancel
redirect_to [:admin, @invoice], notice: t('.cancelled')
end
- def forward
- @invoice.billing_email = @invoice.buyer.billing_email
-
- return unless request.post?
-
- @invoice.billing_email = params[:invoice][:billing_email]
-
- if @invoice.forward(render_to_string('registrar/invoices/pdf', layout: false))
- flash[:notice] = t(:invoice_forwared)
- redirect_to([:admin, @invoice])
- else
- flash.now[:alert] = t(:failed_to_forward_invoice)
- end
- end
-
def download_pdf
pdf = @invoice.pdf(render_to_string('registrar/invoices/pdf', layout: false))
send_data pdf, filename: @invoice.pdf_name
diff --git a/app/controllers/registrar/invoices/delivery_controller.rb b/app/controllers/registrar/invoices/delivery_controller.rb
new file mode 100644
index 0000000000..e48d3b38aa
--- /dev/null
+++ b/app/controllers/registrar/invoices/delivery_controller.rb
@@ -0,0 +1,26 @@
+class Registrar
+ module Invoices
+ class DeliveryController < BaseController
+ before_action :find_invoice
+
+ def new
+ authorize! :manage, @invoice
+ @recipient = @invoice.buyer.billing_email
+ end
+
+ def create
+ authorize! :manage, @invoice
+
+ InvoiceMailer.invoice_email(invoice: @invoice, recipient: params[:recipient]).deliver_now
+
+ redirect_to registrar_invoice_path(@invoice), notice: t('.delivered')
+ end
+
+ private
+
+ def find_invoice
+ @invoice = Invoice.find(params[:invoice_id])
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/app/controllers/registrar/invoices_controller.rb b/app/controllers/registrar/invoices_controller.rb
index 3736bfa6ab..fb84794245 100644
--- a/app/controllers/registrar/invoices_controller.rb
+++ b/app/controllers/registrar/invoices_controller.rb
@@ -2,7 +2,7 @@ class Registrar
class InvoicesController < BaseController
load_and_authorize_resource
- before_action :set_invoice, only: [:show, :forward, :download_pdf]
+ before_action :set_invoice, only: [:show, :download_pdf]
def index
params[:q] ||= {}
@@ -18,21 +18,6 @@ def index
def show;
end
- def forward
- @invoice.billing_email = @invoice.buyer.billing_email
-
- return unless request.post?
-
- @invoice.billing_email = params[:invoice][:billing_email]
-
- if @invoice.forward(render_to_string('pdf', layout: false))
- flash[:notice] = t(:invoice_forwared)
- redirect_to([:registrar, @invoice])
- else
- flash.now[:alert] = t(:failed_to_forward_invoice)
- end
- end
-
def cancel
@invoice.cancel
redirect_to [:registrar, @invoice], notice: t('.cancelled')
diff --git a/app/mailers/invoice_mailer.rb b/app/mailers/invoice_mailer.rb
index 74821c25a9..aa3309674a 100644
--- a/app/mailers/invoice_mailer.rb
+++ b/app/mailers/invoice_mailer.rb
@@ -1,17 +1,9 @@
class InvoiceMailer < ApplicationMailer
- include Que::Mailer
+ def invoice_email(invoice:, recipient:)
+ @invoice = invoice
- def invoice_email(invoice_id, html, billing_email)
- @invoice = Invoice.find_by(id: invoice_id)
- billing_email ||= @invoice.billing_email
- return unless @invoice
- return if whitelist_blocked?(billing_email)
-
- kit = PDFKit.new(html)
- pdf = kit.to_pdf
- invoice = @invoice
-
- attachments[invoice.pdf_name] = pdf
- mail(to: format(billing_email), subject: invoice)
+ subject = default_i18n_subject(invoice_number: invoice.number)
+ attachments[invoice.pdf_name] = invoice.invoice_pdf
+ mail(to: recipient, subject: subject)
end
-end
+end
\ No newline at end of file
diff --git a/app/models/invoice.rb b/app/models/invoice.rb
index 6942ce5b85..2c84a2e084 100644
--- a/app/models/invoice.rb
+++ b/app/models/invoice.rb
@@ -26,9 +26,6 @@ class Invoice < ActiveRecord::Base
scope :overdue, -> { unpaid.non_cancelled.where('due_date < ?', Time.zone.today) }
- attr_accessor :billing_email
- validates :billing_email, email_format: { message: :invalid }, allow_blank: true
-
validates :issue_date, presence: true
validates :due_date, :currency, :seller_name,
:seller_iban, :buyer_name, :items, presence: true
@@ -93,14 +90,6 @@ def pdf_name
"invoice-#{number}.pdf"
end
- def forward(html)
- return false unless valid?
- return false unless billing_email.present?
-
- InvoiceMailer.invoice_email(id, html, billing_email).deliver
- true
- end
-
def subtotal
items.map(&:item_sum_without_vat).reduce(:+)
end
@@ -119,6 +108,11 @@ def each
items.each { |item| yield item }
end
+ def invoice_pdf
+ generator = PDFKit.new(html)
+ generator.to_pdf
+ end
+
private
def apply_default_vat_rate
@@ -132,4 +126,12 @@ def apply_default_buyer_vat_no
def calculate_total
self.total = subtotal + vat_amount
end
+
+ def html
+ view = ActionView::Base.new(ActionController::Base.view_paths, { invoice: self })
+ view.class_eval do
+ include ApplicationHelper
+ end
+ view.render(file: 'registrar/invoices/pdf', layout: false)
+ end
end
diff --git a/app/views/admin/invoices/delivery/new.html.erb b/app/views/admin/invoices/delivery/new.html.erb
new file mode 100644
index 0000000000..384902e633
--- /dev/null
+++ b/app/views/admin/invoices/delivery/new.html.erb
@@ -0,0 +1,24 @@
+
+ - <%= link_to t('admin.invoices.index.header'), admin_invoices_path %>
+ - <%= link_to @invoice, admin_invoice_path(@invoice) %>
+
+
+
+
+<%= form_tag(admin_invoice_delivery_path(@invoice)) do %>
+
+
+
+ <%= label_tag :recipient %>
+ <%= email_field_tag :recipient, @recipient, autofocus: true, class: 'form-control' %>
+
+
+
+ <%= submit_tag t('.submit_btn'), class: 'btn btn-warning' %>
+
+
+
+
+<% end %>
\ No newline at end of file
diff --git a/app/views/admin/invoices/forward.haml b/app/views/admin/invoices/forward.haml
deleted file mode 100644
index 25f59d3ad9..0000000000
--- a/app/views/admin/invoices/forward.haml
+++ /dev/null
@@ -1,15 +0,0 @@
-- content_for :actions do
- = link_to(t(:back_to_invoice), admin_invoice_path(@invoice), class: 'btn btn-default')
-= render 'shared/title', name: t(:forward_invoice)
-
-= form_for([:admin, @invoice], url: { action: :forward }, method: :post) do |f|
- .row
- .col-md-4.col-md-offset-4
- = render 'shared/full_errors', object: @invoice
- .form-group
- = f.label :billing_email
- = f.text_field :billing_email, class: 'form-control', autocomplete: 'off'
-
- .row
- .col-md-12.text-right
- = button_tag(t(:forward), class: 'btn btn-warning')
diff --git a/app/views/admin/invoices/show.haml b/app/views/admin/invoices/show.haml
index bdeb69d6a5..4cd7fd5f4e 100644
--- a/app/views/admin/invoices/show.haml
+++ b/app/views/admin/invoices/show.haml
@@ -7,7 +7,7 @@
- if @invoice.unpaid?
= link_to(t(:payment_received), new_admin_bank_statement_path(invoice_id: @invoice.id), class: 'btn btn-default')
= link_to(t(:download), admin_invoice_download_pdf_path(@invoice), class: 'btn btn-default')
- = link_to(t(:forward), admin_invoice_forward_path(@invoice), class: 'btn btn-default')
+ = link_to(t('.deliver_btn'), new_admin_invoice_delivery_path(@invoice), class: 'btn btn-default')
- if @invoice.cancellable?
= link_to(t(:cancel), cancel_admin_invoice_path(@invoice), method: :patch, class: 'btn btn-warning')
= link_to(t(:back), admin_invoices_path, class: 'btn btn-default')
diff --git a/app/views/registrar/invoices/delivery/new.html.erb b/app/views/registrar/invoices/delivery/new.html.erb
new file mode 100644
index 0000000000..f600349e53
--- /dev/null
+++ b/app/views/registrar/invoices/delivery/new.html.erb
@@ -0,0 +1,24 @@
+
+ - <%= link_to t('registrar.invoices.index.header'), registrar_invoices_path %>
+ - <%= link_to @invoice, registrar_invoice_path(@invoice) %>
+
+
+
+
+<%= form_tag(registrar_invoice_delivery_path(@invoice)) do %>
+
+
+
+ <%= label_tag :recipient %>
+ <%= email_field_tag :recipient, @recipient, autofocus: true, class: 'form-control' %>
+
+
+
+ <%= submit_tag t('.submit_btn'), class: 'btn btn-warning' %>
+
+
+
+
+<% end %>
\ No newline at end of file
diff --git a/app/views/registrar/invoices/forward.haml b/app/views/registrar/invoices/forward.haml
deleted file mode 100644
index 2f2b3a66f1..0000000000
--- a/app/views/registrar/invoices/forward.haml
+++ /dev/null
@@ -1,15 +0,0 @@
-- content_for :actions do
- = link_to(t(:back_to_invoice), registrar_invoice_path(@invoice), class: 'btn btn-default')
-= render 'shared/title', name: t(:forward_invoice)
-
-= form_for([:registrar, @invoice], url: { action: :forward }, method: :post) do |f|
- .row
- .col-md-4.col-md-offset-4
- = render 'shared/full_errors', object: @invoice
- .form-group
- = f.label :billing_email
- = f.text_field :billing_email, class: 'form-control', autocomplete: 'off'
-
- .row
- .col-md-12.text-right
- = button_tag(t(:forward), class: 'btn btn-warning')
diff --git a/app/views/registrar/invoices/show.haml b/app/views/registrar/invoices/show.haml
index f44660c9a4..d93ce83705 100644
--- a/app/views/registrar/invoices/show.haml
+++ b/app/views/registrar/invoices/show.haml
@@ -1,6 +1,6 @@
- content_for :actions do
= link_to(t(:download), download_pdf_registrar_invoice_path(@invoice), class: 'btn btn-default')
- = link_to(t(:forward), forward_registrar_invoice_path(@invoice), class: 'btn btn-default')
+ = link_to(t('.deliver_btn'), new_registrar_invoice_delivery_path(@invoice), class: 'btn btn-default')
- if @invoice.cancellable?
= link_to(t(:cancel), cancel_registrar_invoice_path(@invoice), method: :patch, class: 'btn btn-warning')
= link_to(t(:back), registrar_invoices_path, class: 'btn btn-default')
diff --git a/config/locales/admin/invoices.en.yml b/config/locales/admin/invoices.en.yml
index fba6ea26d9..d72eeea511 100644
--- a/config/locales/admin/invoices.en.yml
+++ b/config/locales/admin/invoices.en.yml
@@ -1,5 +1,11 @@
en:
admin:
invoices:
+ index:
+ header: Invoices
+
+ show:
+ deliver_btn: Send
+
cancel:
cancelled: Invoice has been cancelled
\ No newline at end of file
diff --git a/config/locales/admin/invoices/delivery.en.yml b/config/locales/admin/invoices/delivery.en.yml
new file mode 100644
index 0000000000..a678c7f29b
--- /dev/null
+++ b/config/locales/admin/invoices/delivery.en.yml
@@ -0,0 +1,10 @@
+en:
+ admin:
+ invoices:
+ delivery:
+ new:
+ header: Send invoice
+ submit_btn: Send
+
+ create:
+ delivered: Invoice has been sent
\ No newline at end of file
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 3e6e01a1cb..12cc56ab90 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -558,11 +558,6 @@ en:
registrant_head_title: 'EIS Registrant'
registrant_head_title_sufix: ' - EIS Registrant'
bind_manually: 'Bind manually'
- forward_invoice: 'Forward invoice'
- forward: 'Forward'
- back_to_invoice: 'Back to invoice'
- invoice_forwared: 'Invoice forwarded'
- failed_to_forward_invoice: 'Failed to forward invoice'
client: 'Client'
you_have_a_new_invoice: 'You have a new invoice.'
sincerely: 'Sincerely'
diff --git a/config/locales/mailers/invoice.en.yml b/config/locales/mailers/invoice.en.yml
new file mode 100644
index 0000000000..000dfb9cb4
--- /dev/null
+++ b/config/locales/mailers/invoice.en.yml
@@ -0,0 +1,4 @@
+en:
+ invoice_mailer:
+ invoice_email:
+ subject: Invoice no. %{invoice_number}
\ No newline at end of file
diff --git a/config/locales/registrar/invoices.en.yml b/config/locales/registrar/invoices.en.yml
index 8e79ee2e04..548ac46020 100644
--- a/config/locales/registrar/invoices.en.yml
+++ b/config/locales/registrar/invoices.en.yml
@@ -7,8 +7,13 @@ en:
go_to_intermediary: 'Go to intermediary'
pay_by_credit_card: Pay by credit card
payment_complete: Credit Card payment Complete
+
index:
+ header: Invoices
reset_btn: Reset
+ show:
+ deliver_btn: Send
+
cancel:
cancelled: Invoice has been cancelled
\ No newline at end of file
diff --git a/config/locales/registrar/invoices/delivery.en.yml b/config/locales/registrar/invoices/delivery.en.yml
new file mode 100644
index 0000000000..5178b2a719
--- /dev/null
+++ b/config/locales/registrar/invoices/delivery.en.yml
@@ -0,0 +1,10 @@
+en:
+ registrar:
+ invoices:
+ delivery:
+ new:
+ header: Send invoice
+ submit_btn: Send
+
+ create:
+ delivered: Invoice has been sent
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index 1c9350327c..05e1023d09 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -54,9 +54,10 @@
end
resources :invoices do
+ resource :delivery, controller: 'invoices/delivery', only: %i[new create]
+
member do
get 'download_pdf'
- match 'forward', via: [:post, :get]
patch 'cancel'
end
end
@@ -202,7 +203,7 @@
resources :invoices do
get 'download_pdf'
patch 'cancel', on: :member
- match 'forward', via: [:post, :get]
+ resource :delivery, controller: 'invoices/delivery', only: %i[new create]
end
resources :domains, except: %i[new create destroy] do
diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml
index d163ba732a..9957f787ae 100644
--- a/test/fixtures/invoices.yml
+++ b/test/fixtures/invoices.yml
@@ -9,6 +9,7 @@ one:
vat_rate: 0.1
total: 16.50
reference_no: 13
+ number: 1
for_payments_test:
number: 1
diff --git a/test/fixtures/registrars.yml b/test/fixtures/registrars.yml
index 7afaf8edf3..e4889326bd 100644
--- a/test/fixtures/registrars.yml
+++ b/test/fixtures/registrars.yml
@@ -10,7 +10,7 @@ bestnames:
country_code: US
accounting_customer_code: bestnames
language: en
- billing_email: billing@example.com
+ billing_email: billing@bestnames.best
website: https://bestnames.test
reference_no: 13
diff --git a/test/mailers/invoice_mailer_test.rb b/test/mailers/invoice_mailer_test.rb
new file mode 100644
index 0000000000..f84a9d5789
--- /dev/null
+++ b/test/mailers/invoice_mailer_test.rb
@@ -0,0 +1,22 @@
+require 'test_helper'
+
+class InvoiceMailerTest < ActiveSupport::TestCase
+ include ActionMailer::TestHelper
+
+ setup do
+ @invoice = invoices(:one)
+ ActionMailer::Base.deliveries.clear
+ end
+
+ def test_delivers_invoice_email
+ assert_equal 1, @invoice.number
+
+ email = InvoiceMailer.invoice_email(invoice: @invoice, recipient: 'billing@bestnames.test')
+ .deliver_now
+
+ assert_emails 1
+ assert_equal ['billing@bestnames.test'], email.to
+ assert_equal 'Invoice no. 1', email.subject
+ assert email.attachments['invoice-1.pdf']
+ end
+end
\ No newline at end of file
diff --git a/test/mailers/previews/invoice_mailer_preview.rb b/test/mailers/previews/invoice_mailer_preview.rb
new file mode 100644
index 0000000000..d00f1c9298
--- /dev/null
+++ b/test/mailers/previews/invoice_mailer_preview.rb
@@ -0,0 +1,6 @@
+class InvoiceMailerPreview < ActionMailer::Preview
+ def invoice_email
+ invoice = Invoice.first
+ InvoiceMailer.invoice_email(invoice: invoice, recipient: 'billing@registrar.test')
+ end
+end
\ No newline at end of file
diff --git a/test/system/admin_area/invoices_test.rb b/test/system/admin_area/invoices_test.rb
index 081bbb8507..9ae72cbc85 100644
--- a/test/system/admin_area/invoices_test.rb
+++ b/test/system/admin_area/invoices_test.rb
@@ -1,9 +1,13 @@
require 'test_helper'
class AdminAreaInvoicesTest < ApplicationSystemTestCase
+ include ActionMailer::TestHelper
+
setup do
sign_in users(:admin)
@invoice = invoices(:one)
+
+ ActionMailer::Base.deliveries.clear
end
def test_cancels_an_invoice
@@ -17,4 +21,23 @@ def test_cancels_an_invoice
assert @invoice.cancelled?
assert_text 'Invoice has been cancelled'
end
+
+ def test_invoice_delivery_form_is_pre_populated_with_billing_email_of_a_registrar
+ assert_equal 'billing@bestnames.best', @invoice.buyer.billing_email
+ visit new_admin_invoice_delivery_url(@invoice)
+ assert_field 'Recipient', with: 'billing@bestnames.best'
+ end
+
+ def test_delivers_an_invoice
+ visit admin_invoice_url(@invoice)
+ click_on 'Send'
+ fill_in 'Recipient', with: 'billing@registrar.test'
+ click_on 'Send'
+
+ assert_emails 1
+ email = ActionMailer::Base.deliveries.first
+ assert_equal ['billing@registrar.test'], email.to
+ assert_current_path admin_invoice_path(@invoice)
+ assert_text 'Invoice has been sent'
+ end
end
\ No newline at end of file
diff --git a/test/system/registrar_area/invoices_test.rb b/test/system/registrar_area/invoices_test.rb
index a8fa6891f2..1fc07fdab5 100644
--- a/test/system/registrar_area/invoices_test.rb
+++ b/test/system/registrar_area/invoices_test.rb
@@ -1,9 +1,13 @@
require 'test_helper'
class RegistrarAreaInvoicesTest < ApplicationSystemTestCase
+ include ActionMailer::TestHelper
+
setup do
sign_in users(:api_bestnames)
@invoice = invoices(:one)
+
+ ActionMailer::Base.deliveries.clear
end
def test_cancels_an_invoice
@@ -17,4 +21,23 @@ def test_cancels_an_invoice
assert @invoice.cancelled?
assert_text 'Invoice has been cancelled'
end
+
+ def test_invoice_delivery_form_is_pre_populated_with_billing_email_of_a_registrar
+ assert_equal 'billing@bestnames.best', @invoice.buyer.billing_email
+ visit new_registrar_invoice_delivery_url(@invoice)
+ assert_field 'Recipient', with: 'billing@bestnames.best'
+ end
+
+ def test_delivers_an_invoice
+ visit registrar_invoice_url(@invoice)
+ click_on 'Send'
+ fill_in 'Recipient', with: 'billing@registrar.test'
+ click_on 'Send'
+
+ assert_emails 1
+ email = ActionMailer::Base.deliveries.first
+ assert_equal ['billing@registrar.test'], email.to
+ assert_current_path registrar_invoice_path(@invoice)
+ assert_text 'Invoice has been sent'
+ end
end
\ No newline at end of file