From b1ec0fd2fc706966e2f7ff652a602f483385fac9 Mon Sep 17 00:00:00 2001 From: viehlieb Date: Thu, 23 Feb 2023 00:14:00 +0100 Subject: [PATCH] add bnn_upload plugin that overrides some controller methods to use order_bnn --- plugins/bnn_upload/app/mailers/mailer.rb | 8 ---- .../orders_controller_override.rb} | 10 ++--- .../app/overrides/mailers/mailer_override.rb | 8 ++++ .../models/order_override.rb} | 2 +- ...e_send_to_supplier_button.html.haml.deface | 6 +-- ... _send_result_to_supplier_modal.html.haml} | 6 ++- ... => send_result_to_supplier_modal.js.haml} | 2 +- plugins/bnn_upload/config/routes.rb | 3 +- plugins/bnn_upload/lib/foodsoft_bnn_upload.rb | 1 - .../lib/foodsoft_bnn_upload/engine.rb | 5 +++ .../lib/foodsoft_bnn_upload/order_bnn.rb | 42 +++++++++++++++++++ .../show/add_message_button.html.haml.deface | 4 -- 12 files changed, 70 insertions(+), 27 deletions(-) delete mode 100644 plugins/bnn_upload/app/mailers/mailer.rb rename plugins/bnn_upload/app/{controllers/orders_controller.rb => overrides/controllers/orders_controller_override.rb} (50%) create mode 100644 plugins/bnn_upload/app/overrides/mailers/mailer_override.rb rename plugins/bnn_upload/app/{models/order.rb => overrides/models/order_override.rb} (90%) rename plugins/bnn_upload/app/views/orders/{_send_to_supplier_modal.html.haml => _send_result_to_supplier_modal.html.haml} (71%) rename plugins/bnn_upload/app/views/orders/{send_to_supplier_modal.js.haml => send_result_to_supplier_modal.js.haml} (54%) create mode 100644 plugins/bnn_upload/lib/foodsoft_bnn_upload/order_bnn.rb delete mode 100644 plugins/messages/app/overrides/orders/show/add_message_button.html.haml.deface diff --git a/plugins/bnn_upload/app/mailers/mailer.rb b/plugins/bnn_upload/app/mailers/mailer.rb deleted file mode 100644 index 7059361e..00000000 --- a/plugins/bnn_upload/app/mailers/mailer.rb +++ /dev/null @@ -1,8 +0,0 @@ -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/controllers/orders_controller.rb b/plugins/bnn_upload/app/overrides/controllers/orders_controller_override.rb similarity index 50% rename from plugins/bnn_upload/app/controllers/orders_controller.rb rename to plugins/bnn_upload/app/overrides/controllers/orders_controller_override.rb index 50e20f0d..97dc0dc8 100644 --- a/plugins/bnn_upload/app/controllers/orders_controller.rb +++ b/plugins/bnn_upload/app/overrides/controllers/orders_controller_override.rb @@ -1,13 +1,13 @@ -class OrdersController < ApplicationController - # Send a order to the supplier. +OrdersController.class_eval do 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) + redirect_to order_path(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 diff --git a/plugins/bnn_upload/app/overrides/mailers/mailer_override.rb b/plugins/bnn_upload/app/overrides/mailers/mailer_override.rb new file mode 100644 index 00000000..df0010ae --- /dev/null +++ b/plugins/bnn_upload/app/overrides/mailers/mailer_override.rb @@ -0,0 +1,8 @@ +Mailer.class_eval do + require 'foodsoft_bnn_upload/order_bnn' + 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'] = FoodsoftBnnUpload::OrderBnn.new(order, options).to_csv if options[:file_formats][:bnn].present? + end +end diff --git a/plugins/bnn_upload/app/models/order.rb b/plugins/bnn_upload/app/overrides/models/order_override.rb similarity index 90% rename from plugins/bnn_upload/app/models/order.rb rename to plugins/bnn_upload/app/overrides/models/order_override.rb index e53e500c..ec80731f 100644 --- a/plugins/bnn_upload/app/models/order.rb +++ b/plugins/bnn_upload/app/overrides/models/order_override.rb @@ -1,4 +1,4 @@ -class Order +Order.class_eval do def send_to_supplier!(user, options = {}) Mailer.deliver_now_with_default_locale do Mailer.order_result_supplier(user, self, options) 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 index 49a38576..c5a54687 100644 --- 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 @@ -1,5 +1,3 @@ -/ replace '#send_button' +/ replace 'erb:contains("send_to_supplier")' - if FoodsoftBnnUpload.enabled? - = link_to t('.send_to_supplier'), send_result_to_supplier_modal_order_path(@order), remote: true, - class: "btn#{' btn-primary', - data: {confirm: @order.last_sent_mail && t('.confirm_send_to_supplier', when: format_time(@order.last_sent_mail)) } \ No newline at end of file + = link_to t('.send_to_supplier'), order_send_result_to_supplier_modal_path(@order), class: 'btn btn-primary', remote: true \ 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_result_to_supplier_modal.html.haml similarity index 71% rename from plugins/bnn_upload/app/views/orders/_send_to_supplier_modal.html.haml rename to plugins/bnn_upload/app/views/orders/_send_result_to_supplier_modal.html.haml index f55d326e..d9ba1cf3 100644 --- a/plugins/bnn_upload/app/views/orders/_send_to_supplier_modal.html.haml +++ b/plugins/bnn_upload/app/views/orders/_send_result_to_supplier_modal.html.haml @@ -5,8 +5,10 @@ .modal-body = f.check_box 'file_formats[pdf]' = f.label 'file_formats[pdf]', "PDF" + = f.check_box 'file_formats[csv]' + = f.label 'file_formats[csv]', "CSV" + = f.check_box 'file_formats[bnn]' + = f.label 'file_formats[bnn]', "BNN" .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_result_to_supplier_modal.js.haml similarity index 54% rename from plugins/bnn_upload/app/views/orders/send_to_supplier_modal.js.haml rename to plugins/bnn_upload/app/views/orders/send_result_to_supplier_modal.js.haml index 368dec09..181fe611 100644 --- a/plugins/bnn_upload/app/views/orders/send_to_supplier_modal.js.haml +++ b/plugins/bnn_upload/app/views/orders/send_result_to_supplier_modal.js.haml @@ -1,3 +1,3 @@ -$('#modalContainer').html('#{j(render("send_to_supplier_modal"))}'); +$('#modalContainer').html('#{escape_javascript(render("send_result_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 index aefdddd3..7c2dc396 100644 --- a/plugins/bnn_upload/config/routes.rb +++ b/plugins/bnn_upload/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do scope '/:foodcoop' do - get '/order/send_result_to_supplier_modal' => 'orders#send_result_to_supplier_modal' + get '/order/:id/send_result_to_supplier_modal', controller: 'orders', action: :send_result_to_supplier_modal, as: :order_send_result_to_supplier_modal + post '/order/:id/send_result_to_supplier', controller: 'orders', action: :send_result_to_supplier, as: :send_result_to_supplier end end diff --git a/plugins/bnn_upload/lib/foodsoft_bnn_upload.rb b/plugins/bnn_upload/lib/foodsoft_bnn_upload.rb index 66d7d279..6ce87955 100644 --- a/plugins/bnn_upload/lib/foodsoft_bnn_upload.rb +++ b/plugins/bnn_upload/lib/foodsoft_bnn_upload.rb @@ -1,6 +1,5 @@ require "deface" require 'foodsoft_bnn_upload/engine' - module FoodsoftBnnUpload def self.enabled? FoodsoftConfig[:use_bnn_upload] diff --git a/plugins/bnn_upload/lib/foodsoft_bnn_upload/engine.rb b/plugins/bnn_upload/lib/foodsoft_bnn_upload/engine.rb index eebdc0e1..4e0cd6d7 100644 --- a/plugins/bnn_upload/lib/foodsoft_bnn_upload/engine.rb +++ b/plugins/bnn_upload/lib/foodsoft_bnn_upload/engine.rb @@ -1,5 +1,10 @@ module FoodsoftBnnUpload class Engine < ::Rails::Engine + config.to_prepare do + Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c| + require_dependency(c) + end + end def default_foodsoft_config(cfg) cfg[:use_bnn_upload] = true end diff --git a/plugins/bnn_upload/lib/foodsoft_bnn_upload/order_bnn.rb b/plugins/bnn_upload/lib/foodsoft_bnn_upload/order_bnn.rb new file mode 100644 index 00000000..5f0d963c --- /dev/null +++ b/plugins/bnn_upload/lib/foodsoft_bnn_upload/order_bnn.rb @@ -0,0 +1,42 @@ +module FoodsoftBnnUpload + class OrderBnn < RenderCsv + def initialize(object, options = {}) + super + @options[:col_sep] = "" + @options[:row_sep] = "\n" + @options[:encoding] = 'IBM850' + end + + def header + customer_id = "000001" + delivery_date = Time.zone.now.strftime("%y%m%d") + pickup = " " + + ["D##{customer_id}#{delivery_date}#{pickup}#{@object.id}"] + end + + def data + @object.order_articles.ordered.includes([:article, :article_price]).all.map do |oa| + yield [ + pad_to_length(oa.article.order_number, 13), + "+", + pad_float_values(oa.units_to_order), + pad_to_length(oa.article.name, 30), + pad_float_values(oa.article.unit_quantity), + pad_to_length(oa.article.unit, 14), + pad_to_length(oa.article.manufacturer, 3), + pad_to_length("", 26) + ] + end + end + + def pad_float_values(number, digits_before=4, digits_after=3) + format_string = "%0#{digits_before + digits_after}d" + formatted_number = sprintf(format_string, (number * 10 ** digits_after).to_i) + end + + def pad_to_length(string, length) + string.to_s.rjust(length, " ")[0, length] + end + end +end \ No newline at end of file diff --git a/plugins/messages/app/overrides/orders/show/add_message_button.html.haml.deface b/plugins/messages/app/overrides/orders/show/add_message_button.html.haml.deface deleted file mode 100644 index d17af42a..00000000 --- a/plugins/messages/app/overrides/orders/show/add_message_button.html.haml.deface +++ /dev/null @@ -1,4 +0,0 @@ -/ insert_after 'erb:contains("title t(\'.title\'")' -- content_for :actionbar do - - if FoodsoftMessages.enabled? - = link_to_new_message message_params: {order_id: @order.id}