Fixed order.close workflow. (formerly known as order.balance)

This commit is contained in:
Benjamin Meichsner 2009-01-30 22:27:55 +01:00
parent 288566f000
commit ec402ffa7b
4 changed files with 22 additions and 21 deletions

View file

@ -1,10 +1,10 @@
class Finance::BalancingController < ApplicationController
before_filter :authenticate_finance
verify :method => :post, :only => [:close_direct]
verify :method => :post, :only => [:close, :close_direct]
def index
@financial_transactions = FinancialTransaction.find(:all, :order => "created_on DESC", :limit => 8)
@orders = Order.finished
@orders = Order.finished_not_closed
@unpaid_invoices = Invoice.unpaid
end
@ -200,12 +200,12 @@ class Finance::BalancingController < ApplicationController
def close
@order = Order.find(params[:id])
begin
@order.balance(@current_user)
@order.close!(@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
redirect_to :action => "new", :id => @order
end
end

View file

@ -30,7 +30,7 @@ class GroupOrder < ActiveRecord::Base
named_scope :finished, lambda { {:conditions => ["order_id IN (?)", Order.finished.collect{|o| o.id}]} }
# Updates the "price" attribute.
# This will be the maximum value of a current order
# This will be the maximum value of an order
def update_price!
total = 0
for article in group_order_articles.find(:all, :include => :order_article)

View file

@ -35,10 +35,12 @@ class Order < ActiveRecord::Base
after_update :update_price_of_group_orders
# Finders
named_scope :finished, :conditions => { :state => 'finished' },
:order => 'ends DESC'
named_scope :open, :conditions => { :state => 'open' },
:order => 'ends DESC'
named_scope :finished, :conditions => "state = 'finished' OR state = 'closed'",
:order => 'ends DESC'
named_scope :finished_not_closed, :conditions => { :state => 'finished' },
:order => 'ends DESC'
named_scope :closed, :conditions => { :state => 'closed' },
:order => 'ends DESC'
@ -177,19 +179,20 @@ class Order < ActiveRecord::Base
end
end
# Sets "booked"-attribute to true and updates all Ordergroup_account_balances
def balance(user)
raise "Bestellung wurde schon abgerechnet" if self.booked
transaction_note = "Bestellung: #{name}, von #{starts.strftime('%d.%m.%Y')} bis #{ends.strftime('%d.%m.%Y')}"
transaction do
# update Ordergroups
group_order_results.each do |result|
price = result.price * -1 # decrease! account balance
Ordergroup.find_by_name(result.group_name).addFinancialTransaction(price, transaction_note, user)
# Sets order.status to 'close' and updates all Ordergroup.account_balances
def close!(user)
raise "Bestellung wurde schon abgerechnet" if closed?
transaction_note = "Bestellung: #{supplier.name}, bis #{ends.strftime('%d.%m.%Y')}"
gos = group_orders.all(:include => :ordergroup) # Fetch group_orders
gos.each { |group_order| group_order.update_price! } # Update prices of group_orders
transaction do # Start updating account balances
for group_order in gos
price = group_order.price * -1 # decrease! account balance
group_order.ordergroup.addFinancialTransaction(price, transaction_note, user)
end
self.booked = true
self.updated_by = user
self.save!
self.update_attributes! :state => 'closed', :updated_by => user
end
end

View file

@ -11,7 +11,6 @@
%thead
%tr
%th Name
%th Anbieter
%th Ende
%th Status
%th zuletzt bearbeitet von
@ -20,7 +19,6 @@
- @orders.each do |order|
%tr{:class => cycle("even","odd", :name => "order")}
%td= link_to truncate(order.supplier.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.closed? ? "abgerechnet (#{number_to_currency order.profit})" : "beendet"
%td= order.updated_by.nil? ? '??' : order.updated_by.nick