2009-01-08 16:33:27 +01:00
|
|
|
# An OrderArticle represents a single Article that is part of an Order.
|
2019-01-13 07:05:54 +01:00
|
|
|
class OrderArticle < ApplicationRecord
|
2016-06-04 21:45:24 +02:00
|
|
|
include FindEachWithOrder
|
2009-01-06 15:45:19 +01:00
|
|
|
|
2014-01-04 21:35:19 +01:00
|
|
|
attr_reader :update_global_price
|
2012-12-11 10:32:59 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
belongs_to :order
|
2013-03-16 17:53:24 +01:00
|
|
|
belongs_to :article
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :article_price, optional: true
|
2009-01-06 11:49:19 +01:00
|
|
|
has_many :group_order_articles, :dependent => :destroy
|
|
|
|
|
2009-02-09 20:12:56 +01:00
|
|
|
validates_presence_of :order_id, :article_id
|
2009-01-29 21:28:22 +01:00
|
|
|
validate :article_and_price_exist
|
2012-06-21 17:19:00 +02:00
|
|
|
validates_uniqueness_of :article_id, scope: :order_id
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2014-09-11 13:47:03 +02:00
|
|
|
_ordered_sql = "order_articles.units_to_order > 0 OR order_articles.units_billed > 0 OR order_articles.units_received > 0"
|
2013-11-26 13:22:44 +01:00
|
|
|
scope :ordered, -> { where(_ordered_sql) }
|
2014-09-11 13:47:03 +02:00
|
|
|
scope :ordered_or_member, -> { includes(:group_order_articles).where("#{_ordered_sql} OR order_articles.quantity > 0 OR group_order_articles.result > 0") }
|
2009-01-29 01:57:51 +01:00
|
|
|
|
2012-06-21 17:19:00 +02:00
|
|
|
before_create :init_from_balancing
|
|
|
|
after_destroy :update_ordergroup_prices
|
2009-01-29 21:28:22 +01:00
|
|
|
|
2018-10-13 16:21:37 +02:00
|
|
|
def self.ransackable_attributes(auth_object = nil)
|
|
|
|
%w(id order_id article_id quantity tolerance units_to_order)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ransackable_associations(auth_object = nil)
|
|
|
|
%w(order article)
|
|
|
|
end
|
|
|
|
|
2009-02-03 21:14:48 +01:00
|
|
|
# This method returns either the ArticlePrice or the Article
|
|
|
|
# The first will be set, when the the order is finished
|
2009-01-29 01:57:51 +01:00
|
|
|
def price
|
|
|
|
article_price || article
|
|
|
|
end
|
2013-11-25 13:48:54 +01:00
|
|
|
|
|
|
|
# latest information on available units
|
|
|
|
def units
|
|
|
|
return units_received unless units_received.nil?
|
|
|
|
return units_billed unless units_billed.nil?
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2013-11-25 13:48:54 +01:00
|
|
|
units_to_order
|
|
|
|
end
|
|
|
|
|
2015-09-23 22:38:20 +02:00
|
|
|
# Count quantities of belonging group_orders.
|
2009-01-29 21:28:22 +01:00
|
|
|
# In balancing this can differ from ordered (by supplier) quantity for this article.
|
|
|
|
def group_orders_sum
|
2009-02-04 16:41:01 +01:00
|
|
|
quantity = group_order_articles.collect(&:result).sum
|
2021-03-01 15:27:26 +01:00
|
|
|
{ :quantity => quantity, :price => quantity * price.fc_price }
|
2009-01-29 21:28:22 +01:00
|
|
|
end
|
|
|
|
|
2009-02-03 21:14:48 +01:00
|
|
|
# Update quantity/tolerance/units_to_order from group_order_articles
|
|
|
|
def update_results!
|
2009-02-06 16:26:35 +01:00
|
|
|
if order.open?
|
2015-09-23 22:38:20 +02:00
|
|
|
self.quantity = group_order_articles.collect(&:quantity).sum
|
|
|
|
self.tolerance = group_order_articles.collect(&:tolerance).sum
|
|
|
|
self.units_to_order = calculate_units_to_order(quantity, tolerance)
|
|
|
|
enforce_boxfill if order.boxfill?
|
|
|
|
save!
|
2009-02-06 16:26:35 +01:00
|
|
|
elsif order.finished?
|
|
|
|
update_attribute(:units_to_order, group_order_articles.collect(&:result).sum)
|
|
|
|
end
|
2009-02-03 21:14:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns how many units of the belonging article need to be ordered given the specified order quantity and tolerance.
|
|
|
|
# This is determined by calculating how many units can be ordered from the given order quantity, using
|
|
|
|
# the tolerance to order an additional unit if the order quantity is not quiet sufficient.
|
|
|
|
# There must always be at least one item in a unit that is an ordered quantity (no units are ever entirely
|
|
|
|
# filled by tolerance items only).
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# unit_quantity | quantity | tolerance | calculate_units_to_order
|
|
|
|
# --------------+----------+-----------+-----------------------
|
|
|
|
# 4 | 0 | 2 | 0
|
|
|
|
# 4 | 0 | 5 | 0
|
|
|
|
# 4 | 2 | 2 | 1
|
|
|
|
# 4 | 4 | 2 | 1
|
|
|
|
# 4 | 4 | 4 | 1
|
|
|
|
# 4 | 5 | 3 | 2
|
|
|
|
# 4 | 5 | 4 | 2
|
|
|
|
#
|
|
|
|
def calculate_units_to_order(quantity, tolerance = 0)
|
|
|
|
unit_size = price.unit_quantity
|
|
|
|
units = quantity / unit_size
|
|
|
|
remainder = quantity % unit_size
|
|
|
|
units += ((remainder > 0) && (remainder + tolerance >= unit_size) ? 1 : 0)
|
|
|
|
end
|
|
|
|
|
2009-03-22 11:58:01 +01:00
|
|
|
# Calculate price for ordered quantity.
|
|
|
|
def total_price
|
2014-01-02 21:36:45 +01:00
|
|
|
units * price.unit_quantity * price.price
|
2009-03-22 11:58:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Calculate gross price for ordered qunatity.
|
|
|
|
def total_gross_price
|
2014-01-02 21:36:45 +01:00
|
|
|
units * price.unit_quantity * price.gross_price
|
2009-03-22 11:58:01 +01:00
|
|
|
end
|
|
|
|
|
2021-03-01 15:27:26 +01:00
|
|
|
def ordered_quantities_different_from_group_orders?(ordered_mark = "!", billed_mark = "?", received_mark = "?")
|
2013-12-23 12:32:26 +01:00
|
|
|
if not units_received.nil?
|
|
|
|
((units_received * price.unit_quantity) == group_orders_sum[:quantity]) ? false : received_mark
|
|
|
|
elsif not units_billed.nil?
|
|
|
|
((units_billed * price.unit_quantity) == group_orders_sum[:quantity]) ? false : billed_mark
|
|
|
|
elsif not units_to_order.nil?
|
|
|
|
((units_to_order * price.unit_quantity) == group_orders_sum[:quantity]) ? false : ordered_mark
|
|
|
|
else
|
|
|
|
nil # can happen in integration tests
|
|
|
|
end
|
2009-02-04 16:41:01 +01:00
|
|
|
end
|
|
|
|
|
2013-11-26 23:48:21 +01:00
|
|
|
# redistribute articles over ordergroups
|
|
|
|
# quantity Number of units to distribute
|
|
|
|
# surplus What to do when there are more articles than ordered quantity
|
|
|
|
# :tolerance fill member orders' tolerance
|
|
|
|
# :stock move to stock
|
|
|
|
# nil nothing; for catching the remaining count
|
|
|
|
# update_totals Whether to update group_order and ordergroup totals
|
|
|
|
# returns array with counts for each surplus method
|
|
|
|
def redistribute(quantity, surplus = [:tolerance], update_totals = true)
|
|
|
|
qty_left = quantity
|
|
|
|
counts = [0] * surplus.length
|
|
|
|
|
|
|
|
if surplus.index(:tolerance).nil?
|
|
|
|
qty_for_members = [qty_left, self.quantity].min
|
|
|
|
else
|
2021-03-01 15:27:26 +01:00
|
|
|
qty_for_members = [qty_left, self.quantity + self.tolerance].min
|
|
|
|
counts[surplus.index(:tolerance)] = [0, qty_for_members - self.quantity].max
|
2013-11-26 23:48:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Recompute
|
2021-03-01 15:27:26 +01:00
|
|
|
group_order_articles.each { |goa| goa.save_results! qty_for_members }
|
2013-11-26 23:48:21 +01:00
|
|
|
qty_left -= qty_for_members
|
|
|
|
|
|
|
|
# if there's anything left, move to stock if wanted
|
2015-01-14 21:15:08 +01:00
|
|
|
if qty_left > 0 && surplus.index(:stock)
|
2013-11-26 23:48:21 +01:00
|
|
|
counts[surplus.index(:stock)] = qty_left
|
|
|
|
# 1) find existing stock article with same name, unit, price
|
|
|
|
# 2) if not found, create new stock article
|
|
|
|
# avoiding duplicate stock article names
|
|
|
|
end
|
2015-01-14 21:15:08 +01:00
|
|
|
if qty_left > 0 && surplus.index(nil)
|
2013-11-26 23:48:21 +01:00
|
|
|
counts[surplus.index(nil)] = qty_left
|
|
|
|
end
|
2013-11-25 13:48:54 +01:00
|
|
|
|
|
|
|
# Update GroupOrder prices & Ordergroup stats
|
|
|
|
# TODO only affected group_orders, and once after redistributing all articles
|
2013-11-26 23:48:21 +01:00
|
|
|
if update_totals
|
|
|
|
update_ordergroup_prices
|
|
|
|
order.ordergroups.each(&:update_stats!)
|
|
|
|
end
|
2013-11-25 13:48:54 +01:00
|
|
|
|
|
|
|
# TODO notifications
|
2013-11-26 23:48:21 +01:00
|
|
|
|
|
|
|
counts
|
2013-11-25 13:48:54 +01:00
|
|
|
end
|
|
|
|
|
2009-02-09 20:12:56 +01:00
|
|
|
# Updates order_article and belongings during balancing process
|
2013-03-17 18:33:04 +01:00
|
|
|
def update_article_and_price!(order_article_attributes, article_attributes, price_attributes = nil)
|
2009-02-09 20:12:56 +01:00
|
|
|
OrderArticle.transaction do
|
2012-12-11 10:32:59 +01:00
|
|
|
# Updates self
|
|
|
|
self.update_attributes!(order_article_attributes)
|
|
|
|
|
2009-02-09 20:12:56 +01:00
|
|
|
# Updates article
|
|
|
|
article.update_attributes!(article_attributes)
|
|
|
|
|
2012-12-11 10:32:59 +01:00
|
|
|
# Updates article_price belonging to current order article
|
2013-03-17 18:33:04 +01:00
|
|
|
if price_attributes.present?
|
|
|
|
article_price.attributes = price_attributes
|
|
|
|
if article_price.changed?
|
2014-01-04 21:35:19 +01:00
|
|
|
# Updates also price attributes of article if update_global_price is selected
|
|
|
|
if update_global_price
|
2013-03-17 18:33:04 +01:00
|
|
|
article.update_attributes!(price_attributes)
|
2014-01-04 21:22:57 +01:00
|
|
|
self.article_price = article.article_prices.first and save # Assign new created article price to order article
|
2013-03-17 18:33:04 +01:00
|
|
|
else
|
|
|
|
# Creates a new article_price if neccessary
|
|
|
|
# Set created_at timestamp to order ends, to make sure the current article price isn't changed
|
2020-08-17 14:00:26 +02:00
|
|
|
create_article_price!(price_attributes.merge(article_id: article_id, created_at: order.ends)) and save
|
2013-03-17 18:33:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Updates ordergroup values
|
|
|
|
update_ordergroup_prices
|
2012-12-11 10:32:59 +01:00
|
|
|
end
|
2009-02-09 20:12:56 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-04 21:35:19 +01:00
|
|
|
def update_global_price=(value)
|
2021-03-01 15:27:26 +01:00
|
|
|
@update_global_price = (value == true || value == '1') ? true : false
|
2012-12-11 10:32:59 +01:00
|
|
|
end
|
|
|
|
|
2014-06-04 10:05:18 +02:00
|
|
|
# @return [Number] Units missing for the last +unit_quantity+ of the article.
|
2011-06-19 19:56:04 +02:00
|
|
|
def missing_units
|
2015-09-23 22:38:20 +02:00
|
|
|
_missing_units(price.unit_quantity, quantity, tolerance)
|
|
|
|
end
|
|
|
|
|
|
|
|
def missing_units_was
|
|
|
|
_missing_units(price.unit_quantity, quantity_was, tolerance_was)
|
2011-06-19 19:56:04 +02:00
|
|
|
end
|
2015-09-23 22:38:20 +02:00
|
|
|
|
2014-01-03 10:33:09 +01:00
|
|
|
# Check if the result of any associated GroupOrderArticle was overridden manually
|
|
|
|
def result_manually_changed?
|
2021-03-01 15:27:26 +01:00
|
|
|
group_order_articles.any? { |goa| goa.result_manually_changed? }
|
2014-01-03 10:33:09 +01:00
|
|
|
end
|
2011-06-19 19:56:04 +02:00
|
|
|
|
2021-02-27 17:24:25 +01:00
|
|
|
def difference_received_ordered
|
|
|
|
(units_received || 0) - units_to_order
|
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
private
|
2015-09-23 22:38:20 +02:00
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
def article_and_price_exist
|
2013-12-31 12:27:10 +01:00
|
|
|
errors.add(:article, I18n.t('model.order_article.error_price')) if !(article = Article.find(article_id)) || article.fc_price.nil?
|
|
|
|
rescue
|
|
|
|
errors.add(:article, I18n.t('model.order_article.error_price'))
|
2009-01-29 21:28:22 +01:00
|
|
|
end
|
|
|
|
|
2012-06-21 17:19:00 +02:00
|
|
|
# Associate with current article price if created in a finished order
|
|
|
|
def init_from_balancing
|
2015-01-14 21:15:08 +01:00
|
|
|
if order.present? && order.finished?
|
2012-06-21 17:19:00 +02:00
|
|
|
self.article_price = article.article_prices.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_ordergroup_prices
|
2013-05-30 10:54:22 +02:00
|
|
|
# updates prices of ALL ordergroups - these are actually too many
|
|
|
|
# in case of performance issues, update only ordergroups, which ordered this article
|
|
|
|
# CAUTION: in after_destroy callback related records (e.g. group_order_articles) are already non-existent
|
2013-11-26 23:48:21 +01:00
|
|
|
order.group_orders.each(&:update_price!)
|
2012-06-21 17:19:00 +02:00
|
|
|
end
|
|
|
|
|
2015-09-23 22:38:20 +02:00
|
|
|
# Throws an exception when the changed article decreases the amount of filled boxes.
|
|
|
|
def enforce_boxfill
|
2015-10-13 23:29:23 +02:00
|
|
|
# Either nothing changes, or the tolerance increases,
|
|
|
|
# missing_units decreases and the amount doesn't decrease, or
|
2015-09-23 22:38:20 +02:00
|
|
|
# tolerance was moved to quantity. Only then are changes allowed in the boxfill phase.
|
|
|
|
delta_q = quantity - quantity_was
|
|
|
|
delta_t = tolerance - tolerance_was
|
|
|
|
delta_mis = missing_units - missing_units_was
|
|
|
|
delta_box = units_to_order - units_to_order_was
|
2015-10-13 23:29:23 +02:00
|
|
|
unless (delta_q == 0 && delta_t >= 0) ||
|
2015-09-23 22:38:20 +02:00
|
|
|
(delta_mis < 0 && delta_box >= 0 && delta_t >= 0) ||
|
|
|
|
(delta_q > 0 && delta_q == -delta_t)
|
2015-10-13 23:29:23 +02:00
|
|
|
raise ActiveRecord::RecordNotSaved.new("Change not acceptable in boxfill phase for '#{article.name}', sorry.", self)
|
2015-09-23 22:38:20 +02:00
|
|
|
end
|
|
|
|
end
|
2011-05-07 21:55:24 +02:00
|
|
|
|
2015-09-23 22:38:20 +02:00
|
|
|
def _missing_units(unit_quantity, quantity, tolerance)
|
2021-03-01 15:27:26 +01:00
|
|
|
units = unit_quantity - ((quantity % unit_quantity) + tolerance)
|
2015-09-23 22:38:20 +02:00
|
|
|
units = 0 if units < 0
|
|
|
|
units = 0 if units == unit_quantity
|
|
|
|
units
|
|
|
|
end
|
|
|
|
end
|