Add stock_article_selection (deleting multiple stock articles at once)

This commit is contained in:
Julius 2013-01-12 16:14:06 +01:00
parent ecb7ce7cd3
commit 099e2b9b06
13 changed files with 217 additions and 34 deletions

View file

@ -3,6 +3,7 @@ class StockitController < ApplicationController
def index def index
@stock_articles = StockArticle.includes(:supplier, :article_category). @stock_articles = StockArticle.includes(:supplier, :article_category).
order('suppliers.name, article_categories.name, articles.name') order('suppliers.name, article_categories.name, articles.name')
@stock_article_selection = StockArticleSelection.new
end end
def new def new

View file

@ -0,0 +1,56 @@
# encoding: utf-8
class StockitSelectionsController < ApplicationController
def index
@stock_article_selections = StockArticleSelection.all
end
def show
@stock_article_selection = StockArticleSelection.find(params[:id])
end
def create
@stock_article_selection = StockArticleSelection.new(params[:stock_article_selection])
@stock_article_selection.created_by = current_user
if @stock_article_selection.save
redirect_to(@stock_article_selection, :notice => 'Löschvorschlag für gewählte Artikel wurde erstellt.')
else
@stock_articles = StockArticle.includes(:supplier, :article_category).
order('suppliers.name, article_categories.name, articles.name')
render 'stockit/index'
end
end
def destroy # destroy (open or finished) selection without deleting articles
stock_article_selection = StockArticleSelection.find(params[:id])
stock_article_selection.destroy
redirect_to stock_article_selections_path, :notice => 'Löschvorschlag wurde verworfen.'
end
def articles # destroy articles, finish selection
stock_article_selection = StockArticleSelection.find(params[:id])
destroyed_articles_count = 0
failed_articles_count = 0
stock_article_selection.stock_articles.each do |article|
begin
article.destroy # article.delete would save some effort, but validations are important
destroyed_articles_count += 1
rescue => error # recover if article.destroy fails and continue with next article
failed_articles_count += 1
end
end
if destroyed_articles_count>0 # note that 1 successful article.destroy is enough to destroy selection
stock_article_selection.destroy
flash[:notice] = "#{destroyed_articles_count} gewählte Artikel sind nun gelöscht."
flash[:error] = "#{failed_articles_count} Artikel konnten nicht gelöscht werden." unless 0==failed_articles_count
else
flash[:error] = "Löschvorgang fehlgeschlagen. Es wurden keine Artikel gelöscht."
end
redirect_to stock_articles_path
end
end

View file

@ -142,7 +142,7 @@ module ApplicationHelper
# offers a link for writing message to user # offers a link for writing message to user
# checks for nil (useful for relations) # checks for nil (useful for relations)
def link_to_user_message_if_valid(user) def link_to_user_message_if_valid(user)
user.nil? ? '??' : ( link_to user.nick, user_message_path(user), :title => 'Nachricht schreiben' ) user.nil? ? '??' : ( link_to user.nick, new_message_path(:message => {:mail_to => user.id}), :title => 'Nachricht schreiben' )
end end
def bootstrap_flash def bootstrap_flash

View file

@ -0,0 +1,2 @@
module StockArticleSelectionsHelper
end

View file

@ -1,7 +1,18 @@
# encoding: utf-8
module StockitHelper module StockitHelper
def stock_article_classes(article) def stock_article_classes(article)
class_names = [] class_names = []
class_names << "unavailable" if article.quantity_available <= 0 class_names << "unavailable" if article.quantity_available <= 0
class_names.join(" ") class_names.join(" ")
end end
def stock_article_delete_checkbox(article)
if article.quantity_available <= 0
check_box_tag "stock_article_selection[stock_article_ids][]", article.id, false,
{ :id => "checkbox_#{article.id}", :title => 'Zum löschen markieren' }
else
check_box_tag 'checkall', '1', false,
{ :disabled => true, :title => 'Verfügbare Artikel können nicht gelöscht werden.', :class => 'unavailable' }
end
end
end end

View file

@ -0,0 +1,24 @@
# encoding: utf-8
class StockArticleSelection < ActiveRecord::Base
# Associations
has_and_belongs_to_many :stock_articles
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'
belongs_to :finished_by, :class_name => 'User', :foreign_key => 'finished_by_user_id'
# Validations
validate :include_stock_articles
def all_articles
all_articles = stock_article_ids
end
protected
def include_stock_articles
errors.add(:stock_articles, "Es muss mindestens ein Lagerartikel ausgewählt sein.") if stock_articles.empty?
end
private
end

View file

@ -2,7 +2,7 @@
- content_for :javascript do - content_for :javascript do
:javascript :javascript
$(function() { $(function() {
$('tr.unavailable').hide(); $('tr.unavailable,input.unavailable').hide();
}) })
.well.well-small .well.well-small
@ -12,7 +12,7 @@
Ansichtsoptionen Ansichtsoptionen
%span.caret %span.caret
%ul.dropdown-menu %ul.dropdown-menu
%li= link_to "Nicht verfügbare Artikel zeigen/verstecken", "#", 'data-toggle-this' => 'tr.unavailable', tabindex: -1 %li= link_to "Nicht verfügbare Artikel zeigen/verstecken", "#", 'data-toggle-this' => 'tr.unavailable,input.unavailable', tabindex: -1
.btn-group .btn-group
= link_to "Neuen Lagerartikel anlegen", new_stock_article_path, class: 'btn btn-primary' = link_to "Neuen Lagerartikel anlegen", new_stock_article_path, class: 'btn btn-primary'
@ -20,6 +20,7 @@
class: 'btn' class: 'btn'
= link_to "Inventur anlegen", new_stock_taking_path, class: 'btn' = link_to "Inventur anlegen", new_stock_taking_path, class: 'btn'
= link_to "Inventurübersicht", stock_takings_path, class: 'btn' = link_to "Inventurübersicht", stock_takings_path, class: 'btn'
= link_to 'Löschvorschläge', stock_article_selections_path, class: 'btn'
.btn-group .btn-group
= link_to '#', data: {toggle: 'dropdown'}, class: 'btn dropdown-toggle' do = link_to '#', data: {toggle: 'dropdown'}, class: 'btn dropdown-toggle' do
@ -29,36 +30,43 @@
- Supplier.all.each do |supplier| - Supplier.all.each do |supplier|
%li= link_to supplier.name, new_supplier_delivery_path(supplier), tabindex: -1 %li= link_to supplier.name, new_supplier_delivery_path(supplier), tabindex: -1
= form_for @stock_article_selection do |form|
%table.table.table-hover#articles - if @stock_article_selection.errors.has_key?(:stock_articles)
%thead .alert.alert-error
%tr = @stock_article_selection.errors.get(:stock_articles).join(" ")
%th Artikel %table.table.table-hover#articles
%th im Lager %thead
%th davon bestellt %tr
%th verfügbar %th= check_box_tag 'checkall', '1', false,
%th Einheit { 'data-check-all' => 'table#articles tr.unavailable', :title => 'Alle löschbaren Artikel', :class => 'unavailable' }
%th Preis %th Artikel
%th MwSt %th im Lager
%th Lieferantin %th davon bestellt
%th Kategorie %th verfügbar
%th %th Einheit
%tbody %th Preis
- for article in @stock_articles %th MwSt
%tr{:class => stock_article_classes(article)} %th Lieferantin
%td=h article.name %th Kategorie
%td= article.quantity %th
%td= article.quantity - article.quantity_available %tbody
%th= article.quantity_available - for article in @stock_articles
%td= article.unit %tr{:class => stock_article_classes(article)}
%td= article.price %td= stock_article_delete_checkbox(article)
%td= number_to_percentage article.tax %td=h article.name
%td= link_to article.supplier.name, article.supplier %td= article.quantity
%td= article.article_category.name %td= article.quantity - article.quantity_available
%td %th= article.quantity_available
= link_to "Bearbeiten", edit_stock_article_path(article), class: 'btn btn-mini' %td= article.unit
= link_to "Löschen", article, :method => :delete, :confirm => "Bist Du sicher?", %td= article.price
class: 'btn btn-mini btn-danger' %td= number_to_percentage article.tax
%td= link_to article.supplier.name, article.supplier
%td= article.article_category.name
%td
= link_to "Bearbeiten", edit_stock_article_path(article), class: 'btn btn-mini'
= link_to "Löschen", article, :method => :delete, :confirm => "Bist Du sicher?",
class: 'btn btn-mini btn-danger'
%p= submit_tag "Artikel zum Löschen vormerken", { :class => 'unavailable btn' }
%p %p
Aktueller Lagerwert: Aktueller Lagerwert:
= number_to_currency StockArticle.stock_value = number_to_currency StockArticle.stock_value

View file

@ -0,0 +1,19 @@
%table.table.table-hover
%tr
%th Artikel
%th Erstellt am
%th Erstellt von
%th Optionen
- stock_article_selections.each do |stock_article_selection|
%tr
%td=h truncate stock_article_selection.stock_articles.map{ |article| article.name}.join(', ')
%td=h stock_article_selection.created_at
%td=h link_to_user_message_if_valid stock_article_selection.created_by
%td
= link_to 'Anzeigen', stock_article_selection, class: 'btn btn-small'
= link_to "Artikel löschen", articles_stock_article_selection_path(stock_article_selection), :method => :delete,
:confirm => 'Diesen Löschvorschlag wirklich ausführen und markierte Artikel löschen?', class: 'btn btn-small btn-danger'
= link_to "Verwerfen", stock_article_selection, :method => :delete,
:confirm => 'Diesen Löschvorschlag wirklich verwerfen?', class: 'btn btn-small'

View file

@ -0,0 +1,7 @@
- title "Löschvorschläge für Lagerartikel"
- if @stock_article_selections.empty?
%p Es gibt keine ausstehenden Löschvorschläge.
%ul
%li= link_to "Löschvorschlag erstellen", stock_articles_path
- else
= render :partial => 'overview', :locals => {:stock_article_selections => @stock_article_selections}

View file

@ -0,0 +1,18 @@
- title "Löschvorschlag für #{@stock_article_selection.stock_articles.count} Lagerartikel"
%dl
%dt Löschvorschlag vom:
%dd=h @stock_article_selection.created_at
%dt Erstellt durch:
%dd=h link_to_user_message_if_valid(@stock_article_selection.created_by)
%dt Zu löschende Artikel
- for article in @stock_article_selection.stock_articles
%dd=h article.name
%p
= link_to 'Artikel löschen', articles_stock_article_selection_path(@stock_article_selection), :method => :delete,
:confirm => 'Diesen Löschvorschlag wirklich ausführen und markierte Artikel löschen?', class: 'btn btn-danger'
= link_to 'Verwerfen', @stock_article_selection, :method => :delete,
:confirm => 'Diesen Löschvorschlag wirklich verwerfen?', class: 'btn'
= link_to 'Alle Löschvorschläge zeigen', stock_article_selections_path, class: 'btn'

View file

@ -89,6 +89,14 @@ Foodsoft::Application.routes.draw do
post :add_stock_article post :add_stock_article
end end
end end
resources :stock_article_selections, :controller => 'stockit_selections',
:only => [ :create, :show, :index, :destroy ], :path => "/stock_articles/selections" do
member do
delete 'articles'
end
end
resources :stock_articles, :to => 'stockit' do resources :stock_articles, :to => 'stockit' do
collection do collection do

View file

@ -0,0 +1,18 @@
class CreateStockArticleSelections < ActiveRecord::Migration
def up
create_table :stock_article_selections do |t|
t.integer :created_by_user_id
t.timestamps
end
create_table :stock_article_selections_stock_articles, :id => false do |t|
t.integer :stock_article_id
t.integer :stock_article_selection_id
end
end
def down
drop_table :stock_article_selections
drop_table :stock_article_selections_stock_articles
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20121230142516) do ActiveRecord::Schema.define(:version => 20130112121840) do
create_table "article_categories", :force => true do |t| create_table "article_categories", :force => true do |t|
t.string "name", :default => "", :null => false t.string "name", :default => "", :null => false
@ -268,6 +268,17 @@ ActiveRecord::Schema.define(:version => 20121230142516) do
add_index "pages", ["permalink"], :name => "index_pages_on_permalink" add_index "pages", ["permalink"], :name => "index_pages_on_permalink"
add_index "pages", ["title"], :name => "index_pages_on_title" add_index "pages", ["title"], :name => "index_pages_on_title"
create_table "stock_article_selections", :force => true do |t|
t.integer "created_by_user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "stock_article_selections_stock_articles", :id => false, :force => true do |t|
t.integer "stock_article_id"
t.integer "stock_article_selection_id"
end
create_table "stock_changes", :force => true do |t| create_table "stock_changes", :force => true do |t|
t.integer "delivery_id" t.integer "delivery_id"
t.integer "order_id" t.integer "order_id"