2012-04-16 08:48:01 +02:00
|
|
|
# encoding: utf-8
|
2009-01-10 22:22:16 +01:00
|
|
|
class Finance::BalancingController < ApplicationController
|
|
|
|
before_filter :authenticate_finance
|
2012-05-12 10:55:20 +02:00
|
|
|
|
2009-01-10 22:22:16 +01:00
|
|
|
def index
|
2012-06-21 17:19:00 +02:00
|
|
|
@financial_transactions = FinancialTransaction.order(:created_on.desc).limit(8)
|
2009-01-30 22:27:55 +01:00
|
|
|
@orders = Order.finished_not_closed
|
2009-01-19 16:40:06 +01:00
|
|
|
@unpaid_invoices = Invoice.unpaid
|
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
2009-01-10 22:22:16 +01:00
|
|
|
@orders = Order.finished.paginate :page => params[:page], :per_page => 10, :order => 'ends DESC'
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@order = Order.find(params[:id])
|
|
|
|
@comments = @order.comments
|
2009-04-01 00:46:01 +02:00
|
|
|
|
|
|
|
if params['sort']
|
2009-05-04 17:56:52 +02:00
|
|
|
sort = case params['sort']
|
2009-04-01 00:46:01 +02:00
|
|
|
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
|
2009-05-04 17:56:52 +02:00
|
|
|
sort = "id"
|
2009-04-01 00:46:01 +02:00
|
|
|
end
|
|
|
|
|
2012-05-12 11:18:46 +02:00
|
|
|
@articles = @order.order_articles.ordered.includes(:article).order(sort)
|
2009-05-04 17:56:52 +02:00
|
|
|
|
2009-04-03 17:50:17 +02:00
|
|
|
if params[:sort] == "order_number"
|
2012-05-12 11:18:46 +02:00
|
|
|
@articles = @articles.to_a.sort { |a,b| a.article.order_number.gsub(/[^[:digit:]]/, "").to_i <=> b.article.order_number.gsub(/[^[:digit:]]/, "").to_i }
|
2009-04-03 17:50:17 +02:00
|
|
|
elsif params[:sort] == "order_number_reverse"
|
2012-05-12 11:18:46 +02:00
|
|
|
@articles = @articles.to_a.sort { |a,b| b.article.order_number.gsub(/[^[:digit:]]/, "").to_i <=> a.article.order_number.gsub(/[^[:digit:]]/, "").to_i }
|
2009-04-03 17:50:17 +02:00
|
|
|
end
|
|
|
|
|
2009-04-03 17:35:00 +02:00
|
|
|
view = params[:view]
|
|
|
|
params[:view] = nil
|
|
|
|
|
2012-05-12 11:18:46 +02:00
|
|
|
case view.try(:to_sym)
|
2009-01-10 22:22:16 +01:00
|
|
|
when 'editResults'
|
2009-04-01 00:46:01 +02:00
|
|
|
render :partial => 'edit_results_by_articles' and return
|
2012-05-12 11:18:46 +02:00
|
|
|
when :groups_overview
|
2009-04-01 00:46:01 +02:00
|
|
|
render :partial => 'shared/articles_by_groups', :locals => {:order => @order} and return
|
2009-01-10 22:22:16 +01:00
|
|
|
when 'articlesOverview'
|
2009-04-01 00:46:01 +02:00
|
|
|
render :partial => 'shared/articles_by_articles', :locals => {:order => @order} and return
|
|
|
|
end
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
def new_group_order_article
|
2009-02-04 16:41:01 +01:00
|
|
|
goa = OrderArticle.find(params[:id]).group_order_articles.build
|
2009-01-10 22:22:16 +01:00
|
|
|
render :update do |page|
|
2009-01-29 21:28:22 +01:00
|
|
|
page["edit_box"].replace_html :partial => "new_group_order_article",
|
2009-02-04 16:41:01 +01:00
|
|
|
:locals => {:group_order_article => goa}
|
2009-01-10 22:22:16 +01:00
|
|
|
page["edit_box"].show
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
# Creates a new GroupOrderArticle
|
|
|
|
# If the the chosen Ordergroup hasn't ordered yet, a GroupOrder will also be created
|
2009-04-05 14:59:55 +02:00
|
|
|
#FIXME: Clean up this messy code !
|
2009-01-29 21:28:22 +01:00
|
|
|
def create_group_order_article
|
|
|
|
goa = GroupOrderArticle.new(params[:group_order_article])
|
|
|
|
order_article = goa.order_article
|
|
|
|
order = order_article.order
|
|
|
|
|
|
|
|
# creates a new GroupOrder if necessary
|
|
|
|
group_order = GroupOrder.first :conditions => {:order_id => order.id, :ordergroup_id => goa.ordergroup_id}
|
|
|
|
unless group_order
|
|
|
|
goa.create_group_order(:order_id => order.id, :ordergroup_id => goa.ordergroup_id)
|
|
|
|
else
|
|
|
|
goa.group_order = group_order
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
2009-01-29 21:28:22 +01:00
|
|
|
|
2009-04-05 14:59:55 +02:00
|
|
|
# If there is an GroupOrderArticle already, only update result attribute.
|
|
|
|
if group_order_article = GroupOrderArticle.first(:conditions => {:group_order_id => goa.group_order, :order_article_id => goa.order_article})
|
|
|
|
goa = group_order_article
|
|
|
|
goa.result = params[:group_order_article]["result"]
|
|
|
|
end
|
|
|
|
|
2009-01-10 22:22:16 +01:00
|
|
|
render :update do |page|
|
2009-01-29 21:28:22 +01:00
|
|
|
if goa.save
|
2009-02-06 16:26:35 +01:00
|
|
|
goa.group_order.update_price! # Update the price attribute of new GroupOrder
|
|
|
|
order_article.update_results! if order_article.article.is_a?(StockArticle) # Update units_to_order of order_article
|
2009-01-10 22:22:16 +01:00
|
|
|
page["edit_box"].hide
|
2009-02-09 20:12:56 +01:00
|
|
|
page["order_article_#{order_article.id}"].replace_html :partial => 'order_article', :locals => {:order_article => order_article}
|
2009-01-10 22:22:16 +01:00
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
page["group_order_articles_#{order_article.id}"].replace_html :partial => 'group_order_articles',
|
|
|
|
:locals => {:order_article => order_article}
|
|
|
|
page["group_order_article_#{goa.id}"].visual_effect :highlight, :duration => 2
|
|
|
|
|
|
|
|
page["summary"].replace_html :partial => 'summary', :locals => {:order => order}
|
|
|
|
page["order_profit"].visual_effect :highlight, :duration => 2
|
2009-01-10 22:22:16 +01:00
|
|
|
else
|
2009-01-29 21:28:22 +01:00
|
|
|
page["edit_box"].replace_html :partial => "new_group_order_article",
|
|
|
|
:locals => {:group_order_article => goa}
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
def edit_group_order_article
|
|
|
|
group_order_article = GroupOrderArticle.find(params[:id])
|
|
|
|
render :partial => 'edit_group_order_article',
|
|
|
|
:locals => {:group_order_article => group_order_article}
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
def update_group_order_article
|
|
|
|
goa = GroupOrderArticle.find(params[:id])
|
|
|
|
|
2009-01-10 22:22:16 +01:00
|
|
|
render :update do |page|
|
2009-01-29 21:28:22 +01:00
|
|
|
if goa.update_attributes(params[:group_order_article])
|
2009-02-06 16:26:35 +01:00
|
|
|
goa.group_order.update_price! # Update the price attribute of new GroupOrder
|
|
|
|
goa.order_article.update_results! if goa.order_article.article.is_a?(StockArticle) # Update units_to_order of order_article
|
2009-01-10 22:22:16 +01:00
|
|
|
|
|
|
|
page["edit_box"].hide
|
2009-02-09 20:12:56 +01:00
|
|
|
page["order_article_#{goa.order_article.id}"].replace_html :partial => 'order_article', :locals => {:order_article => goa.order_article}
|
2009-01-29 21:28:22 +01:00
|
|
|
page["group_order_articles_#{goa.order_article.id}"].replace_html :partial => 'group_order_articles',
|
|
|
|
:locals => {:order_article => goa.order_article}
|
|
|
|
page["summary"].replace_html :partial => 'summary', :locals => {:order => goa.order_article.order}
|
|
|
|
page["order_profit"].visual_effect :highlight, :duration => 2
|
2009-01-10 22:22:16 +01:00
|
|
|
else
|
2009-01-29 21:28:22 +01:00
|
|
|
page["edit_box"].replace_html :partial => 'edit_group_order_article'
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-20 18:56:17 +01:00
|
|
|
def update_group_order_article_result
|
|
|
|
goa = GroupOrderArticle.find(params[:id])
|
|
|
|
|
|
|
|
if params[:modifier] == '-'
|
|
|
|
goa.update_attributes({:result => goa.result - 1})
|
|
|
|
elsif params[:modifier] == '+'
|
|
|
|
goa.update_attributes({:result => goa.result + 1})
|
|
|
|
end
|
|
|
|
|
|
|
|
render :update do |page|
|
|
|
|
goa.group_order.update_price! # Update the price attribute of new GroupOrder
|
|
|
|
goa.order_article.update_results! if goa.order_article.article.is_a?(StockArticle) # Update units_to_order of order_article
|
|
|
|
|
|
|
|
page["order_article_#{goa.order_article.id}"].replace_html :partial => 'order_article', :locals => {:order_article => goa.order_article}
|
|
|
|
page["group_order_articles_#{goa.order_article.id}"].replace_html :partial => 'group_order_articles',
|
|
|
|
:locals => {:order_article => goa.order_article}
|
|
|
|
page["summary"].replace_html :partial => 'summary', :locals => {:order => goa.order_article.order}
|
|
|
|
page["order_profit"].visual_effect :highlight, :duration => 2
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
def destroy_group_order_article
|
|
|
|
goa = GroupOrderArticle.find(params[:id])
|
|
|
|
goa.destroy
|
|
|
|
goa.group_order.update_price! # Updates the price attribute of new GroupOrder
|
2009-02-06 16:26:35 +01:00
|
|
|
goa.order_article.update_results! if goa.order_article.article.is_a?(StockArticle) # Update units_to_order of order_article
|
|
|
|
|
2009-01-10 22:22:16 +01:00
|
|
|
render :update do |page|
|
2009-01-29 21:28:22 +01:00
|
|
|
page["edit_box"].hide
|
2009-02-09 20:12:56 +01:00
|
|
|
page["order_article_#{goa.order_article.id}"].replace_html :partial => 'order_article', :locals => {:order_article => goa.order_article}
|
2009-01-29 21:28:22 +01:00
|
|
|
page["group_order_articles_#{goa.order_article.id}"].replace_html :partial => 'group_order_articles',
|
|
|
|
:locals => {:order_article => goa.order_article}
|
|
|
|
page["summary"].replace_html :partial => 'summary', :locals => {:order => goa.order_article.order}
|
|
|
|
page["order_profit"].visual_effect :highlight, :duration => 2
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# before the order will booked, a view lists all Ordergroups and its order_prices
|
2009-01-10 22:22:16 +01:00
|
|
|
def confirm
|
|
|
|
@order = Order.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# Balances the Order, Update of the Ordergroup.account_balances
|
2009-01-10 22:22:16 +01:00
|
|
|
def close
|
|
|
|
@order = Order.find(params[:id])
|
|
|
|
begin
|
2009-01-30 22:27:55 +01:00
|
|
|
@order.close!(@current_user)
|
2009-01-10 22:22:16 +01:00
|
|
|
flash[:notice] = "Bestellung wurde erfolgreich abgerechnet, die Kontostände aktualisiert."
|
|
|
|
redirect_to :action => "index"
|
|
|
|
rescue => e
|
|
|
|
flash[:error] = "Ein Fehler ist beim Abrechnen aufgetreten: " + e
|
2009-01-30 22:27:55 +01:00
|
|
|
redirect_to :action => "new", :id => @order
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
# Close the order directly, without automaticly updating ordergroups account balances
|
|
|
|
def close_direct
|
2009-01-10 22:22:16 +01:00
|
|
|
@order = Order.find(params[:id])
|
2009-01-29 21:28:22 +01:00
|
|
|
if @order.finished?
|
|
|
|
@order.update_attributes(:state => 'closed', :updated_by => @current_user)
|
2009-01-10 22:22:16 +01:00
|
|
|
flash[:notice] = 'Die Bestellung wurde auf "gebucht" gesetzt.'
|
|
|
|
redirect_to :action => 'listOrders', :id => @order
|
|
|
|
else
|
|
|
|
flash[:error] = 'Die Bestellung ist noch nicht beendet.'
|
|
|
|
redirect_to :action => 'listOrders', :id => @order
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|