foodsoft/app/controllers/articles_controller.rb

250 lines
9.2 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"
2013-10-10 19:22:43 +02:00
when "article_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"
2013-10-10 19:22:43 +02:00
when "article_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 => FoodsoftConfig[:tax_default])
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? && @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
2009-01-06 11:49:19 +01:00
# 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
end
2009-01-06 11:49:19 +01:00
# 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
def update_all
invalid_articles = false
2009-01-06 11:49:19 +01:00
begin
Article.transaction do
unless params[:articles].blank?
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 ActiveRecord::Rollback if invalid_articles # Rollback all changes
2009-01-06 11:49:19 +01:00
end
end
end
if invalid_articles
2009-01-06 11:49:19 +01:00
# An error has occurred, transaction has been rolled back.
flash.now.alert = I18n.t('articles.controller.error_invalid')
render :edit_all
else
# Successfully done.
redirect_to supplier_articles_path(@supplier), notice: I18n.t('articles.controller.update_all.notice')
2009-01-06 11:49:19 +01:00
end
end
2009-01-06 11:49:19 +01:00
# makes different actions on selected articles
def update_selected
raise I18n.t('articles.controller.error_nosel') 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] = I18n.t('articles.controller.update_sel.notice_destroy')
when 'setNotAvailable'
articles.each {|a| a.update_attribute(:availability, false) }
flash[:notice] = I18n.t('articles.controller.update_sel.notice_unavail')
when 'setAvailable'
articles.each {|a| a.update_attribute(:availability, true) }
flash[:notice] = I18n.t('articles.controller.update_sel.notice_avail')
else
flash[:alert] = I18n.t('articles.controller.update_sel.notice_noaction')
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]),
2013-03-22 00:50:30 +01:00
:alert => I18n.t('errors.general_msg', :msg => error)
2009-01-06 11:49:19 +01:00
end
2009-01-06 11:49:19 +01:00
# lets start with parsing articles from uploaded file, yeah
# Renders the upload form
def upload
2009-01-06 11:49:19 +01:00
end
2012-10-28 18:03:50 +01:00
# Update articles from a spreadsheet
def parse_upload
uploaded_file = params[:articles]['file']
options = {filename: uploaded_file.original_filename}
@updated_article_pairs, @outlisted_articles, @new_articles = [], [], []
FoodsoftFile::parse uploaded_file.tempfile, options do |status, new_attrs, line|
article = @supplier.articles.where(order_number: new_attrs[:order_number]).first
new_attrs[:article_category] = ArticleCategory.find_match(new_attrs[:article_category])
new_attrs[:tax] ||= FoodsoftConfig[:tax_default]
new_article = @supplier.articles.build(new_attrs)
if status.nil? && article.nil?
@new_articles << new_article
elsif status.nil? && article.present?
unequal_attributes = article.unequal_attributes(new_article)
article.attributes = unequal_attributes
@updated_article_pairs << [article, unequal_attributes]
elsif status == :outlisted && article.present?
@outlisted_articles << article
# stop when there is a parsing error
elsif status.is_a? String
raise I18n.t('articles.controller.error_parse', :msg => status, :line => line.to_s)
2009-01-06 11:49:19 +01:00
end
2012-10-28 18:03:50 +01:00
2014-02-24 14:07:03 +01:00
end
@ignored_article_count = 0
render :sync
rescue => error
redirect_to upload_supplier_articles_path(@supplier), :alert => I18n.t('errors.general_msg', :msg => error.message)
2009-01-06 11:49:19 +01:00
end
2009-01-06 11:49:19 +01:00
# 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 => I18n.t('articles.controller.sync.shared_alert', :supplier => @supplier.name)
2009-01-06 11:49:19 +01:00
end
# sync articles against external database
@updated_article_pairs, @outlisted_articles, @new_articles = @supplier.sync_all
if @updated_article_pairs.empty? && @outlisted_articles.empty? && @new_articles.empty?
redirect_to supplier_articles_path(@supplier), :notice => I18n.t('articles.controller.sync.notice')
2009-01-06 11:49:19 +01:00
end
@ignored_article_count = @supplier.articles.where(order_number: [nil, '']).count
2009-01-06 11:49:19 +01:00
end
# Updates, deletes articles when upload or sync form is submitted
def update_synchronized
begin
Article.transaction do
# delete articles
if params[:outlisted_articles]
Article.find(params[:outlisted_articles].keys).each(&:mark_as_deleted)
end
# Update articles
if params[:articles]
params[:articles].each do |id, attrs|
Article.find(id).update_attributes! attrs
end
end
# Add new articles
if params[:new_articles]
params[:new_articles].each do |attrs|
article = Article.new attrs
article.supplier = @supplier
article.availability = true if @supplier.shared_sync_method == 'all_available'
article.availability = false if @supplier.shared_sync_method == 'all_unavailable'
article.save!
end
end
end
# Successfully done.
redirect_to supplier_articles_path(@supplier), notice: I18n.t('articles.controller.update_sync.notice')
rescue ActiveRecord::RecordInvalid => invalid
# An error has occurred, transaction has been rolled back.
redirect_to supplier_articles_path(@supplier),
alert: I18n.t('articles.controller.error_update', :article => invalid.record.name, :msg => invalid.record.errors.full_messages)
rescue => error
redirect_to supplier_articles_path(@supplier),
alert: I18n.t('errors.general_msg', :msg => error.message)
end
end
# renders a view to import articles in local database
#
def shared
# build array of keywords, required for ransack _all suffix
params[:q][:name_cont_all] = params[:q][:name_cont_all].split(' ') if params[:q]
# Build search with meta search plugin
@search = @supplier.shared_supplier.shared_articles.search(params[:q])
@articles = @search.result.page(params[:page]).per(10)
render :layout => false
end
# fills a form whith values of the selected shared_article
# when the direct parameter is set and the article is valid, it is imported directly
def import
@article = SharedArticle.find(params[:shared_article_id]).build_new_article(@supplier)
@article.article_category_id = params[:article_category_id] unless params[:article_category_id].blank?
if params[:direct] && !params[:article_category_id].blank? && @article.valid? && @article.save
render :action => 'create', :layout => false
else
render :action => 'new', :layout => false
end
end
end