diff --git a/app/controllers/spree/admin/orders_controller_decorator.rb b/app/controllers/spree/admin/orders_controller_decorator.rb index 87c6f3329a..88b1508a09 100644 --- a/app/controllers/spree/admin/orders_controller_decorator.rb +++ b/app/controllers/spree/admin/orders_controller_decorator.rb @@ -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 diff --git a/app/mailers/spree/order_mailer_decorator.rb b/app/mailers/spree/order_mailer_decorator.rb index c6eeb31df8..016b61efa4 100644 --- a/app/mailers/spree/order_mailer_decorator.rb +++ b/app/mailers/spree/order_mailer_decorator.rb @@ -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 diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index fe8995f213..5e6dba10b6 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -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) diff --git a/app/overrides/spree/admin/orders/edit/add_invoice_button.html.haml.deface b/app/overrides/spree/admin/orders/edit/add_invoice_button.html.haml.deface new file mode 100644 index 0000000000..919ccad71a --- /dev/null +++ b/app/overrides/spree/admin/orders/edit/add_invoice_button.html.haml.deface @@ -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) } diff --git a/app/views/spree/admin/orders/invoice.html.haml b/app/views/spree/admin/orders/invoice.html.haml new file mode 100644 index 0000000000..8a301f5869 --- /dev/null +++ b/app/views/spree/admin/orders/invoice.html.haml @@ -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' diff --git a/app/views/spree/order_mailer/invoice_email.html.haml b/app/views/spree/order_mailer/invoice_email.html.haml new file mode 100644 index 0000000000..e2a2aa8967 --- /dev/null +++ b/app/views/spree/order_mailer/invoice_email.html.haml @@ -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' diff --git a/config/locales/en.yml b/config/locales/en.yml index 3149620eab..b9a330b20e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -35,4 +35,5 @@ en: footer_email: "Email" footer_links_md: "Links" footer_about_url: "About URL" - footer_tos_url: "Terms of Service URL" \ No newline at end of file + footer_tos_url: "Terms of Service URL" + invoice: "Invoice" diff --git a/config/routes.rb b/config/routes.rb index f2ae6ea78f..ca1c34d611 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -204,6 +204,7 @@ Spree::Core::Engine.routes.prepend do end resources :orders do + put :invoice, on: :member get :managed, on: :collection end end diff --git a/spec/controllers/spree/admin/orders_controller_spec.rb b/spec/controllers/spree/admin/orders_controller_spec.rb index dae1edcf29..f19ab4cdbb 100644 --- a/spec/controllers/spree/admin/orders_controller_spec.rb +++ b/spec/controllers/spree/admin/orders_controller_spec.rb @@ -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