Skip to content

Commit 895589c

Browse files
Viehliebyksflip
authored andcommitted
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
1 parent caa32de commit 895589c

13 files changed

+111
-17
lines changed

app/controllers/orders_controller.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,27 @@ def show
5050
send_order_pdf @order, params[:document]
5151
end
5252
format.csv do
53-
send_data OrderCsv.new(@order).to_csv, filename: @order.name + '.csv', type: 'text/csv'
53+
send_data OrderCsv.new(@order, options= {custom_csv: params[:custom_csv]}).to_csv, filename: @order.name + '.csv', type: 'text/csv'
5454
end
5555
format.text do
5656
send_data OrderTxt.new(@order).to_txt, filename: @order.name + '.txt', type: 'text/plain'
5757
end
5858
end
5959
end
6060

61+
def custom_csv
62+
@order = Order.find(params[:id])
63+
@view = (params[:view] || 'default').gsub(/[^-_a-zA-Z0-9]/, '')
64+
@partial = case @view
65+
when 'default' then 'articles'
66+
when 'groups' then 'shared/articles_by/groups'
67+
when 'articles' then 'shared/articles_by/articles'
68+
else 'articles'
69+
end
70+
71+
render :layout => false
72+
end
73+
6174
# Page to create a new order.
6275
def new
6376
if params[:order_id]

app/helpers/orders_helper.rb

+12
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,16 @@ def receive_button(order, options = {})
155155
class: "btn#{' btn-success' unless order.received?} #{options[:class]}"
156156
end
157157
end
158+
159+
def custom_csv_collection
160+
[
161+
OrderArticle.human_attribute_name(:units_to_order),
162+
Article.human_attribute_name(:order_number),
163+
Article.human_attribute_name(:name),
164+
Article.human_attribute_name(:unit),
165+
Article.human_attribute_name(:unit_quantity_short),
166+
ArticlePrice.human_attribute_name(:price),
167+
OrderArticle.human_attribute_name(:total_price)
168+
]
169+
end
158170
end

app/lib/order_csv.rb

+48-16
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,60 @@
22

33
class OrderCsv < RenderCsv
44
def header
5-
[
6-
OrderArticle.human_attribute_name(:units_to_order),
7-
Article.human_attribute_name(:order_number),
8-
Article.human_attribute_name(:name),
9-
Article.human_attribute_name(:unit),
10-
Article.human_attribute_name(:unit_quantity_short),
11-
ArticlePrice.human_attribute_name(:price),
12-
OrderArticle.human_attribute_name(:total_price)
13-
]
5+
params = @options[:custom_csv]
6+
arr = if params.nil?
7+
[
8+
OrderArticle.human_attribute_name(:units_to_order),
9+
Article.human_attribute_name(:order_number),
10+
Article.human_attribute_name(:name),
11+
Article.human_attribute_name(:unit),
12+
Article.human_attribute_name(:unit_quantity_short),
13+
ArticlePrice.human_attribute_name(:price),
14+
OrderArticle.human_attribute_name(:total_price)
15+
]
16+
else
17+
[
18+
params[:first],
19+
params[:second],
20+
params[:third],
21+
params[:fourth],
22+
params[:fifth],
23+
params[:sixth],
24+
params[:seventh]
25+
]
26+
end
1427
end
1528

1629
def data
1730
@object.order_articles.ordered.includes(%i[article article_price]).all.map do |oa|
1831
yield [
19-
oa.units_to_order,
20-
oa.article.order_number,
21-
oa.article.name,
22-
oa.article.unit,
23-
oa.price.unit_quantity > 1 ? oa.price.unit_quantity : nil,
24-
number_to_currency(oa.price.price * oa.price.unit_quantity),
25-
number_to_currency(oa.total_price)
32+
match_params(oa, header[0]),
33+
match_params(oa, header[1]),
34+
match_params(oa, header[2]),
35+
match_params(oa, header[3]),
36+
match_params(oa, header[4]),
37+
match_params(oa, header[5]),
38+
match_params(oa, header[6])
2639
]
2740
end
2841
end
42+
43+
def match_params(object, attribute)
44+
case attribute
45+
when OrderArticle.human_attribute_name(:units_to_order)
46+
object.units_to_order
47+
when Article.human_attribute_name(:order_number)
48+
object.article.order_number
49+
when Article.human_attribute_name(:name)
50+
object.article.name
51+
when Article.human_attribute_name(:unit)
52+
object.article.unit
53+
when Article.human_attribute_name(:unit_quantity_short)
54+
object.price.unit_quantity > 1 ? object.price.unit_quantity : nil
55+
when ArticlePrice.human_attribute_name(:price)
56+
number_to_currency(object.price.price * object.price.unit_quantity)
57+
when OrderArticle.human_attribute_name(:total_price)
58+
number_to_currency(object.total_price)
59+
end
60+
end
2961
end

app/lib/render_csv.rb

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def to_csv
2020
end
2121
data { |d| csv << d }
2222
end
23+
ret << I18n.t('.orders.articles.prices_sum') << ";" << "#{number_to_currency(@object.sum(:gross))}/#{number_to_currency(@object.sum(:net))}" if @options[:custom_csv]
2324
ret.encode(@options[:encoding], invalid: :replace, undef: :replace)
2425
end
2526

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
= simple_form_for :custom_csv,format: :csv, :url => order_path(@order, view: @view, format: :csv), method: :get do |f|
2+
.modal-header
3+
= close_button :modal
4+
.h3=I18n.t('.orders.custom_csv.description')
5+
.modal-body
6+
= f.input :first, as: :select, collection: custom_csv_collection, label: "1. " + I18n.t('.orders.custom_csv.column')
7+
= f.input :second, as: :select, collection: custom_csv_collection, required: false, label: "2. " + I18n.t('.orders.custom_csv.column')
8+
= f.input :third, as: :select, collection: custom_csv_collection, required: false, label: "3. " + I18n.t('.orders.custom_csv.column')
9+
= f.input :fourth, as: :select, collection: custom_csv_collection, required: false, label: "4. " + I18n.t('.orders.custom_csv.column')
10+
= f.input :fifth, as: :select, collection: custom_csv_collection, required: false, label: "5. " + I18n.t('.orders.custom_csv.column')
11+
= f.input :sixth, as: :select, collection: custom_csv_collection, required: false, label: "6. " + I18n.t('.orders.custom_csv.column')
12+
= f.input :seventh, as: :select, collection: custom_csv_collection, required: false, label: "7. " + I18n.t('.orders.custom_csv.column')
13+
.modal-footer
14+
= link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'}
15+
= f.submit class: 'btn btn-primary'

app/views/orders/custom_csv.js.haml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$('#modalContainer').html('#{j(render("custom_csv_form"))}');
2+
$('#modalContainer').modal();
3+
$('#modalContainer').submit(function() {$('#modalContainer').modal('hide');});

app/views/shared/_order_download_button.html.haml

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
- unless order.stockit?
1111
%li= link_to t('.fax_txt'), order_path(order, format: :txt), {title: t('.download_file')}
1212
%li= link_to t('.fax_csv'), order_path(order, format: :csv), {title: t('.download_file')}
13+
%li= link_to t('.custom_csv'), custom_csv_order_path(order), remote: true

config/locales/de.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,9 @@ de:
14671467
units_ordered: Bestellte Einheiten
14681468
create:
14691469
notice: Die Bestellung wurde erstellt.
1470+
custom_csv:
1471+
description: Wähle die Attribute und deren Reihenfolge für die zu erzeugende CSV Datei
1472+
column: Spalte
14701473
edit:
14711474
title: 'Bestellung bearbeiten: %{name}'
14721475
edit_amount:

config/locales/en.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,9 @@ en:
14771477
units_ordered: Units ordered
14781478
create:
14791479
notice: The order was created.
1480+
custom_csv:
1481+
description: Please choose the order as well as the attributes for the csv file
1482+
column: column
14801483
edit:
14811484
title: 'Edit order: %{name}'
14821485
edit_amount:
@@ -1630,6 +1633,7 @@ en:
16301633
who_ordered: Who ordered?
16311634
order_download_button:
16321635
article_pdf: Article PDF
1636+
custom_csv: Custom CSV
16331637
download_file: Download file
16341638
fax_csv: Fax CSV
16351639
fax_pdf: Fax PDF

config/locales/es.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,9 @@ es:
12631263
units_ordered: Unidades pedidas
12641264
create:
12651265
notice: Se ha creado el pedido
1266+
custom_csv:
1267+
description: Por favor elija el orden de los atributos así como los atributos para el archivo csv
1268+
column: columna
12661269
edit:
12671270
title: 'Edita pedido: %{name}'
12681271
edit_amount:

config/locales/fr.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,9 @@ fr:
10141014
units_ordered: Unités commandées
10151015
create:
10161016
notice: La commande a bien été définie.
1017+
custom_csv:
1018+
description: Veuillez choisir l'ordre des attributs ainsi que les attributs pour le fichier csv
1019+
column: colonne
10171020
edit:
10181021
title: 'Modifier la commande: %{name}'
10191022
edit_amount:

config/locales/nl.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,9 @@ nl:
14421442
units_ordered: Bestelde eenheden
14431443
create:
14441444
notice: De bestelling is aangemaakt.
1445+
custom_csv:
1446+
description: Kies de volgorde van de attributen en de attributen voor het csv-bestand
1447+
column: kolom
14451448
edit:
14461449
title: 'Bestelling aanpassen: %{name}'
14471450
edit_amount:

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
get :receive
4949
post :receive
5050

51+
get :custom_csv
5152
get :receive_on_order_article_create
5253
get :receive_on_order_article_update
5354
end

0 commit comments

Comments
 (0)