Skip to content

Commit

Permalink
Merge pull request #3444 from mkllnk/3437-supplier-name-on-invoice
Browse files Browse the repository at this point in the history
3437 supplier name on invoice
  • Loading branch information
mkllnk authored Feb 15, 2019
2 parents 6f2b894 + 612ea4c commit d4589cc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
10 changes: 2 additions & 8 deletions app/controllers/spree/admin/orders_controller_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ def resend
end

def invoice
pdf = render_to_string pdf: "invoice-#{@order.number}.pdf",
template: invoice_template,
formats: [:html], encoding: "UTF-8"
pdf = InvoiceRenderer.new.render_to_string(@order)

Spree::OrderMailer.invoice_email(@order.id, pdf).deliver
flash[:success] = t('admin.orders.invoice_email_sent')
Expand All @@ -48,7 +46,7 @@ def invoice
end

def print
render pdf: "invoice-#{@order.number}", template: invoice_template, encoding: "UTF-8"
render InvoiceRenderer.new.args(@order)
end

def print_ticket
Expand All @@ -61,10 +59,6 @@ def update_distribution_charge

private

def invoice_template
Spree::Config.invoice_style2? ? "spree/admin/orders/invoice2" : "spree/admin/orders/invoice"
end

def require_distributor_abn
unless @order.distributor.abn.present?
flash[:error] = t(:must_have_valid_business_number, enterprise_name: @order.distributor.name)
Expand Down
12 changes: 2 additions & 10 deletions app/services/bulk_invoice_service.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class BulkInvoiceService
include WickedPdf::PdfHelper
attr_reader :id

def initialize
Expand All @@ -11,10 +10,7 @@ def start_pdf_job(order_ids)
orders = Spree::Order.where(id: order_ids)

orders.each do |order|
invoice = renderer.render_to_string pdf: "invoice-#{order.number}.pdf",
template: invoice_template,
formats: [:html], encoding: "UTF-8",
locals: { :@order => order }
invoice = renderer.render_to_string(order)

pdf << CombinePDF.parse(invoice)
end
Expand Down Expand Up @@ -42,11 +38,7 @@ def directory
end

def renderer
ApplicationController.new
end

def invoice_template
Spree::Config.invoice_style2? ? "spree/admin/orders/invoice2" : "spree/admin/orders/invoice"
@renderer ||= InvoiceRenderer.new
end

def file_directory
Expand Down
29 changes: 29 additions & 0 deletions app/services/invoice_renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class InvoiceRenderer
def render_to_string(order)
renderer.render_to_string(args(order))
end

def args(order)
{
pdf: "invoice-#{order.number}.pdf",
template: invoice_template,
formats: [:html],
encoding: "UTF-8",
locals: { :@order => order }
}
end

private

def renderer
ApplicationController.new
end

def invoice_template
if Spree::Config.invoice_style2?
"spree/admin/orders/invoice2"
else
"spree/admin/orders/invoice"
end
end
end
3 changes: 3 additions & 0 deletions app/views/spree/admin/orders/_invoice_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
%tr
%td
= render 'spree/shared/line_item_name', line_item: item
%br
%small
%em= raw(item.variant.product.supplier.name)
%td{:align => "right"}
= item.quantity
%td{:align => "right"}
Expand Down
3 changes: 3 additions & 0 deletions app/views/spree/admin/orders/_invoice_table2.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
%tr
%td
= render 'spree/shared/line_item_name', line_item: item
%br
%small
%em= raw(item.variant.product.supplier.name)
%td{:align => "right"}
= item.quantity
%td{:align => "right"}
Expand Down
10 changes: 10 additions & 0 deletions spec/services/bulk_invoice_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

expect(Delayed::Job.last.payload_object.method_name).to eq :start_pdf_job_without_delay
end

it "creates a PDF invoice" do
order = create(:completed_order_with_fees)
order.bill_address = order.ship_address
order.save!

service.start_pdf_job_without_delay([order.id])

expect(service.invoice_created?(service.id)).to be_truthy
end
end

describe "#invoice_created?" do
Expand Down
20 changes: 20 additions & 0 deletions spec/services/invoice_renderer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe InvoiceRenderer do
let(:service) { described_class.new }

it "creates a PDF invoice with two different templates" do
order = create(:completed_order_with_fees)
order.bill_address = order.ship_address
order.save!

result = service.render_to_string(order)
expect(result).to match /^%PDF/

allow(Spree::Config).to receive(:invoice_style2?).and_return true

alternative = service.render_to_string(order)
expect(alternative).to match /^%PDF/
expect(alternative).to_not eq result
end
end

0 comments on commit d4589cc

Please sign in to comment.