Skip to content

Commit

Permalink
Refactor invoice delivery
Browse files Browse the repository at this point in the history
- Remove `Que::Mailer` (#895)
- Extract controller
- Extract translations
- Convert HAML to ERB
- Add mailer preview
- Improve UI
- Add tests
  • Loading branch information
Artur Beljajev committed Apr 10, 2019
1 parent c192cc0 commit 05f02e3
Show file tree
Hide file tree
Showing 25 changed files with 237 additions and 97 deletions.
26 changes: 26 additions & 0 deletions app/controllers/admin/invoices/delivery_controller.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 1 addition & 16 deletions app/controllers/admin/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/registrar/invoices/delivery_controller.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 1 addition & 16 deletions app/controllers/registrar/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: %i[show download_pdf]

def index
params[:q] ||= {}
Expand All @@ -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')
Expand Down
20 changes: 6 additions & 14 deletions app/mailers/invoice_mailer.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 13 additions & 11 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
24 changes: 24 additions & 0 deletions app/views/admin/invoices/delivery/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<ol class="breadcrumb">
<li><%= link_to t('admin.invoices.index.header'), admin_invoices_path %></li>
<li><%= link_to @invoice, admin_invoice_path(@invoice) %></li>
</ol>

<div class="page-header">
<h1><%= t '.header' %></h1>
</div>

<%= form_tag(admin_invoice_delivery_path(@invoice)) do %>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<%= label_tag :recipient %>
<%= email_field_tag :recipient, @recipient, autofocus: true, class: 'form-control' %>
</div>
<div class="row">
<div class="col-md-12 text-right">
<%= submit_tag t('.submit_btn'), class: 'btn btn-warning' %>
</div>
</div>
</div>
</div>
<% end %>
15 changes: 0 additions & 15 deletions app/views/admin/invoices/forward.haml

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/admin/invoices/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
24 changes: 24 additions & 0 deletions app/views/registrar/invoices/delivery/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<ol class="breadcrumb">
<li><%= link_to t('registrar.invoices.index.header'), registrar_invoices_path %></li>
<li><%= link_to @invoice, registrar_invoice_path(@invoice) %></li>
</ol>

<div class="page-header">
<h1><%= t '.header' %></h1>
</div>

<%= form_tag(registrar_invoice_delivery_path(@invoice)) do %>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<%= label_tag :recipient %>
<%= email_field_tag :recipient, @recipient, autofocus: true, class: 'form-control' %>
</div>
<div class="row">
<div class="col-md-12 text-right">
<%= submit_tag t('.submit_btn'), class: 'btn btn-warning' %>
</div>
</div>
</div>
</div>
<% end %>
15 changes: 0 additions & 15 deletions app/views/registrar/invoices/forward.haml

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/registrar/invoices/show.haml
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
6 changes: 6 additions & 0 deletions config/locales/admin/invoices.en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
en:
admin:
invoices:
index:
header: Invoices

show:
deliver_btn: Send

cancel:
cancelled: Invoice has been cancelled
10 changes: 10 additions & 0 deletions config/locales/admin/invoices/delivery.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
en:
admin:
invoices:
delivery:
new:
header: Send invoice
submit_btn: Send

create:
delivered: Invoice has been sent
5 changes: 0 additions & 5 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions config/locales/mailers/invoice.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
invoice_mailer:
invoice_email:
subject: Invoice no. %{invoice_number}
5 changes: 5 additions & 0 deletions config/locales/registrar/invoices.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions config/locales/registrar/invoices/delivery.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
en:
registrar:
invoices:
delivery:
new:
header: Send invoice
submit_btn: Send

create:
delivered: Invoice has been sent
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/invoices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ one:
vat_rate: 0.1
total: 16.50
reference_no: 13
number: 1

for_payments_test:
number: 1
Expand Down
Loading

0 comments on commit 05f02e3

Please sign in to comment.