From 099e2b9b06bca02b205d15dd00c1872620c5b39f Mon Sep 17 00:00:00 2001 From: Julius Date: Sat, 12 Jan 2013 16:14:06 +0100 Subject: [PATCH] Add stock_article_selection (deleting multiple stock articles at once) --- app/controllers/stockit_controller.rb | 1 + .../stockit_selections_controller.rb | 56 +++++++++++++++ app/helpers/application_helper.rb | 2 +- .../stock_article_selections_helper.rb | 2 + app/helpers/stockit_helper.rb | 11 +++ app/models/stock_article_selection.rb | 24 +++++++ app/views/stockit/index.html.haml | 72 ++++++++++--------- .../stockit_selections/_overview.html.haml | 19 +++++ app/views/stockit_selections/index.html.haml | 7 ++ app/views/stockit_selections/show.html.haml | 18 +++++ config/routes.rb | 8 +++ ...2121840_create_stock_article_selections.rb | 18 +++++ db/schema.rb | 13 +++- 13 files changed, 217 insertions(+), 34 deletions(-) create mode 100644 app/controllers/stockit_selections_controller.rb create mode 100644 app/helpers/stock_article_selections_helper.rb create mode 100644 app/models/stock_article_selection.rb create mode 100644 app/views/stockit_selections/_overview.html.haml create mode 100644 app/views/stockit_selections/index.html.haml create mode 100644 app/views/stockit_selections/show.html.haml create mode 100644 db/migrate/20130112121840_create_stock_article_selections.rb diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index cdd9b633..2c130fce 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -3,6 +3,7 @@ class StockitController < ApplicationController def index @stock_articles = StockArticle.includes(:supplier, :article_category). order('suppliers.name, article_categories.name, articles.name') + @stock_article_selection = StockArticleSelection.new end def new diff --git a/app/controllers/stockit_selections_controller.rb b/app/controllers/stockit_selections_controller.rb new file mode 100644 index 00000000..ae498c40 --- /dev/null +++ b/app/controllers/stockit_selections_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 19269cdb..b5dfbaf5 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -142,7 +142,7 @@ module ApplicationHelper # offers a link for writing message to user # checks for nil (useful for relations) 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 def bootstrap_flash diff --git a/app/helpers/stock_article_selections_helper.rb b/app/helpers/stock_article_selections_helper.rb new file mode 100644 index 00000000..9d7090f5 --- /dev/null +++ b/app/helpers/stock_article_selections_helper.rb @@ -0,0 +1,2 @@ +module StockArticleSelectionsHelper +end diff --git a/app/helpers/stockit_helper.rb b/app/helpers/stockit_helper.rb index 2888603c..c2665a96 100644 --- a/app/helpers/stockit_helper.rb +++ b/app/helpers/stockit_helper.rb @@ -1,7 +1,18 @@ +# encoding: utf-8 module StockitHelper def stock_article_classes(article) class_names = [] class_names << "unavailable" if article.quantity_available <= 0 class_names.join(" ") 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 diff --git a/app/models/stock_article_selection.rb b/app/models/stock_article_selection.rb new file mode 100644 index 00000000..20aafd7a --- /dev/null +++ b/app/models/stock_article_selection.rb @@ -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 diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index 40e3410f..f19e767d 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -2,7 +2,7 @@ - content_for :javascript do :javascript $(function() { - $('tr.unavailable').hide(); + $('tr.unavailable,input.unavailable').hide(); }) .well.well-small @@ -12,7 +12,7 @@ Ansichtsoptionen %span.caret %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 = link_to "Neuen Lagerartikel anlegen", new_stock_article_path, class: 'btn btn-primary' @@ -20,6 +20,7 @@ class: 'btn' = link_to "Inventur anlegen", new_stock_taking_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 = link_to '#', data: {toggle: 'dropdown'}, class: 'btn dropdown-toggle' do @@ -29,36 +30,43 @@ - Supplier.all.each do |supplier| %li= link_to supplier.name, new_supplier_delivery_path(supplier), tabindex: -1 - -%table.table.table-hover#articles - %thead - %tr - %th Artikel - %th im Lager - %th davon bestellt - %th verfügbar - %th Einheit - %th Preis - %th MwSt - %th Lieferantin - %th Kategorie - %th - %tbody - - for article in @stock_articles - %tr{:class => stock_article_classes(article)} - %td=h article.name - %td= article.quantity - %td= article.quantity - article.quantity_available - %th= article.quantity_available - %td= article.unit - %td= article.price - %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' += form_for @stock_article_selection do |form| + - if @stock_article_selection.errors.has_key?(:stock_articles) + .alert.alert-error + = @stock_article_selection.errors.get(:stock_articles).join(" ") + %table.table.table-hover#articles + %thead + %tr + %th= check_box_tag 'checkall', '1', false, + { 'data-check-all' => 'table#articles tr.unavailable', :title => 'Alle löschbaren Artikel', :class => 'unavailable' } + %th Artikel + %th im Lager + %th davon bestellt + %th verfügbar + %th Einheit + %th Preis + %th MwSt + %th Lieferantin + %th Kategorie + %th + %tbody + - for article in @stock_articles + %tr{:class => stock_article_classes(article)} + %td= stock_article_delete_checkbox(article) + %td=h article.name + %td= article.quantity + %td= article.quantity - article.quantity_available + %th= article.quantity_available + %td= article.unit + %td= article.price + %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 Aktueller Lagerwert: = number_to_currency StockArticle.stock_value diff --git a/app/views/stockit_selections/_overview.html.haml b/app/views/stockit_selections/_overview.html.haml new file mode 100644 index 00000000..15f9b7f9 --- /dev/null +++ b/app/views/stockit_selections/_overview.html.haml @@ -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' diff --git a/app/views/stockit_selections/index.html.haml b/app/views/stockit_selections/index.html.haml new file mode 100644 index 00000000..99cac93f --- /dev/null +++ b/app/views/stockit_selections/index.html.haml @@ -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} diff --git a/app/views/stockit_selections/show.html.haml b/app/views/stockit_selections/show.html.haml new file mode 100644 index 00000000..63ea5dfa --- /dev/null +++ b/app/views/stockit_selections/show.html.haml @@ -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' diff --git a/config/routes.rb b/config/routes.rb index aa8e0e29..9c27bbe7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -89,6 +89,14 @@ Foodsoft::Application.routes.draw do post :add_stock_article 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 collection do diff --git a/db/migrate/20130112121840_create_stock_article_selections.rb b/db/migrate/20130112121840_create_stock_article_selections.rb new file mode 100644 index 00000000..2ead9008 --- /dev/null +++ b/db/migrate/20130112121840_create_stock_article_selections.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 188ea83b..bec2b950 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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| 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", ["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| t.integer "delivery_id" t.integer "order_id"