2019-01-13 07:05:54 +01:00
|
|
|
class Order < ApplicationRecord
|
2020-03-19 00:22:20 +01:00
|
|
|
attr_accessor :ignore_warnings, :transport_distribution
|
2013-09-04 10:52:14 +02:00
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
# Associations
|
2023-05-12 13:01:12 +02:00
|
|
|
has_many :order_articles, dependent: :destroy
|
|
|
|
has_many :articles, through: :order_articles
|
|
|
|
has_many :group_orders, dependent: :destroy
|
|
|
|
has_many :ordergroups, through: :group_orders
|
|
|
|
has_many :users_ordered, through: :ordergroups, source: :users
|
|
|
|
has_many :comments, -> { order('created_at') }, class_name: 'OrderComment'
|
2009-02-06 16:26:35 +01:00
|
|
|
has_many :stock_changes
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :invoice, optional: true
|
|
|
|
belongs_to :supplier, optional: true
|
2023-05-12 13:01:12 +02:00
|
|
|
belongs_to :updated_by, class_name: 'User', foreign_key: 'updated_by_user_id'
|
|
|
|
belongs_to :created_by, class_name: 'User', foreign_key: 'created_by_user_id'
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2017-10-12 00:20:30 +02:00
|
|
|
enum end_action: { no_end_action: 0, auto_close: 1, auto_close_and_send: 2, auto_close_and_send_min_quantity: 3 }
|
2023-05-12 13:01:12 +02:00
|
|
|
enum transport_distribution: { skip: 0, ordergroup: 1, price: 2, articles: 3 }
|
2017-10-12 00:20:30 +02:00
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
# Validations
|
2023-05-12 13:01:12 +02:00
|
|
|
validates :starts, presence: true
|
2009-02-05 16:40:02 +01:00
|
|
|
validate :starts_before_ends, :include_articles
|
2013-09-03 12:09:33 +02:00
|
|
|
validate :keep_ordered_articles
|
2009-01-10 22:22:16 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
before_validation :distribute_transport
|
2009-01-29 01:57:51 +01:00
|
|
|
# Callbacks
|
2021-02-17 18:42:52 +01:00
|
|
|
after_save :save_order_articles, :update_price_of_group_orders!
|
2012-10-30 00:20:47 +01:00
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
# Finders
|
2020-02-25 00:38:06 +01:00
|
|
|
scope :started, -> { where('starts <= ?', Time.now) }
|
2018-10-13 16:27:24 +02:00
|
|
|
scope :closed, -> { where(state: 'closed').order(ends: :desc) }
|
|
|
|
scope :stockit, -> { where(supplier_id: nil).order(ends: :desc) }
|
|
|
|
scope :recent, -> { order(starts: :desc).limit(10) }
|
2016-02-25 02:27:17 +01:00
|
|
|
scope :stock_group_order, -> { group_orders.where(ordergroup_id: nil).first }
|
2020-02-28 12:17:27 +01:00
|
|
|
scope :with_invoice, -> { where.not(invoice: nil) }
|
2014-02-24 14:33:05 +01:00
|
|
|
|
2021-02-03 20:57:53 +01:00
|
|
|
# State related finders
|
|
|
|
# Diagram for `Order.state` looks like this:
|
|
|
|
# * -> open -> finished (-> received) -> closed
|
|
|
|
# So orders can
|
|
|
|
# 1. ...only transition in one direction (e.g. an order that has been `finished` currently cannot be reopened)
|
|
|
|
# 2. ...be set to `closed` when having the `finished` state. (`received` is optional)
|
2018-10-13 16:27:24 +02:00
|
|
|
scope :open, -> { where(state: 'open').order(ends: :desc) }
|
|
|
|
scope :finished, -> { where(state: %w[finished received closed]).order(ends: :desc) }
|
|
|
|
scope :finished_not_closed, -> { where(state: %w[finished received]).order(ends: :desc) }
|
2021-02-03 20:57:53 +01:00
|
|
|
|
2014-02-20 12:37:51 +01:00
|
|
|
# Allow separate inputs for date and time
|
2014-06-02 15:47:29 +02:00
|
|
|
# with workaround for https://github.com/einzige/date_time_attribute/issues/14
|
|
|
|
include DateTimeAttributeValidate
|
2015-09-23 22:38:20 +02:00
|
|
|
date_time_attribute :starts, :boxfill, :ends
|
2014-02-20 12:37:51 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
def self.ransackable_attributes(_auth_object = nil)
|
|
|
|
%w[id state supplier_id starts boxfill ends pickup]
|
2018-10-13 16:16:44 +02:00
|
|
|
end
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
def self.ransackable_associations(_auth_object = nil)
|
|
|
|
%w[supplier articles order_articles]
|
2018-10-13 16:16:44 +02:00
|
|
|
end
|
|
|
|
|
2009-02-05 16:40:02 +01:00
|
|
|
def stockit?
|
2020-08-13 16:23:01 +02:00
|
|
|
supplier_id.nil?
|
2009-02-05 16:40:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
2013-06-15 18:43:42 +02:00
|
|
|
stockit? ? I18n.t('orders.model.stock') : supplier.name
|
2009-02-05 16:40:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def articles_for_ordering
|
2010-02-09 21:45:57 +01:00
|
|
|
if stockit?
|
2013-03-15 08:50:55 +01:00
|
|
|
# make sure to include those articles which are no longer available
|
|
|
|
# but which have already been ordered in this stock order
|
2021-03-01 15:27:26 +01:00
|
|
|
StockArticle.available.includes(:article_category)
|
2023-05-12 13:01:12 +02:00
|
|
|
.order('article_categories.name', 'articles.name').reject do |a|
|
2015-01-14 21:15:08 +01:00
|
|
|
a.quantity_available <= 0 && !a.ordered_in_order?(self)
|
2023-05-12 13:01:12 +02:00
|
|
|
end.group_by { |a| a.article_category.name }
|
2010-02-09 21:45:57 +01:00
|
|
|
else
|
2014-02-20 15:04:53 +01:00
|
|
|
supplier.articles.available.group_by { |a| a.article_category.name }
|
2010-02-09 21:45:57 +01:00
|
|
|
end
|
2009-02-05 16:40:02 +01:00
|
|
|
end
|
|
|
|
|
2014-02-24 11:37:06 +01:00
|
|
|
def supplier_articles
|
|
|
|
if stockit?
|
|
|
|
StockArticle.undeleted.reorder('articles.name')
|
|
|
|
else
|
|
|
|
supplier.articles.undeleted.reorder('articles.name')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-30 00:20:47 +01:00
|
|
|
# Save ids, and create/delete order_articles after successfully saved the order
|
2023-05-12 13:01:12 +02:00
|
|
|
attr_writer :article_ids
|
2009-01-10 22:22:16 +01:00
|
|
|
|
2012-10-30 00:20:47 +01:00
|
|
|
def article_ids
|
2013-09-03 12:09:33 +02:00
|
|
|
@article_ids ||= order_articles.map { |a| a.article_id.to_s }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
|
2013-09-03 17:37:49 +02:00
|
|
|
# Returns an array of article ids that lead to a validation error.
|
|
|
|
def erroneous_article_ids
|
|
|
|
@erroneous_article_ids ||= []
|
|
|
|
end
|
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
def open?
|
2023-05-12 13:01:12 +02:00
|
|
|
state == 'open'
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
|
|
|
|
def finished?
|
2023-05-12 13:01:12 +02:00
|
|
|
state == 'finished' || state == 'received'
|
2021-02-03 20:57:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def received?
|
2023-05-12 13:01:12 +02:00
|
|
|
state == 'received'
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
|
|
|
|
def closed?
|
2023-05-12 13:01:12 +02:00
|
|
|
state == 'closed'
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2015-09-23 22:38:20 +02:00
|
|
|
def boxfill?
|
2018-10-13 16:16:44 +02:00
|
|
|
!!FoodsoftConfig[:use_boxfill] && open? && boxfill.present? && boxfill < Time.now
|
2015-09-23 22:38:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_boxfill_useful?
|
2018-10-13 16:16:44 +02:00
|
|
|
!!FoodsoftConfig[:use_boxfill] && !!supplier.try(:has_tolerance?)
|
2015-09-23 22:38:20 +02:00
|
|
|
end
|
|
|
|
|
2010-06-19 12:23:18 +02:00
|
|
|
def expired?
|
2015-09-23 22:38:20 +02:00
|
|
|
ends.present? && ends < Time.now
|
2010-06-19 12:23:18 +02:00
|
|
|
end
|
2012-10-30 00:20:47 +01:00
|
|
|
|
2014-11-22 00:33:16 +01:00
|
|
|
# sets up first guess of dates when initializing a new object
|
|
|
|
# I guess `def initialize` would work, but it's tricky http://stackoverflow.com/questions/1186400
|
|
|
|
def init_dates
|
|
|
|
self.starts ||= Time.now
|
|
|
|
if FoodsoftConfig[:order_schedule]
|
|
|
|
# try to be smart when picking a reference day
|
2023-05-12 13:01:12 +02:00
|
|
|
last = begin
|
|
|
|
DateTime.parse(FoodsoftConfig[:order_schedule][:initial])
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
|
|
|
end
|
2014-11-22 00:33:16 +01:00
|
|
|
last ||= Order.finished.reorder(:starts).first.try(:starts)
|
|
|
|
last ||= self.starts
|
2015-09-23 22:38:20 +02:00
|
|
|
# adjust boxfill and end date
|
2023-05-12 13:01:12 +02:00
|
|
|
if is_boxfill_useful?
|
|
|
|
self.boxfill ||= FoodsoftDateUtil.next_occurrence last, self.starts,
|
|
|
|
FoodsoftConfig[:order_schedule][:boxfill]
|
|
|
|
end
|
2014-11-22 00:33:16 +01:00
|
|
|
self.ends ||= FoodsoftDateUtil.next_occurrence last, self.starts, FoodsoftConfig[:order_schedule][:ends]
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2020-07-12 10:26:36 +02:00
|
|
|
# fetch current Order scope's records and map the current user's GroupOrders in (if any)
|
|
|
|
# (performance enhancement as opposed to fetching each GroupOrder separately from the view)
|
|
|
|
def self.ordergroup_group_orders_map(ordergroup)
|
|
|
|
orders = includes(:supplier)
|
|
|
|
group_orders = GroupOrder.where(ordergroup_id: ordergroup.id, order_id: orders.map(&:id))
|
2023-05-12 13:01:12 +02:00
|
|
|
group_orders_hash = group_orders.index_by { |go| go.order_id }
|
2020-07-12 10:26:36 +02:00
|
|
|
orders.map do |order|
|
|
|
|
{
|
|
|
|
order: order,
|
|
|
|
group_order: group_orders_hash[order.id]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# search GroupOrder of given Ordergroup
|
2009-01-06 11:49:19 +01:00
|
|
|
def group_order(ordergroup)
|
2023-05-12 13:01:12 +02:00
|
|
|
group_orders.where(ordergroup_id: ordergroup.id).first
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2012-10-30 00:20:47 +01:00
|
|
|
|
2016-02-25 02:27:17 +01:00
|
|
|
def stock_group_order
|
2023-05-12 13:01:12 +02:00
|
|
|
group_orders.where(ordergroup_id: nil).first
|
2016-02-25 02:27:17 +01:00
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Returns OrderArticles in a nested Array, grouped by category and ordered by article name.
|
|
|
|
# The array has the following form:
|
|
|
|
# e.g: [["drugs",[teethpaste, toiletpaper]], ["fruits" => [apple, banana, lemon]]]
|
2009-02-04 16:41:01 +01:00
|
|
|
def articles_grouped_by_category
|
2021-03-01 15:27:26 +01:00
|
|
|
@articles_grouped_by_category ||= order_articles
|
2023-05-12 13:01:12 +02:00
|
|
|
.includes([:article_price, :group_order_articles, { article: :article_category }])
|
2021-03-01 15:27:26 +01:00
|
|
|
.order('articles.name')
|
|
|
|
.group_by { |a| a.article.article_category.name }
|
|
|
|
.sort { |a, b| a[0] <=> b[0] }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-02-04 16:41:01 +01:00
|
|
|
|
|
|
|
def articles_sort_by_category
|
2021-03-01 15:27:26 +01:00
|
|
|
order_articles.includes(:article).order('articles.name').sort do |a, b|
|
2009-02-04 16:41:01 +01:00
|
|
|
a.article.article_category.name <=> b.article.article_category.name
|
|
|
|
end
|
|
|
|
end
|
2012-10-30 00:20:47 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Returns the defecit/benefit for the foodcoop
|
2009-01-29 21:28:22 +01:00
|
|
|
# Requires a valid invoice, belonging to this order
|
2021-03-01 15:27:26 +01:00
|
|
|
# FIXME: Consider order.foodcoop_result
|
2009-01-29 21:28:22 +01:00
|
|
|
def profit(options = {})
|
2009-03-17 19:43:41 +01:00
|
|
|
markup = options[:without_markup] || false
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless invoice
|
|
|
|
|
|
|
|
groups_sum = markup ? sum(:groups_without_markup) : sum(:groups)
|
|
|
|
groups_sum - invoice.net_amount
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2012-10-30 00:20:47 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Returns the all round price of a finished order
|
2009-01-29 01:57:51 +01:00
|
|
|
# :groups returns the sum of all GroupOrders
|
|
|
|
# :clear returns the price without tax, deposit and markup
|
|
|
|
# :gross includes tax and deposit. this amount should be equal to suppliers bill
|
|
|
|
# :fc, guess what...
|
|
|
|
def sum(type = :gross)
|
|
|
|
total = 0
|
2023-09-28 22:42:52 +02:00
|
|
|
if %i[net gross net_deposit gross_without_deposit fc_without_deposit fc_deposit deposit fc].include?(type)
|
2013-03-17 15:12:14 +01:00
|
|
|
for oa in order_articles.ordered.includes(:article, :article_price)
|
2014-02-13 15:59:04 +01:00
|
|
|
quantity = oa.units * oa.price.unit_quantity
|
2009-01-29 01:57:51 +01:00
|
|
|
case type
|
2021-03-01 15:27:26 +01:00
|
|
|
when :net
|
|
|
|
total += quantity * oa.price.price
|
|
|
|
when :gross
|
|
|
|
total += quantity * oa.price.gross_price
|
2023-09-28 22:42:52 +02:00
|
|
|
when :gross_without_deposit
|
|
|
|
total += quantity * oa.price.gross_price_without_deposit
|
2021-03-01 15:27:26 +01:00
|
|
|
when :fc
|
|
|
|
total += quantity * oa.price.fc_price
|
2023-09-28 22:42:52 +02:00
|
|
|
when :fc_without_deposit
|
|
|
|
total += quantity * oa.price.fc_price_without_deposit
|
|
|
|
when :net_deposit
|
|
|
|
total += quantity * oa.price.net_deposit_price
|
2023-07-24 10:50:35 +02:00
|
|
|
when :fc_deposit
|
|
|
|
total += quantity * oa.price.fc_deposit_price
|
|
|
|
when :deposit
|
|
|
|
total += quantity * oa.price.deposit
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
elsif %i[groups groups_without_markup].include?(type)
|
|
|
|
for go in group_orders.includes(group_order_articles: { order_article: %i[article article_price] })
|
2013-03-17 15:12:14 +01:00
|
|
|
for goa in go.group_order_articles
|
2009-01-06 11:49:19 +01:00
|
|
|
case type
|
2021-03-01 15:27:26 +01:00
|
|
|
when :groups
|
2023-09-28 22:42:52 +02:00
|
|
|
total += goa.result * goa.order_article.price.fc_price
|
2021-03-01 15:27:26 +01:00
|
|
|
when :groups_without_markup
|
|
|
|
total += goa.result * goa.order_article.price.gross_price
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
total
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
# Finishes this order. This will set the order state to "finish" and the end property to the current time.
|
2009-01-06 11:49:19 +01:00
|
|
|
# Ignored if the order is already finished.
|
2009-01-29 01:57:51 +01:00
|
|
|
def finish!(user)
|
2023-05-12 13:01:12 +02:00
|
|
|
return if finished?
|
|
|
|
|
|
|
|
Order.transaction do
|
|
|
|
# set new order state (needed by notify_order_finished)
|
|
|
|
update!(state: 'finished', ends: Time.now, updated_by: user)
|
|
|
|
|
|
|
|
# Update order_articles. Save the current article_price to keep price consistency
|
|
|
|
# Also save results for each group_order_result
|
|
|
|
# Clean up
|
|
|
|
order_articles.includes(:article).find_each do |oa|
|
|
|
|
oa.update_attribute(:article_price, oa.article.article_prices.first)
|
|
|
|
oa.group_order_articles.each do |goa|
|
|
|
|
goa.save_results!
|
|
|
|
# Delete no longer required order-history (group_order_article_quantities) and
|
|
|
|
# TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?)
|
|
|
|
# A: Yes, we do - for redistributing articles when the number of articles
|
|
|
|
# delivered changes, and for statistics on popular articles. Records
|
|
|
|
# with both tolerance and quantity zero can be deleted.
|
|
|
|
# goa.group_order_article_quantities.clear
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
end
|
2009-03-17 10:47:00 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
# Update GroupOrder prices
|
|
|
|
group_orders.each(&:update_price!)
|
2012-08-24 14:24:36 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
# Stats
|
|
|
|
ordergroups.each(&:update_stats!)
|
2012-08-24 14:24:36 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
# Notifications
|
|
|
|
NotifyFinishedOrderJob.perform_later(self)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
2012-10-30 00:20:47 +01:00
|
|
|
|
2009-01-30 22:27:55 +01:00
|
|
|
# Sets order.status to 'close' and updates all Ordergroup.account_balances
|
2017-03-04 14:15:18 +01:00
|
|
|
def close!(user, transaction_type = nil)
|
2013-03-22 01:21:44 +01:00
|
|
|
raise I18n.t('orders.model.error_closed') if closed?
|
2009-01-30 22:27:55 +01:00
|
|
|
|
2021-02-17 18:42:52 +01:00
|
|
|
update_price_of_group_orders!
|
2009-01-30 22:27:55 +01:00
|
|
|
|
|
|
|
transaction do # Start updating account balances
|
2021-02-17 18:42:52 +01:00
|
|
|
charge_group_orders!(user, transaction_type)
|
2009-02-06 16:26:35 +01:00
|
|
|
|
|
|
|
if stockit? # Decreases the quantity of stock_articles
|
2014-02-20 15:04:53 +01:00
|
|
|
for oa in order_articles.includes(:article)
|
2009-02-06 16:26:35 +01:00
|
|
|
oa.update_results! # Update units_to_order of order_article
|
2023-05-12 13:01:12 +02:00
|
|
|
stock_changes.create! stock_article: oa.article, quantity: oa.units_to_order * -1
|
2009-02-06 16:26:35 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
update!(state: 'closed', updated_by: user, foodcoop_result: profit)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
2009-03-11 16:58:31 +01:00
|
|
|
|
2012-07-27 18:03:46 +02:00
|
|
|
# Close the order directly, without automaticly updating ordergroups account balances
|
|
|
|
def close_direct!(user)
|
2013-03-22 01:21:44 +01:00
|
|
|
raise I18n.t('orders.model.error_closed') if closed?
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
unless FoodsoftConfig[:charge_members_manually]
|
|
|
|
comments.create(user: user,
|
|
|
|
text: I18n.t('orders.model.close_direct_message'))
|
|
|
|
end
|
2022-10-13 18:25:52 +02:00
|
|
|
update!(state: 'closed', updated_by: user)
|
2012-07-27 18:03:46 +02:00
|
|
|
end
|
|
|
|
|
2017-10-12 00:20:30 +02:00
|
|
|
def send_to_supplier!(user)
|
2019-01-23 23:00:33 +01:00
|
|
|
Mailer.deliver_now_with_default_locale do
|
|
|
|
Mailer.order_result_supplier(user, self)
|
|
|
|
end
|
2017-10-12 00:20:30 +02:00
|
|
|
update!(last_sent_mail: Time.now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_end_action!
|
|
|
|
if auto_close?
|
|
|
|
finish!(created_by)
|
|
|
|
elsif auto_close_and_send?
|
|
|
|
finish!(created_by)
|
|
|
|
send_to_supplier!(created_by)
|
|
|
|
elsif auto_close_and_send_min_quantity?
|
|
|
|
finish!(created_by)
|
2018-06-19 22:12:34 +02:00
|
|
|
send_to_supplier!(created_by) if sum >= supplier.min_order_quantity.to_r
|
2017-10-12 00:20:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.finish_ended!
|
2023-05-12 13:01:12 +02:00
|
|
|
orders = Order.where.not(end_action: Order.end_actions[:no_end_action]).where(state: 'open').where('ends <= ?',
|
|
|
|
DateTime.now)
|
2017-10-12 00:20:30 +02:00
|
|
|
orders.each do |order|
|
2023-05-12 13:01:12 +02:00
|
|
|
order.do_end_action!
|
|
|
|
rescue StandardError => e
|
|
|
|
ExceptionNotifier.notify_exception(e, data: { foodcoop: FoodsoftConfig.scope, order_id: order.id })
|
2017-10-12 00:20:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
protected
|
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
def starts_before_ends
|
2015-01-02 17:46:39 +01:00
|
|
|
delta = Rails.env.test? ? 1 : 0 # since Rails 4.2 tests appear to have time differences, with this validation failing
|
2021-03-01 15:27:26 +01:00
|
|
|
errors.add(:ends, I18n.t('orders.model.error_starts_before_ends')) if ends && starts && ends <= (starts - delta)
|
|
|
|
errors.add(:ends, I18n.t('orders.model.error_boxfill_before_ends')) if ends && boxfill && ends <= (boxfill - delta)
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless boxfill && starts && boxfill <= (starts - delta)
|
|
|
|
|
|
|
|
errors.add(:boxfill,
|
|
|
|
I18n.t('orders.model.error_starts_before_boxfill'))
|
2009-01-29 01:57:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_articles
|
2013-09-04 10:52:14 +02:00
|
|
|
errors.add(:articles, I18n.t('orders.model.error_nosel')) if article_ids.empty?
|
2012-10-30 00:20:47 +01:00
|
|
|
end
|
|
|
|
|
2013-09-03 12:09:33 +02:00
|
|
|
def keep_ordered_articles
|
2014-02-20 15:04:53 +01:00
|
|
|
chosen_order_articles = order_articles.where(article_id: article_ids)
|
2013-09-03 17:37:49 +02:00
|
|
|
to_be_removed = order_articles - chosen_order_articles
|
2015-01-14 21:15:08 +01:00
|
|
|
to_be_removed_but_ordered = to_be_removed.select { |a| a.quantity > 0 || a.tolerance > 0 }
|
2023-05-12 13:01:12 +02:00
|
|
|
return if to_be_removed_but_ordered.empty? || ignore_warnings
|
|
|
|
|
|
|
|
errors.add(:articles, I18n.t(stockit? ? 'orders.model.warning_ordered_stock' : 'orders.model.warning_ordered'))
|
|
|
|
@erroneous_article_ids = to_be_removed_but_ordered.map { |a| a.article_id }
|
2013-09-03 12:09:33 +02:00
|
|
|
end
|
2013-03-12 11:26:14 +01:00
|
|
|
|
2013-09-03 12:09:33 +02:00
|
|
|
def save_order_articles
|
2013-03-12 11:26:14 +01:00
|
|
|
# fetch selected articles
|
|
|
|
articles_list = Article.find(article_ids)
|
|
|
|
# create new order_articles
|
2023-05-12 13:01:12 +02:00
|
|
|
(articles_list - articles).each { |article| order_articles.create(article: article) }
|
2013-03-12 11:26:14 +01:00
|
|
|
# delete old order_articles
|
|
|
|
articles.reject { |article| articles_list.include?(article) }.each do |article|
|
|
|
|
order_articles.detect { |order_article| order_article.article_id == article.id }.destroy
|
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
private
|
2012-10-30 00:20:47 +01:00
|
|
|
|
2020-03-19 00:22:20 +01:00
|
|
|
def distribute_transport
|
|
|
|
return unless group_orders.any?
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2020-03-19 00:22:20 +01:00
|
|
|
case transport_distribution.try(&:to_i)
|
2023-05-12 13:01:12 +02:00
|
|
|
when Order.transport_distributions[:ordergroup]
|
2020-03-19 00:22:20 +01:00
|
|
|
amount = transport / group_orders.size
|
|
|
|
group_orders.each do |go|
|
|
|
|
go.transport = amount.ceil(2)
|
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
when Order.transport_distributions[:price]
|
2020-03-19 00:22:20 +01:00
|
|
|
amount = transport / group_orders.sum(:price)
|
|
|
|
group_orders.each do |go|
|
|
|
|
go.transport = (amount * go.price).ceil(2)
|
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
when Order.transport_distributions[:articles]
|
2020-03-19 00:22:20 +01:00
|
|
|
amount = transport / group_orders.includes(:group_order_articles).sum(:result)
|
|
|
|
group_orders.each do |go|
|
|
|
|
go.transport = (amount * go.group_order_articles.sum(:result)).ceil(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
# Updates the "price" attribute of GroupOrders or GroupOrderResults
|
|
|
|
# This will be either the maximum value of a current order or the actual order value of a finished order.
|
2021-02-17 18:42:52 +01:00
|
|
|
def update_price_of_group_orders!
|
|
|
|
group_orders.each(&:update_price!)
|
2009-01-29 01:57:51 +01:00
|
|
|
end
|
|
|
|
|
2021-02-17 18:42:52 +01:00
|
|
|
def charge_group_orders!(user, transaction_type = nil)
|
|
|
|
note = transaction_note
|
2023-05-12 13:01:12 +02:00
|
|
|
group_orders.includes(:ordergroup).find_each do |group_order|
|
2021-02-17 18:42:52 +01:00
|
|
|
if group_order.ordergroup
|
2021-03-19 03:36:36 +01:00
|
|
|
price = group_order.total * -1 # decrease! account balance
|
2021-02-17 18:42:52 +01:00
|
|
|
group_order.ordergroup.add_financial_transaction!(price, note, user, transaction_type, nil, group_order)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def transaction_note
|
|
|
|
I18n.t('orders.model.notice_close', name: name, ends: ends.strftime(I18n.t('date.formats.default')))
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|