From d81ae10dc886f2eb8f6fd2ee7803fa2f58560f81 Mon Sep 17 00:00:00 2001 From: viehlieb Date: Tue, 14 Feb 2023 16:51:22 +0100 Subject: [PATCH] feat(order): export order to custom csv file add custom_csv_collection to orders helper add rute and controller method to orders controller add custom csv to download dropdown add functionality to choose column headers + order for custom csv and append order.sum gross&net to custom csv --- app/controllers/orders_controller.rb | 15 ++++- app/helpers/orders_helper.rb | 12 ++++ app/lib/order_csv.rb | 64 ++++++++++++++----- app/lib/render_csv.rb | 1 + app/views/orders/_custom_csv_form.html.haml | 15 +++++ app/views/orders/custom_csv.js.haml | 3 + .../shared/_order_download_button.html.haml | 1 + config/locales/de.yml | 3 + config/locales/en.yml | 4 ++ config/locales/es.yml | 3 + config/locales/fr.yml | 3 + config/locales/nl.yml | 3 + config/routes.rb | 1 + 13 files changed, 111 insertions(+), 17 deletions(-) create mode 100644 app/views/orders/_custom_csv_form.html.haml create mode 100644 app/views/orders/custom_csv.js.haml diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index cfa7cef6..1e041bf2 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -49,7 +49,7 @@ class OrdersController < ApplicationController send_order_pdf @order, params[:document] end format.csv do - send_data OrderCsv.new(@order).to_csv, filename: @order.name + '.csv', type: 'text/csv' + send_data OrderCsv.new(@order, options= {custom_csv: params[:custom_csv]}).to_csv, filename: @order.name + '.csv', type: 'text/csv' end format.text do send_data OrderTxt.new(@order).to_txt, filename: @order.name + '.txt', type: 'text/plain' @@ -57,6 +57,19 @@ class OrdersController < ApplicationController end end + def custom_csv + @order = Order.find(params[:id]) + @view = (params[:view] || 'default').gsub(/[^-_a-zA-Z0-9]/, '') + @partial = case @view + when 'default' then 'articles' + when 'groups' then 'shared/articles_by/groups' + when 'articles' then 'shared/articles_by/articles' + else 'articles' + end + + render :layout => false + end + # Page to create a new order. def new if params[:order_id] diff --git a/app/helpers/orders_helper.rb b/app/helpers/orders_helper.rb index ff238730..5f7fb904 100644 --- a/app/helpers/orders_helper.rb +++ b/app/helpers/orders_helper.rb @@ -155,4 +155,16 @@ module OrdersHelper link_to t('orders.index.action_receive'), receive_order_path(order), class: "btn#{' btn-success' unless order.received?} #{options[:class]}" end end + + def custom_csv_collection + [ + OrderArticle.human_attribute_name(:units_to_order), + Article.human_attribute_name(:order_number), + Article.human_attribute_name(:name), + Article.human_attribute_name(:unit), + Article.human_attribute_name(:unit_quantity_short), + ArticlePrice.human_attribute_name(:price), + OrderArticle.human_attribute_name(:total_price) + ] + end end diff --git a/app/lib/order_csv.rb b/app/lib/order_csv.rb index b238f90c..653edf90 100644 --- a/app/lib/order_csv.rb +++ b/app/lib/order_csv.rb @@ -2,28 +2,60 @@ require 'csv' class OrderCsv < RenderCsv def header - [ - OrderArticle.human_attribute_name(:units_to_order), - Article.human_attribute_name(:order_number), - Article.human_attribute_name(:name), - Article.human_attribute_name(:unit), - Article.human_attribute_name(:unit_quantity_short), - ArticlePrice.human_attribute_name(:price), - OrderArticle.human_attribute_name(:total_price) - ] + params = @options[:custom_csv] + arr = if params.nil? + [ + OrderArticle.human_attribute_name(:units_to_order), + Article.human_attribute_name(:order_number), + Article.human_attribute_name(:name), + Article.human_attribute_name(:unit), + Article.human_attribute_name(:unit_quantity_short), + ArticlePrice.human_attribute_name(:price), + OrderArticle.human_attribute_name(:total_price) + ] + else + [ + params[:first], + params[:second], + params[:third], + params[:fourth], + params[:fifth], + params[:sixth], + params[:seventh] + ] + end end def data @object.order_articles.ordered.includes([:article, :article_price]).all.map do |oa| yield [ - oa.units_to_order, - oa.article.order_number, - oa.article.name, - oa.article.unit, - oa.price.unit_quantity > 1 ? oa.price.unit_quantity : nil, - number_to_currency(oa.price.price * oa.price.unit_quantity), - number_to_currency(oa.total_price) + match_params(oa, header[0]), + match_params(oa, header[1]), + match_params(oa, header[2]), + match_params(oa, header[3]), + match_params(oa, header[4]), + match_params(oa, header[5]), + match_params(oa, header[6]) ] end end + + def match_params(object, attribute) + case attribute + when OrderArticle.human_attribute_name(:units_to_order) + object.units_to_order + when Article.human_attribute_name(:order_number) + object.article.order_number + when Article.human_attribute_name(:name) + object.article.name + when Article.human_attribute_name(:unit) + object.article.unit + when Article.human_attribute_name(:unit_quantity_short) + object.price.unit_quantity > 1 ? object.price.unit_quantity : nil + when ArticlePrice.human_attribute_name(:price) + number_to_currency(object.price.price * object.price.unit_quantity) + when OrderArticle.human_attribute_name(:total_price) + number_to_currency(object.total_price) + end + end end diff --git a/app/lib/render_csv.rb b/app/lib/render_csv.rb index 1f20b075..c1fd24db 100644 --- a/app/lib/render_csv.rb +++ b/app/lib/render_csv.rb @@ -20,6 +20,7 @@ class RenderCsv end data { |d| csv << d } end + ret << I18n.t('.orders.articles.prices_sum') << ";" << "#{number_to_currency(@object.sum(:gross))}/#{number_to_currency(@object.sum(:net))}" if @options[:custom_csv] ret.encode(@options[:encoding], invalid: :replace, undef: :replace) end diff --git a/app/views/orders/_custom_csv_form.html.haml b/app/views/orders/_custom_csv_form.html.haml new file mode 100644 index 00000000..87295af0 --- /dev/null +++ b/app/views/orders/_custom_csv_form.html.haml @@ -0,0 +1,15 @@ += simple_form_for :custom_csv,format: :csv, :url => order_path(@order, view: @view, format: :csv), method: :get do |f| + .modal-header + = close_button :modal + .h3=I18n.t('.orders.custom_csv.description') + .modal-body + = f.input :first, as: :select, collection: custom_csv_collection, label: "1. " + I18n.t('.orders.custom_csv.column') + = f.input :second, as: :select, collection: custom_csv_collection, required: false, label: "2. " + I18n.t('.orders.custom_csv.column') + = f.input :third, as: :select, collection: custom_csv_collection, required: false, label: "3. " + I18n.t('.orders.custom_csv.column') + = f.input :fourth, as: :select, collection: custom_csv_collection, required: false, label: "4. " + I18n.t('.orders.custom_csv.column') + = f.input :fifth, as: :select, collection: custom_csv_collection, required: false, label: "5. " + I18n.t('.orders.custom_csv.column') + = f.input :sixth, as: :select, collection: custom_csv_collection, required: false, label: "6. " + I18n.t('.orders.custom_csv.column') + = f.input :seventh, as: :select, collection: custom_csv_collection, required: false, label: "7. " + I18n.t('.orders.custom_csv.column') + .modal-footer + = link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'} + = f.submit class: 'btn btn-primary' diff --git a/app/views/orders/custom_csv.js.haml b/app/views/orders/custom_csv.js.haml new file mode 100644 index 00000000..41a6ec83 --- /dev/null +++ b/app/views/orders/custom_csv.js.haml @@ -0,0 +1,3 @@ +$('#modalContainer').html('#{j(render("custom_csv_form"))}'); +$('#modalContainer').modal(); +$('#modalContainer').submit(function() {$('#modalContainer').modal('hide');}); \ No newline at end of file diff --git a/app/views/shared/_order_download_button.html.haml b/app/views/shared/_order_download_button.html.haml index 6890c3ca..2c362533 100644 --- a/app/views/shared/_order_download_button.html.haml +++ b/app/views/shared/_order_download_button.html.haml @@ -10,3 +10,4 @@ - unless order.stockit? %li= link_to t('.fax_txt'), order_path(order, format: :txt), {title: t('.download_file')} %li= link_to t('.fax_csv'), order_path(order, format: :csv), {title: t('.download_file')} + %li= link_to t('.custom_csv'), custom_csv_order_path(order), remote: true diff --git a/config/locales/de.yml b/config/locales/de.yml index 9b569572..9d51bdf1 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -1465,6 +1465,9 @@ de: units_ordered: Bestellte Einheiten create: notice: Die Bestellung wurde erstellt. + custom_csv: + description: Wähle die Attribute und deren Reihenfolge für die zu erzeugende CSV Datei + column: Spalte edit: title: 'Bestellung bearbeiten: %{name}' edit_amount: diff --git a/config/locales/en.yml b/config/locales/en.yml index 30a1fb53..ec6baab5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1475,6 +1475,9 @@ en: units_ordered: Units ordered create: notice: The order was created. + custom_csv: + description: Please choose the order as well as the attributes for the csv file + column: column edit: title: 'Edit order: %{name}' edit_amount: @@ -1628,6 +1631,7 @@ en: who_ordered: Who ordered? order_download_button: article_pdf: Article PDF + custom_csv: Custom CSV download_file: Download file fax_csv: Fax CSV fax_pdf: Fax PDF diff --git a/config/locales/es.yml b/config/locales/es.yml index 8bbd69d6..4004b5c5 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -1261,6 +1261,9 @@ es: units_ordered: Unidades pedidas create: notice: Se ha creado el pedido + custom_csv: + description: Por favor elija el orden de los atributos así como los atributos para el archivo csv + column: columna edit: title: 'Edita pedido: %{name}' edit_amount: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index b1199dc7..304a7b9d 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -1011,6 +1011,9 @@ fr: units_ordered: Unités commandées create: notice: La commande a bien été définie. + custom_csv: + description: Veuillez choisir l'ordre des attributs ainsi que les attributs pour le fichier csv + column: colonne edit: title: 'Modifier la commande: %{name}' edit_amount: diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 31179a6d..facd55d0 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -1440,6 +1440,9 @@ nl: units_ordered: Bestelde eenheden create: notice: De bestelling is aangemaakt. + custom_csv: + description: Kies de volgorde van de attributen en de attributen voor het csv-bestand + column: kolom edit: title: 'Bestelling aanpassen: %{name}' edit_amount: diff --git a/config/routes.rb b/config/routes.rb index 83e65707..b82699ec 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -47,6 +47,7 @@ Rails.application.routes.draw do get :receive post :receive + get :custom_csv get :receive_on_order_article_create get :receive_on_order_article_update end