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']
|
|
|
|
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 = "articles.name"
|
|
|
|
end
|
|
|
|
|
|
|
|
@articles = @order.order_articles.ordered.find(
|
|
|
|
:all,
|
|
|
|
:include => :article,
|
|
|
|
:order => sort
|
|
|
|
)
|
|
|
|
|
2009-04-03 17:50:17 +02:00
|
|
|
if params[:sort] == "order_number"
|
|
|
|
@articles = @articles.sort { |a,b| a.article.order_number.gsub(/[a-zA-Z]/, "").to_i <=> b.article.order_number.gsub(/[a-zA-Z]/, "").to_i }
|
|
|
|
elsif params[:sort] == "order_number_reverse"
|
|
|
|
@articles = @articles.sort { |a,b| b.article.order_number.gsub(/[a-zA-Z]/, "").to_i <=> a.article.order_number.gsub(/[a-zA-Z]/, "").to_i }
|
|
|
|
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])
|
|
|
|
type = order.stockit? ? "type = 'StockArticle'" : "type IS NULL"
|
|
|
|
@articles = Article.find(:all,
|
|
|
|
:conditions => [ "supplier_id = ? AND #{type} AND LOWER(name) LIKE ?",
|
|
|
|
order.supplier_id,
|
|
|
|
'%' + params[:article][:name].downcase + '%' ],
|
|
|
|
:order => 'name ASC',
|
|
|
|
:limit => 8)
|
|
|
|
render :partial => 'shared/auto_complete_articles'
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_order_article
|
|
|
|
@order = Order.find(params[:order_id])
|
|
|
|
order_article = @order.order_articles.find_by_article_id(params[:order_article][:article_id])
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
@order_article.update_article_and_price!(params[:article], params[:price], params[:order_article])
|
|
|
|
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-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
|
|
|
|
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-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
|