Move bulk invoice to new RESTful route

This commit is contained in:
Matt-Yorkley
2018-11-05 18:23:13 +00:00
parent 4a88fb0626
commit 7d82020657
5 changed files with 74 additions and 19 deletions

View File

@@ -77,5 +77,5 @@ angular.module("admin.orders").controller "ordersCtrl", ($scope, RequestMonitor,
angular.forEach $scope.selected_orders, (order_id) ->
params += 'order_ids[]='+order_id+'&'
$window.open('/admin/orders/bulk_invoice?'+params)
$window.open('/admin/orders/invoices/show?'+params)
true

View File

@@ -0,0 +1,29 @@
require 'combine_pdf'
module Spree
module Admin
class InvoicesController < Spree::Admin::BaseController
def show
combined_pdf = CombinePDF.new
Spree::Order.where(id: params[:order_ids]).each do |order|
@order = order
pdf_data = render_to_string pdf: "invoice-#{order.number}.pdf",
template: invoice_template,
formats: [:html], encoding: "UTF-8"
combined_pdf << CombinePDF.parse(pdf_data)
end
send_data combined_pdf.to_pdf, filename: "invoices.pdf",
type: "application/pdf", disposition: :inline
end
private
def invoice_template
Spree::Config.invoice_style2? ? "spree/admin/orders/invoice2" : "spree/admin/orders/invoice"
end
end
end
end

View File

@@ -1,5 +1,4 @@
require 'open_food_network/spree_api_key_loader'
require 'combine_pdf'
Spree::Admin::OrdersController.class_eval do
include OpenFoodNetwork::SpreeApiKeyLoader
@@ -48,23 +47,6 @@ Spree::Admin::OrdersController.class_eval do
respond_with(@order) { |format| format.html { redirect_to edit_admin_order_path(@order) } }
end
def bulk_invoice
orders = Spree::Order.where(id: params[:order_ids])
combined_pdf = CombinePDF.new
orders.each do |order|
@order = order
pdf_data = render_to_string pdf: "invoice-#{order.number}.pdf", template: invoice_template,
formats: [:html], encoding: "UTF-8"
combined_pdf << CombinePDF.parse(pdf_data)
end
send_data combined_pdf.to_pdf, filename: "invoices.pdf",
type: "application/pdf", disposition: :inline
end
def print
render pdf: "invoice-#{@order.number}", template: invoice_template, encoding: "UTF-8"
end

View File

@@ -74,6 +74,10 @@ Spree::Core::Engine.routes.prepend do
get :print, on: :member
get :print_ticket, on: :member
get :managed, on: :collection
collection do
resources :invoices, only: [:show]
end
end
end

View File

@@ -0,0 +1,40 @@
require 'spec_helper'
describe Spree::Admin::InvoicesController, type: :controller do
let(:order) { create(:order_with_totals_and_distribution) }
before do
controller.stub spree_current_user: create(:admin_user)
end
describe "#create" do
it "enqueues a job to create a bulk invoice and returns the filename" do
expect do
spree_post :create, order_ids: [order.id]
end.to enqueue_job BulkInvoiceJob
end
end
describe "#poll" do
let(:invoice_id) { '479186263' }
context "when the file is available" do
it "returns true" do
allow(File).to receive(:exist?).and_return(true)
spree_get :poll, invoice_id: invoice_id
expect(response.body).to eq({ created: true }.to_json)
expect(response.status).to eq 200
end
end
context "when the file is not available" do
it "returns false" do
spree_get :poll, invoice_id: invoice_id
expect(response.body).to eq({ created: false }.to_json)
expect(response.status).to eq 422
end
end
end
end