foodsoft/app/controllers/articles_controller.rb

236 lines
8.8 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2009-01-06 11:49:19 +01:00
class ArticlesController < ApplicationController
before_filter :authenticate_article_meta, :find_supplier
2009-01-06 11:49:19 +01:00
def index
if params['sort']
sort = case params['sort']
when "name" then "articles.name"
when "unit" then "articles.unit"
when "category" then "article_categories.name"
when "note" then "articles.note"
when "availability" then "articles.availability"
when "name_reverse" then "articles.name DESC"
when "unit_reverse" then "articles.unit DESC"
when "category_reverse" then "article_categories.name DESC"
when "note_reverse" then "articles.note DESC"
when "availability_reverse" then "articles.availability DESC"
end
2009-01-06 11:49:19 +01:00
else
sort = "article_categories.name, articles.name"
end
@articles = Article.undeleted.where(supplier_id: @supplier, :type => nil).includes(:article_category).order(sort)
2012-10-19 01:12:47 +02:00
@articles = @articles.where('articles.name LIKE ?', "%#{params[:query]}%") unless params[:query].nil?
2012-10-19 01:12:47 +02:00
@articles = @articles.page(params[:page]).per(@per_page)
respond_to do |format|
format.html
format.js { render :layout => false }
end
2009-01-06 11:49:19 +01:00
end
def new
@article = @supplier.articles.build(:tax => 7.0)
render :layout => false
2009-01-06 11:49:19 +01:00
end
def create
2009-01-06 11:49:19 +01:00
@article = Article.new(params[:article])
if @article.valid? and @article.save
render :layout => false
2009-01-06 11:49:19 +01:00
else
render :action => 'new', :layout => false
end
2009-01-06 11:49:19 +01:00
end
def edit
2009-01-06 11:49:19 +01:00
@article = Article.find(params[:id])
render :action => 'new', :layout => false
2009-01-06 11:49:19 +01:00
end
# Updates one Article and highlights the line if succeded
def update
@article = Article.find(params[:id])
2009-01-06 11:49:19 +01:00
if @article.update_attributes(params[:article])
render :layout => false
2009-01-06 11:49:19 +01:00
else
render :action => 'new', :layout => false
2009-01-06 11:49:19 +01:00
end
end
# Deletes article from database. send error msg, if article is used in a current order
def destroy
2009-01-06 11:49:19 +01:00
@article = Article.find(params[:id])
@article.mark_as_deleted unless @order = @article.in_open_order # If article is in an active Order, the Order will be returned
render :layout => false
2009-01-06 11:49:19 +01:00
end
# Renders a form for editing all articles from a supplier
def edit_all
@articles = @supplier.articles.undeleted
2009-01-06 11:49:19 +01:00
end
2009-01-06 11:49:19 +01:00
# Updates all article of specific supplier
# deletes all articles from params[outlisted_articles]
def update_all
begin
Article.transaction do
unless params[:articles].blank?
2012-10-28 18:03:50 +01:00
invalid_articles = false
2009-01-06 11:49:19 +01:00
# Update other article attributes...
2012-10-28 18:03:50 +01:00
@articles = Article.find(params[:articles].keys)
@articles.each do |article|
unless article.update_attributes(params[:articles][article.id.to_s])
invalid_articles = true unless invalid_articles # Remember that there are validation errors
end
2009-01-06 11:49:19 +01:00
end
2012-10-28 18:03:50 +01:00
raise "Artikel sind fehlerhaft. Bitte überprüfe Deine Eingaben." if invalid_articles
2009-01-06 11:49:19 +01:00
end
# delete articles
if params[:outlisted_articles]
params[:outlisted_articles].keys.each {|id| Article.find(id).mark_as_deleted }
2009-01-06 11:49:19 +01:00
end
end
2009-01-06 11:49:19 +01:00
# Successfully done.
2012-10-28 18:03:50 +01:00
redirect_to supplier_articles_path(@supplier), notice: "Alle Artikel und Preise wurden aktalisiert"
2009-01-06 11:49:19 +01:00
rescue => e
# An error has occurred, transaction has been rolled back.
2012-10-28 18:03:50 +01:00
if params[:sync]
flash[:error] = "Es trat ein Fehler beim Aktualisieren des Artikels '#{current_article.name}' auf: #{e.message}"
redirect_to(supplier_articles_path(@supplier))
else
2012-10-28 18:03:50 +01:00
flash.now.alert = e.message
render :edit_all
2009-01-06 11:49:19 +01:00
end
end
end
# makes different actions on selected articles
def update_selected
raise 'Du hast keine Artikel ausgewählt' if params[:selected_articles].nil?
articles = Article.find(params[:selected_articles])
Article.transaction do
case params[:selected_action]
when 'destroy'
articles.each(&:mark_as_deleted)
flash[:notice] = 'Alle gewählten Artikel wurden gelöscht'
when 'setNotAvailable'
articles.each {|a| a.update_attribute(:availability, false) }
flash[:notice] = 'Alle gewählten Artikel wurden auf "nicht verfügbar" gesetzt'
when 'setAvailable'
articles.each {|a| a.update_attribute(:availability, true) }
flash[:notice] = 'Alle gewählten Artikel wurden auf "verfügbar" gesetzt'
else
flash[:alert] = 'Keine Aktion ausgewählt!'
end
2009-01-06 11:49:19 +01:00
end
# action succeded
redirect_to supplier_articles_url(@supplier, :per_page => params[:per_page])
rescue => error
redirect_to supplier_articles_url(@supplier, :per_page => params[:per_page]),
:alert => "Ein Fehler ist aufgetreten: #{error}"
2009-01-06 11:49:19 +01:00
end
# lets start with parsing articles from uploaded file, yeah
# Renders the upload form
def upload
2009-01-06 11:49:19 +01:00
end
# parses the articles from a csv and creates a form-table with the parsed data.
# the csv must have the following format:
# status | number | name | note | manufacturer | origin | unit | clear price | unit_quantity | tax | deposit | scale quantity | scale price | category
# the first line will be ignored.
# field-seperator: ";"
# text-seperator: ""
def parse_upload
2009-01-06 11:49:19 +01:00
begin
@articles = Array.new
articles, outlisted_articles = FoodsoftFile::parse(params[:articles]["file"])
articles.each do |row|
# creates a new article and price
article = Article.new( :name => row[:name],
:note => row[:note],
:manufacturer => row[:manufacturer],
:origin => row[:origin],
:unit => row[:unit],
:article_category => ArticleCategory.find_by_name(row[:category]),
:price => row[:price],
2009-01-06 11:49:19 +01:00
:unit_quantity => row[:unit_quantity],
:order_number => row[:number],
:deposit => row[:deposit],
:tax => row[:tax])
# stop parsing, when an article isn't valid
unless article.valid?
raise article.errors.full_messages.join(", ") + " ..in line " + (articles.index(row) + 2).to_s
2009-01-06 11:49:19 +01:00
end
@articles << article
end
flash.now[:notice] = "#{@articles.size} articles are parsed successfully."
rescue => error
redirect_to upload_supplier_articles_path(@supplier), :alert => "An error has occurred: #{error.message}"
2009-01-06 11:49:19 +01:00
end
end
# creates articles from form
def create_from_upload
2009-01-06 11:49:19 +01:00
begin
Article.transaction do
2012-10-28 18:03:50 +01:00
invalid_articles = false
@articles = []
params[:articles].each do |_key, article_attributes|
@articles << (article = @supplier.articles.build(article_attributes))
invalid_articles = true unless article.save
2009-01-06 11:49:19 +01:00
end
2012-10-28 18:03:50 +01:00
raise "Artikel sind fehlerhaft" if invalid_articles
2009-01-06 11:49:19 +01:00
end
# Successfully done.
2012-10-28 18:03:50 +01:00
redirect_to supplier_articles_path(@supplier), notice: "Es wurden #{@articles.size} neue Artikel gespeichert."
rescue => error
2009-01-06 11:49:19 +01:00
# An error has occurred, transaction has been rolled back.
2012-10-28 18:03:50 +01:00
flash.now[:error] = "An error occured: #{error.message}"
render :parse_upload
2009-01-06 11:49:19 +01:00
end
end
# renders a view to import articles in local database
#
def shared
# build array of keywords, required for meta search _all suffix
params[:search][:name_contains_all] = params[:search][:name_contains_all].split(' ') if params[:search]
# Build search with meta search plugin
@search = @supplier.shared_supplier.shared_articles.search(params[:search])
2012-10-19 01:12:47 +02:00
@articles = @search.page(params[:page]).per(10)
render :layout => false
2009-01-06 11:49:19 +01:00
end
# fills a form whith values of the selected shared_article
def import
@article = SharedArticle.find(params[:shared_article_id]).build_new_article(@supplier)
render :action => 'new', :layout => false
2009-01-06 11:49:19 +01:00
end
# sync all articles with the external database
# renders a form with articles, which should be updated
def sync
2009-01-06 11:49:19 +01:00
# check if there is an shared_supplier
unless @supplier.shared_supplier
redirect_to supplier_articles_url(@supplier), :alert => "#{@supplier.name} ist nicht mit einer externen Datenbank verknüpft."
2009-01-06 11:49:19 +01:00
end
# sync articles against external database
@updated_articles, @outlisted_articles = @supplier.sync_all
2009-02-10 13:26:10 +01:00
# convert to db-compatible-string
2009-01-06 11:49:19 +01:00
@updated_articles.each {|a, b| a.shared_updated_on = a.shared_updated_on.to_formatted_s(:db)}
if @updated_articles.empty? && @outlisted_articles.empty?
redirect_to supplier_articles_path(@supplier), :notice => "Der Katalog ist aktuell."
2009-01-06 11:49:19 +01:00
end
end
end