From ec402ffa7bad71a3a2e958aa02f397ed0922d9a8 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Fri, 30 Jan 2009 22:27:55 +0100 Subject: [PATCH] Fixed order.close workflow. (formerly known as order.balance) --- .../finance/balancing_controller.rb | 8 ++--- app/models/group_order.rb | 2 +- app/models/order.rb | 31 ++++++++++--------- app/views/finance/balancing/list.html.haml | 2 -- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/app/controllers/finance/balancing_controller.rb b/app/controllers/finance/balancing_controller.rb index f506b174..f68fec00 100644 --- a/app/controllers/finance/balancing_controller.rb +++ b/app/controllers/finance/balancing_controller.rb @@ -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 diff --git a/app/models/group_order.rb b/app/models/group_order.rb index e730cac1..3b860de5 100644 --- a/app/models/group_order.rb +++ b/app/models/group_order.rb @@ -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) diff --git a/app/models/order.rb b/app/models/order.rb index fa93039d..501ebbb7 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/app/views/finance/balancing/list.html.haml b/app/views/finance/balancing/list.html.haml index 74906e72..ae6e4c24 100644 --- a/app/views/finance/balancing/list.html.haml +++ b/app/views/finance/balancing/list.html.haml @@ -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