2009-01-08 16:33:27 +01:00
# == Schema Information
2009-01-29 21:28:22 +01:00
# Schema version: 20090120184410
2009-01-08 16:33:27 +01:00
#
# Table name: groups
#
2009-02-02 16:35:43 +01:00
# id :integer not null, primary key
2009-01-08 16:33:27 +01:00
# type :string(255) default(""), not null
# name :string(255) default(""), not null
# description :string(255)
2009-02-02 16:35:43 +01:00
# account_balance :decimal(, ) default(0.0), not null
2009-01-08 16:33:27 +01:00
# account_updated :datetime
# created_on :datetime not null
2009-02-02 16:35:43 +01:00
# 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
2009-01-08 16:33:27 +01:00
# task_name :string(255)
# task_description :string(255)
2009-02-02 16:35:43 +01:00
# task_required_users :integer default(1)
2009-01-29 21:28:22 +01:00
# deleted_at :datetime
2009-02-02 16:35:43 +01:00
# contact_person :string(255)
# contact_phone :string(255)
# contact_address :string(255)
2009-01-08 16:33:27 +01:00
#
2009-01-14 12:46:01 +01:00
# Ordergroups can order, they are "children" of the class Group
2009-01-06 11:49:19 +01:00
#
2009-01-14 12:46:01 +01:00
# Ordergroup have the following attributes, in addition to Group
2009-01-06 11:49:19 +01:00
# * account_balance (decimal)
# * account_updated (datetime)
2009-01-14 12:46:01 +01:00
class Ordergroup < Group
2009-01-29 01:57:51 +01:00
acts_as_paranoid # Avoid deleting the ordergroup for consistency of order-results
extend ActiveSupport :: Memoizable # Ability to cache method results. Use memoize :expensive_method
2009-02-03 21:14:48 +01:00
has_many :financial_transactions , :order = > " created_on DESC "
2009-01-29 01:57:51 +01:00
has_many :group_orders
2009-01-06 11:49:19 +01:00
has_many :orders , :through = > :group_orders
validates_numericality_of :account_balance , :message = > 'ist keine gültige Zahl'
2009-01-29 01:57:51 +01:00
2009-02-02 16:35:43 +01:00
def contact
" #{ contact_phone } ( #{ contact_person } ) "
end
2009-02-01 20:56:23 +01:00
def non_members
User . all ( :order = > 'nick' ) . reject { | u | ( users . include? ( u ) || u . ordergroup ) }
end
2009-01-29 01:57:51 +01:00
def value_of_open_orders ( exclude = nil )
group_orders . open . reject { | go | go == exclude } . collect ( & :price ) . sum
2009-01-06 11:49:19 +01:00
end
2009-01-29 01:57:51 +01:00
def value_of_finished_orders ( exclude = nil )
group_orders . finished . reject { | go | go == exclude } . collect ( & :price ) . sum
end
# Returns the available funds for this order group (the account_balance minus price of all non-closed GroupOrders of this group).
# * exclude (GroupOrder): exclude this GroupOrder from the calculation
def get_available_funds ( exclude = nil )
account_balance - value_of_open_orders ( exclude ) - value_of_finished_orders ( exclude )
end
memoize :get_available_funds
2009-01-14 12:46:01 +01:00
# Creates a new FinancialTransaction for this Ordergroup and updates the account_balance accordingly.
2009-01-06 11:49:19 +01:00
# Throws an exception if it fails.
def addFinancialTransaction ( amount , note , user )
transaction do
2009-01-14 12:46:01 +01:00
trans = FinancialTransaction . new ( :ordergroup = > self , :amount = > amount , :note = > note , :user = > user )
2009-01-06 11:49:19 +01:00
trans . save!
self . account_balance += trans . amount
self . account_updated = trans . created_on
save!
notifyNegativeBalance ( trans )
end
end
# Returns all GroupOrders by this group that are currently running.
def findCurrent
group_orders . find ( :all , :conditions = > [ " orders.finished = ? AND orders.starts < ? AND (orders.ends IS NULL OR orders.ends > ?) " , false , Time . now , Time . now ] , :include = > :order )
end
#find expired (lapsed) but not manually finished orders
def findExpiredOrders
group_orders . find ( :all , :conditions = > [ " orders.ends < ? " , Time . now ] , :include = > :order , :order = > 'orders.ends DESC' )
end
# Returns all GroupOrderResults by this group that are finished but not booked yet.
def findFinishedNotBooked
GroupOrderResult . find ( :all ,
:conditions = > [ " group_order_results.group_name = ? AND group_order_results.order_id = orders.id AND orders.finished = ? AND orders.booked = ? " , self . name , true , false ] ,
:include = > :order ,
:order = > 'orders.ends DESC' )
end
# Returns all GroupOrderResults for booked orders
def findBookedOrders ( limit = false , offset = 0 )
GroupOrderResult . find ( :all ,
:conditions = > [ " group_order_results.group_name = ? AND group_order_results.order_id = orders.id AND orders.finished = ? AND orders.booked = ? " , self . name , true , true ] ,
:include = > :order ,
:order = > " orders.ends DESC " ,
:limit = > limit ,
:offset = > offset )
end
private
# If this order group's account balance is made negative by the given/last transaction,
# a message is sent to all users who have enabled notification.
def notifyNegativeBalance ( transaction )
# Notify only when order group had a positive balance before the last transaction:
if ( transaction . amount < 0 && self . account_balance < 0 && self . account_balance - transaction . amount > = 0 )
2009-01-15 18:26:37 +01:00
users = self . users . reject { | u | u . settings [ " notify.negativeBalance " ] != '1' }
unless users . empty?
Message . from_template (
'negative_balance' ,
{ :group = > self , :transaction = > transaction } ,
{ :recipients_ids = > users . collect ( & :id ) , :subject = > " Gruppenkonto im Minus " }
) . save!
2009-01-06 11:49:19 +01:00
end
2009-01-15 20:10:50 +01:00
end
2009-01-06 11:49:19 +01:00
end
end