2012-04-16 08:48:01 +02:00
|
|
|
# encoding: utf-8
|
2009-01-06 11:49:19 +01:00
|
|
|
class ArticlesController < ApplicationController
|
2009-01-16 16:19:26 +01:00
|
|
|
before_filter :authenticate_article_meta, :find_supplier
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
def index
|
2009-01-16 16:19:26 +01:00
|
|
|
if params['sort']
|
2012-11-10 16:44:05 +01:00
|
|
|
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"
|
2012-11-10 16:44:05 +01:00
|
|
|
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"
|
2012-11-10 16:44:05 +01:00
|
|
|
when "note_reverse" then "articles.note DESC"
|
|
|
|
when "availability_reverse" then "articles.availability DESC"
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
2009-01-16 16:19:26 +01:00
|
|
|
sort = "article_categories.name, articles.name"
|
|
|
|
end
|
|
|
|
|
2013-03-16 17:53:24 +01:00
|
|
|
@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?
|
2009-01-16 16:19:26 +01:00
|
|
|
|
2012-10-19 01:12:47 +02:00
|
|
|
@articles = @articles.page(params[:page]).per(@per_page)
|
2009-01-16 16:19:26 +01:00
|
|
|
|
|
|
|
respond_to do |format|
|
2011-05-18 17:37:10 +02:00
|
|
|
format.html
|
|
|
|
format.js { render :layout => false }
|
2009-01-16 16:19:26 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2009-01-16 16:19:26 +01:00
|
|
|
def new
|
2013-07-06 23:11:50 +02:00
|
|
|
@article = @supplier.articles.build(:tax => FoodsoftConfig[:tax_default])
|
2011-05-18 17:37:10 +02:00
|
|
|
render :layout => false
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2009-01-16 16:19:26 +01:00
|
|
|
def create
|
2009-01-06 11:49:19 +01:00
|
|
|
@article = Article.new(params[:article])
|
|
|
|
if @article.valid? and @article.save
|
2011-05-18 17:37:10 +02:00
|
|
|
render :layout => false
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
2011-05-18 17:37:10 +02:00
|
|
|
render :action => 'new', :layout => false
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2009-01-16 16:19:26 +01:00
|
|
|
def edit
|
2009-01-06 11:49:19 +01:00
|
|
|
@article = Article.find(params[:id])
|
2011-05-18 17:37:10 +02:00
|
|
|
render :action => 'new', :layout => false
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Updates one Article and highlights the line if succeded
|
2009-01-16 16:19:26 +01:00
|
|
|
def update
|
|
|
|
@article = Article.find(params[:id])
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
if @article.update_attributes(params[:article])
|
2011-05-18 17:37:10 +02:00
|
|
|
render :layout => false
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
2011-05-18 17:37:10 +02:00
|
|
|
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
|
2009-01-16 16:19:26 +01:00
|
|
|
def destroy
|
2009-01-06 11:49:19 +01:00
|
|
|
@article = Article.find(params[:id])
|
2013-03-16 17:53:24 +01:00
|
|
|
@article.mark_as_deleted unless @order = @article.in_open_order # If article is in an active Order, the Order will be returned
|
2011-05-18 17:37:10 +02:00
|
|
|
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
|
2013-03-16 17:53:24 +01:00
|
|
|
@articles = @supplier.articles.undeleted
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-16 16:19:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Updates all article of specific supplier
|
|
|
|
def update_all
|
2013-04-08 01:00:49 +02:00
|
|
|
invalid_articles = false
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
begin
|
|
|
|
Article.transaction do
|
2009-01-16 16:19:26 +01:00
|
|
|
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
|
|
|
|
2013-04-08 01:00:49 +02:00
|
|
|
raise ActiveRecord::Rollback if invalid_articles # Rollback all changes
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-16 16:19:26 +01:00
|
|
|
end
|
2013-04-08 01:00:49 +02:00
|
|
|
end
|
2009-01-16 16:19:26 +01:00
|
|
|
|
2013-04-08 01:00:49 +02:00
|
|
|
if invalid_articles
|
2009-01-06 11:49:19 +01:00
|
|
|
# An error has occurred, transaction has been rolled back.
|
2013-04-12 16:14:54 +02:00
|
|
|
flash.now.alert = I18n.t('articles.controller.error_invalid')
|
2013-04-08 01:00:49 +02:00
|
|
|
render :edit_all
|
|
|
|
else
|
|
|
|
# Successfully done.
|
2013-04-12 16:14:54 +02:00
|
|
|
redirect_to supplier_articles_path(@supplier), notice: I18n.t('articles.controller.update_all.notice')
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# makes different actions on selected articles
|
2009-01-16 16:19:26 +01:00
|
|
|
def update_selected
|
2013-04-12 16:14:54 +02:00
|
|
|
raise I18n.t('articles.controller.error_nosel') if params[:selected_articles].nil?
|
2009-01-16 16:19:26 +01:00
|
|
|
articles = Article.find(params[:selected_articles])
|
2013-03-16 17:53:24 +01:00
|
|
|
Article.transaction do
|
|
|
|
case params[:selected_action]
|
|
|
|
when 'destroy'
|
|
|
|
articles.each(&:mark_as_deleted)
|
2013-04-12 16:14:54 +02:00
|
|
|
flash[:notice] = I18n.t('articles.controller.update_sel.notice_destroy')
|
2013-03-16 17:53:24 +01:00
|
|
|
when 'setNotAvailable'
|
|
|
|
articles.each {|a| a.update_attribute(:availability, false) }
|
2013-04-12 16:14:54 +02:00
|
|
|
flash[:notice] = I18n.t('articles.controller.update_sel.notice_unavail')
|
2013-03-16 17:53:24 +01:00
|
|
|
when 'setAvailable'
|
|
|
|
articles.each {|a| a.update_attribute(:availability, true) }
|
2013-04-12 16:14:54 +02:00
|
|
|
flash[:notice] = I18n.t('articles.controller.update_sel.notice_avail')
|
2013-03-16 17:53:24 +01:00
|
|
|
else
|
2013-04-12 16:14:54 +02:00
|
|
|
flash[:alert] = I18n.t('articles.controller.update_sel.notice_noaction')
|
2013-03-16 17:53:24 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-16 16:19:26 +01:00
|
|
|
# action succeded
|
2011-05-19 19:49:37 +02:00
|
|
|
redirect_to supplier_articles_url(@supplier, :per_page => params[:per_page])
|
2013-03-16 17:53:24 +01:00
|
|
|
|
2011-05-19 19:49:37 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
# lets start with parsing articles from uploaded file, yeah
|
|
|
|
# Renders the upload form
|
2009-01-16 16:19:26 +01:00
|
|
|
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: ""
|
2009-01-16 16:19:26 +01:00
|
|
|
def parse_upload
|
2009-01-06 11:49:19 +01:00
|
|
|
begin
|
|
|
|
@articles = Array.new
|
|
|
|
articles, outlisted_articles = FoodsoftFile::parse(params[:articles]["file"])
|
2013-07-06 17:49:10 +02:00
|
|
|
no_category = ArticleCategory.new
|
2009-01-06 11:49:19 +01:00
|
|
|
articles.each do |row|
|
2013-07-06 17:49:10 +02:00
|
|
|
# fallback to Others category
|
|
|
|
category = (ArticleCategory.find_by_name(row[:category]) or no_category)
|
2009-01-06 11:49:19 +01:00
|
|
|
# creates a new article and price
|
|
|
|
article = Article.new( :name => row[:name],
|
|
|
|
:note => row[:note],
|
|
|
|
:manufacturer => row[:manufacturer],
|
|
|
|
:origin => row[:origin],
|
|
|
|
:unit => row[:unit],
|
2013-07-06 17:49:10 +02:00
|
|
|
:article_category => category,
|
2009-01-29 01:57:51 +01:00
|
|
|
:price => row[:price],
|
2009-01-06 11:49:19 +01:00
|
|
|
:unit_quantity => row[:unit_quantity],
|
|
|
|
:order_number => row[:number],
|
|
|
|
:deposit => row[:deposit],
|
2013-07-06 23:11:50 +02:00
|
|
|
:tax => (row[:tax] or FoodsoftConfig[:tax_default]))
|
2009-01-06 11:49:19 +01:00
|
|
|
# stop parsing, when an article isn't valid
|
|
|
|
unless article.valid?
|
2013-04-12 16:14:54 +02:00
|
|
|
raise I18n.t('articles.controller.error_parse', :msg => article.errors.full_messages.join(", "), :line => (articles.index(row) + 2).to_s)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
@articles << article
|
|
|
|
end
|
2013-04-12 16:14:54 +02:00
|
|
|
flash.now[:notice] = I18n.t('articles.controller.parse_upload.notice', :count => @articles.size)
|
2011-05-19 19:49:37 +02:00
|
|
|
rescue => error
|
2013-03-22 00:50:30 +01:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
# creates articles from form
|
2009-01-16 16:19:26 +01:00
|
|
|
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
|
|
|
|
2013-04-12 16:14:54 +02:00
|
|
|
raise I18n.t('articles.controller.error_invalid') if invalid_articles
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
# Successfully done.
|
2013-04-12 16:14:54 +02:00
|
|
|
redirect_to supplier_articles_path(@supplier), notice: I18n.t('articles.controller.create_from_upload.notice', :count => @articles.size)
|
2012-10-28 18:03:50 +01:00
|
|
|
|
2009-01-16 16:19:26 +01:00
|
|
|
rescue => error
|
2009-01-06 11:49:19 +01:00
|
|
|
# An error has occurred, transaction has been rolled back.
|
2013-03-22 00:50:30 +01:00
|
|
|
flash.now[:error] = I18n.t('errors.general_msg', :msg => error.message)
|
2012-10-28 18:03:50 +01:00
|
|
|
render :parse_upload
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# renders a view to import articles in local database
|
|
|
|
#
|
2009-01-16 16:19:26 +01:00
|
|
|
def shared
|
2014-02-20 15:04:53 +01:00
|
|
|
# build array of keywords, required for ransack _all suffix
|
|
|
|
params[:q][:name_cont_all] = params[:q][:name_cont_all].split(' ') if params[:q]
|
2011-05-19 22:22:05 +02:00
|
|
|
# Build search with meta search plugin
|
2014-02-20 15:04:53 +01:00
|
|
|
@search = @supplier.shared_supplier.shared_articles.search(params[:q])
|
|
|
|
@articles = @search.result.page(params[:page]).per(10)
|
2011-05-19 22:22:05 +02:00
|
|
|
render :layout => false
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# fills a form whith values of the selected shared_article
|
2014-02-24 14:07:03 +01:00
|
|
|
# when the direct parameter is set and the article is valid, it is imported directly
|
2009-01-16 16:19:26 +01:00
|
|
|
def import
|
2013-06-04 11:28:25 +02:00
|
|
|
@article = SharedArticle.find(params[:shared_article_id]).build_new_article(@supplier)
|
2014-02-24 14:07:03 +01:00
|
|
|
@article.article_category_id = params[:article_category_id] unless params[:article_category_id].blank?
|
|
|
|
if params[:direct] and not params[:article_category_id].blank? and @article.valid? and @article.save
|
|
|
|
render :action => 'create', :layout => false
|
|
|
|
else
|
|
|
|
render :action => 'new', :layout => false
|
|
|
|
end
|
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
|
2009-01-16 16:19:26 +01:00
|
|
|
def sync
|
2009-01-06 11:49:19 +01:00
|
|
|
# check if there is an shared_supplier
|
|
|
|
unless @supplier.shared_supplier
|
2013-04-12 16:14:54 +02:00
|
|
|
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_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?
|
2013-04-12 16:14:54 +02:00
|
|
|
redirect_to supplier_articles_path(@supplier), :notice => I18n.t('articles.controller.sync.notice')
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2014-01-24 22:20:29 +01:00
|
|
|
@ignored_article_count = @supplier.articles.where(order_number: [nil, '']).count
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2013-04-08 01:00:49 +02:00
|
|
|
|
|
|
|
# Updates, deletes articles when 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
|
|
|
|
params[:articles].each do |id, attrs|
|
|
|
|
Article.find(id).update_attributes! attrs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Successfully done.
|
2013-04-12 16:14:54 +02:00
|
|
|
redirect_to supplier_articles_path(@supplier), notice: I18n.t('articles.controller.update_sync.notice')
|
2013-04-08 01:00:49 +02:00
|
|
|
|
|
|
|
rescue ActiveRecord::RecordInvalid => invalid
|
|
|
|
# An error has occurred, transaction has been rolled back.
|
|
|
|
redirect_to supplier_articles_path(@supplier),
|
2013-04-12 16:14:54 +02:00
|
|
|
alert: I18n.t('articles.controller.error_update', :article => invalid.record.name, :msg => invalid.record.errors.full_messages)
|
2013-04-08 01:00:49 +02:00
|
|
|
|
|
|
|
rescue => error
|
|
|
|
redirect_to supplier_articles_path(@supplier),
|
2013-04-12 15:45:24 +02:00
|
|
|
alert: I18n.t('errors.general_msg', :msg => error.message)
|
2013-04-08 01:00:49 +02:00
|
|
|
end
|
|
|
|
end
|
2013-01-30 21:21:49 +01:00
|
|
|
end
|