Introduced acts_as_paranoid. Avoid deleting of suppliers and articles. (for consistency of order-results)

This commit is contained in:
Benjamin Meichsner 2009-01-20 19:37:15 +01:00
parent b820577382
commit fc45345e0d
30 changed files with 1238 additions and 151 deletions

View file

@ -2,17 +2,10 @@
# Management actions that require the "orders" role are handled by the OrdersController.
class OrderingController < ApplicationController
# Security
before_filter :ensureOrdergroupMember
verify :method => :post, :only => [:saveOrder], :redirect_to => { :action => :index }
before_filter :ensure_ordergroup_member
before_filter :ensure_open_order, :only => [:order, :saveOrder]
# Messages
ERROR_ALREADY_FINISHED = 'Diese Bestellung ist bereits abgeschlossen.'
ERROR_NO_ORDERGROUP = 'Sie gehören keiner Bestellgruppe an.'
ERROR_INSUFFICIENT_FUNDS = 'Der Bestellwert übersteigt das verfügbare Guthaben.'
MSG_ORDER_UPDATED = 'Die Bestellung wurde gespeichert.'
MSG_ORDER_CREATED = 'Die Bestellung wurde angelegt.'
ERROR_UPDATE_FAILED = 'Die Bestellung konnte nicht aktualisiert werden, da ein Fehler auftrat.'
ERROR_UPDATE_CONFLICT = 'In der Zwischenzeit hat jemand anderes auch bestellt, daher konnte die Bestellung nicht aktualisiert werden.'
verify :method => :post, :only => [:saveOrder], :redirect_to => { :action => :index }
# Index page.
def index
@ -29,72 +22,57 @@ class OrderingController < ApplicationController
# Edit a current order.
def order
@order = Order.find(params[:id], :include => [:supplier, :order_articles])
if !@order.current?
flash[:notice] = ERROR_ALREADY_FINISHED
redirect_to :action => 'index'
elsif !(@ordergroup = @current_user.find_ordergroup)
flash[:notice] = ERROR_NO_ORDERGROUP
redirect_to :controller => 'index'
else
@current_orders = Order.find_current
@other_orders = @current_orders.reject{|order| order == @order}
# Load order article data...
@articles_by_category = @order.get_articles
# save results of earlier orders in array
ordered_articles = Array.new
@group_order = @order.group_orders.find(:first, :conditions => "ordergroup_id = #{@ordergroup.id}", :include => :group_order_articles)
if @group_order
# Group has already ordered, so get the results...
for article in @group_order.group_order_articles
result = article.orderResult
ordered_articles[article.order_article_id] = { 'quantity' => article.quantity,
'tolerance' => article.tolerance,
'quantity_result' => result[:quantity],
'tolerance_result' => result[:tolerance]}
end
@version = @group_order.lock_version
@availableFunds = @ordergroup.getAvailableFunds(@group_order)
else
@version = 0
@availableFunds = @ordergroup.getAvailableFunds
@current_orders = Order.find_current
@other_orders = @current_orders.reject{|order| order == @order}
# Load order article data...
@articles_by_category = @order.get_articles
# save results of earlier orders in array
ordered_articles = Array.new
@group_order = @order.group_orders.find(:first, :conditions => "ordergroup_id = #{@ordergroup.id}", :include => :group_order_articles)
if @group_order
# Group has already ordered, so get the results...
for article in @group_order.group_order_articles
result = article.orderResult
ordered_articles[article.order_article_id] = { 'quantity' => article.quantity,
'tolerance' => article.tolerance,
'quantity_result' => result[:quantity],
'tolerance_result' => result[:tolerance]}
end
# load prices ....
@price = Array.new; @unit = Array.new;
@others_quantity = Array.new; @quantity = Array.new; @quantity_result = Array.new; @used_quantity = Array.new; @unused_quantity = Array.new
@others_tolerance = Array.new; @tolerance = Array.new; @tolerance_result = Array.new; @used_tolerance = Array.new; @unused_tolerance = Array.new
i = 0;
@articles_by_category.each do |category, order_articles|
for order_article in order_articles
# price/unit size
@price[i] = order_article.article.gross_price
@unit[i] = order_article.article.unit_quantity
# quantity
@quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id]['quantity'] : 0)
@others_quantity[i] = order_article.quantity - @quantity[i]
@used_quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id]['quantity_result'] : 0)
# tolerance
@tolerance[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id]['tolerance'] : 0)
@others_tolerance[i] = order_article.tolerance - @tolerance[i]
@used_tolerance[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id]['tolerance_result'] : 0)
i += 1
end
@version = @group_order.lock_version
@availableFunds = @ordergroup.getAvailableFunds(@group_order)
else
@version = 0
@availableFunds = @ordergroup.getAvailableFunds
end
# load prices ....
@price = Array.new; @unit = Array.new;
@others_quantity = Array.new; @quantity = Array.new; @quantity_result = Array.new; @used_quantity = Array.new; @unused_quantity = Array.new
@others_tolerance = Array.new; @tolerance = Array.new; @tolerance_result = Array.new; @used_tolerance = Array.new; @unused_tolerance = Array.new
i = 0;
@articles_by_category.each do |category, order_articles|
for order_article in order_articles
# price/unit size
@price[i] = order_article.article.gross_price
@unit[i] = order_article.article.unit_quantity
# quantity
@quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id]['quantity'] : 0)
@others_quantity[i] = order_article.quantity - @quantity[i]
@used_quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id]['quantity_result'] : 0)
# tolerance
@tolerance[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id]['tolerance'] : 0)
@others_tolerance[i] = order_article.tolerance - @tolerance[i]
@used_tolerance[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id]['tolerance_result'] : 0)
i += 1
end
end
end
# Update changes to a current order.
def saveOrder
order = Order.find(params[:id], :include => [:supplier, :order_articles])
if (!order.current?)
flash[:error] = ERROR_ALREADY_FINISHED
redirect_to :action => 'index'
elsif !(ordergroup = @current_user.find_ordergroup)
flash[:error] = ERROR_NO_ORDERGROUP
redirect_to :controller => 'index'
elsif (params[:total_balance].to_i < 0)
flash[:error] = ERROR_INSUFFICIENT_FUNDS
order = @order # Get the object through before_filter
if (params[:total_balance].to_i < 0)
flash[:error] = 'Der Bestellwert übersteigt das verfügbare Guthaben.'
redirect_to :action => 'order'
elsif (ordered = params[:ordered])
begin
@ -131,12 +109,12 @@ class OrderingController < ApplicationController
order.updateQuantities
order.save!
end
flash[:notice] = MSG_ORDER_UPDATED
flash[:notice] = 'Die Bestellung wurde gespeichert.'
rescue ActiveRecord::StaleObjectError
flash[:error] = ERROR_UPDATE_CONFLICT
flash[:error] = 'In der Zwischenzeit hat jemand anderes auch bestellt, daher konnte die Bestellung nicht aktualisiert werden.'
rescue => exception
logger.error('Failed to update order: ' + exception.message)
flash[:error] = ERROR_UPDATE_FAILED
flash[:error] = 'Die Bestellung konnte nicht aktualisiert werden, da ein Fehler auftrat.'
end
redirect_to :action => 'my_order_result', :id => order
end
@ -213,8 +191,19 @@ class OrderingController < ApplicationController
# Returns true if @current_user is member of an Ordergroup.
# Used as a :before_filter by OrderingController.
def ensureOrdergroupMember
!@current_user.find_ordergroup.nil?
end
def ensure_ordergroup_member
unless @current_user.find_ordergroup
flash[:notice] = 'Sie gehören keiner Bestellgruppe an.'
redirect_to :controller => root_path
end
end
def ensure_open_order
@order = Order.find(params[:id], :include => [:supplier, :order_articles])
unless @order.current?
flash[:notice] = 'Diese Bestellung ist bereits abgeschlossen.'
redirect_to :action => 'index'
end
end
end

View file

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090115232435
# Schema version: 20090119155930
#
# Table name: articles
#
@ -22,9 +22,12 @@
# created_at :datetime
# updated_at :datetime
# quantity :decimal(6, 2) default(0.0)
# deleted_at :datetime
#
class Article < ActiveRecord::Base
acts_as_paranoid # Avoid deleting the article for consistency of order-results
belongs_to :supplier
belongs_to :article_category

View file

@ -32,10 +32,6 @@ class Group < ActiveRecord::Base
validates_length_of :name, :in => 1..25
validates_uniqueness_of :name
# messages
ERR_LAST_ADMIN_GROUP_UPDATE = "Der letzten Gruppe mit Admin-Rechten darf die Admin-Rolle nicht entzogen werden"
ERR_LAST_ADMIN_GROUP_DELETE = "Die letzte Gruppe mit Admin-Rechten darf nicht gelöscht werden"
# Returns true if the given user if is an member of this group.
def member?(user)
memberships.find_by_user_id(user.id)
@ -54,7 +50,9 @@ class Group < ActiveRecord::Base
# Check before destroy a group, if this is the last group with admin role
def before_destroy
raise ERR_LAST_ADMIN_GROUP_DELETE if self.role_admin == true && Group.find_all_by_role_admin(true).size == 1
if self.role_admin == true && Group.find_all_by_role_admin(true).size == 1
raise "Die letzte Gruppe mit Admin-Rechten darf nicht gelöscht werden"
end
end
# get all groups, which are NOT Ordergroups
@ -72,7 +70,9 @@ class Group < ActiveRecord::Base
# add validation check on update
def validate_on_update
# error if this is the last group with admin role and role_admin should set to false
errors.add(:role_admin, ERR_LAST_ADMIN_GROUP_UPDATE) if self.role_admin == false && Group.find_all_by_role_admin(true).size == 1 && self == Group.find(:first, :conditions => "role_admin = 1")
if self.role_admin == false && Group.find_all_by_role_admin(true).size == 1 && self == Group.find(:first, :conditions => "role_admin = 1")
errors.add(:role_admin, "Der letzten Gruppe mit Admin-Rechten darf die Admin-Rolle nicht entzogen werden")
end
end
end

View file

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090102171850
# Schema version: 20090119155930
#
# Table name: suppliers
#
@ -18,9 +18,12 @@
# note :string(255)
# shared_supplier_id :integer(4)
# min_order_quantity :string(255)
# deleted_at :datetime
#
class Supplier < ActiveRecord::Base
acts_as_paranoid # Avoid deleting the supplier for consistency of order-results
has_many :articles, :dependent => :destroy
has_many :orders
has_many :deliveries

View file

@ -150,7 +150,6 @@ class User < ActiveRecord::Base
# Returns the user's Ordergroup or nil if none found.
def find_ordergroup
ordergroups.first
#groups.find(:first, :conditions => "type = 'Ordergroup'")
end
# Find all tasks, for which the current user should be responsible