Adding invoice action to orders controller, to allow sending of invoice email

This commit is contained in:
Rob Harrington
2015-10-16 13:06:09 +11:00
parent 104b100fe6
commit 6e7fc6a0f7
9 changed files with 88 additions and 3 deletions

View File

@@ -41,6 +41,14 @@ Spree::Admin::OrdersController.class_eval do
respond_with(@order) { |format| format.html { redirect_to :back } }
end
def invoice
pdf = render_to_string pdf: "invoice-#{@order.number}.pdf", template: "spree/admin/orders/invoice", formats: [:html], encoding: "UTF-8"
Spree::OrderMailer.invoice_email(@order.id, pdf).deliver
flash[:success] = t(:invoice_email_sent)
respond_with(@order) { |format| format.html { redirect_to edit_admin_order_path(@order) } }
end
def update_distribution_charge
@order.update_distribution_charge!
end

View File

@@ -21,4 +21,14 @@ Spree::OrderMailer.class_eval do
:from => from_address,
:subject => subject)
end
def invoice_email(order, pdf)
find_order(order) # Finds an order instance from an id
attachments["invoice-#{@order.number}.pdf"] = pdf if pdf.present?
subject = "#{Spree::Config[:site_name]} #{t(:invoice)} ##{@order.number}"
mail(:to => @order.email,
:from => from_address,
:subject => subject,
:reply_to => @order.distributor.email)
end
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], Spree::Order do |order|
can [:read, :update, :fire, :resend, :invoice], 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,3 @@
/ insert_after "code[erb-loud]:contains('button_link_to t(:resend)')"
- if @order.complete?
%li= button_link_to t(:invoice), invoice_admin_order_url(@order), :method => :put, :icon => 'icon-email', :data => { :confirm => t(:are_you_sure) }

View File

@@ -0,0 +1,15 @@
= wicked_pdf_stylesheet_link_tag "mail/email"
%table{:width => "100%"}
%tbody
%tr
%td{ :align => "left" }
= "Order ##{@order.number}"
%td{ :align => "right" }
- if @order.total_tax > 0
= "TAX"
= "INVOICE"
= render 'spree/order_mailer/order_summary'
= render 'spree/order_mailer/payment'

View File

@@ -0,0 +1,10 @@
%table.social.white-bg{:width => "100%"}
%tr
%td
%h3
Hi #{@order.bill_address.firstname},
%h4
Please find attached an invoice for your recent order from
%strong= "#{@order.distributor.name}"
= render 'signoff'

View File

@@ -35,4 +35,5 @@ en:
footer_email: "Email"
footer_links_md: "Links"
footer_about_url: "About URL"
footer_tos_url: "Terms of Service URL"
footer_tos_url: "Terms of Service URL"
invoice: "Invoice"

View File

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

View File

@@ -29,7 +29,7 @@ describe Spree::Admin::OrdersController do
end
end
describe "managed" do
describe "#managed" do
render_views
let(:order_attributes) { [:id, :full_name, :email, :phone, :completed_at, :line_items, :distributor, :order_cycle, :number] }
@@ -164,4 +164,41 @@ describe Spree::Admin::OrdersController do
end
end
end
describe "#invoice" 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 :invoice, 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 :invoice, 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
# expect do
spree_get :invoice, params
# end.to change{Spree::OrderMailer.deliveries.count}.by(1)
expect(response).to redirect_to spree.edit_admin_order_path(order)
end
end
end
end
end