Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor invoice PDF generation, download and delivery #1159

Merged
merged 1 commit into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/controllers/admin/invoices/delivery_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Admin
module Invoices
class DeliveryController < BaseController
include Deliverable

private

def redirect_url
admin_invoice_path(@invoice)
end
end
end
end
31 changes: 4 additions & 27 deletions app/controllers/admin/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Admin
class InvoicesController < BaseController
load_and_authorize_resource

before_action :set_invoice, only: [:forward, :download_pdf]

def new
@deposit = Deposit.new
end
Expand All @@ -28,43 +26,22 @@ def index
@invoices = @q.result.page(params[:page])
end

def show
@invoice = Invoice.find(params[:id])
end
def show; end

def cancel
@invoice.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
def download
filename = "invoice-#{@invoice.number}.pdf"
send_data @invoice.as_pdf, filename: filename
end

private

def deposit_params
params.require(:deposit).permit(:amount, :description, :registrar_id)
end

def set_invoice
@invoice = Invoice.find(params[:invoice_id])
end
end
end
26 changes: 26 additions & 0 deletions app/controllers/concerns/deliverable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Deliverable
extend ActiveSupport::Concern

included do
before_action :find_invoice
end

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 redirect_url, notice: t('.delivered')
end

private

def find_invoice
@invoice = Invoice.find(params[:invoice_id])
end
end
13 changes: 13 additions & 0 deletions app/controllers/registrar/invoices/delivery_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Registrar
module Invoices
class DeliveryController < BaseController
include Deliverable

private

def redirect_url
registrar_invoice_path(@invoice)
end
end
end
end
30 changes: 4 additions & 26 deletions app/controllers/registrar/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ class Registrar
class InvoicesController < BaseController
load_and_authorize_resource

before_action :set_invoice, only: [:show, :forward, :download_pdf]

def index
params[:q] ||= {}
invoices = current_registrar_user.registrar.invoices.includes(:items, :account_activity)
Expand All @@ -15,40 +13,20 @@ def index
end
end

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 show; end

def cancel
@invoice.cancel
redirect_to [:registrar, @invoice], notice: t('.cancelled')
end

def download_pdf
pdf = @invoice.pdf(render_to_string('pdf', layout: false))
send_data pdf, filename: @invoice.pdf_name
def download
filename = "invoice-#{@invoice.number}.pdf"
send_data @invoice.as_pdf, filename: filename
end

private

def set_invoice
@invoice = Invoice.find(params[:id])
end

def normalize_search_parameters
params[:q][:total_gteq].gsub!(',', '.') if params[:q][:total_gteq]
params[:q][:total_lteq].gsub!(',', '.') if params[:q][:total_lteq]
Expand Down
19 changes: 6 additions & 13 deletions app/mailers/invoice_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,16 +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

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-#{invoice.number}.pdf"] = invoice.as_pdf
mail(to: recipient, subject: subject)
end
end
end
27 changes: 6 additions & 21 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 @@ -84,23 +81,6 @@ def order
"Order nr. #{number}"
end

def pdf(html)
kit = PDFKit.new(html)
kit.to_pdf
end

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 +99,11 @@ def each
items.each { |item| yield item }
end

def as_pdf
generator = PdfGenerator.new(self)
generator.as_pdf
end

private

def apply_default_vat_rate
Expand All @@ -132,4 +117,4 @@ def apply_default_buyer_vat_no
def calculate_total
self.total = subtotal + vat_amount
end
end
end
22 changes: 22 additions & 0 deletions app/models/invoice/pdf_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Invoice
class PdfGenerator
attr_reader :invoice

def initialize(invoice)
@invoice = invoice
end

def as_pdf
generator = PDFKit.new(invoice_html)
generator.to_pdf
end

private

def invoice_html
view = ActionView::Base.new(ActionController::Base.view_paths, invoice: invoice)
view.class_eval { include ApplicationHelper }
view.render(file: 'invoice/pdf', layout: false)
end
end
end
25 changes: 25 additions & 0 deletions app/views/admin/invoices/delivery/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<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, required: true, 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.

4 changes: 2 additions & 2 deletions app/views/admin/invoices/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
%h1.text-right.text-center-xs
- 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('.download_btn'), download_admin_invoice_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
File renamed without changes.
25 changes: 25 additions & 0 deletions app/views/registrar/invoices/delivery/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<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, required: true, 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.

4 changes: 2 additions & 2 deletions 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('.download_btn'), download_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
7 changes: 7 additions & 0 deletions config/locales/admin/invoices.en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
en:
admin:
invoices:
index:
header: Invoices

show:
download_btn: Download
deliver_btn: Send

cancel:
cancelled: Invoice has been cancelled
Loading