Skip to content

Commit

Permalink
Add proper filename, some CSV fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Altmann committed Jul 27, 2016
1 parent b2825c9 commit db8f643
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/business_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def export

exporter = BusinessTransactionExporter.new(@user, @time_range)

send_data(exporter.csv_string, filename: 'text.csv', type: 'text/csv; charset=utf-8',
send_data(exporter.csv_string, filename: exporter.filename, type: 'text/csv; charset=utf-8',
disposition: 'attachment')
end

Expand Down
1 change: 1 addition & 0 deletions app/models/business_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BusinessTransaction < ActiveRecord::Base

has_one :seller, through: :line_item_group
has_one :buyer, through: :line_item_group
has_one :refund

enumerize :selected_transport, in: Article::TRANSPORT_TYPES
enumerize :selected_payment, in: Article::PAYMENT_TYPES
Expand Down
18 changes: 16 additions & 2 deletions app/objects/service/business_transaction_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def csv_string
CSV.generate(encoding: 'utf-8', col_sep: ';') do |csv|
csv << ['Datum', 'Bestellnr.', 'Artikelname', 'Anzahl', 'Einzelkosten', 'Endbetrag',
'Mehrwertsteuersatz', 'Versandart', 'Zahlungsart', 'Nachricht', 'Käufer',
'E-Mail-Adresse', 'Rechnungsadresse', 'Lieferadresse']
'Name', 'Firma', 'E-Mail-Adresse', 'Rechnungsadresse', 'Lieferadresse']
query.find_each do |bt|
csv << [
bt.sold_at.strftime('%d.%m.%Y'),
bt.line_item_group_id,
bt.line_item_group.purchase_id,
bt.article.title,
bt.quantity_bought,
bt.article.price,
Expand All @@ -26,9 +26,17 @@ def csv_string
bt.selected_payment,
bt.line_item_group.message,
bt.line_item_group.buyer.nickname,
"#{bt.buyer.standard_address_first_name} #{bt.buyer.standard_address_last_name}",
bt.buyer.standard_address_company_name,
bt.line_item_group.buyer.email,
bt.line_item_group.payment_address.to_s,
bt.line_item_group.transport_address.to_s
#bt.refund.present?,
#bt.refund.reason
# Kombiversand ja/nein
# Kaufpreis Bestellung
# Versandkosten Bestellung
# Gesamtbetrag Bestellung
]
end
end
Expand All @@ -39,4 +47,10 @@ def query
.joins(:article, line_item_group: [:payment_address, :transport_address, :buyer])
.where(line_item_groups: { seller_id: @user.id }, sold_at: @time_range)
end

def filename
start_date = @time_range.begin.strftime('%Y%m%d')
end_date = @time_range.end.strftime('%Y%m%d')
"fairmondo-bestellungen_#{start_date}-#{end_date}.csv"
end
end
2 changes: 1 addition & 1 deletion app/views/users/show/_line_item_group_accordion.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@

= form_tag(export_business_transactions_path, method: 'get') do
= select_year(Time.now, start_year: 2013, end_year: 2016)
= select_month(Time.now, use_month_numbers: true)
= select_month(Time.now)
= submit_tag('Exportieren', id: 'export_business_transactions_submit', class: 'Button')
4 changes: 2 additions & 2 deletions test/fixtures/business_transaction_export1.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Datum;Bestellnr.;Artikelname;Anzahl;Einzelkosten;Endbetrag;Mehrwertsteuersatz;Versandart;Zahlungsart;Nachricht;Käufer;E-Mail-Adresse;Rechnungsadresse;Lieferadresse
01.04.2016;2;Book 1;1;39,95;39,95;19;type1;bank_transfer;MyText;User 2;user2@example.com;Erika Mustermann, Heidestraße 17, 51147 Köln, Deutschland;Erika Mustermann, Eldenaer Straße 17, 10247 Berlin, Deutschland
Datum;Bestellnr.;Artikelname;Anzahl;Einzelkosten;Endbetrag;Mehrwertsteuersatz;Versandart;Zahlungsart;Nachricht;Käufer;Name;Firma;E-Mail-Adresse;Rechnungsadresse;Lieferadresse
01.04.2016;F00000012;Book 1;1;39,95;39,95;19;type1;bank_transfer;MyText;User 2;Erika Mustermann;;user2@example.com;Erika Mustermann, Heidestraße 17, 51147 Köln, Deutschland;Erika Mustermann, Eldenaer Straße 17, 10247 Berlin, Deutschland
16 changes: 15 additions & 1 deletion test/objects/business_transaction_exporter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
describe BusinessTransactionExporter do
let(:bt) { create :business_transaction_from_legal_entity }

describe 'integration tests' do
describe 'csv export' do
it 'should return a csv string with the business transactions' do
FactoryGirl.reload

Expand All @@ -21,6 +21,20 @@
assert_equal(expected_csv, exporter.csv_string)
end
end

it 'should return an expressive filename' do
travel_to Time.new(2016, 04, 01, 12) do
time_range = Time.new(2016, 03, 01, 12)..Time.new(2016, 04, 30, 12)
user = build_stubbed :legal_entity

exporter = BusinessTransactionExporter.new(user, time_range)

assert_equal(
'fairmondo-bestellungen_20160301-20160430.csv',
exporter.filename
)
end
end
end

describe 'date' do
Expand Down

0 comments on commit db8f643

Please sign in to comment.