Adding new route for printing an order to pdf

This commit is contained in:
Rob Harrington
2015-10-16 15:10:08 +11:00
parent 6e7fc6a0f7
commit 40d627cb34
5 changed files with 60 additions and 1 deletions

View File

@@ -49,6 +49,10 @@ Spree::Admin::OrdersController.class_eval do
respond_with(@order) { |format| format.html { redirect_to edit_admin_order_path(@order) } }
end
def print
render pdf: "invoice-#{@order.number}", encoding: "UTF-8"
end
def update_distribution_charge
@order.update_distribution_charge!
end

View File

@@ -142,7 +142,7 @@ class AbilityDecorator
def add_order_management_abilities(user)
# Enterprise User can only access orders that they are a distributor for
can [:index, :create], Spree::Order
can [:read, :update, :fire, :resend, :invoice], Spree::Order do |order|
can [:read, :update, :fire, :resend, :invoice, :print], Spree::Order do |order|
# We allow editing orders with a nil distributor as this state occurs
# during the order creation process from the admin backend
order.distributor.nil? || user.enterprises.include?(order.distributor)

View File

@@ -0,0 +1,19 @@
= wicked_pdf_stylesheet_link_tag "mail/email"
%table{:width => "100%"}
%tbody
%tr
%td{ :align => "left" }
%h4
Order confirmation
%strong ##{@order.number}
%h5
#{@order.bill_address.firstname} #{@order.bill_address.lastname}
%strong= " <#{@order.email}>" if @order.email
= @order.bill_address.phone if @order.bill_address.phone
%h5= "Customer Code: #{@order.customer.code}"
= render 'spree/order_mailer/order_summary'
= render 'spree/order_mailer/payment'
= render 'spree/order_mailer/shipping'

View File

@@ -205,6 +205,7 @@ Spree::Core::Engine.routes.prepend do
resources :orders do
put :invoice, on: :member
get :print, on: :member
get :managed, on: :collection
end
end

View File

@@ -201,4 +201,39 @@ describe Spree::Admin::OrdersController do
end
end
end
describe "#print" do
let!(:user) { create(:user) }
let!(:enterprise_user) { create(:user) }
let!(:order) { create(:order_with_distributor, bill_address: create(:address), ship_address: create(:address)) }
let!(:distributor) { order.distributor }
let(:params) { { id: order.number } }
context "as a normal user" do
before { controller.stub spree_current_user: user }
it "should prevent me from sending order invoices" do
spree_get :print, params
expect(response).to redirect_to spree.unauthorized_path
end
end
context "as an enterprise user" do
context "which is not a manager of the distributor for an order" do
before { controller.stub spree_current_user: user }
it "should prevent me from sending order invoices" do
spree_get :print, params
expect(response).to redirect_to spree.unauthorized_path
end
end
context "which is a manager of the distributor for an order" do
before { controller.stub spree_current_user: distributor.owner }
it "should allow me to send order invoices" do
spree_get :print, params
expect(response).to render_template :print
end
end
end
end
end