2009-01-18 17:42:51 +01:00
|
|
|
class StockitController < ApplicationController
|
|
|
|
def index
|
2021-03-01 15:27:26 +01:00
|
|
|
@stock_articles = StockArticle.undeleted.includes(:supplier, :article_category)
|
|
|
|
.order('suppliers.name, article_categories.name, articles.name')
|
2009-02-11 18:09:04 +01:00
|
|
|
end
|
2016-05-20 21:30:56 +02:00
|
|
|
|
2013-12-07 11:20:03 +01:00
|
|
|
def index_on_stock_article_create # See publish/subscribe design pattern in /doc.
|
|
|
|
@stock_article = StockArticle.find(params[:id])
|
2016-05-20 21:30:56 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2013-12-07 11:20:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def index_on_stock_article_update # See publish/subscribe design pattern in /doc.
|
|
|
|
@stock_article = StockArticle.find(params[:id])
|
2016-05-20 21:30:56 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@stock_article = StockArticle.find(params[:id])
|
|
|
|
@stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC')
|
2013-12-07 11:20:03 +01:00
|
|
|
end
|
2009-02-11 18:09:04 +01:00
|
|
|
|
2013-12-07 11:20:03 +01:00
|
|
|
# three possibilites to fill a new_stock_article form
|
|
|
|
# (1) start from blank or use params
|
2009-02-11 18:09:04 +01:00
|
|
|
def new
|
2013-12-07 11:20:03 +01:00
|
|
|
@stock_article = StockArticle.new(params[:stock_article])
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2013-12-07 11:20:03 +01:00
|
|
|
end
|
2016-05-20 21:30:56 +02:00
|
|
|
|
2013-12-07 11:20:03 +01:00
|
|
|
# (2) StockArticle as template
|
|
|
|
def copy
|
|
|
|
@stock_article = StockArticle.find(params[:stock_article_id]).dup
|
2016-05-20 21:30:56 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2013-12-07 11:20:03 +01:00
|
|
|
end
|
2016-05-20 21:30:56 +02:00
|
|
|
|
2013-12-07 11:20:03 +01:00
|
|
|
# (3) non-stock Article as template
|
|
|
|
def derive
|
|
|
|
@stock_article = Article.find(params[:old_article_id]).becomes(StockArticle).dup
|
2016-05-20 21:30:56 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@stock_article = StockArticle.find(params[:id])
|
|
|
|
|
|
|
|
render layout: false
|
2009-02-11 18:09:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2021-03-01 15:27:26 +01:00
|
|
|
@stock_article = StockArticle.new({ quantity: 0 }.merge(params[:stock_article]))
|
2016-05-20 21:30:56 +02:00
|
|
|
@stock_article.save!
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2016-05-20 21:30:56 +02:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: 'new', layout: false
|
2009-01-18 17:42:51 +01:00
|
|
|
end
|
|
|
|
|
2009-02-11 18:09:04 +01:00
|
|
|
def update
|
|
|
|
@stock_article = StockArticle.find(params[:id])
|
2022-10-13 18:25:52 +02:00
|
|
|
@stock_article.update!(params[:stock_article])
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2016-05-20 21:30:56 +02:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: 'edit', layout: false
|
2013-10-03 22:24:21 +02:00
|
|
|
end
|
|
|
|
|
2013-12-07 11:20:03 +01:00
|
|
|
def show_on_stock_article_update # See publish/subscribe design pattern in /doc.
|
|
|
|
@stock_article = StockArticle.find(params[:id])
|
2016-05-20 21:30:56 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
2013-12-07 11:20:03 +01:00
|
|
|
end
|
|
|
|
|
2009-02-11 18:09:04 +01:00
|
|
|
def destroy
|
2013-12-07 11:20:03 +01:00
|
|
|
@stock_article = StockArticle.find(params[:id])
|
|
|
|
@stock_article.mark_as_deleted
|
2023-05-12 13:01:12 +02:00
|
|
|
render layout: false
|
|
|
|
rescue StandardError => e
|
|
|
|
render partial: 'destroy_fail', layout: false,
|
|
|
|
locals: { fail_msg: I18n.t('errors.general_msg', msg: e.message) }
|
2009-02-11 18:09:04 +01:00
|
|
|
end
|
|
|
|
|
2021-03-01 15:27:26 +01:00
|
|
|
# TODO: Fix this!!
|
2011-05-27 14:09:01 +02:00
|
|
|
def articles_search
|
2012-09-30 21:15:55 +02:00
|
|
|
@articles = Article.not_in_stock.limit(8).where('name LIKE ?', "%#{params[:term]}%")
|
2023-05-12 13:01:12 +02:00
|
|
|
render json: @articles.map(&:name)
|
2009-02-11 18:09:04 +01:00
|
|
|
end
|
2009-01-18 17:42:51 +01:00
|
|
|
end
|