2009-01-08 16:33:27 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: group_orders
|
|
|
|
#
|
2009-08-01 13:41:22 +02:00
|
|
|
# id :integer(4) not null, primary key
|
|
|
|
# ordergroup_id :integer(4) default(0), not null
|
|
|
|
# order_id :integer(4) default(0), not null
|
2009-01-08 16:33:27 +01:00
|
|
|
# price :decimal(8, 2) default(0.0), not null
|
2009-08-01 13:41:22 +02:00
|
|
|
# lock_version :integer(4) default(0), not null
|
2009-01-08 16:33:27 +01:00
|
|
|
# updated_on :datetime not null
|
2009-08-01 13:41:22 +02:00
|
|
|
# updated_by_user_id :integer(4)
|
2009-01-06 11:49:19 +01:00
|
|
|
#
|
2009-01-08 16:33:27 +01:00
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# A GroupOrder represents an Order placed by an Ordergroup.
|
2009-01-06 11:49:19 +01:00
|
|
|
class GroupOrder < ActiveRecord::Base
|
|
|
|
|
|
|
|
belongs_to :order
|
2009-01-14 12:46:01 +01:00
|
|
|
belongs_to :ordergroup
|
2009-01-06 11:49:19 +01:00
|
|
|
has_many :group_order_articles, :dependent => :destroy
|
|
|
|
has_many :order_articles, :through => :group_order_articles
|
|
|
|
belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by_user_id"
|
|
|
|
|
|
|
|
validates_presence_of :order_id
|
2009-01-14 12:46:01 +01:00
|
|
|
validates_presence_of :ordergroup_id
|
2009-01-06 11:49:19 +01:00
|
|
|
validates_numericality_of :price
|
2009-01-14 12:46:01 +01:00
|
|
|
validates_uniqueness_of :ordergroup_id, :scope => :order_id # order groups can only order once per order
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2009-02-05 16:40:02 +01:00
|
|
|
named_scope :open, lambda { {:conditions => ["order_id IN (?)", Order.open.collect(&:id)]} }
|
2009-03-13 00:06:20 +01:00
|
|
|
named_scope :finished, lambda { {:conditions => ["order_id IN (?)", Order.finished_not_closed.collect(&:id)]} }
|
2009-01-29 01:57:51 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Updates the "price" attribute.
|
2009-03-11 16:34:36 +01:00
|
|
|
# This will be the maximum value of an open order or
|
|
|
|
# the value depending of the article results.
|
2009-01-29 01:57:51 +01:00
|
|
|
def update_price!
|
2009-01-06 11:49:19 +01:00
|
|
|
total = 0
|
|
|
|
for article in group_order_articles.find(:all, :include => :order_article)
|
2009-03-11 16:34:36 +01:00
|
|
|
unless order.finished?
|
|
|
|
total += article.order_article.article.fc_price * (article.quantity + article.tolerance)
|
|
|
|
else
|
|
|
|
total += article.order_article.price.fc_price * article.result
|
|
|
|
end
|
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
update_attribute(:price, total)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|