Improve stock_article_selections

This commit is contained in:
Julius 2013-01-28 20:46:35 +01:00
parent 524819b86f
commit 396a47b6af
10 changed files with 104 additions and 30 deletions

View file

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

View file

@ -2,7 +2,7 @@
class StockitSelectionsController < ApplicationController class StockitSelectionsController < ApplicationController
def index def index
@stock_article_selections = StockArticleSelection.all @stock_article_selections = StockArticleSelection.find(:all, :order => 'created_at DESC')
end end
def show def show
@ -14,43 +14,48 @@ class StockitSelectionsController < ApplicationController
@stock_article_selection.created_by = current_user @stock_article_selection.created_by = current_user
if @stock_article_selection.save if @stock_article_selection.save
redirect_to(@stock_article_selection, :notice => 'Löschvorschlag für gewählte Artikel wurde erstellt.') redirect_to(@stock_article_selection, :notice => 'Löschvorschlag für gewählte Artikel erstellt.')
else else
@stock_articles = StockArticle.includes(:supplier, :article_category). @stock_articles = StockArticle.elements_for_index
order('suppliers.name, article_categories.name, articles.name')
render 'stockit/index' render 'stockit/index'
end end
end end
def destroy # destroy (open or finished) selection without deleting articles def destroy # destroy selection without deleting articles
stock_article_selection = StockArticleSelection.find(params[:id]) stock_article_selection = StockArticleSelection.find(params[:id])
stock_article_selection.destroy stock_article_selection.destroy
redirect_to stock_article_selections_path, :notice => 'Löschvorschlag wurde verworfen.' redirect_to stock_article_selections_path, :notice => 'Löschvorschlag verworfen.'
end end
def articles # destroy articles, finish selection def articles # destroy articles
stock_article_selection = StockArticleSelection.find(params[:id]) stock_article_selection = StockArticleSelection.find(params[:id])
destroyed_articles_count = 0 destroyed_articles_count = 0
failed_articles_count = 0 failed_articles_count = 0
stock_article_selection.stock_articles.each do |article| stock_article_selection.stock_articles.each do |article|
begin begin
article.destroy # article.delete would save some effort, but validations are important article.destroy
destroyed_articles_count += 1 destroyed_articles_count += 1
rescue => error # recover if article.destroy fails and continue with next article rescue => error # recover if article.destroy fails and continue with next article
failed_articles_count += 1 failed_articles_count += 1
end end
end end
if destroyed_articles_count>0 # note that 1 successful article.destroy is enough to destroy selection if destroyed_articles_count > 0
stock_article_selection.destroy flash[:notice] = "#{destroyed_articles_count} gewählte Artikel gelöscht."
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 flash[:error] = "#{failed_articles_count} Artikel konnten nicht gelöscht werden." unless 0==failed_articles_count
else else
flash[:error] = "Löschvorgang fehlgeschlagen. Es wurden keine Artikel gelöscht." flash[:error] = 'Löschvorgang fehlgeschlagen. Keine Artikel gelöscht.'
end end
redirect_to stock_articles_path redirect_to stock_articles_path
end end
def finished # delete all finished selections
finished_selections = StockArticleSelection.all.select { |sel| sel.deletable_count + sel.nondeletable_count <= 0 }
finished_selections.each { |sel| sel.destroy }
redirect_to stock_article_selections_path, :notice => 'Alle erledigten Löschvorschläge entfernt.'
end
end end

View file

@ -1,2 +1,18 @@
# encoding: utf-8
module StockArticleSelectionsHelper module StockArticleSelectionsHelper
def article_deletion_classes(article)
className = "label label-success" # usual deletable case, maybe modified below
className = "label label-important" if article.quantity_available > 0
className = "label" if article.deleted?
className
end
def article_deletion_title(article)
myTitle = "Löschbar" # usual deletable case, maybe modified below
myTitle = "Nicht löschbar, da im Lager vorhanden" if article.quantity_available > 0
myTitle = "Bereits gelöscht" if article.deleted?
myTitle
end
end end

View file

@ -23,6 +23,11 @@ class StockArticle < Article
available.collect { |a| a.quantity * a.gross_price }.sum available.collect { |a| a.quantity * a.gross_price }.sum
end end
def self.elements_for_index
StockArticle.includes(:supplier, :article_category).
order('suppliers.name, article_categories.name, articles.name')
end
protected protected
def check_quantity def check_quantity

View file

@ -13,6 +13,18 @@ class StockArticleSelection < ActiveRecord::Base
all_articles = stock_article_ids all_articles = stock_article_ids
end end
def deletable_count
stock_articles.select { |a| a.quantity_available<=0 }.length
end
def nondeletable_count
stock_articles.select { |a| a.quantity_available>0 }.length
end
def deleted_count
stock_articles.only_deleted.count
end
protected protected
def include_stock_articles def include_stock_articles

View file

@ -2,7 +2,7 @@
- content_for :javascript do - content_for :javascript do
:javascript :javascript
$(function() { $(function() {
$('tr.unavailable,input.unavailable').hide(); $('tr.unavailable,input.unavailable,div.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,input.unavailable', tabindex: -1 %li= link_to "Nicht verfügbare Artikel zeigen/verstecken", "#", 'data-toggle-this' => 'tr.unavailable,input.unavailable,div.unavailable', tabindex: -1
.btn-group .btn-group
= link_to_if @current_user.role_orders?, "Lagerbestellung online stellen", new_order_path(supplier_id: 0), = link_to_if @current_user.role_orders?, "Lagerbestellung online stellen", new_order_path(supplier_id: 0),
@ -66,7 +66,8 @@
= link_to "Bearbeiten", edit_stock_article_path(article), class: 'btn btn-mini' = link_to "Bearbeiten", edit_stock_article_path(article), class: 'btn btn-mini'
= link_to "Löschen", article, :method => :delete, :confirm => "Bist Du sicher?", = link_to "Löschen", article, :method => :delete, :confirm => "Bist Du sicher?",
class: 'btn btn-mini btn-danger' class: 'btn btn-mini btn-danger'
%p= submit_tag "Artikel zum Löschen vormerken", { :class => 'unavailable btn' } .form-actions.unavailable
= 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

@ -2,15 +2,22 @@
%table.table.table-hover %table.table.table-hover
%tr %tr
%th Artikel %th Artikel
%th Zusammenfassung
%th Erstellt am %th Erstellt am
%th Erstellt von %th Erstellt von
%th Optionen %th Optionen
- stock_article_selections.each do |stock_article_selection| - stock_article_selections.each do |stock_article_selection|
%tr %tr
%td=h truncate stock_article_selection.stock_articles.map{ |article| article.name}.join(', ') %td
%td=h stock_article_selection.created_at - for article in stock_article_selection.stock_articles.with_deleted
%td=h link_to_user_message_if_valid stock_article_selection.created_by %span{:class => article_deletion_classes(article), :title => article_deletion_title(article)}= article.name
%td
%span{:class => 'label label-success'}= "#{stock_article_selection.deletable_count} Löschbar"
%span{:class => 'label'}= "#{stock_article_selection.deleted_count} Gelöscht"
%span{:class => 'label label-important'}= "#{stock_article_selection.nondeletable_count} Nicht löschbar"
%td= format_date(stock_article_selection.created_at)
%td= link_to_user_message_if_valid stock_article_selection.created_by
%td %td
= link_to 'Anzeigen', stock_article_selection, class: 'btn btn-small' = 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, = link_to "Artikel löschen", articles_stock_article_selection_path(stock_article_selection), :method => :delete,

View file

@ -1,7 +1,25 @@
- title "Löschvorschläge für Lagerartikel" - title "Löschvorschläge für Lagerartikel"
- if @stock_article_selections.empty?
.well.well-small
.btn-toolbar
.btn-group
= link_to "Lager anzeigen", stock_articles_path, class: 'btn'
= link_to "Aufräumen", finished_stock_article_selections_path, :method => 'delete',
:confirm => 'Wirklich alle erledigten Löschvorschläge entfernen?', class: 'btn'
.well
%h2 Ausstehende Löschvorschläge
- open_selections = @stock_article_selections.select { |sel| sel.deletable_count + sel.nondeletable_count > 0 }
- if open_selections.length == 0
%p Es gibt keine ausstehenden Löschvorschläge. %p Es gibt keine ausstehenden Löschvorschläge.
%ul %ul
%li= link_to "Löschvorschlag erstellen", stock_articles_path %li= link_to "Löschvorschlag erstellen", stock_articles_path
- else - else
= render :partial => 'overview', :locals => {:stock_article_selections => @stock_article_selections} = render :partial => 'overview', :locals => {:stock_article_selections => open_selections}
%h2 Erledigte Löschvorschläge
- finished_selections = @stock_article_selections.select { |sel| sel.deletable_count + sel.nondeletable_count <= 0 }
- if finished_selections.length == 0
%p Es gibt keine erledigten Löschvorschläge.
- else
= render :partial => 'overview', :locals => {:stock_article_selections => finished_selections}

View file

@ -1,13 +1,20 @@
- title "Löschvorschlag für #{@stock_article_selection.stock_articles.count} Lagerartikel" - title "Löschvorschlag für #{@stock_article_selection.stock_articles.count} Lagerartikel"
.well.well-small
.btn-toolbar
.btn-group
= link_to "Lager anzeigen", stock_articles_path, class: 'btn'
= link_to 'Alle Löschvorschläge anzeigen', stock_article_selections_path, class: 'btn'
%dl %dl
%dt Löschvorschlag vom: %dt Löschvorschlag vom:
%dd=h @stock_article_selection.created_at %dd= format_time(@stock_article_selection.created_at)
%dt Erstellt durch: %dt Erstellt durch:
%dd=h link_to_user_message_if_valid(@stock_article_selection.created_by) %dd= link_to_user_message_if_valid(@stock_article_selection.created_by)
%dt Zu löschende Artikel %dt Zu löschende Artikel
- for article in @stock_article_selection.stock_articles - for article in @stock_article_selection.stock_articles.with_deleted
%dd=h article.name %dd
%span{:class => article_deletion_classes(article), :title => article_deletion_title(article)}= article.name
%p %p
@ -15,4 +22,4 @@
:confirm => 'Diesen Löschvorschlag wirklich ausführen und markierte Artikel löschen?', class: 'btn btn-danger' :confirm => 'Diesen Löschvorschlag wirklich ausführen und markierte Artikel löschen?', class: 'btn btn-danger'
= link_to 'Verwerfen', @stock_article_selection, :method => :delete, = link_to 'Verwerfen', @stock_article_selection, :method => :delete,
:confirm => 'Diesen Löschvorschlag wirklich verwerfen?', class: 'btn' :confirm => 'Diesen Löschvorschlag wirklich verwerfen?', class: 'btn'
= link_to 'Alle Löschvorschläge zeigen', stock_article_selections_path, class: 'btn' = link_to 'Alle Löschvorschläge anzeigen', stock_article_selections_path, class: 'btn'

View file

@ -95,6 +95,10 @@ Foodsoft::Application.routes.draw do
member do member do
delete 'articles' delete 'articles'
end end
collection do
delete 'finished'
end
end end