2009-01-06 11:49:19 +01:00
|
|
|
# A GroupOrderArticle stores the sum of how many items of an OrderArticle are ordered as part of a GroupOrder.
|
2009-01-14 12:46:01 +01:00
|
|
|
# The chronologically order of the Ordergroup - activity are stored in GroupOrderArticleQuantity
|
2009-01-06 11:49:19 +01:00
|
|
|
#
|
|
|
|
class GroupOrderArticle < ActiveRecord::Base
|
2009-02-03 21:14:48 +01:00
|
|
|
extend ActiveSupport::Memoizable # Ability to cache method results. Use memoize :expensive_method
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
belongs_to :group_order
|
|
|
|
belongs_to :order_article
|
|
|
|
has_many :group_order_article_quantities, :dependent => :destroy
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
validates_presence_of :group_order_id, :order_article_id
|
2009-01-06 11:49:19 +01:00
|
|
|
validates_inclusion_of :quantity, :in => 0..99
|
2009-03-20 18:56:17 +01:00
|
|
|
validates_inclusion_of :result, :in => 0..99, :allow_nil => true
|
2009-01-06 11:49:19 +01:00
|
|
|
validates_inclusion_of :tolerance, :in => 0..99
|
|
|
|
validates_uniqueness_of :order_article_id, :scope => :group_order_id # just once an article per group order
|
|
|
|
|
2011-05-11 12:21:21 +02:00
|
|
|
scope :ordered, :conditions => 'result > 0'
|
2009-04-05 15:23:21 +02:00
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
localize_input_of :result
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Updates the quantity/tolerance for this GroupOrderArticle by updating both GroupOrderArticle properties
|
|
|
|
# and the associated GroupOrderArticleQuantities chronologically.
|
|
|
|
#
|
|
|
|
# See description of the ordering algorithm in the general application documentation for details.
|
2009-01-29 01:57:51 +01:00
|
|
|
def update_quantities(quantity, tolerance)
|
|
|
|
logger.debug("GroupOrderArticle[#{id}].update_quantities(#{quantity}, #{tolerance})")
|
2009-01-06 11:49:19 +01:00
|
|
|
logger.debug("Current quantity = #{self.quantity}, tolerance = #{self.tolerance}")
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Get quantities ordered with the newest item first.
|
|
|
|
quantities = group_order_article_quantities.find(:all, :order => 'created_on desc')
|
|
|
|
logger.debug("GroupOrderArticleQuantity items found: #{quantities.size}")
|
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
if (quantities.size == 0)
|
2009-01-06 11:49:19 +01:00
|
|
|
# There is no GroupOrderArticleQuantity item yet, just insert with desired quantities...
|
|
|
|
logger.debug("No quantities entry at all, inserting a new one with the desired quantities")
|
|
|
|
quantities.push(GroupOrderArticleQuantity.new(:group_order_article => self, :quantity => quantity, :tolerance => tolerance))
|
2011-06-19 15:30:33 +02:00
|
|
|
self.quantity, self.tolerance = quantity, tolerance
|
|
|
|
else
|
2009-01-06 11:49:19 +01:00
|
|
|
# Decrease quantity/tolerance if necessary by going through the existing items and decreasing their values...
|
|
|
|
i = 0
|
|
|
|
while (i < quantities.size && (quantity < self.quantity || tolerance < self.tolerance))
|
|
|
|
logger.debug("Need to decrease quantities for GroupOrderArticleQuantity[#{quantities[i].id}]")
|
|
|
|
if (quantity < self.quantity && quantities[i].quantity > 0)
|
|
|
|
delta = self.quantity - quantity
|
|
|
|
delta = (delta > quantities[i].quantity ? quantities[i].quantity : delta)
|
|
|
|
logger.debug("Decreasing quantity by #{delta}")
|
|
|
|
quantities[i].quantity -= delta
|
2011-06-19 15:30:33 +02:00
|
|
|
self.quantity -= delta
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
if (tolerance < self.tolerance && quantities[i].tolerance > 0)
|
|
|
|
delta = self.tolerance - tolerance
|
|
|
|
delta = (delta > quantities[i].tolerance ? quantities[i].tolerance : delta)
|
|
|
|
logger.debug("Decreasing tolerance by #{delta}")
|
|
|
|
quantities[i].tolerance -= delta
|
2011-06-19 15:30:33 +02:00
|
|
|
self.tolerance -= delta
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
i += 1
|
2011-06-19 15:30:33 +02:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
# If there is at least one increased value: insert a new GroupOrderArticleQuantity object
|
|
|
|
if (quantity > self.quantity || tolerance > self.tolerance)
|
|
|
|
logger.debug("Inserting a new GroupOrderArticleQuantity")
|
|
|
|
quantities.insert(0, GroupOrderArticleQuantity.new(
|
2011-06-19 15:30:33 +02:00
|
|
|
:group_order_article => self,
|
|
|
|
:quantity => (quantity > self.quantity ? quantity - self.quantity : 0),
|
|
|
|
:tolerance => (tolerance > self.tolerance ? tolerance - self.tolerance : 0)
|
2009-01-06 11:49:19 +01:00
|
|
|
))
|
|
|
|
# Recalc totals:
|
|
|
|
self.quantity += quantities[0].quantity
|
2011-06-19 15:30:33 +02:00
|
|
|
self.tolerance += quantities[0].tolerance
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Check if something went terribly wrong and quantites have not been adjusted as desired.
|
|
|
|
if (self.quantity != quantity || self.tolerance != tolerance)
|
|
|
|
raise 'Invalid state: unable to update GroupOrderArticle/-Quantities to desired quantities!'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove zero-only items.
|
|
|
|
quantities = quantities.reject { | q | q.quantity == 0 && q.tolerance == 0}
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Save
|
|
|
|
transaction do
|
|
|
|
quantities.each { | i | i.save! }
|
|
|
|
self.group_order_article_quantities = quantities
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
end
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# Determines how many items of this article the Ordergroup receives.
|
2009-01-06 11:49:19 +01:00
|
|
|
# Returns a hash with three keys: :quantity / :tolerance / :total
|
|
|
|
#
|
|
|
|
# See description of the ordering algorithm in the general application documentation for details.
|
2009-02-03 21:14:48 +01:00
|
|
|
def calculate_result
|
2009-01-06 11:49:19 +01:00
|
|
|
quantity = tolerance = 0
|
2009-02-06 16:26:35 +01:00
|
|
|
stockit = order_article.article.is_a?(StockArticle)
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Get total
|
2009-02-06 16:26:35 +01:00
|
|
|
total = stockit ? order_article.article.quantity : order_article.units_to_order * order_article.price.unit_quantity
|
2009-02-03 21:14:48 +01:00
|
|
|
logger.debug("<#{order_article.article.name}>.unitsToOrder => items ordered: #{order_article.units_to_order} => #{total}")
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
if (total > 0)
|
2009-02-03 21:14:48 +01:00
|
|
|
# In total there are enough units ordered. Now check the individual result for the ordergroup (group_order).
|
|
|
|
#
|
2009-01-06 11:49:19 +01:00
|
|
|
# Get all GroupOrderArticleQuantities for this OrderArticle...
|
2009-02-03 21:14:48 +01:00
|
|
|
order_quantities = GroupOrderArticleQuantity.all(
|
2011-06-19 15:30:33 +02:00
|
|
|
:conditions => ["group_order_article_id IN (?)", order_article.group_order_article_ids], :order => 'created_on')
|
2009-02-03 21:14:48 +01:00
|
|
|
logger.debug("GroupOrderArticleQuantity records found: #{order_quantities.size}")
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Determine quantities to be ordered...
|
2009-02-03 21:14:48 +01:00
|
|
|
total_quantity = i = 0
|
|
|
|
while (i < order_quantities.size && total_quantity < total)
|
|
|
|
q = (order_quantities[i].quantity <= total - total_quantity ? order_quantities[i].quantity : total - total_quantity)
|
|
|
|
total_quantity += q
|
|
|
|
if (order_quantities[i].group_order_article_id == self.id)
|
2009-01-06 11:49:19 +01:00
|
|
|
logger.debug("increasing quantity by #{q}")
|
|
|
|
quantity += q
|
|
|
|
end
|
|
|
|
i += 1
|
|
|
|
end
|
2009-02-06 16:26:35 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Determine tolerance to be ordered...
|
2009-02-03 21:14:48 +01:00
|
|
|
if (total_quantity < total)
|
2009-01-06 11:49:19 +01:00
|
|
|
logger.debug("determining additional items to be ordered from tolerance")
|
|
|
|
i = 0
|
2009-02-03 21:14:48 +01:00
|
|
|
while (i < order_quantities.size && total_quantity < total)
|
|
|
|
q = (order_quantities[i].tolerance <= total - total_quantity ? order_quantities[i].tolerance : total - total_quantity)
|
|
|
|
total_quantity += q
|
|
|
|
if (order_quantities[i].group_order_article_id == self.id)
|
2009-01-06 11:49:19 +01:00
|
|
|
logger.debug("increasing tolerance by #{q}")
|
|
|
|
tolerance += q
|
|
|
|
end
|
|
|
|
i += 1
|
2009-02-06 16:26:35 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2009-02-03 21:14:48 +01:00
|
|
|
logger.debug("determined quantity/tolerance/total: #{quantity} / #{tolerance} / #{quantity + tolerance}")
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2009-02-03 21:14:48 +01:00
|
|
|
{:quantity => quantity, :tolerance => tolerance, :total => quantity + tolerance}
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-02-03 21:14:48 +01:00
|
|
|
memoize :calculate_result
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2009-02-03 21:14:48 +01:00
|
|
|
# Returns order result,
|
2009-02-05 16:40:02 +01:00
|
|
|
# either calcualted on the fly or fetched from result attribute
|
|
|
|
# Result is set when finishing the order.
|
2009-02-04 16:41:01 +01:00
|
|
|
def result(type = :total)
|
|
|
|
self[:result] || calculate_result[type]
|
2009-02-03 21:14:48 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 16:41:01 +01:00
|
|
|
# This is used during order.finish!.
|
2009-02-03 21:14:48 +01:00
|
|
|
def save_results!
|
2009-02-04 16:41:01 +01:00
|
|
|
self.update_attribute(:result, calculate_result[:total])
|
2009-02-03 21:14:48 +01:00
|
|
|
end
|
2011-06-19 15:30:33 +02:00
|
|
|
|
|
|
|
# Returns total price for this individual article
|
|
|
|
# Until the order is finished this will be the maximum price or
|
|
|
|
# the minimum price depending on configuration. When the order is finished it
|
|
|
|
# will be the value depending of the article results.
|
|
|
|
def total_price
|
|
|
|
unless order_article.order.finished?
|
|
|
|
if Foodsoft.config[:tolerance_is_costly]
|
|
|
|
order_article.article.fc_price * (quantity + tolerance)
|
|
|
|
else
|
|
|
|
order_article.article.fc_price * quantity
|
|
|
|
end
|
|
|
|
else
|
|
|
|
order_article.price.fc_price * result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|
2011-05-07 21:55:24 +02:00
|
|
|
|
2011-05-07 20:50:39 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: group_order_articles
|
|
|
|
#
|
2011-05-07 21:55:24 +02:00
|
|
|
# id :integer(4) not null, primary key
|
|
|
|
# group_order_id :integer(4) default(0), not null
|
|
|
|
# order_article_id :integer(4) default(0), not null
|
|
|
|
# quantity :integer(4) default(0), not null
|
|
|
|
# tolerance :integer(4) default(0), not null
|
2011-05-07 20:50:39 +02:00
|
|
|
# updated_on :datetime not null
|
|
|
|
# result :decimal(8, 3)
|
|
|
|
#
|
|
|
|
|