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
|
|
|
#
|
2019-01-13 07:05:54 +01:00
|
|
|
class GroupOrderArticle < ApplicationRecord
|
2022-02-03 08:37:38 +01:00
|
|
|
include LocalizeInput
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
belongs_to :group_order
|
|
|
|
belongs_to :order_article
|
2018-10-13 16:27:24 +02:00
|
|
|
has_many :group_order_article_quantities, dependent: :destroy
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
validates :group_order, :order_article, presence: true
|
|
|
|
validates :order_article_id, uniqueness: { scope: :group_order_id } # just once an article per group order
|
2019-03-26 20:14:39 +01:00
|
|
|
validate :check_order_not_closed # don't allow changes to closed (aka settled) orders
|
2018-10-13 16:27:24 +02:00
|
|
|
validates :quantity, :tolerance, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
scope :ordered, -> { includes(group_order: :ordergroup).order('groups.name') }
|
2009-04-05 15:23:21 +02:00
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
localize_input_of :result
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
def self.ransackable_attributes(_auth_object = nil)
|
|
|
|
%w[id quantity tolerance result]
|
2018-10-13 16:27:24 +02:00
|
|
|
end
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
def self.ransackable_associations(_auth_object = nil)
|
|
|
|
%w[order_article group_order]
|
2018-10-13 16:27:24 +02:00
|
|
|
end
|
|
|
|
|
2012-07-27 18:03:46 +02:00
|
|
|
# Setter used in group_order_article#new
|
|
|
|
# We have to create an group_order, if the ordergroup wasn't involved in the order yet
|
|
|
|
def ordergroup_id=(id)
|
2014-02-20 15:04:53 +01:00
|
|
|
self.group_order = GroupOrder.where(order_id: order_article.order_id, ordergroup_id: id).first_or_initialize
|
2012-07-27 18:03:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def ordergroup_id
|
2023-05-12 13:01:12 +02:00
|
|
|
group_order&.ordergroup_id
|
2012-07-27 18:03:46 +02:00
|
|
|
end
|
|
|
|
|
2015-09-23 22:38:20 +02:00
|
|
|
# Updates the quantity/tolerance for this GroupOrderArticle by updating both GroupOrderArticle properties
|
2009-01-06 11:49:19 +01:00
|
|
|
# and the associated GroupOrderArticleQuantities chronologically.
|
2015-09-23 22:38:20 +02:00
|
|
|
#
|
2009-01-06 11:49:19 +01:00
|
|
|
# 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
|
|
|
|
2014-03-18 17:29:40 +01:00
|
|
|
# When quantity and tolerance are zero, we don't serve any purpose
|
2015-01-14 21:15:08 +01:00
|
|
|
if quantity == 0 && tolerance == 0
|
2023-05-12 13:01:12 +02:00
|
|
|
logger.debug('Self-destructing since requested quantity and tolerance are zero')
|
2014-03-18 17:29:40 +01:00
|
|
|
destroy!
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Get quantities ordered with the newest item first.
|
2014-02-20 15:04:53 +01:00
|
|
|
quantities = group_order_article_quantities.order('created_on DESC').to_a
|
2009-01-06 11:49:19 +01:00
|
|
|
logger.debug("GroupOrderArticleQuantity items found: #{quantities.size}")
|
|
|
|
|
2023-05-12 13:01:12 +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...
|
2023-05-12 13:01:12 +02:00
|
|
|
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))
|
|
|
|
self.quantity = quantity
|
|
|
|
self.tolerance = tolerance
|
2011-06-19 15:30:33 +02:00
|
|
|
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
|
2023-05-12 13:01:12 +02:00
|
|
|
while i < quantities.size && (quantity < self.quantity || tolerance < self.tolerance)
|
2009-01-06 11:49:19 +01:00
|
|
|
logger.debug("Need to decrease quantities for GroupOrderArticleQuantity[#{quantities[i].id}]")
|
2023-05-12 13:01:12 +02:00
|
|
|
if quantity < self.quantity && quantities[i].quantity > 0
|
2009-01-06 11:49:19 +01:00
|
|
|
delta = self.quantity - quantity
|
2023-05-12 13:01:12 +02:00
|
|
|
delta = [delta, quantities[i].quantity].min
|
2009-01-06 11:49:19 +01:00
|
|
|
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
|
2023-05-12 13:01:12 +02:00
|
|
|
if tolerance < self.tolerance && quantities[i].tolerance > 0
|
2009-01-06 11:49:19 +01:00
|
|
|
delta = self.tolerance - tolerance
|
2023-05-12 13:01:12 +02:00
|
|
|
delta = [delta, quantities[i].tolerance].min
|
2009-01-06 11:49:19 +01:00
|
|
|
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
|
2023-05-12 13:01:12 +02:00
|
|
|
if quantity > self.quantity || tolerance > self.tolerance
|
|
|
|
logger.debug('Inserting a new GroupOrderArticleQuantity')
|
2009-01-06 11:49:19 +01:00
|
|
|
quantities.insert(0, GroupOrderArticleQuantity.new(
|
2023-05-12 13:01:12 +02:00
|
|
|
group_order_article: self,
|
|
|
|
quantity: (quantity > self.quantity ? quantity - self.quantity : 0),
|
|
|
|
tolerance: (tolerance > self.tolerance ? tolerance - self.tolerance : 0)
|
2021-03-01 15:27:26 +01:00
|
|
|
))
|
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.
|
2023-05-12 13:01:12 +02:00
|
|
|
if self.quantity != quantity || self.tolerance != tolerance
|
|
|
|
raise ActiveRecord::RecordNotSaved.new('Unable to update GroupOrderArticle/-Quantities to desired quantities!',
|
|
|
|
self)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Remove zero-only items.
|
2021-03-01 15:27:26 +01:00
|
|
|
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
|
2021-03-01 15:27:26 +01:00
|
|
|
quantities.each { |i| i.save! }
|
2009-01-06 11:49:19 +01:00
|
|
|
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
|
2015-09-23 22:38:20 +02:00
|
|
|
#
|
2009-01-06 11:49:19 +01:00
|
|
|
# See description of the ordering algorithm in the general application documentation for details.
|
2013-11-25 13:48:54 +01:00
|
|
|
def calculate_result(total = nil)
|
|
|
|
# return memoized result unless a total is given
|
2015-01-14 21:15:08 +01:00
|
|
|
return @calculate_result if total.nil? && !@calculate_result.nil?
|
2013-11-25 13:48:54 +01:00
|
|
|
|
|
|
|
quantity = tolerance = total_quantity = 0
|
|
|
|
|
|
|
|
# Get total
|
2023-05-12 13:01:12 +02:00
|
|
|
if !total.nil?
|
2013-11-25 13:48:54 +01:00
|
|
|
logger.debug "<#{order_article.article.name}> => #{total} (given)"
|
|
|
|
elsif order_article.article.is_a?(StockArticle)
|
|
|
|
total = order_article.article.quantity
|
|
|
|
logger.debug "<#{order_article.article.name}> (stock) => #{total}"
|
|
|
|
else
|
|
|
|
total = order_article.units_to_order * order_article.price.unit_quantity
|
|
|
|
logger.debug "<#{order_article.article.name}> units_to_order #{order_article.units_to_order} => #{total}"
|
|
|
|
end
|
2013-09-18 22:45:57 +02:00
|
|
|
|
2013-11-25 13:48:54 +01:00
|
|
|
if total > 0
|
|
|
|
# In total there are enough units ordered. Now check the individual result for the ordergroup (group_order).
|
|
|
|
#
|
|
|
|
# Get all GroupOrderArticleQuantities for this OrderArticle...
|
2014-02-20 15:04:53 +01:00
|
|
|
order_quantities = GroupOrderArticleQuantity.where(group_order_article_id: order_article.group_order_article_ids).order('created_on')
|
2013-11-25 13:48:54 +01:00
|
|
|
logger.debug "GroupOrderArticleQuantity records found: #{order_quantities.size}"
|
|
|
|
|
2021-02-05 16:19:05 +01:00
|
|
|
first_order_first_serve = (FoodsoftConfig[:distribution_strategy] == FoodsoftConfig::DistributionStrategy::FIRST_ORDER_FIRST_SERVE)
|
|
|
|
|
2013-11-25 13:48:54 +01:00
|
|
|
# Determine quantities to be ordered...
|
|
|
|
order_quantities.each do |goaq|
|
2021-02-05 16:19:05 +01:00
|
|
|
q = goaq.quantity
|
|
|
|
q = [q, total - total_quantity].min if first_order_first_serve
|
2013-11-25 13:48:54 +01:00
|
|
|
total_quantity += q
|
2023-05-12 13:01:12 +02:00
|
|
|
if goaq.group_order_article_id == id
|
2013-11-25 13:48:54 +01:00
|
|
|
logger.debug "increasing quantity by #{q}"
|
|
|
|
quantity += q
|
|
|
|
end
|
2021-02-05 16:19:05 +01:00
|
|
|
break if total_quantity >= total && first_order_first_serve
|
2013-11-25 13:48:54 +01:00
|
|
|
end
|
2013-09-18 22:45:57 +02:00
|
|
|
|
2013-11-25 13:48:54 +01:00
|
|
|
# Determine tolerance to be ordered...
|
|
|
|
if total_quantity < total
|
2023-05-12 13:01:12 +02:00
|
|
|
logger.debug 'determining additional items to be ordered from tolerance'
|
2013-11-22 23:40:44 +01:00
|
|
|
order_quantities.each do |goaq|
|
2013-11-25 13:48:54 +01:00
|
|
|
q = [goaq.tolerance, total - total_quantity].min
|
2009-02-03 21:14:48 +01:00
|
|
|
total_quantity += q
|
2023-05-12 13:01:12 +02:00
|
|
|
if goaq.group_order_article_id == id
|
2013-11-25 13:48:54 +01:00
|
|
|
logger.debug "increasing tolerance by #{q}"
|
|
|
|
tolerance += q
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2013-11-22 23:40:44 +01:00
|
|
|
break if total_quantity >= total
|
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
|
|
|
|
2013-11-25 13:48:54 +01:00
|
|
|
logger.debug "determined quantity/tolerance/total: #{quantity} / #{tolerance} / #{quantity + tolerance}"
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2013-11-25 13:48:54 +01:00
|
|
|
|
|
|
|
# memoize result unless a total is given
|
2023-05-12 13:01:12 +02:00
|
|
|
r = { quantity: quantity, tolerance: tolerance, total: quantity + tolerance }
|
2013-11-25 13:48:54 +01:00
|
|
|
@calculate_result = r if total.nil?
|
|
|
|
r
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2014-01-03 10:33:09 +01:00
|
|
|
# This is used for automatic distribution, e.g., in order.finish! or when receiving orders
|
2013-11-25 13:48:54 +01:00
|
|
|
def save_results!(article_total = nil)
|
2014-01-03 10:33:09 +01:00
|
|
|
new_result = calculate_result(article_total)[:total]
|
2023-05-12 13:01:12 +02:00
|
|
|
update_attribute(:result_computed, new_result)
|
|
|
|
update_attribute(:result, new_result)
|
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.
|
2012-12-16 16:50:09 +01:00
|
|
|
def total_price(order_article = self.order_article)
|
2013-07-02 12:01:25 +02:00
|
|
|
if order_article.order.open?
|
2012-08-24 19:52:38 +02:00
|
|
|
if FoodsoftConfig[:tolerance_is_costly]
|
2011-06-19 15:30:33 +02:00
|
|
|
order_article.article.fc_price * (quantity + tolerance)
|
|
|
|
else
|
|
|
|
order_article.article.fc_price * quantity
|
|
|
|
end
|
|
|
|
else
|
|
|
|
order_article.price.fc_price * result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-24 10:50:35 +02:00
|
|
|
def total_price_without_deposit(order_article = self.order_article)
|
|
|
|
if order_article.order.open?
|
|
|
|
if FoodsoftConfig[:tolerance_is_costly]
|
|
|
|
order_article.price.fc_price_without_deposit * (quantity + tolerance)
|
|
|
|
else
|
|
|
|
order_article.price.fc_price_without_deposit * quantity
|
|
|
|
end
|
|
|
|
else
|
|
|
|
order_article.price.fc_price_without_deposit * result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-03 10:33:09 +01:00
|
|
|
# Check if the result deviates from the result_computed
|
|
|
|
def result_manually_changed?
|
2014-01-03 13:03:28 +01:00
|
|
|
result != result_computed unless result.nil?
|
2014-01-03 10:33:09 +01:00
|
|
|
end
|
2019-03-26 20:14:39 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_order_not_closed
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless order_article.order.closed?
|
|
|
|
|
|
|
|
errors.add(:order_article, I18n.t('model.group_order_article.order_closed'))
|
2019-03-26 20:14:39 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|