Second part of admin-namespace-reorder. Also renamed OrderGroup into Ordergroup. More view-sharing between groups is neccessary.

This commit is contained in:
Benjamin Meichsner 2009-01-14 12:46:01 +01:00
parent 2d5dc03b90
commit fadc951208
83 changed files with 410 additions and 518 deletions

View file

@ -1,23 +1,23 @@
# == Schema Information
# Schema version: 20090102171850
# Schema version: 20090114101610
#
# Table name: financial_transactions
#
# id :integer(4) not null, primary key
# order_group_id :integer(4) default(0), not null
# amount :decimal(8, 2) default(0.0), not null
# note :text default(""), not null
# user_id :integer(4) default(0), not null
# created_on :datetime not null
# id :integer(4) not null, primary key
# ordergroup_id :integer(4) default(0), not null
# amount :decimal(8, 2) default(0.0), not null
# note :text default(""), not null
# user_id :integer(4) default(0), not null
# created_on :datetime not null
#
# financial transactions are the foodcoop internal financial transactions
# only order_groups have an account balance and are happy to transfer money
# only ordergroups have an account balance and are happy to transfer money
class FinancialTransaction < ActiveRecord::Base
belongs_to :order_group
belongs_to :ordergroup
belongs_to :user
validates_presence_of :note, :user_id, :order_group_id
validates_presence_of :note, :user_id, :ordergroup_id
validates_numericality_of :amount
# Custom attribute setter that accepts decimal numbers using localized decimal separator.

View file

@ -41,11 +41,11 @@ class Group < ActiveRecord::Base
memberships.find_by_user_id(user.id)
end
# Returns all NONmembers and a checks for possible multiple OrderGroup-Memberships
# Returns all NONmembers and a checks for possible multiple Ordergroup-Memberships
def non_members
nonMembers = Array.new
for user in User.find(:all, :order => "nick")
unless self.users.include?(user) || ( self.is_a?(OrderGroup) && user.find_ordergroup )
unless self.users.include?(user) || ( self.is_a?(Ordergroup) && user.find_ordergroup )
nonMembers << user
end
end
@ -57,16 +57,16 @@ class Group < ActiveRecord::Base
raise ERR_LAST_ADMIN_GROUP_DELETE if self.role_admin == true && Group.find_all_by_role_admin(true).size == 1
end
# get all groups, which are NOT OrderGroups
# get all groups, which are NOT Ordergroups
def self.workgroups
Workgroup.all
end
protected
# validates uniqueness of the Group.name. Checks groups and order_groups
# validates uniqueness of the Group.name. Checks groups and ordergroups
def validate
errors.add(:name, "ist schon vergeben") if (group = Group.find_by_name(name) || group = OrderGroup.find_by_name(name)) && self != group
errors.add(:name, "ist schon vergeben") if (group = Group.find_by_name(name) || group = Ordergroup.find_by_name(name)) && self != group
end
# add validation check on update

View file

@ -1,10 +1,10 @@
# == Schema Information
# Schema version: 20090102171850
# Schema version: 20090114101610
#
# Table name: group_orders
#
# id :integer(4) not null, primary key
# order_group_id :integer(4) default(0), not null
# ordergroup_id :integer(4) default(0), not null
# order_id :integer(4) default(0), not null
# price :decimal(8, 2) default(0.0), not null
# lock_version :integer(4) default(0), not null
@ -12,21 +12,21 @@
# updated_by_user_id :integer(4) default(0), not null
#
# A GroupOrder represents an Order placed by an OrderGroup.
# A GroupOrder represents an Order placed by an Ordergroup.
class GroupOrder < ActiveRecord::Base
belongs_to :order
belongs_to :order_group
belongs_to :ordergroup
has_many :group_order_articles, :dependent => :destroy
has_many :order_articles, :through => :group_order_articles
has_many :group_order_article_results
belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by_user_id"
validates_presence_of :order_id
validates_presence_of :order_group_id
validates_presence_of :ordergroup_id
validates_presence_of :updated_by
validates_numericality_of :price
validates_uniqueness_of :order_group_id, :scope => :order_id # order groups can only order once per order
validates_uniqueness_of :ordergroup_id, :scope => :order_id # order groups can only order once per order
# Updates the "price" attribute.
# This will be the maximum value of a current order

View file

@ -12,7 +12,7 @@
#
# A GroupOrderArticle stores the sum of how many items of an OrderArticle are ordered as part of a GroupOrder.
# The chronologically order of the OrderGroup - activity are stored in GroupOrderArticleQuantity
# The chronologically order of the Ordergroup - activity are stored in GroupOrderArticleQuantity
#
class GroupOrderArticle < ActiveRecord::Base
@ -94,7 +94,7 @@ class GroupOrderArticle < ActiveRecord::Base
end
end
# Determines how many items of this article the OrderGroup receives.
# Determines how many items of this article the Ordergroup receives.
# Returns a hash with three keys: :quantity / :tolerance / :total
#
# See description of the ordering algorithm in the general application documentation for details.

View file

@ -9,13 +9,13 @@
# price :decimal(8, 2) default(0.0), not null
#
# OrderGroups, which participate on a specific order will have a line
# Ordergroups, which participate on a specific order will have a line
class GroupOrderResult < ActiveRecord::Base
belongs_to :order
has_many :group_order_article_results, :dependent => :destroy
# Calculates the Order-Price for the OrderGroup and updates the price-attribute
# Calculates the Order-Price for the Ordergroup and updates the price-attribute
def updatePrice
total = 0
group_order_article_results.each do |result|

View file

@ -95,7 +95,7 @@ class Message < ActiveRecord::Base
# Example:
# Message.from_template(
# 'order_finished',
# {:user => user, :group => order_group, :order => self, :results => results, :total => group_order.price},
# {:user => user, :group => ordergroup, :order => self, :results => results, :total => group_order.price},
# {:recipient_id => user.id, :recipients => recipients, :subject => "Bestellung beendet: #{self.name}"}
# ).save!
def self.from_template(template, vars, attributes)

View file

@ -24,7 +24,7 @@ class Order < ActiveRecord::Base
has_many :order_articles, :dependent => :destroy
has_many :articles, :through => :order_articles
has_many :group_orders, :dependent => :destroy
has_many :order_groups, :through => :group_orders
has_many :ordergroups, :through => :group_orders
has_many :order_article_results, :dependent => :destroy
has_many :group_order_results, :dependent => :destroy
belongs_to :supplier
@ -96,10 +96,10 @@ class Order < ActiveRecord::Base
find :all, :conditions => ['booked = ?', true], :order => 'ends desc', :include => :supplier
end
# search GroupOrder of given OrderGroup
# search GroupOrder of given Ordergroup
def group_order(ordergroup)
unless finished
return group_orders.detect {|o| o.order_group_id == ordergroup.id}
return group_orders.detect {|o| o.ordergroup_id == ordergroup.id}
else
return group_order_results.detect {|o| o.group_name == ordergroup.name}
end
@ -180,7 +180,7 @@ class Order < ActiveRecord::Base
#saves ordergroups, which take part in this order
self.group_orders.each do |go|
group_order_result = GroupOrderResult.create!(:order => self,
:group_name => go.order_group.name,
:group_name => go.ordergroup.name,
:price => go.price)
end
# saves every article of the order
@ -203,7 +203,7 @@ class Order < ActiveRecord::Base
oa.group_order_articles.each do |goa|
result = goa.orderResult
# find appropriate GroupOrderResult
group_order_result = GroupOrderResult.find(:first, :conditions => ['order_id = ? AND group_name = ?', self.id, goa.group_order.order_group.name])
group_order_result = GroupOrderResult.find(:first, :conditions => ['order_id = ? AND group_name = ?', self.id, goa.group_order.ordergroup.name])
group_order_article_result = GroupOrderArticleResult.new(:order_article_result => article_result,
:group_order_result => group_order_result,
:quantity => result[:total],
@ -274,15 +274,15 @@ class Order < ActiveRecord::Base
end
end
# Sets "booked"-attribute to true and updates all OrderGroup_account_balances
# 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
# 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)
Ordergroup.find_by_name(result.group_name).addFinancialTransaction(price, transaction_note, user)
end
self.booked = true
self.updated_by = user
@ -319,9 +319,9 @@ class Order < ActiveRecord::Base
def notifyOrderFinished
# Loop through GroupOrderResults for this order:
for group_order in self.group_order_results
order_group = OrderGroup.find_by_name(group_order.group_name)
ordergroup = Ordergroup.find_by_name(group_order.group_name)
# Determine group users that want a notification message:
users = order_group.users.reject{|u| u.settings["notify.orderFinished"] != '1'}
users = ordergroup.users.reject{|u| u.settings["notify.orderFinished"] != '1'}
unless (users.empty?)
# Assemble the order message text:
results = group_order.group_order_article_results.find(:all, :include => [:order_article_result])
@ -330,7 +330,7 @@ class Order < ActiveRecord::Base
for user in users
Message.from_template(
'order_finished',
{:user => user, :group => order_group, :order => self, :results => results, :total => group_order.price},
{:user => user, :group => ordergroup, :order => self, :results => results, :total => group_order.price},
{:recipient_id => user.id, :recipients => recipients, :subject => "Bestellung beendet: #{self.name}"}
).save!
end

View file

@ -23,13 +23,13 @@
# task_required_users :integer(4) default(1)
#
# OrderGroups can order, they are "children" of the class Group
# Ordergroups can order, they are "children" of the class Group
#
# OrderGroup have the following attributes, in addition to Group
# Ordergroup have the following attributes, in addition to Group
# * account_balance (decimal)
# * account_updated (datetime)
# * actual_size (int) : how many persons are ordering through the OrderGroup
class OrderGroup < Group
# * actual_size (int) : how many persons are ordering through the Ordergroup
class Ordergroup < Group
has_many :financial_transactions, :dependent => :destroy
has_many :group_orders, :dependent => :destroy
has_many :orders, :through => :group_orders
@ -44,9 +44,9 @@ class OrderGroup < Group
# messages
ERR_NAME_IS_USED_IN_ARCHIVE = "Der Name ist von einer ehemaligen Gruppe verwendet worden."
# if the order_group.name is changed, group_order_result.name has to be adapted
# if the ordergroup.name is changed, group_order_result.name has to be adapted
def before_update
ordergroup = OrderGroup.find(self.id)
ordergroup = Ordergroup.find(self.id)
unless (ordergroup.name == self.name) || ordergroup.group_order_results.empty?
# rename all finished orders
for result in ordergroup.group_order_results
@ -59,7 +59,7 @@ class OrderGroup < Group
# * excludeGroupOrder (GroupOrder): exclude this GroupOrder from the calculation
def getAvailableFunds(excludeGroupOrder = nil)
funds = account_balance
for order in GroupOrder.find_all_by_order_group_id(self.id)
for order in GroupOrder.find_all_by_ordergroup_id(self.id)
unless order == excludeGroupOrder
funds -= order.price
end
@ -70,11 +70,11 @@ class OrderGroup < Group
return funds
end
# Creates a new FinancialTransaction for this OrderGroup and updates the account_balance accordingly.
# Creates a new FinancialTransaction for this Ordergroup and updates the account_balance accordingly.
# Throws an exception if it fails.
def addFinancialTransaction(amount, note, user)
transaction do
trans = FinancialTransaction.new(:order_group => self, :amount => amount, :note => note, :user => user)
trans = FinancialTransaction.new(:ordergroup => self, :amount => amount, :note => note, :user => user)
trans.save!
self.account_balance += trans.amount
self.account_updated = trans.created_on
@ -137,7 +137,7 @@ class OrderGroup < Group
errors.add(:name, ERR_NAME_IS_USED_IN_ARCHIVE) unless GroupOrderResult.find_all_by_group_name(self.name).empty?
end
def validate_on_update
ordergroup = OrderGroup.find(self.id)
ordergroup = Ordergroup.find(self.id)
errors.add(:name, ERR_NAME_IS_USED_IN_ARCHIVE) unless ordergroup.name == self.name || GroupOrderResult.find_all_by_group_name(self.name).empty?
end

View file

@ -23,7 +23,7 @@ require 'digest/sha1'
class User < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
has_many :order_groups, :through => :memberships, :source => :group
has_many :ordergroups, :through => :memberships, :source => :group
has_many :assignments, :dependent => :destroy
has_many :tasks, :through => :assignments
@ -146,10 +146,10 @@ class User < ActiveRecord::Base
groups.detect {|group| group.role_orders?}
end
# Returns the user's OrderGroup or nil if none found.
# Returns the user's Ordergroup or nil if none found.
def find_ordergroup
order_groups.first
#groups.find(:first, :conditions => "type = 'OrderGroup'")
ordergroups.first
#groups.find(:first, :conditions => "type = 'Ordergroup'")
end
# Find all tasks, for which the current user should be responsible
@ -195,7 +195,7 @@ class User < ActiveRecord::Base
return true if group.users.detect {|user| user == self}
end
#Returns an array with the users groups (but without the OrderGroups -> because tpye=>"")
#Returns an array with the users groups (but without the Ordergroups -> because tpye=>"")
def member_of_groups()
self.groups.find(:all, :conditions => {:type => ""})
end