Add download for articles

This commit is contained in:
Patrick Gansterer 2020-06-22 16:03:32 +02:00
parent b6f5295267
commit 5a9cc811c9
5 changed files with 52 additions and 0 deletions

View file

@ -21,6 +21,12 @@ class ArticlesController < ApplicationController
end end
@articles = Article.undeleted.where(supplier_id: @supplier, :type => nil).includes(:article_category).order(sort) @articles = Article.undeleted.where(supplier_id: @supplier, :type => nil).includes(:article_category).order(sort)
if request.format.csv?
send_data ArticlesCsv.new(@articles, encoding: 'utf-8').to_csv, filename: 'articles.csv', type: 'text/csv'
return
end
@articles = @articles.where('articles.name LIKE ?', "%#{params[:query]}%") unless params[:query].nil? @articles = @articles.where('articles.name LIKE ?', "%#{params[:query]}%") unless params[:query].nil?
@articles = @articles.page(params[:page]).per(@per_page) @articles = @articles.page(params[:page]).per(@per_page)

View file

@ -26,6 +26,7 @@
= link_to t('.new'), new_supplier_article_path(@supplier), remote: true, class: "btn #{'btn-primary' unless @supplier.shared_supplier}" = link_to t('.new'), new_supplier_article_path(@supplier), remote: true, class: "btn #{'btn-primary' unless @supplier.shared_supplier}"
= link_to t('.edit_all'), edit_all_supplier_articles_path(@supplier), class: 'btn' = link_to t('.edit_all'), edit_all_supplier_articles_path(@supplier), class: 'btn'
= link_to t('.upload'), upload_supplier_articles_path(@supplier), class: 'btn' = link_to t('.upload'), upload_supplier_articles_path(@supplier), class: 'btn'
= link_to t('.download'), supplier_articles_path(@supplier, format: :csv), class: 'btn'
- if current_user.role_orders? - if current_user.role_orders?
= link_to t('.new_order'), new_order_path(supplier_id: @supplier), class: 'btn' = link_to t('.new_order'), new_order_path(supplier_id: @supplier), class: 'btn'

View file

@ -455,6 +455,7 @@ de:
not_found: Keine Artikel gefunden not_found: Keine Artikel gefunden
index: index:
change_supplier: Lieferant wechseln ... change_supplier: Lieferant wechseln ...
download: Artikel herunterladen
edit_all: Alle bearbeiten edit_all: Alle bearbeiten
ext_db: ext_db:
import: Suchen/Importieren import: Suchen/Importieren

View file

@ -474,6 +474,7 @@ en:
not_found: No articles found not_found: No articles found
index: index:
change_supplier: Change supplier ... change_supplier: Change supplier ...
download: Download articles
edit_all: Edit all edit_all: Edit all
ext_db: ext_db:
import: Import article import: Import article

43
lib/articles_csv.rb Normal file
View file

@ -0,0 +1,43 @@
class ArticlesCsv < RenderCSV
include ApplicationHelper
def header
[
'',
Article.human_attribute_name(:order_number),
Article.human_attribute_name(:name),
Article.human_attribute_name(:note),
Article.human_attribute_name(:manufacturer),
Article.human_attribute_name(:origin),
Article.human_attribute_name(:unit),
Article.human_attribute_name(:price),
Article.human_attribute_name(:tax),
Article.human_attribute_name(:deposit),
Article.human_attribute_name(:unit_quantity),
'',
'',
Article.human_attribute_name(:article_category),
]
end
def data
@object.each do |o|
yield [
'',
o.order_number,
o.name,
o.note,
o.manufacturer,
o.origin,
o.unit,
o.price,
o.tax,
o.deposit,
o.unit_quantity,
'',
'',
o.article_category.try(:name),
]
end
end
end