Moved balancing logic into new namespace 'finance'.

This commit is contained in:
Benjamin Meichsner 2009-01-10 22:22:16 +01:00
parent c282cae79c
commit 47398c6a70
27 changed files with 277 additions and 268 deletions

View File

@ -0,0 +1,244 @@
class Finance::BalancingController < ApplicationController
before_filter :authenticate_finance
def index
@orders = Order.finished.paginate :page => params[:page], :per_page => 10, :order => 'ends DESC'
end
def new
@order = Order.find(params[:id])
@comments = @order.comments
case params[:view]
when 'editResults'
render :partial => 'editResults'
when 'groupsOverview'
render :partial => 'groupsOverview'
when 'articlesOverview'
render :partial => 'articlesOverview'
when "editNote"
render :partial => "editNote"
end
end
def newArticleResult
@order = Order.find(params[:id])
@article = @order.order_article_results.build(:tax => 7, :deposit => 0)
render :update do |page|
page["edit_box"].replace_html :partial => "newArticleResult"
page["edit_box"].show
end
end
def createArticleResult
render :update do |page|
@article = OrderArticleResult.new(params[:order_article_result])
@article.fc_markup = APP_CONFIG[:price_markup]
@article.make_gross if @article.tax && @article.deposit && @article.net_price
if @article.valid?
@article.save
@order = @article.order
page["edit_box"].hide
page["order_summary"].replace_html :partial => 'summary'
page.insert_html :bottom, "result_table", :partial => "articleResults"
page["order_article_result_#{@article.id}"].visual_effect :highlight, :duration => 2
page["group_order_article_results_#{@article.id}"].show
else
page["edit_box"].replace_html :partial => "newArticleResult"
end
end
end
def editArticleResult
@article = OrderArticleResult.find(params[:id])
render :update do |page|
page["edit_box"].replace_html :partial => 'editArticleResult'
page["edit_box"].show
end
end
def updateArticleResult
@article = OrderArticleResult.find(params[:id])
@article.attributes=(params[:order_article_result]) # update attributes but doesn't save
@article.make_gross
@order = @article.order
@ordered_articles = @order.order_article_results
@group_orders = @order.group_order_results
render :update do |page|
if @article.save
page["edit_box"].hide
page["order_summary"].replace_html :partial => 'summary'
page["order_summary"].visual_effect :highlight, :duration => 2
page["order_article_result_#{@article.id}"].replace_html :partial => 'articleResult'
page['order_article_result_'+@article.id.to_s].visual_effect :highlight, :delay => 0.5, :duration => 2
page["group_order_article_results_#{@article.id}"].replace_html :partial => "groupOrderArticleResults"
else
page['edit_box'].replace_html :partial => 'editArticleResult'
end
end
end
def destroyArticleResult
if @article = OrderArticleResult.find(params[:id]).destroy
@order = @article.order
render :update do |page|
page["order_article_result_#{@article.id}"].remove
page["group_order_article_results_#{@article.id}"].remove
page["order_summary"].replace_html :partial => 'summary'
page["order_summary"].visual_effect :highlight, :duration => 2
end
end
end
def newGroupResult
@result = OrderArticleResult.find(params[:id]).group_order_article_results.build
render :update do |page|
page["edit_box"].replace_html :partial => "newGroupResult"
page["edit_box"].show
end
end
# Creates a new GroupOrderArticleResult
# If the the chosen OrderGroup hasn't ordered yet, a GroupOrderResult will created
def createGroupResult
@result = GroupOrderArticleResult.new(params[:group_order_article_result])
order = @result.order_article_result.order
orderGroup = OrderGroup.find(params[:group_order_article_result][:group_order_result_id])
# creates a new GroupOrderResult if necessary
unless @result.group_order_result = GroupOrderResult.find(:first,
:conditions => ["group_order_results.group_name = ? AND group_order_results.order_id = ?", orderGroup.name, order.id ])
@result.group_order_result = GroupOrderResult.create(:order => order, :group_name => orderGroup.name)
end
render :update do |page|
if @result.valid? && @result.save
@result.group_order_result.updatePrice #updates the price attribute
article = @result.order_article_result
page["edit_box"].hide
page.insert_html :after, "groups_results_#{article.id}", :partial => "groupResults"
page["group_order_article_result_#{@result.id}"].visual_effect :highlight, :duration => 2
page["groups_amount"].replace_html number_to_currency(article.order.sumPrice('groups'))
page["fcProfit"].replace_html number_to_currency(article.order.fcProfit)
page["fcProfit"].visual_effect :highlight, :duration => 2
# get the new sums for quantity and price and replace it
total = article.total
page["totalArticleQuantity_#{article.id}"].replace_html total[:quantity]
page["totalArticlePrice_#{article.id}"].replace_html number_to_currency(total[:price])
else
page["edit_box"].replace_html :partial => "newGroupResult"
end
end
end
def updateGroupResult
@result = GroupOrderArticleResult.find(params[:id])
render :update do |page|
if params[:group_order_article_result]
if @result.update_attribute(:quantity, params[:group_order_article_result][:quantity])
order = @result.group_order_result.order
groups_amount = order.sumPrice("groups")
article = @result.order_article_result
total = article.total
page["edit_box"].hide
page["groups_amount"].replace_html number_to_currency(groups_amount)
page["fcProfit"].replace_html number_to_currency(order.fcProfit)
page["groups_amount"].visual_effect :highlight, :duration => 2
page["fcProfit"].visual_effect :highlight, :duration => 2
page["group_order_article_result_#{@result.id}"].replace_html :partial => "groupResult"
page["group_order_article_result_#{@result.id}"].visual_effect :highlight, :duration => 2
page["totalArticleQuantity_#{article.id}"].replace_html total[:quantity]
page["totalArticlePrice_#{article.id}"].replace_html total[:price]
page["sum_of_article_#{article.id}"].visual_effect :highlight, :duration => 2
end
else
page["edit_box"].replace_html :partial => 'editGroupResult'
page["edit_box"].show
end
end
end
def destroyGroupResult
@result = GroupOrderArticleResult.find(params[:id])
if @result.destroy
render :update do |page|
article = @result.order_article_result
page["group_order_article_result_#{@result.id}"].remove
page["groups_amount"].replace_html number_to_currency(article.order.sumPrice('groups'))
page["fcProfit"].replace_html number_to_currency(article.order.fcProfit)
page["fcProfit"].visual_effect :highlight, :duration => 2
total = article.total # get total quantity and price for the ArticleResult
page["totalArticleQuantity_#{article.id}"].replace_html total[:quantity]
page["totalArticleQuantity_#{article.id}"].visual_effect :highlight, :duration => 2
page["totalArticlePrice_#{article.id}"].replace_html number_to_currency(total[:price])
page["totalArticlePrice_#{article.id}"].visual_effect :highlight, :duration => 2
end
end
end
def editOrderSummary
@order = Order.find(params[:id])
render :update do |page|
page["edit_box"].replace_html :partial => 'editSummary'
page["edit_box"].show
end
end
def updateOrderSummary
@order = Order.find(params[:id])
render :update do |page|
if @order.update_attributes(params[:order])
page["edit_box"].hide
page["order_summary"].replace_html :partial => "summary"
page["clear_invoice"].visual_effect :highlight, :duration => 2
else
page["edit_box"].replace_html :partial => 'editSummary'
end
end
end
def updateOrderNote
@order = Order.find(params[:id])
render :update do |page|
if @order.update_attribute(:note, params[:order][:note])
page["note"].replace_html simple_format(@order.note)
page["results"].replace_html :partial => "groupsOverview"
else
page["results"].replace_html :partial => "editNote"
end
end
end
# before the order will booked, a view lists all OrderGroups and its order_prices
def confirm
@order = Order.find(params[:id])
end
# Balances the Order, Update of the OrderGroup.account_balances
def close
@order = Order.find(params[:id])
begin
@order.balance(@current_user)
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
redirect_to :action =>"editOrder", :id => @order
end
end
# Set all GroupOrders that belong to this finished order to status 'booked'.
def setAllBooked
@order = Order.find(params[:id])
if (@order.finished?)
@order.booked = true
@order.updated_by = @current_user
@order.save!
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

View File

@ -1,259 +1,10 @@
class FinanceController < ApplicationController
before_filter :authenticate_finance
# Messages
MSG_TRANSACTION_SUCCESS = 'Transaktion erfolgreich angelegt.'
ERROR_TRANSACTION_FAILED = 'Transaktion konnte nicht angelegt werden!'
MSG_ORDER_SET_BOOKED = 'Die Bestellung wurde auf "gebucht" gesetzt.'
ERROR_ORDER_NOT_FINISHED = 'Die Bestellung ist noch nicht beendet.'
MSG_ORDER_BALANCED = "Bestellung wurde erfolgreich abgerechnet, die Kontostände aktualisiert."
ERROR_BALANCE_ORDER = "Ein Fehler ist beim Abrechnen aufgetreten: "
def index
@financial_transactions = FinancialTransaction.find(:all, :order => "created_on DESC", :limit => 8)
@orders = Order.find(:all, :conditions => 'finished = 1 AND booked = 0', :order => 'ends DESC')
@unpaid_invoices = Invoice.unpaid
end
# list finished orders for the order-clearing
def listOrders
@orders = Order.paginate_all_by_finished true, :page => params[:page], :per_page => 10, :order => 'ends DESC'
end
def editOrder
@order = Order.find(params[:id])
@comments = @order.comments
case params[:view]
when 'editResults'
render :partial => 'editResults'
when 'groupsOverview'
render :partial => 'groupsOverview'
when 'articlesOverview'
render :partial => 'articlesOverview'
when "editNote"
render :partial => "editNote"
end
end
def newArticleResult
@order = Order.find(params[:id])
@article = @order.order_article_results.build(:tax => 7, :deposit => 0)
render :update do |page|
page["edit_box"].replace_html :partial => "newArticleResult"
page["edit_box"].show
end
end
def createArticleResult
render :update do |page|
@article = OrderArticleResult.new(params[:order_article_result])
@article.fc_markup = APP_CONFIG[:price_markup]
@article.make_gross if @article.tax && @article.deposit && @article.net_price
if @article.valid?
@article.save
@order = @article.order
page["edit_box"].hide
page["order_summary"].replace_html :partial => 'summary'
page.insert_html :bottom, "result_table", :partial => "articleResults"
page["order_article_result_#{@article.id}"].visual_effect :highlight, :duration => 2
page["group_order_article_results_#{@article.id}"].show
else
page["edit_box"].replace_html :partial => "newArticleResult"
end
end
end
def editArticleResult
@article = OrderArticleResult.find(params[:id])
render :update do |page|
page["edit_box"].replace_html :partial => 'editArticleResult'
page["edit_box"].show
end
end
def updateArticleResult
@article = OrderArticleResult.find(params[:id])
@article.attributes=(params[:order_article_result]) # update attributes but doesn't save
@article.make_gross
@order = @article.order
@ordered_articles = @order.order_article_results
@group_orders = @order.group_order_results
render :update do |page|
if @article.save
page["edit_box"].hide
page["order_summary"].replace_html :partial => 'summary'
page["order_summary"].visual_effect :highlight, :duration => 2
page["order_article_result_#{@article.id}"].replace_html :partial => 'articleResult'
page['order_article_result_'+@article.id.to_s].visual_effect :highlight, :delay => 0.5, :duration => 2
page["group_order_article_results_#{@article.id}"].replace_html :partial => "groupOrderArticleResults"
else
page['edit_box'].replace_html :partial => 'editArticleResult'
end
end
end
def destroyArticleResult
if @article = OrderArticleResult.find(params[:id]).destroy
@order = @article.order
render :update do |page|
page["order_article_result_#{@article.id}"].remove
page["group_order_article_results_#{@article.id}"].remove
page["order_summary"].replace_html :partial => 'summary'
page["order_summary"].visual_effect :highlight, :duration => 2
end
end
end
def newGroupResult
@result = OrderArticleResult.find(params[:id]).group_order_article_results.build
render :update do |page|
page["edit_box"].replace_html :partial => "newGroupResult"
page["edit_box"].show
end
end
# Creates a new GroupOrderArticleResult
# If the the chosen OrderGroup hasn't ordered yet, a GroupOrderResult will created
def createGroupResult
@result = GroupOrderArticleResult.new(params[:group_order_article_result])
order = @result.order_article_result.order
orderGroup = OrderGroup.find(params[:group_order_article_result][:group_order_result_id])
# creates a new GroupOrderResult if necessary
unless @result.group_order_result = GroupOrderResult.find(:first,
:conditions => ["group_order_results.group_name = ? AND group_order_results.order_id = ?", orderGroup.name, order.id ])
@result.group_order_result = GroupOrderResult.create(:order => order, :group_name => orderGroup.name)
end
render :update do |page|
if @result.valid? && @result.save
@result.group_order_result.updatePrice #updates the price attribute
article = @result.order_article_result
page["edit_box"].hide
page.insert_html :after, "groups_results_#{article.id}", :partial => "groupResults"
page["group_order_article_result_#{@result.id}"].visual_effect :highlight, :duration => 2
page["groups_amount"].replace_html number_to_currency(article.order.sumPrice('groups'))
page["fcProfit"].replace_html number_to_currency(article.order.fcProfit)
page["fcProfit"].visual_effect :highlight, :duration => 2
# get the new sums for quantity and price and replace it
total = article.total
page["totalArticleQuantity_#{article.id}"].replace_html total[:quantity]
page["totalArticlePrice_#{article.id}"].replace_html number_to_currency(total[:price])
else
page["edit_box"].replace_html :partial => "newGroupResult"
end
end
end
def updateGroupResult
@result = GroupOrderArticleResult.find(params[:id])
render :update do |page|
if params[:group_order_article_result]
if @result.update_attribute(:quantity, params[:group_order_article_result][:quantity])
order = @result.group_order_result.order
groups_amount = order.sumPrice("groups")
article = @result.order_article_result
total = article.total
page["edit_box"].hide
page["groups_amount"].replace_html number_to_currency(groups_amount)
page["fcProfit"].replace_html number_to_currency(order.fcProfit)
page["groups_amount"].visual_effect :highlight, :duration => 2
page["fcProfit"].visual_effect :highlight, :duration => 2
page["group_order_article_result_#{@result.id}"].replace_html :partial => "groupResult"
page["group_order_article_result_#{@result.id}"].visual_effect :highlight, :duration => 2
page["totalArticleQuantity_#{article.id}"].replace_html total[:quantity]
page["totalArticlePrice_#{article.id}"].replace_html total[:price]
page["sum_of_article_#{article.id}"].visual_effect :highlight, :duration => 2
end
else
page["edit_box"].replace_html :partial => 'editGroupResult'
page["edit_box"].show
end
end
end
def destroyGroupResult
@result = GroupOrderArticleResult.find(params[:id])
if @result.destroy
render :update do |page|
article = @result.order_article_result
page["group_order_article_result_#{@result.id}"].remove
page["groups_amount"].replace_html number_to_currency(article.order.sumPrice('groups'))
page["fcProfit"].replace_html number_to_currency(article.order.fcProfit)
page["fcProfit"].visual_effect :highlight, :duration => 2
total = article.total # get total quantity and price for the ArticleResult
page["totalArticleQuantity_#{article.id}"].replace_html total[:quantity]
page["totalArticleQuantity_#{article.id}"].visual_effect :highlight, :duration => 2
page["totalArticlePrice_#{article.id}"].replace_html number_to_currency(total[:price])
page["totalArticlePrice_#{article.id}"].visual_effect :highlight, :duration => 2
end
end
end
def editOrderSummary
@order = Order.find(params[:id])
render :update do |page|
page["edit_box"].replace_html :partial => 'editSummary'
page["edit_box"].show
end
end
def updateOrderSummary
@order = Order.find(params[:id])
render :update do |page|
if @order.update_attributes(params[:order])
page["edit_box"].hide
page["order_summary"].replace_html :partial => "summary"
page["clear_invoice"].visual_effect :highlight, :duration => 2
else
page["edit_box"].replace_html :partial => 'editSummary'
end
end
end
def updateOrderNote
@order = Order.find(params[:id])
render :update do |page|
if @order.update_attribute(:note, params[:order][:note])
page["note"].replace_html simple_format(@order.note)
page["results"].replace_html :partial => "groupsOverview"
else
page["results"].replace_html :partial => "editNote"
end
end
end
# before the order will booked, a view lists all OrderGroups and its order_prices
def confirmOrder
@order = Order.find(params[:id])
end
# Balances the Order, Update of the OrderGroup.account_balances
def balanceOrder
@order = Order.find(params[:id])
begin
@order.balance(@current_user)
flash[:notice] = MSG_ORDER_BALANCED
redirect_to :action => "index"
rescue => e
flash[:error] = ERROR_BALANCE_ORDER + e
redirect_to :action =>"editOrder", :id => @order
end
end
# Set all GroupOrders that belong to this finished order to status 'booked'.
def setAllBooked
@order = Order.find(params[:id])
if (@order.finished?)
@order.booked = true
@order.updated_by = @current_user
@order.save!
flash[:notice] = MSG_ORDER_SET_BOOKED
redirect_to :action => 'listOrders', :id => @order
else
flash[:error] = ERROR_ORDER_NOT_FINISHED
redirect_to :action => 'listOrders', :id => @order
end
end
end

View File

@ -0,0 +1,2 @@
module Finance::OrdersHelper
end

View File

@ -45,7 +45,10 @@ class Order < ActiveRecord::Base
# easyier find of next or previous model
acts_as_ordered :order => "ends"
named_scope :finished, :conditions => { :finished => true },
:order => 'ends DESC', :include => :supplier
# Custom attribute setter that accepts decimal numbers using localized decimal separator.
def invoice_amount=(amount)
self[:invoice_amount] = String.delocalized_decimal(amount)

View File

@ -7,4 +7,4 @@
%p
= submit_tag "Speichern"
|
= link_to_remote 'Abbrechen', :update => 'results', :url => { :action => 'editOrder', :id => @order, :view => 'groupsOverview' }, :before => "Element.show('loader')", :success => "Element.hide('loader')"
= link_to_remote 'Abbrechen', :update => 'results', :url => { :action => 'edit', :id => @order, :view => 'groupsOverview' }, :before => "Element.show('loader')", :success => "Element.hide('loader')"

View File

@ -11,6 +11,6 @@
%td= number_to_currency(groupResult.price)
%p
%div{:style => "float:left"}
= button_to "Abschließen", :action => "balanceOrder", :id => @order
= button_to "Abschließen", :action => "close", :id => @order
%div{:style => "float:right"}
= link_to 'Zurück zur Abrechnung', :action => "editOrder", :id => @order
= link_to 'Zurück zur Abrechnung', :action => "new", :id => @order

View File

@ -23,14 +23,14 @@
%tbody
- @orders.each do |order|
%tr{:class => cycle("even","odd", :name => "order")}
%td= link_to truncate(order.name), :action => "editOrder", :id => order
%td= link_to truncate(order.name), :action => "new", :id => order
%td=h order.supplier ? order.supplier.name : _('nonexistent')
%td=h format_time(order.ends) unless order.ends.nil?
%td= order.booked ? "abgerechnet (#{number_to_currency order.fcProfit})" : "beendet"
%td= order.updated_by.nil? ? '??' : order.updated_by.nick
%td
- unless order.booked
= link_to "abrechnen", :action => "editOrder", :id => order
= link_to "abrechnen", :action => "new", :id => order
|
= link_to 'auf "gebucht" setzen', {:action => 'setAllBooked', :id => order}, :confirm => 'Wirklich alle Gruppenbestellungen für diese Bestellung auf "gebucht" setzen?', :method => "post"
- else

View File

@ -1,4 +1,5 @@
%h1 Bestellung abrechnen
- if @order.booked
%p
%b Achtung, Bestellung wurde schon abgerechnet!
@ -15,7 +16,7 @@
.column_content
%ul
- unless @order.booked
%li= link_to "Bestellung abschließen", :action => "confirmOrder", :id => @order
%li= link_to "Bestellung abschließen", :action => "confirm", :id => @order
.right_column{:style => 'clear:both;width: 28%'}
.box_title
@ -28,7 +29,7 @@
%p Hier kannst Du deine Abrechnung kommentieren
%small Protokoll bearbeiten:
= link_to_remote image_tag("b_edit.png", :size => "16x16", :border => "0"), |
:update => 'results', :url => { :action => 'editOrder', :id => @order, :view => 'editNote' }, |
:update => 'results', :url => { :action => 'new', :id => @order, :view => 'editNote' }, |
:before => "Element.show('loader')", :success => "Element.hide('loader')" |
%hr/
#comments
@ -38,9 +39,9 @@
.box_title
#editOrderNav
%ul
%li= link_to_remote 'Gruppenübersicht', :update => 'results', :url => { :action => 'editOrder', :id => @order, :view => 'groupsOverview' }, :before => "Element.show('loader')", :success => "Element.hide('loader')"
%li= link_to_remote 'Artikelübersicht', :update => 'results', :url => { :action => 'editOrder', :id => @order, :view => 'articlesOverview' }, :before => "Element.show('loader')", :success => "Element.hide('loader')"
%li= link_to_remote 'Bestellung bearbeiten', :update => 'results', :url => { :action => 'editOrder', :id => @order, :view => 'editResults' }, :before => "Element.show('loader')", :success => "Element.hide('loader')"
%li= link_to_remote 'Gruppenübersicht', :update => 'results', :url => { :action => 'new', :id => @order, :view => 'groupsOverview' }, :before => "Element.show('loader')", :success => "Element.hide('loader')"
%li= link_to_remote 'Artikelübersicht', :update => 'results', :url => { :action => 'new', :id => @order, :view => 'articlesOverview' }, :before => "Element.show('loader')", :success => "Element.hide('loader')"
%li= link_to_remote 'Bestellung bearbeiten', :update => 'results', :url => { :action => 'new', :id => @order, :view => 'editResults' }, :before => "Element.show('loader')", :success => "Element.hide('loader')"
.column_content
#results
= render :partial => 'groupsOverview'

View File

@ -3,7 +3,7 @@
.box_title
%h2 Unpaid invoices
.column_content
%p= link_to "Show all invoices", invoices_path
%p= link_to "Show all invoices", finance_invoices_path
%table.list
%thead
%tr
@ -17,13 +17,13 @@
%td= format_date(invoice.date)
%td= number_to_currency(invoice.amount)
%td=h invoice.supplier.name
%td= link_to "Edit", edit_invoice_path(invoice)
%td= link_to "Edit", edit_finance_invoice_path(invoice)
.box_title
%h2 letzte Überweisungen
.column_content
%p
= link_to "Bestellgruppen", :action => "listOrdergroups"
= link_to "Bestellgruppen", :controller => 'finance/transactions'
%table.list
%thead
%tr
@ -43,7 +43,7 @@
.box_title
%h2 noch nicht abgerechnet
.column_content
%p= link_to "Bestellungsübersicht", :action => "listOrders"
%p= link_to "Bestellungsübersicht", :controller => 'finance/orders'
- unless @orders.empty?
%table.list
%thead

View File

@ -26,11 +26,11 @@
{ :name => "Suppliers", :url => suppliers_path, :access? => (u.role_suppliers?) }
]
},
{ :name => "Finance", :url => "/finance", :active => ["finance", "invoices", "transactions"],
{ :name => "Finance", :url => "/finance", :active => ["finance", "invoices", "transactions", "balancing"],
:access? => (u.role_finance?),
:subnav => [
{ :name => "Manage accounts", :url => "/finance/transactions" },
{ :name => "Balance orders", :url => "/finance/listOrders" },
{ :name => "Balance orders", :url => "/finance/balancing" },
{ :name => "Invoices", :url => finance_invoices_path }
]
},

View File

@ -3,9 +3,9 @@ ActionController::Routing::Routes.draw do |map|
map.namespace :finance do |finance|
finance.resources :invoices
finance.connect 'transactions/:action/:id', :controller => 'transactions'
finance.connect 'balancing/:action/:id', :controller => 'balancing'
end
#map.resources :invoices
map.resources :suppliers,
:has_many => [:deliveries],
:collection => { :shared_suppliers => :get }

View File

@ -0,0 +1,8 @@
require 'test_helper'
class Finance::OrdersControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end