make csv export more compatible with Microsoft Excel
This commit is contained in:
parent
71a922a034
commit
724ce6ff60
2 changed files with 62 additions and 31 deletions
|
@ -1,39 +1,28 @@
|
|||
require 'csv'
|
||||
|
||||
class OrderCsv
|
||||
include ActionView::Helpers::NumberHelper
|
||||
class OrderCsv < RenderCSV
|
||||
|
||||
def initialize(order, options={})
|
||||
@order = order
|
||||
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),
|
||||
ArticlePrice.human_attribute_name(:price),
|
||||
OrderArticle.human_attribute_name(:total_price)
|
||||
]
|
||||
end
|
||||
|
||||
def to_csv
|
||||
CSV.generate do |csv|
|
||||
# header
|
||||
csv << [
|
||||
OrderArticle.human_attribute_name(:units_to_order),
|
||||
Article.human_attribute_name(:order_number),
|
||||
Article.human_attribute_name(:name),
|
||||
Article.human_attribute_name(:unit),
|
||||
ArticlePrice.human_attribute_name(:price),
|
||||
OrderArticle.human_attribute_name(:total_price)
|
||||
]
|
||||
# data
|
||||
@order.order_articles.ordered.includes([:article, :article_price]).all.map do |oa|
|
||||
csv << [
|
||||
oa.units_to_order,
|
||||
oa.article.order_number,
|
||||
oa.article.name,
|
||||
oa.article.unit + (oa.price.unit_quantity > 1 ? " × #{oa.price.unit_quantity}" : ''),
|
||||
number_to_currency(oa.article_price.price * oa.article_price.unit_quantity),
|
||||
number_to_currency(oa.total_price)
|
||||
]
|
||||
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}" : ''),
|
||||
number_to_currency(oa.article_price.price * oa.article_price.unit_quantity),
|
||||
number_to_currency(oa.total_price)
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
# Helper method to test pdf via rails console: OrderCsv.new(order).save_tmp
|
||||
def save_tmp
|
||||
File.open("#{Rails.root}/tmp/#{self.class.to_s.underscore}.csv", 'w') {|f| f.write(to_csv.force_encoding("UTF-8")) }
|
||||
end
|
||||
end
|
||||
|
|
42
lib/render_csv.rb
Normal file
42
lib/render_csv.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
require 'csv'
|
||||
|
||||
class RenderCSV
|
||||
include ActionView::Helpers::NumberHelper
|
||||
|
||||
def initialize(object, options={})
|
||||
@object = object
|
||||
@options = options
|
||||
# defaults to please Microsoft Excel ...
|
||||
@options[:col_sep] ||= FoodsoftConfig[:csv_col_sep] || ';'
|
||||
@options[:row_sep] ||= FoodsoftConfig[:csv_row_sep] if FoodsoftConfig[:csv_row_sep]
|
||||
@options[:encoding] ||= FoodsoftConfig[:csv_encoding] || 'ISO-8859-15'
|
||||
end
|
||||
|
||||
def to_csv
|
||||
CSV.generate @options do |csv|
|
||||
if h = header
|
||||
csv << h
|
||||
end
|
||||
data {|d| csv << d}
|
||||
end
|
||||
end
|
||||
|
||||
def header
|
||||
nil
|
||||
end
|
||||
|
||||
def data
|
||||
yield []
|
||||
end
|
||||
|
||||
# Helper method to test pdf via rails console: OrderCsv.new(order).save_tmp
|
||||
def save_tmp
|
||||
encoding = @options[:encoding] || 'UTF-8'
|
||||
File.open("#{Rails.root}/tmp/#{self.class.to_s.underscore}.csv", 'w') {|f| f.write(to_csv.force_encoding(encoding)) }
|
||||
end
|
||||
|
||||
# XXX avoid encoding confusion when using unicode whitespace
|
||||
def number_to_currency(number, options={})
|
||||
super(number, options).gsub("\u202f", ' ')
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue