Skip to content

Commit 690ea87

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 fb8ccfe commit 690ea87

13 files changed

+111
-17
lines changed

app/controllers/orders_controller.rb

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

60+
def custom_csv
61+
@order = Order.find(params[:id])
62+
@view = (params[:view] || 'default').gsub(/[^-_a-zA-Z0-9]/, '')
63+
@partial = case @view
64+
when 'default' then 'articles'
65+
when 'groups' then 'shared/articles_by/groups'
66+
when 'articles' then 'shared/articles_by/articles'
67+
else 'articles'
68+
end
69+
70+
render :layout => false
71+
end
72+
6073
# Page to create a new order.
6174
def new
6275
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
link_to t('orders.index.action_receive'), receive_order_path(order), 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([: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
@@ -1463,6 +1463,9 @@ de:
14631463
units_ordered: Bestellte Einheiten
14641464
create:
14651465
notice: Die Bestellung wurde erstellt.
1466+
custom_csv:
1467+
description: Wähle die Attribute und deren Reihenfolge für die zu erzeugende CSV Datei
1468+
column: Spalte
14661469
edit:
14671470
title: 'Bestellung bearbeiten: %{name}'
14681471
edit_amount:

config/locales/en.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,9 @@ en:
14731473
units_ordered: Units ordered
14741474
create:
14751475
notice: The order was created.
1476+
custom_csv:
1477+
description: Please choose the order as well as the attributes for the csv file
1478+
column: column
14761479
edit:
14771480
title: 'Edit order: %{name}'
14781481
edit_amount:
@@ -1626,6 +1629,7 @@ en:
16261629
who_ordered: Who ordered?
16271630
order_download_button:
16281631
article_pdf: Article PDF
1632+
custom_csv: Custom CSV
16291633
download_file: Download file
16301634
fax_csv: Fax CSV
16311635
fax_pdf: Fax PDF

config/locales/es.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,9 @@ es:
12591259
units_ordered: Unidades pedidas
12601260
create:
12611261
notice: Se ha creado el pedido
1262+
custom_csv:
1263+
description: Por favor elija el orden de los atributos así como los atributos para el archivo csv
1264+
column: columna
12621265
edit:
12631266
title: 'Edita pedido: %{name}'
12641267
edit_amount:

config/locales/fr.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,9 @@ fr:
10101010
units_ordered: Unités commandées
10111011
create:
10121012
notice: La commande a bien été définie.
1013+
custom_csv:
1014+
description: Veuillez choisir l'ordre des attributs ainsi que les attributs pour le fichier csv
1015+
column: colonne
10131016
edit:
10141017
title: 'Modifier la commande: %{name}'
10151018
edit_amount:

config/locales/nl.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,9 @@ nl:
14381438
units_ordered: Bestelde eenheden
14391439
create:
14401440
notice: De bestelling is aangemaakt.
1441+
custom_csv:
1442+
description: Kies de volgorde van de attributen en de attributen voor het csv-bestand
1443+
column: kolom
14411444
edit:
14421445
title: 'Bestelling aanpassen: %{name}'
14431446
edit_amount:

config/routes.rb

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

50+
get :custom_csv
5051
get :receive_on_order_article_create
5152
get :receive_on_order_article_update
5253
end

0 commit comments

Comments
 (0)