Fixed bug in sorting articles in balancing view.

This commit is contained in:
Benjamin Meichsner 2013-03-12 18:54:51 +01:00
parent c4376f35bf
commit 0d3564492b
2 changed files with 25 additions and 17 deletions

View file

@ -10,25 +10,22 @@ class Finance::BalancingController < Finance::BaseController
flash.now.alert = "Achtung, Bestellung wurde schon abgerechnet" if @order.closed?
@comments = @order.comments
if params['sort']
sort = case params['sort']
when "name" then "articles.name"
when "order_number" then "articles.order_number"
when "name_reverse" then "articles.name DESC"
when "order_number_reverse" then "articles.order_number DESC"
end
else
sort = "id"
end
@articles = @order.order_articles.ordered.includes(:order, :article_price,
group_order_articles: {group_order: :ordergroup}).order(sort)
group_order_articles: {group_order: :ordergroup})
if params[:sort] == "order_number"
@articles = @articles.to_a.sort { |a,b| a.article.order_number.gsub(/[^[:digit:]]/, "").to_i <=> b.article.order_number.gsub(/[^[:digit:]]/, "").to_i }
elsif params[:sort] == "order_number_reverse"
@articles = @articles.to_a.sort { |a,b| b.article.order_number.gsub(/[^[:digit:]]/, "").to_i <=> a.article.order_number.gsub(/[^[:digit:]]/, "").to_i }
end
sort_param = params['sort'] || 'name'
@articles = case sort_param
when 'name' then
OrderArticle.sort_by_name(@articles)
when 'name_reverse' then
OrderArticle.sort_by_name(@articles).reverse
when 'order_number' then
OrderArticle.sort_by_order_number(@articles)
when 'order_number_reverse' then
OrderArticle.sort_by_order_number(@articles).reverse
else
@articles
end
render layout: false if request.xhr?
end

View file

@ -17,6 +17,17 @@ class OrderArticle < ActiveRecord::Base
before_create :init_from_balancing
after_destroy :update_ordergroup_prices
def self.sort_by_name(order_articles)
order_articles.sort { |a,b| a.article.name <=> b.article.name }
end
def self.sort_by_order_number(order_articles)
order_articles.sort do |a,b|
a.article.order_number.to_s.gsub(/[^[:digit:]]/, "").to_i <=>
b.article.order_number.to_s.gsub(/[^[:digit:]]/, "").to_i
end
end
# This method returns either the ArticlePrice or the Article
# The first will be set, when the the order is finished
def price