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 class Finance::BalancingController < ApplicationController
before_filter :authenticate_finance before_filter :authenticate_finance
verify :method => :post, :only => [:close_direct] verify :method => :post, :only => [:close, :close_direct]
def index def index
@financial_transactions = FinancialTransaction.find(:all, :order => "created_on DESC", :limit => 8) @financial_transactions = FinancialTransaction.find(:all, :order => "created_on DESC", :limit => 8)
@orders = Order.finished @orders = Order.finished_not_closed
@unpaid_invoices = Invoice.unpaid @unpaid_invoices = Invoice.unpaid
end end
@ -200,12 +200,12 @@ class Finance::BalancingController < ApplicationController
def close def close
@order = Order.find(params[:id]) @order = Order.find(params[:id])
begin begin
@order.balance(@current_user) @order.close!(@current_user)
flash[:notice] = "Bestellung wurde erfolgreich abgerechnet, die Kontostände aktualisiert." flash[:notice] = "Bestellung wurde erfolgreich abgerechnet, die Kontostände aktualisiert."
redirect_to :action => "index" redirect_to :action => "index"
rescue => e rescue => e
flash[:error] = "Ein Fehler ist beim Abrechnen aufgetreten: " + e flash[:error] = "Ein Fehler ist beim Abrechnen aufgetreten: " + e
redirect_to :action =>"editOrder", :id => @order redirect_to :action => "new", :id => @order
end end
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}]} } named_scope :finished, lambda { {:conditions => ["order_id IN (?)", Order.finished.collect{|o| o.id}]} }
# Updates the "price" attribute. # 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! def update_price!
total = 0 total = 0
for article in group_order_articles.find(:all, :include => :order_article) 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 after_update :update_price_of_group_orders
# Finders # Finders
named_scope :finished, :conditions => { :state => 'finished' },
:order => 'ends DESC'
named_scope :open, :conditions => { :state => 'open' }, named_scope :open, :conditions => { :state => 'open' },
:order => 'ends DESC' :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' }, named_scope :closed, :conditions => { :state => 'closed' },
:order => 'ends DESC' :order => 'ends DESC'
@ -177,19 +179,20 @@ class Order < ActiveRecord::Base
end end
end end
# Sets "booked"-attribute to true and updates all Ordergroup_account_balances # Sets order.status to 'close' and updates all Ordergroup.account_balances
def balance(user) def close!(user)
raise "Bestellung wurde schon abgerechnet" if self.booked raise "Bestellung wurde schon abgerechnet" if closed?
transaction_note = "Bestellung: #{name}, von #{starts.strftime('%d.%m.%Y')} bis #{ends.strftime('%d.%m.%Y')}" transaction_note = "Bestellung: #{supplier.name}, bis #{ends.strftime('%d.%m.%Y')}"
transaction do
# update Ordergroups gos = group_orders.all(:include => :ordergroup) # Fetch group_orders
group_order_results.each do |result| gos.each { |group_order| group_order.update_price! } # Update prices of group_orders
price = result.price * -1 # decrease! account balance
Ordergroup.find_by_name(result.group_name).addFinancialTransaction(price, transaction_note, user) 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 end
self.booked = true self.update_attributes! :state => 'closed', :updated_by => user
self.updated_by = user
self.save!
end end
end end

View file

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