diff --git a/app/controllers/spree/admin/bookkeeping_documents_controller.rb b/app/controllers/spree/admin/bookkeeping_documents_controller.rb index fd074f83..5f8ef9a6 100644 --- a/app/controllers/spree/admin/bookkeeping_documents_controller.rb +++ b/app/controllers/spree/admin/bookkeeping_documents_controller.rb @@ -8,7 +8,10 @@ class BookkeepingDocumentsController < ResourceController def show respond_with(@bookkeeping_document) do |format| format.pdf do - send_data @bookkeeping_document.pdf, type: 'application/pdf', disposition: 'inline' + send_data @bookkeeping_document.pdf, + type: 'application/pdf', + disposition: 'inline', + filename: pdf_filename end end end @@ -39,6 +42,10 @@ def order_focused? def load_order @order = Spree::Order.find_by(number: params[:order_id]) end + + def pdf_filename + "#{@bookkeeping_document.template}_#{@bookkeeping_document.number}_#{@bookkeeping_document.printable.bill_address.firstname}_#{@bookkeeping_document.printable.completed_at.to_i}.pdf" + end end end end diff --git a/app/models/spree/bookkeeping_document.rb b/app/models/spree/bookkeeping_document.rb index bbe3c76e..b839920d 100644 --- a/app/models/spree/bookkeeping_document.rb +++ b/app/models/spree/bookkeeping_document.rb @@ -8,6 +8,10 @@ class BookkeepingDocument < ActiveRecord::Base :number ] + def self.ransackable_attributes(auth_object = nil) + ["created_at", "email", "firstname", "id", "id_value", "lastname", "number", "printable_id", "printable_type", "template", "total", "updated_at"] + end + # Spree::BookkeepingDocument cares about creating PDFs. Whenever it needs to know # anything about the document to send to the view, it asks a view object. # @@ -108,7 +112,8 @@ def storage_path # def render_pdf ApplicationController.render( - template: "#{template_name}.pdf.prawn", + template: template_name, + formats: [:pdf], assigns: { doc: self } ) end diff --git a/app/models/spree_print_invoice/admin/main_menu/print_invoice_builder.rb b/app/models/spree_print_invoice/admin/main_menu/print_invoice_builder.rb new file mode 100644 index 00000000..5bd633eb --- /dev/null +++ b/app/models/spree_print_invoice/admin/main_menu/print_invoice_builder.rb @@ -0,0 +1,30 @@ +module SpreePrintInvoice + module Admin + module MainMenu + class PrintInvoiceBuilder + include ::Spree::Core::Engine.routes.url_helpers + + def build + items = [ + ::Spree::Admin::MainMenu::ItemBuilder.new('invoices', ::Spree::Core::Engine.routes.url_helpers.admin_bookkeeping_documents_path(q: { template_eq: 'invoice' })). + with_label_translation_key('admin.tab.invoices'). + with_manage_ability_check(::Spree::BookkeepingDocument). + with_match_path('/bookkeeping_documents?q%5Btemplate_eq%5D=invoice'). + build, + ::Spree::Admin::MainMenu::ItemBuilder.new('packaging_slips', ::Spree::Core::Engine.routes.url_helpers.admin_bookkeeping_documents_path(q: { template_eq: 'packaging_slip' })). + with_label_translation_key('admin.tab.packaging_slips'). + with_manage_ability_check(::Spree::BookkeepingDocument). + with_match_path('/bookkeeping_documents?q%5Btemplate_eq%5D=packaging_slip'). + build + ] + + ::Spree::Admin::MainMenu::SectionBuilder.new('documents', 'file.svg'). + with_manage_ability_check(::Spree::BookkeepingDocument). + with_label_translation_key('admin.documents'). + with_items(items). + build + end + end + end + end +end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree_print_invoice/order_decorator.rb similarity index 89% rename from app/models/spree/order_decorator.rb rename to app/models/spree_print_invoice/order_decorator.rb index e6bed8d1..bc5e65cb 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree_print_invoice/order_decorator.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module Spree +module SpreePrintInvoice module OrderDecorator def self.prepended(base) base.has_many :bookkeeping_documents, as: :printable, dependent: :destroy @@ -42,9 +42,11 @@ def pdf_storage_path(template) end def invoice_for_order + return if bookkeeping_documents.exists?(template: 'invoice') && bookkeeping_documents.exists?(template: 'packaging_slip') bookkeeping_documents.create(template: 'invoice') bookkeeping_documents.create(template: 'packaging_slip') end end end -::Spree::Order.prepend Spree::OrderDecorator \ No newline at end of file + +::Spree::Order.prepend SpreePrintInvoice::OrderDecorator diff --git a/app/overrides/add_documents_to_main_navigation.rb b/app/overrides/add_documents_to_main_navigation.rb deleted file mode 100644 index cc514835..00000000 --- a/app/overrides/add_documents_to_main_navigation.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -if Spree.version.to_f < 4.0 - Deface::Override.new( - virtual_path: 'spree/layouts/admin', - insert_bottom: '#main-sidebar', - partial: 'spree/admin/shared/menu/documents_tab', - name: 'documents_tab' - ) -else - Deface::Override.new( - virtual_path: 'spree/admin/shared/_main_menu', - insert_bottom: 'nav', - partial: 'spree/admin/shared/menu/documents_tab', - name: 'documents_tab' - ) -end diff --git a/app/overrides/add_print_invoice_links_to_admin_order_tabs.rb b/app/overrides/add_print_invoice_links_to_admin_order_tabs.rb deleted file mode 100644 index cf7382df..00000000 --- a/app/overrides/add_print_invoice_links_to_admin_order_tabs.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -Deface::Override.new( - virtual_path: 'spree/admin/shared/_order_tabs', - name: 'print_invoice_order_tab_links', - insert_bottom: '[data-hook="admin_order_tabs"]', - partial: 'spree/admin/orders/print_invoice_order_tab_links' -) diff --git a/app/overrides/add_print_invoice_to_admin_configuration_sidebar.rb b/app/overrides/add_print_invoice_to_admin_configuration_sidebar.rb deleted file mode 100644 index 9c143549..00000000 --- a/app/overrides/add_print_invoice_to_admin_configuration_sidebar.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -Deface::Override.new( - virtual_path: 'spree/admin/shared/sub_menu/_configuration', - name: 'print_invoice_admin_configurations_menu', - insert_bottom: '[data-hook="admin_configurations_sidebar_menu"]', - text: '<%= configurations_sidebar_menu_item Spree.t(:settings, scope: :print_invoice), - spree.edit_admin_print_invoice_settings_path %>' -) diff --git a/app/views/spree/admin/orders/_print_invoice_order_tab_links.html.erb b/app/views/spree/admin/orders/_print_invoice_order_tab_links.html.erb deleted file mode 100755 index ccc6a9d2..00000000 --- a/app/views/spree/admin/orders/_print_invoice_order_tab_links.html.erb +++ /dev/null @@ -1,12 +0,0 @@ -<% if can? :show, Spree::Order %> - <% if @order && @order.completed_at? %> -
  • - <%= - link_to_with_icon 'file.svg', - Spree.t(:documents, scope: [:print_invoice]), - spree.admin_order_bookkeeping_documents_path(@order), - { class: "nav-link #{'active' if current == :documents}" } - %> -
  • - <% end %> -<% end %> \ No newline at end of file diff --git a/app/views/spree/admin/shared/menu/_documents_tab.html.erb b/app/views/spree/admin/shared/menu/_documents_tab.html.erb deleted file mode 100644 index 7b8556fa..00000000 --- a/app/views/spree/admin/shared/menu/_documents_tab.html.erb +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/app/views/spree/admin/shared/sub_menu/_documents_sub_menu.html.erb b/app/views/spree/admin/shared/sub_menu/_documents_sub_menu.html.erb deleted file mode 100644 index 0a7878b6..00000000 --- a/app/views/spree/admin/shared/sub_menu/_documents_sub_menu.html.erb +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/app/views/spree/printables/shared/_header.pdf.prawn b/app/views/spree/printables/shared/_header.pdf.prawn index 53d8af04..14bb7f61 100644 --- a/app/views/spree/printables/shared/_header.pdf.prawn +++ b/app/views/spree/printables/shared/_header.pdf.prawn @@ -1,6 +1,6 @@ im = (Rails.application.assets || ::Sprockets::Railtie.build_environment(Rails.application)).find_asset(Spree::PrintInvoice::Config[:logo_path]) -if im && File.exist?(im.pathname) +if im && File.exist?(im.filename) pdf.image im.filename, vposition: :top, height: 40, scale: Spree::PrintInvoice::Config[:logo_scale] end @@ -11,4 +11,4 @@ pdf.grid([0,3], [1,4]).bounding_box do pdf.text Spree.t(:invoice_number, scope: :print_invoice, number: printable.number), align: :right pdf.move_down 2 pdf.text Spree.t(:invoice_date, scope: :print_invoice, date: I18n.l(printable.date)), align: :right -end +end \ No newline at end of file diff --git a/config/initializers/print_invoice.rb b/config/initializers/print_invoice.rb new file mode 100644 index 00000000..6a235c4f --- /dev/null +++ b/config/initializers/print_invoice.rb @@ -0,0 +1,18 @@ +Rails.application.config.after_initialize do + if Spree::Core::Engine.backend_available? + Rails.application.config.spree_backend.main_menu.insert_after('dashboard', ::SpreePrintInvoice::Admin::MainMenu::PrintInvoiceBuilder.new.build) + + Rails.application.config.spree_backend.tabs[:order].add( + ::Spree::Admin::Tabs::TabBuilder.new(::Spree.t(:documents, scope: [:print_invoice]), ->(resource) { ::Spree::Core::Engine.routes.url_helpers.admin_order_bookkeeping_documents_path(resource) }). + with_icon_key('file.svg'). + with_active_check. + build + ) + + Rails.application.config.spree_backend.main_menu.add_to_section('settings', + ::Spree::Admin::MainMenu::ItemBuilder.new('print_invoice.settings', ::Spree::Core::Engine.routes.url_helpers.edit_admin_print_invoice_settings_path). + with_manage_ability_check(::Spree::BookkeepingDocument). + build + ) + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 112c5070..77a1ccd6 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,6 +1,10 @@ --- en: spree: + printable_number: Printable Number + bookkeeping_documents: Bookkeeping Documents + print_invoice_settings: Print Invoice Settings + documents: Documents admin: tab: invoices: Invoices diff --git a/lib/spree_print_invoice/engine.rb b/lib/spree_print_invoice/engine.rb index 1ceb6c29..f0e931a9 100644 --- a/lib/spree_print_invoice/engine.rb +++ b/lib/spree_print_invoice/engine.rb @@ -6,7 +6,7 @@ class Engine < Rails::Engine config.autoload_paths += %W(#{config.root}/lib) - initializer 'spree.print_invoice.preferences', before: :load_config_initializers do + config.after_initialize do Spree::PrintInvoice::Config = Spree::PrintInvoiceSetting.new end diff --git a/spree-print-invoice.gemspec b/spree-print-invoice.gemspec index 07157683..d456f7b0 100755 --- a/spree-print-invoice.gemspec +++ b/spree-print-invoice.gemspec @@ -22,8 +22,8 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.requirements << 'none' - s.add_runtime_dependency 'prawn-rails', '~> 1.3' - s.add_runtime_dependency 'spree_core', '>= 3.1.0', '< 5.0' + s.add_runtime_dependency 'prawn-rails', '~> 1.4.2' + s.add_runtime_dependency 'spree_core', '>= 4.8.0', '< 5.0' s.add_runtime_dependency 'spree_extension' s.add_development_dependency 'capybara'