Fixed bug order.profit calculation.

Order.profit will also be saved, when order is closed.
This commit is contained in:
Benjamin Meichsner 2009-03-17 19:43:41 +01:00
parent 843e4a7233
commit da08365816
32 changed files with 408 additions and 34 deletions

View File

@ -80,7 +80,7 @@ class ApplicationController < ActionController::Base
if !(user = current_user)
# No user at all: redirect to login page.
self.return_to = request.request_uri
redirect_to :controller => 'login'
redirect_to :controller => '/login'
return false
else
# We have an authenticated user, now check role...

View File

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090119155930
# Schema version: 20090317175355
#
# Table name: orders
#
@ -11,6 +11,7 @@
# state :string(255) default("open")
# lock_version :integer default(0), not null
# updated_by_user_id :integer
# foodcoop_result :decimal(8, 2)
#
class Order < ActiveRecord::Base
@ -117,10 +118,11 @@ class Order < ActiveRecord::Base
# Returns the defecit/benefit for the foodcoop
# Requires a valid invoice, belonging to this order
#FIXME: Consider order.foodcoop_result
def profit(options = {})
markup = options[:with_markup] || true
markup = options[:without_markup] || false
if invoice
groups_sum = markup ? sum(:groups) : sum(:groups_without_markup)
groups_sum = markup ? sum(:groups_without_markup) : sum(:groups)
groups_sum - invoice.net_amount
end
end
@ -145,8 +147,8 @@ class Order < ActiveRecord::Base
end
end
elsif type == :groups || type == :groups_without_markup
for go in group_orders
for goa in go.group_order_articles
for go in group_orders.all(:include => :group_order_articles)
for goa in go.group_order_articles.all(:include => [:order_article])
case type
when :groups
total += goa.result * goa.order_article.price.fc_price
@ -208,7 +210,7 @@ class Order < ActiveRecord::Base
end
end
self.update_attributes! :state => 'closed', :updated_by => user
self.update_attributes! :state => 'closed', :updated_by => user, :foodcoop_result => profit
end
end

View File

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090120184410
# Schema version: 20090317175355
#
# Table name: stock_changes
#
@ -9,6 +9,7 @@
# stock_article_id :integer
# quantity :integer default(0)
# created_at :datetime
# stock_taking_id :integer
#
class StockChange < ActiveRecord::Base

View File

@ -1,3 +1,14 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: stock_takings
#
# id :integer not null, primary key
# date :date
# note :text
# created_at :datetime
#
class StockTaking < ActiveRecord::Base
has_many :stock_changes, :dependent => :destroy

View File

@ -17,7 +17,7 @@
%td= number_to_currency(order.sum(:groups))
%tr
%td FC Gewinn ohne Aufschlag:
%td= number_to_currency(order.profit(:with_markup => false))
%td= number_to_currency(order.profit(:without_markup => true))
%tr
%td FC Gewinn mit Aufschlag:
%td#order_profit= number_to_currency(order.profit)

View File

@ -20,7 +20,7 @@
%tr{:class => cycle("even","odd", :name => "order")}
%td= link_to truncate(order.name), :action => "new", :id => order
%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.foodcoop_result})" : "beendet"
%td= order.updated_by.nil? ? '??' : order.updated_by.nick
%td
- unless order.closed?

View File

@ -0,0 +1,13 @@
class AddProfitToOrders < ActiveRecord::Migration
def self.up
add_column :orders, :foodcoop_result, :decimal, :precision => 8, :scale => 2
Order.closed.each do |order|
order.update_attribute(:foodcoop_result, order.profit)
end
end
def self.down
remove_column :orders, :foodcoop_result
end
end

View File

@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20090119155930) do
ActiveRecord::Schema.define(:version => 20090317175355) do
create_table "article_categories", :force => true do |t|
t.string "name", :default => "", :null => false
@ -205,9 +205,10 @@ ActiveRecord::Schema.define(:version => 20090119155930) do
t.text "note"
t.datetime "starts"
t.datetime "ends"
t.string "state", :default => "open"
t.integer "lock_version", :default => 0, :null => false
t.string "state", :default => "open"
t.integer "lock_version", :default => 0, :null => false
t.integer "updated_by_user_id"
t.decimal "foodcoop_result", :precision => 8, :scale => 2
end
create_table "stock_changes", :force => true do |t|
@ -216,12 +217,12 @@ ActiveRecord::Schema.define(:version => 20090119155930) do
t.integer "stock_article_id"
t.integer "quantity", :default => 0
t.datetime "created_at"
t.integer "stock_taking_id"
end
create_table "stock_takings", :force => true do |t|
t.date "date"
t.text "note"
t.integer "created_by"
t.datetime "created_at"
end

View File

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090120184410
# Schema version: 20090317175355
#
# Table name: articles
#

View File

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090119155930
# Schema version: 20090317175355
#
# Table name: orders
#
@ -11,6 +11,7 @@
# state :string(255) default("open")
# lock_version :integer default(0), not null
# updated_by_user_id :integer
# foodcoop_result :decimal(8, 2)
#
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

View File

@ -1,5 +1,5 @@
# == Schema Information
# Schema version: 20090120184410
# Schema version: 20090317175355
#
# Table name: stock_changes
#
@ -9,6 +9,7 @@
# stock_article_id :integer
# quantity :integer default(0)
# created_at :datetime
# stock_taking_id :integer
#
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

View File

@ -1,3 +1,14 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: stock_takings
#
# id :integer not null, primary key
# date :date
# note :text
# created_at :datetime
#
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:

View File

@ -1,24 +1,21 @@
# == Schema Information
# Schema version: 20090120184410
# Schema version: 20090317175355
#
# Table name: suppliers
#
# id :integer not null, primary key
# name :string(255) default(""), not null
# address :string(255) default(""), not null
# phone :string(255) default(""), not null
# phone2 :string(255)
# fax :string(255)
# email :string(255)
# url :string(255)
# contact_person :string(255)
# customer_number :string(255)
# delivery_days :string(255)
# order_howto :string(255)
# note :string(255)
# shared_supplier_id :integer
# min_order_quantity :string(255)
# deleted_at :datetime
# id :integer(4) not null, primary key
# name :string(255) not null
# address :string(255) not null
# phone :string(255) not null
# phone2 :string(255)
# fax :string(255)
# email :string(255)
# url :string(255)
# delivery_days :string(255)
# note :string(255)
# created_on :datetime
# updated_on :datetime
# lists :string(255)
#
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

View File

@ -1,3 +1,13 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: article_categories
#
# id :integer not null, primary key
# name :string(255) default(""), not null
# description :string(255)
#
require File.dirname(__FILE__) + '/../test_helper'
class ArticleCategoryTest < Test::Unit::TestCase

View File

@ -1,3 +1,17 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: article_prices
#
# id :integer not null, primary key
# article_id :integer
# price :decimal(8, 2) default(0.0), not null
# tax :decimal(8, 2) default(0.0), not null
# deposit :decimal(8, 2) default(0.0), not null
# unit_quantity :integer
# created_at :datetime
#
require 'test_helper'
class ArticlePriceTest < ActiveSupport::TestCase

View File

@ -1,3 +1,30 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: articles
#
# id :integer not null, primary key
# name :string(255) default(""), not null
# supplier_id :integer default(0), not null
# article_category_id :integer default(0), not null
# unit :string(255) default(""), not null
# note :string(255)
# availability :boolean default(TRUE), not null
# manufacturer :string(255)
# origin :string(255)
# shared_updated_on :datetime
# price :decimal(, )
# tax :float
# deposit :decimal(, ) default(0.0)
# unit_quantity :integer default(1), not null
# order_number :string(255)
# created_at :datetime
# updated_at :datetime
# quantity :integer default(0)
# deleted_at :datetime
# type :string(255)
#
require File.dirname(__FILE__) + '/../test_helper'
class ArticleTest < Test::Unit::TestCase

View File

@ -1,3 +1,14 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: assignments
#
# id :integer not null, primary key
# user_id :integer default(0), not null
# task_id :integer default(0), not null
# accepted :boolean
#
require File.dirname(__FILE__) + '/../test_helper'
class AssignmentTest < Test::Unit::TestCase

View File

@ -1,3 +1,16 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: financial_transactions
#
# id :integer not null, primary key
# ordergroup_id :integer default(0), not null
# amount :decimal(8, 2) default(0.0), not null
# note :text not null
# user_id :integer default(0), not null
# created_on :datetime not null
#
require File.dirname(__FILE__) + '/../test_helper'
class FinancialTransactionTest < Test::Unit::TestCase

View File

@ -1,3 +1,15 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: group_order_article_quantities
#
# id :integer not null, primary key
# group_order_article_id :integer default(0), not null
# quantity :integer default(0)
# tolerance :integer default(0)
# created_on :datetime not null
#
require File.dirname(__FILE__) + '/../test_helper'
class GroupOrderArticleQuantityTest < Test::Unit::TestCase

View File

@ -1,3 +1,31 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: groups
#
# id :integer not null, primary key
# type :string(255) default(""), not null
# name :string(255) default(""), not null
# description :string(255)
# account_balance :decimal(, ) default(0.0), not null
# account_updated :datetime
# created_on :datetime not null
# role_admin :boolean not null
# role_suppliers :boolean not null
# role_article_meta :boolean not null
# role_finance :boolean not null
# role_orders :boolean not null
# weekly_task :boolean
# weekday :integer
# task_name :string(255)
# task_description :string(255)
# task_required_users :integer default(1)
# deleted_at :datetime
# contact_person :string(255)
# contact_phone :string(255)
# contact_address :string(255)
#
require File.dirname(__FILE__) + '/../test_helper'
class GroupTest < Test::Unit::TestCase

View File

@ -1,3 +1,13 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: memberships
#
# id :integer not null, primary key
# group_id :integer default(0), not null
# user_id :integer default(0), not null
#
require File.dirname(__FILE__) + '/../test_helper'
class MembershipTest < Test::Unit::TestCase

View File

@ -1,3 +1,15 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: order_comments
#
# id :integer not null, primary key
# order_id :integer
# user_id :integer
# text :text
# created_at :datetime
#
require 'test_helper'
class OrderCommentTest < ActiveSupport::TestCase

View File

@ -1,3 +1,19 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: orders
#
# id :integer not null, primary key
# supplier_id :integer
# note :text
# starts :datetime
# ends :datetime
# state :string(255) default("open")
# lock_version :integer default(0), not null
# updated_by_user_id :integer
# foodcoop_result :decimal(8, 2)
#
require File.dirname(__FILE__) + '/../test_helper'
class OrderTest < Test::Unit::TestCase

View File

@ -1,3 +1,27 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: articles
#
# id :integer(4) not null, primary key
# name :string(255) not null
# supplier_id :integer(4) not null
# number :string(255)
# note :string(255)
# manufacturer :string(255)
# origin :string(255)
# unit :string(255)
# price :decimal(8, 2) default(0.0), not null
# tax :decimal(3, 1) default(7.0), not null
# deposit :decimal(8, 2) default(0.0), not null
# unit_quantity :decimal(4, 1) default(1.0), not null
# scale_quantity :decimal(4, 2)
# scale_price :decimal(8, 2)
# created_on :datetime
# updated_on :datetime
# list :string(255)
#
require File.dirname(__FILE__) + '/../test_helper'
class SharedArticleTest < Test::Unit::TestCase

View File

@ -1,3 +1,23 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: suppliers
#
# id :integer(4) not null, primary key
# name :string(255) not null
# address :string(255) not null
# phone :string(255) not null
# phone2 :string(255)
# fax :string(255)
# email :string(255)
# url :string(255)
# delivery_days :string(255)
# note :string(255)
# created_on :datetime
# updated_on :datetime
# lists :string(255)
#
require File.dirname(__FILE__) + '/../test_helper'
class SharedSupplierTest < Test::Unit::TestCase

View File

@ -1,3 +1,30 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: articles
#
# id :integer not null, primary key
# name :string(255) default(""), not null
# supplier_id :integer default(0), not null
# article_category_id :integer default(0), not null
# unit :string(255) default(""), not null
# note :string(255)
# availability :boolean default(TRUE), not null
# manufacturer :string(255)
# origin :string(255)
# shared_updated_on :datetime
# price :decimal(, )
# tax :float
# deposit :decimal(, ) default(0.0)
# unit_quantity :integer default(1), not null
# order_number :string(255)
# created_at :datetime
# updated_at :datetime
# quantity :integer default(0)
# deleted_at :datetime
# type :string(255)
#
require 'test_helper'
class StockArticleTest < ActiveSupport::TestCase

View File

@ -1,3 +1,17 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: stock_changes
#
# id :integer not null, primary key
# delivery_id :integer
# order_id :integer
# stock_article_id :integer
# quantity :integer default(0)
# created_at :datetime
# stock_taking_id :integer
#
require 'test_helper'
class StockChangeTest < ActiveSupport::TestCase

View File

@ -1,3 +1,14 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: stock_takings
#
# id :integer not null, primary key
# date :date
# note :text
# created_at :datetime
#
require 'test_helper'
class StockTakingTest < ActiveSupport::TestCase

View File

@ -1,3 +1,26 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: suppliers
#
# id :integer not null, primary key
# name :string(255) default(""), not null
# address :string(255) default(""), not null
# phone :string(255) default(""), not null
# phone2 :string(255)
# fax :string(255)
# email :string(255)
# url :string(255)
# contact_person :string(255)
# customer_number :string(255)
# delivery_days :string(255)
# order_howto :string(255)
# note :string(255)
# shared_supplier_id :integer
# min_order_quantity :string(255)
# deleted_at :datetime
#
require File.dirname(__FILE__) + '/../test_helper'
class SupplierTest < Test::Unit::TestCase

View File

@ -1,3 +1,20 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: tasks
#
# id :integer not null, primary key
# name :string(255) default(""), not null
# description :string(255)
# due_date :date
# done :boolean
# workgroup_id :integer
# assigned :boolean
# created_on :datetime not null
# updated_on :datetime not null
# required_users :integer default(1)
#
require File.dirname(__FILE__) + '/../test_helper'
class TaskTest < Test::Unit::TestCase

View File

@ -1,3 +1,22 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: users
#
# id :integer not null, primary key
# nick :string(255) default(""), not null
# password_hash :string(255) default(""), not null
# password_salt :string(255) default(""), not null
# first_name :string(255) default(""), not null
# last_name :string(255) default(""), not null
# email :string(255) default(""), not null
# phone :string(255)
# created_on :datetime not null
# reset_password_token :string(255)
# reset_password_expires :datetime
# last_login :datetime
#
require File.dirname(__FILE__) + '/../test_helper'
class UserTest < Test::Unit::TestCase

View File

@ -1,3 +1,31 @@
# == Schema Information
# Schema version: 20090317175355
#
# Table name: groups
#
# id :integer not null, primary key
# type :string(255) default(""), not null
# name :string(255) default(""), not null
# description :string(255)
# account_balance :decimal(, ) default(0.0), not null
# account_updated :datetime
# created_on :datetime not null
# role_admin :boolean not null
# role_suppliers :boolean not null
# role_article_meta :boolean not null
# role_finance :boolean not null
# role_orders :boolean not null
# weekly_task :boolean
# weekday :integer
# task_name :string(255)
# task_description :string(255)
# task_required_users :integer default(1)
# deleted_at :datetime
# contact_person :string(255)
# contact_phone :string(255)
# contact_address :string(255)
#
require 'test_helper'
class WorkgroupTest < ActiveSupport::TestCase