diff --git a/Gemfile b/Gemfile index 01c2cfd7..8380f837 100644 --- a/Gemfile +++ b/Gemfile @@ -64,6 +64,7 @@ gem 'rswag-ui' gem 'acts_as_versioned', git: 'https://github.com/technoweenie/acts_as_versioned.git' gem 'foodsoft_wiki', path: 'plugins/wiki' gem 'foodsoft_messages', path: 'plugins/messages' +gem 'foodsoft_bnn_upload', path: 'plugins/bnn_upload' gem 'foodsoft_documents', path: 'plugins/documents' gem 'foodsoft_discourse', path: 'plugins/discourse' gem 'foodsoft_links', path: 'plugins/links' diff --git a/Gemfile.lock b/Gemfile.lock index 5b1a9fe7..9ce71180 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,6 +16,13 @@ GIT acts_as_versioned (0.6.0) activerecord (>= 3.0.9) +PATH + remote: plugins/bnn_upload + specs: + foodsoft_bnn_upload (0.0.1) + deface (~> 1.0) + rails + PATH remote: plugins/discourse specs: @@ -618,6 +625,7 @@ DEPENDENCIES exception_notification factory_bot_rails faker + foodsoft_bnn_upload! foodsoft_discourse! foodsoft_documents! foodsoft_links! diff --git a/plugins/bnn_upload/app/controllers/orders_controller.rb b/plugins/bnn_upload/app/controllers/orders_controller.rb new file mode 100644 index 00000000..50e20f0d --- /dev/null +++ b/plugins/bnn_upload/app/controllers/orders_controller.rb @@ -0,0 +1,14 @@ +class OrdersController < ApplicationController + # Send a order to the supplier. + def send_result_to_supplier + order = Order.find(params[:id]) + options = { file_formats: params[:file_formats] } + order.send_to_supplier!(@current_user, options) + redirect_to order, notice: I18n.t('orders.send_to_supplier.notice') + rescue => error + redirect_to order, alert: I18n.t('errors.general_msg', :msg => error.message) + end + def send_result_to_supplier_modal + @order = Order.find(params[:id]) + end +end diff --git a/plugins/bnn_upload/app/mailers/mailer.rb b/plugins/bnn_upload/app/mailers/mailer.rb new file mode 100644 index 00000000..7059361e --- /dev/null +++ b/plugins/bnn_upload/app/mailers/mailer.rb @@ -0,0 +1,8 @@ +class Mailer + # attaches files to mail sent to supplier + def add_order_result_attachments(order, options = {}) + attachments['order.pdf'] = OrderFax.new(order, options).to_pdf if options[:file_formats][:pdf].present? + attachments['order.csv'] = OrderCsv.new(order, options).to_csv if options[:file_formats][:csv].present? + attachments['order.bnn'] = OrderBnn.new(order, options).to_bnn if options[:file_formats][:bnn].present? + end +end diff --git a/plugins/bnn_upload/app/models/order.rb b/plugins/bnn_upload/app/models/order.rb new file mode 100644 index 00000000..e53e500c --- /dev/null +++ b/plugins/bnn_upload/app/models/order.rb @@ -0,0 +1,8 @@ +class Order + def send_to_supplier!(user, options = {}) + Mailer.deliver_now_with_default_locale do + Mailer.order_result_supplier(user, self, options) + end + update!(last_sent_mail: Time.now) + end +end diff --git a/plugins/bnn_upload/app/overrides/orders/show/replace_send_to_supplier_button.html.haml.deface b/plugins/bnn_upload/app/overrides/orders/show/replace_send_to_supplier_button.html.haml.deface new file mode 100644 index 00000000..7343529c --- /dev/null +++ b/plugins/bnn_upload/app/overrides/orders/show/replace_send_to_supplier_button.html.haml.deface @@ -0,0 +1,6 @@ +/ replace 'erb:contains("send_result_to_supplier")' +- if FoodsoftBnnUpload.enabled? + - raise + = link_to t('.send_to_supplier'), send_result_to_supplier_modal_order_path(@order), remote: true, + class: "btn#{' btn-primary' unless @order.last_sent_mail}", + data: {confirm: @order.last_sent_mail && t('.confirm_send_to_supplier', when: format_time(@order.last_sent_mail)) } \ No newline at end of file diff --git a/plugins/bnn_upload/app/views/orders/_send_to_supplier_modal.html.haml b/plugins/bnn_upload/app/views/orders/_send_to_supplier_modal.html.haml new file mode 100644 index 00000000..f55d326e --- /dev/null +++ b/plugins/bnn_upload/app/views/orders/_send_to_supplier_modal.html.haml @@ -0,0 +1,12 @@ += form_with url: send_result_to_supplier_path(@order) do |f| + .modal-header + = close_button :modal + .h3="Choose file formats that will be attached to your Email" + .modal-body + = f.check_box 'file_formats[pdf]' + = f.label 'file_formats[pdf]', "PDF" + .modal-footer + = link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'} + = f.submit class: 'btn btn-primary' + + \ No newline at end of file diff --git a/plugins/bnn_upload/app/views/orders/send_to_supplier_modal.js.haml b/plugins/bnn_upload/app/views/orders/send_to_supplier_modal.js.haml new file mode 100644 index 00000000..368dec09 --- /dev/null +++ b/plugins/bnn_upload/app/views/orders/send_to_supplier_modal.js.haml @@ -0,0 +1,3 @@ +$('#modalContainer').html('#{j(render("send_to_supplier_modal"))}'); +$('#modalContainer').modal(); +$('#modalContainer').submit(function() {$('#modalContainer').modal('hide');}); \ No newline at end of file diff --git a/plugins/bnn_upload/config/routes.rb b/plugins/bnn_upload/config/routes.rb new file mode 100644 index 00000000..aefdddd3 --- /dev/null +++ b/plugins/bnn_upload/config/routes.rb @@ -0,0 +1,5 @@ +Rails.application.routes.draw do + scope '/:foodcoop' do + get '/order/send_result_to_supplier_modal' => 'orders#send_result_to_supplier_modal' + end +end diff --git a/plugins/bnn_upload/foodsoft_bnn_upload.gemspec b/plugins/bnn_upload/foodsoft_bnn_upload.gemspec new file mode 100644 index 00000000..f3544ab9 --- /dev/null +++ b/plugins/bnn_upload/foodsoft_bnn_upload.gemspec @@ -0,0 +1,18 @@ +$:.push File.expand_path("../lib", __FILE__) + +# Maintain your gem's version: +require "foodsoft_bnn_upload/version" + +# Describe your gem and declare its dependencies: +Gem::Specification.new do |s| + s.name = "foodsoft_bnn_upload" + s.version = FoodsoftBnnUpload::VERSION + s.authors = ["fgu, viehlieb"] + s.email = ["foodsoft@local-it.org"] + s.summary = "Manually choose file to send to supplier. BNN file for NaturkostNord supported." + + s.files = Dir["{app,config,db,lib}/**/*"] + ["Rakefile", "README.md"] + + s.add_dependency "rails" + s.add_dependency "deface", "~> 1.0" +end diff --git a/plugins/bnn_upload/lib/foodsoft_bnn_upload.rb b/plugins/bnn_upload/lib/foodsoft_bnn_upload.rb new file mode 100644 index 00000000..983cb3f1 --- /dev/null +++ b/plugins/bnn_upload/lib/foodsoft_bnn_upload.rb @@ -0,0 +1,7 @@ +require 'foodsoft_bnn_upload/engine' + +module FoodsoftBnnUpload + def self.enabled? + FoodsoftConfig[:use_bnn_upload] + end +end diff --git a/plugins/bnn_upload/lib/foodsoft_bnn_upload/engine.rb b/plugins/bnn_upload/lib/foodsoft_bnn_upload/engine.rb new file mode 100644 index 00000000..eebdc0e1 --- /dev/null +++ b/plugins/bnn_upload/lib/foodsoft_bnn_upload/engine.rb @@ -0,0 +1,7 @@ +module FoodsoftBnnUpload + class Engine < ::Rails::Engine + def default_foodsoft_config(cfg) + cfg[:use_bnn_upload] = true + end + end +end diff --git a/plugins/bnn_upload/lib/foodsoft_bnn_upload/version.rb b/plugins/bnn_upload/lib/foodsoft_bnn_upload/version.rb new file mode 100644 index 00000000..5481fc08 --- /dev/null +++ b/plugins/bnn_upload/lib/foodsoft_bnn_upload/version.rb @@ -0,0 +1,3 @@ +module FoodsoftBnnUpload + VERSION = "0.0.1" +end