diff --git a/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee b/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee index fe85d427fd..6aec2e8496 100644 --- a/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee +++ b/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee @@ -1,4 +1,4 @@ -angular.module("admin.orders").controller "ordersCtrl", ($scope, RequestMonitor, Orders, SortOptions) -> +angular.module("admin.orders").controller "ordersCtrl", ($scope, RequestMonitor, Orders, SortOptions, $http, $window) -> $scope.RequestMonitor = RequestMonitor $scope.pagination = Orders.pagination $scope.orders = Orders.all @@ -53,3 +53,11 @@ angular.module("admin.orders").controller "ordersCtrl", ($scope, RequestMonitor, $scope.changePage = (newPage) -> $scope.page = newPage $scope.fetchResults(newPage) + + $scope.bulkInvoice = -> + params = '' + angular.forEach $scope.selected, (selected, order_id) -> + params += 'order_ids[]='+order_id+'&' if selected + + $window.open('/admin/orders/bulk_invoice?'+params) + true diff --git a/app/assets/stylesheets/admin/orders.css.scss b/app/assets/stylesheets/admin/orders.css.scss index 0955d9fd46..d92fc11f6f 100644 --- a/app/assets/stylesheets/admin/orders.css.scss +++ b/app/assets/stylesheets/admin/orders.css.scss @@ -103,3 +103,10 @@ table.index td.actions { font-size: 1.2em; } } + +.index-controls { + + button { + float: right; + } +} diff --git a/app/controllers/spree/admin/orders_controller_decorator.rb b/app/controllers/spree/admin/orders_controller_decorator.rb index bac3509be3..cebbda5cc6 100644 --- a/app/controllers/spree/admin/orders_controller_decorator.rb +++ b/app/controllers/spree/admin/orders_controller_decorator.rb @@ -1,4 +1,5 @@ require 'open_food_network/spree_api_key_loader' +require 'combine_pdf' Spree::Admin::OrdersController.class_eval do include OpenFoodNetwork::SpreeApiKeyLoader @@ -37,13 +38,33 @@ Spree::Admin::OrdersController.class_eval do end def invoice - pdf = render_to_string pdf: "invoice-#{@order.number}.pdf", template: invoice_template, formats: [:html], encoding: "UTF-8" + pdf = render_to_string pdf: "invoice-#{@order.number}.pdf", + template: invoice_template, + formats: [:html], encoding: "UTF-8" + Spree::OrderMailer.invoice_email(@order.id, pdf).deliver flash[:success] = t('admin.orders.invoice_email_sent') respond_with(@order) { |format| format.html { redirect_to edit_admin_order_path(@order) } } end + def bulk_invoice + orders = params[:order_ids] + + combined_pdf = CombinePDF.new + + orders.each do |order_id| + @order = Spree::Order.find(order_id) + 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 diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 9d0941a220..2c68470840 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -210,7 +210,9 @@ class AbilityDecorator # during the order creation process from the admin backend order.distributor.nil? || user.enterprises.include?(order.distributor) || order.order_cycle.andand.coordinated_by?(user) end - can [:admin, :bulk_management, :managed], Spree::Order if user.admin? || user.enterprises.any?(&:is_distributor) + can [:admin, :bulk_management, :managed, :bulk_invoice], Spree::Order do + user.admin? || user.enterprises.any?(&:is_distributor) + end can [:admin, :visible], Enterprise can [:admin, :index, :create, :update, :destroy], :line_item can [:admin, :index, :create], Spree::LineItem diff --git a/app/views/spree/admin/orders/index.html.haml b/app/views/spree/admin/orders/index.html.haml index 47635c4a86..75a9a4b0fb 100644 --- a/app/views/spree/admin/orders/index.html.haml +++ b/app/views/spree/admin/orders/index.html.haml @@ -16,9 +16,12 @@ - content_for :table_filter do = render partial: 'filters' -.row +.row.index-controls{'ng-show' => '!RequestMonitor.loading && orders.length > 0'} = render partial: 'per_page_controls' + %button{'ng-click' => 'bulkInvoice()'} + = t('.print_invoices') + %table#listing_orders.index.responsive{width: "100%", 'ng-init' => 'initialise()', 'ng-show' => "!RequestMonitor.loading && orders.length > 0" } %colgroup %col{style: "width: 10%"} diff --git a/config/locales/en.yml b/config/locales/en.yml index 6f13fd6291..b9e2d509a9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2696,6 +2696,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using results_found: "%{number} Results found." viewing: "Viewing %{start} to %{end}." select_all: "Select All" + print_invoices: "Print Invoices" invoice: issued_on: Issued on tax_invoice: TAX INVOICE