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) if !(user = current_user)
# No user at all: redirect to login page. # No user at all: redirect to login page.
self.return_to = request.request_uri self.return_to = request.request_uri
redirect_to :controller => 'login' redirect_to :controller => '/login'
return false return false
else else
# We have an authenticated user, now check role... # We have an authenticated user, now check role...

View file

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

View file

@ -1,5 +1,5 @@
# == Schema Information # == Schema Information
# Schema version: 20090120184410 # Schema version: 20090317175355
# #
# Table name: stock_changes # Table name: stock_changes
# #
@ -9,6 +9,7 @@
# stock_article_id :integer # stock_article_id :integer
# quantity :integer default(0) # quantity :integer default(0)
# created_at :datetime # created_at :datetime
# stock_taking_id :integer
# #
class StockChange < ActiveRecord::Base 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 class StockTaking < ActiveRecord::Base
has_many :stock_changes, :dependent => :destroy has_many :stock_changes, :dependent => :destroy

View file

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

View file

@ -20,7 +20,7 @@
%tr{:class => cycle("even","odd", :name => "order")} %tr{:class => cycle("even","odd", :name => "order")}
%td= link_to truncate(order.name), :action => "new", :id => order %td= link_to truncate(order.name), :action => "new", :id => order
%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.foodcoop_result})" : "beendet"
%td= order.updated_by.nil? ? '??' : order.updated_by.nick %td= order.updated_by.nil? ? '??' : order.updated_by.nick
%td %td
- unless order.closed? - 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. # 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| create_table "article_categories", :force => true do |t|
t.string "name", :default => "", :null => false t.string "name", :default => "", :null => false
@ -208,6 +208,7 @@ ActiveRecord::Schema.define(:version => 20090119155930) do
t.string "state", :default => "open" t.string "state", :default => "open"
t.integer "lock_version", :default => 0, :null => false t.integer "lock_version", :default => 0, :null => false
t.integer "updated_by_user_id" t.integer "updated_by_user_id"
t.decimal "foodcoop_result", :precision => 8, :scale => 2
end end
create_table "stock_changes", :force => true do |t| 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 "stock_article_id"
t.integer "quantity", :default => 0 t.integer "quantity", :default => 0
t.datetime "created_at" t.datetime "created_at"
t.integer "stock_taking_id"
end end
create_table "stock_takings", :force => true do |t| create_table "stock_takings", :force => true do |t|
t.date "date" t.date "date"
t.text "note" t.text "note"
t.integer "created_by"
t.datetime "created_at" t.datetime "created_at"
end end

View file

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

View file

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

View file

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

View file

@ -1,24 +1,21 @@
# == Schema Information # == Schema Information
# Schema version: 20090120184410 # Schema version: 20090317175355
# #
# Table name: suppliers # Table name: suppliers
# #
# id :integer not null, primary key # id :integer(4) not null, primary key
# name :string(255) default(""), not null # name :string(255) not null
# address :string(255) default(""), not null # address :string(255) not null
# phone :string(255) default(""), not null # phone :string(255) not null
# phone2 :string(255) # phone2 :string(255)
# fax :string(255) # fax :string(255)
# email :string(255) # email :string(255)
# url :string(255) # url :string(255)
# contact_person :string(255)
# customer_number :string(255)
# delivery_days :string(255) # delivery_days :string(255)
# order_howto :string(255)
# note :string(255) # note :string(255)
# shared_supplier_id :integer # created_on :datetime
# min_order_quantity :string(255) # updated_on :datetime
# deleted_at :datetime # lists :string(255)
# #
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html # 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' require File.dirname(__FILE__) + '/../test_helper'
class ArticleCategoryTest < Test::Unit::TestCase 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' require 'test_helper'
class ArticlePriceTest < ActiveSupport::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class ArticleTest < Test::Unit::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class AssignmentTest < Test::Unit::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class FinancialTransactionTest < Test::Unit::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class GroupOrderArticleQuantityTest < Test::Unit::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class GroupTest < Test::Unit::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class MembershipTest < Test::Unit::TestCase 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' require 'test_helper'
class OrderCommentTest < ActiveSupport::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class OrderTest < Test::Unit::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class SharedArticleTest < Test::Unit::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class SharedSupplierTest < Test::Unit::TestCase 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' require 'test_helper'
class StockArticleTest < ActiveSupport::TestCase 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' require 'test_helper'
class StockChangeTest < ActiveSupport::TestCase 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' require 'test_helper'
class StockTakingTest < ActiveSupport::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class SupplierTest < Test::Unit::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class TaskTest < Test::Unit::TestCase 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' require File.dirname(__FILE__) + '/../test_helper'
class UserTest < Test::Unit::TestCase 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' require 'test_helper'
class WorkgroupTest < ActiveSupport::TestCase class WorkgroupTest < ActiveSupport::TestCase