2009-01-10 22:22:16 +01:00
|
|
|
class Finance::BalancingController < ApplicationController
|
|
|
|
before_filter :authenticate_finance
|
2009-01-30 22:27:55 +01:00
|
|
|
verify :method => :post, :only => [:close, :close_direct]
|
2009-01-29 21:28:22 +01:00
|
|
|
|
2009-01-10 22:22:16 +01:00
|
|
|
def index
|
2009-01-19 16:40:06 +01:00
|
|
|
@financial_transactions = FinancialTransaction.find(:all, :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
|
|
|
|
|
|
|
|
@articles = @order.order_articles.ordered.find(
|
|
|
|
:all,
|
|
|
|
:include => :article,
|
|
|
|
:order => sort
|
|
|
|
)
|
2009-05-04 17:56:52 +02:00
|
|
|
|
2009-04-03 17:50:17 +02:00
|
|
|
if params[:sort] == "order_number"
|
2009-04-06 19:54:07 +02:00
|
|
|
@articles = @articles.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"
|
2009-04-06 19:54:07 +02:00
|
|
|
@articles = @articles.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
|
|
|
|
|
|
|
|
case view
|
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
|
2009-01-10 22:22:16 +01:00
|
|
|
when 'groupsOverview'
|
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 edit_note
|
2009-01-10 22:22:16 +01:00
|
|
|
@order = Order.find(params[:id])
|
2009-01-29 21:28:22 +01:00
|
|
|
render :partial => 'edit_note'
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
def update_note
|
|
|
|
@order = Order.find(params[:id])
|
2009-01-10 22:22:16 +01:00
|
|
|
render :update do |page|
|
2009-01-29 21:28:22 +01:00
|
|
|
if @order.update_attributes(params[:order])
|
|
|
|
page["note"].replace_html simple_format(@order.note)
|
2009-01-10 22:22:16 +01:00
|
|
|
page["edit_box"].hide
|
|
|
|
else
|
2009-01-29 21:28:22 +01:00
|
|
|
page["results"].replace_html :partial => "edit_note"
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-09 20:12:56 +01:00
|
|
|
def new_order_article
|
|
|
|
@order = Order.find(params[:id])
|
|
|
|
render :update do |page|
|
|
|
|
page["edit_box"].replace_html :partial => "new_order_article"
|
|
|
|
page["edit_box"].show
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_complete_for_article_name
|
|
|
|
order = Order.find(params[:order_id])
|
2009-09-07 10:57:49 +02:00
|
|
|
find_params = {
|
|
|
|
:conditions => ["LOWER(articles.name) LIKE ?", "%#{params[:article][:name].downcase}%" ],
|
|
|
|
:order => 'articles.name ASC',
|
|
|
|
:limit => 8
|
|
|
|
}
|
|
|
|
@articles = if order.stockit?
|
|
|
|
StockArticle.all find_params
|
|
|
|
else
|
|
|
|
order.supplier.articles.all find_params
|
|
|
|
end
|
|
|
|
|
2009-02-09 20:12:56 +01:00
|
|
|
render :partial => 'shared/auto_complete_articles'
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_order_article
|
|
|
|
@order = Order.find(params[:order_id])
|
2009-09-07 13:31:03 +02:00
|
|
|
article = Article.find(params[:order_article][:article_id])
|
|
|
|
order_article = @order.order_articles.find_by_article_id(article.id)
|
2009-02-09 20:12:56 +01:00
|
|
|
|
|
|
|
unless order_article
|
|
|
|
# Article wasn't already assigned with this order, lets create a new one
|
|
|
|
order_article = @order.order_articles.build(params[:order_article])
|
|
|
|
order_article.article_price = order_article.article.article_prices.first
|
|
|
|
end
|
|
|
|
# Set units to order to 1, so the article is visible on page
|
|
|
|
order_article.units_to_order = 1
|
|
|
|
|
|
|
|
render :update do |page|
|
|
|
|
if order_article.save
|
|
|
|
page["edit_box"].hide
|
|
|
|
page.insert_html :top, "result_table", :partial => "order_article_result", :locals => {:order_article => order_article}
|
|
|
|
page["order_article_#{order_article.id}"].visual_effect :highlight, :duration => 2
|
|
|
|
page["group_order_articles_#{order_article.id}"].show
|
|
|
|
else
|
|
|
|
page["edit_box"].replace_html :partial => "new_order_article"
|
|
|
|
end
|
|
|
|
end
|
2009-09-07 13:31:03 +02:00
|
|
|
|
|
|
|
rescue
|
|
|
|
render :update do |page|
|
|
|
|
page.replace_html "edit_box", :text => "<b>Keinen Artikel gefunden. Bitte erneut versuchen.</b>"
|
|
|
|
page.insert_html :bottom, "edit_box", :partial => "new_order_article"
|
|
|
|
end
|
2009-02-09 20:12:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit_order_article
|
|
|
|
@order_article = OrderArticle.find(params[:id])
|
|
|
|
render :update do |page|
|
|
|
|
page["edit_box"].replace_html :partial => 'edit_order_article'
|
|
|
|
page["edit_box"].show
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update this article and creates a new articleprice if neccessary
|
|
|
|
def update_order_article
|
|
|
|
@order_article = OrderArticle.find(params[:id])
|
|
|
|
begin
|
2012-07-18 22:16:08 +02:00
|
|
|
@order_article.update_article_and_price!(params[:article], params[:price], params[:order_article], params[:price_global])
|
2009-02-09 20:12:56 +01:00
|
|
|
render :update do |page|
|
|
|
|
page["edit_box"].hide
|
|
|
|
page["summary"].replace_html :partial => 'summary', :locals => {:order => @order_article.order}
|
|
|
|
page["summary"].visual_effect :highlight, :duration => 2
|
|
|
|
page["order_article_#{@order_article.id}"].replace_html :partial => 'order_article', :locals => {:order_article => @order_article}
|
|
|
|
page["order_article_#{@order_article.id}"].visual_effect :highlight, :delay => 0.5, :duration => 2
|
|
|
|
page["group_order_articles_#{@order_article.id}"].replace_html :partial => "group_order_articles", :locals => {:order_article => @order_article}
|
|
|
|
end
|
|
|
|
rescue => @error
|
|
|
|
render :update do |page|
|
|
|
|
page['edit_box'].replace_html :partial => 'edit_order_article'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-01-29 21:28:22 +01:00
|
|
|
|
|
|
|
def destroy_order_article
|
|
|
|
order_article = OrderArticle.find(params[:id])
|
|
|
|
order_article.destroy
|
2009-05-13 13:04:39 +02:00
|
|
|
# Updates ordergroup values
|
|
|
|
order_article.group_order_articles.each { |goa| goa.group_order.update_price! }
|
|
|
|
|
2009-01-10 22:22:16 +01:00
|
|
|
render :update do |page|
|
2009-01-29 21:28:22 +01:00
|
|
|
page["order_article_#{order_article.id}"].remove
|
|
|
|
page["group_order_articles_#{order_article.id}"].remove
|
2009-02-09 20:12:56 +01:00
|
|
|
page["summary"].replace_html :partial => 'summary', :locals => {:order => order_article.order}
|
2009-01-29 21:28:22 +01:00
|
|
|
page["summary"].visual_effect :highlight, :duration => 2
|
2009-01-10 22:22:16 +01:00
|
|
|
end
|
|
|
|
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
|