add functionality to choose column headers + order for custom csv and append order.sum gross&net to custom csv

This commit is contained in:
viehlieb 2023-02-14 16:57:37 +01:00
parent ea0fba9010
commit 928a02ed3f
2 changed files with 49 additions and 16 deletions

View file

@ -2,6 +2,8 @@ require 'csv'
class OrderCsv < RenderCsv
def header
params = @options[:custom_csv]
arr = if params.nil?
[
OrderArticle.human_attribute_name(:units_to_order),
Article.human_attribute_name(:order_number),
@ -11,19 +13,49 @@ class OrderCsv < RenderCsv
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

View file

@ -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