foodsoft/app/models/order.rb

341 lines
12 KiB
Ruby
Raw Normal View History

# encoding: utf-8
#
class Order < ApplicationRecord
attr_accessor :ignore_warnings
# Associations
2009-01-06 11:49:19 +01: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
2015-04-10 18:30:12 +02:00
has_many :users_ordered, :through => :ordergroups, :source => :users
has_many :comments, -> { order('created_at') }, :class_name => "OrderComment"
has_many :stock_changes
belongs_to :invoice
belongs_to :supplier
2012-11-12 14:24:49 +01: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
enum end_action: { no_end_action: 0, auto_close: 1, auto_close_and_send: 2, auto_close_and_send_min_quantity: 3 }
# Validations
validates_presence_of :starts
validate :starts_before_ends, :include_articles
validate :keep_ordered_articles
# Callbacks
after_save :save_order_articles, :update_price_of_group_orders
2012-10-30 00:20:47 +01:00
# Finders
scope :open, -> { where(state: 'open').order('ends DESC') }
scope :finished, -> { where("orders.state = 'finished' OR orders.state = 'closed'").order('ends DESC') }
scope :finished_not_closed, -> { where(state: 'finished').order('ends DESC') }
scope :closed, -> { where(state: 'closed').order('ends DESC') }
scope :stockit, -> { where(supplier_id: 0).order('ends DESC') }
2014-02-24 14:33:05 +01:00
scope :recent, -> { order('starts DESC').limit(10) }
scope :stock_group_order, -> { group_orders.where(ordergroup_id: nil).first }
2014-02-24 14:33:05 +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
def stockit?
supplier_id == 0
end
def name
2013-06-15 18:43:42 +02:00
stockit? ? I18n.t('orders.model.stock') : supplier.name
end
def articles_for_ordering
if stockit?
# make sure to include those articles which are no longer available
# but which have already been ordered in this stock order
StockArticle.available.includes(:article_category).
2014-05-06 10:51:45 +02:00
order('article_categories.name', 'articles.name').reject{ |a|
a.quantity_available <= 0 && !a.ordered_in_order?(self)
}.group_by { |a| a.article_category.name }
else
supplier.articles.available.group_by { |a| a.article_category.name }
end
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
def article_ids=(ids)
@article_ids = ids
end
2012-10-30 00:20:47 +01:00
def article_ids
@article_ids ||= order_articles.map { |a| a.article_id.to_s }
2009-01-06 11:49:19 +01:00
end
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
def open?
state == "open"
2009-01-06 11:49:19 +01:00
end
def finished?
state == "finished"
2009-01-06 11:49:19 +01:00
end
def closed?
state == "closed"
2009-01-06 11:49:19 +01:00
end
2015-09-23 22:38:20 +02:00
def boxfill?
FoodsoftConfig[:use_boxfill] && open? && boxfill.present? && boxfill < Time.now
end
def is_boxfill_useful?
FoodsoftConfig[:use_boxfill] && supplier.try(:has_tolerance?)
end
def expired?
2015-09-23 22:38:20 +02:00
ends.present? && ends < Time.now
end
2012-10-30 00:20:47 +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
last = (DateTime.parse(FoodsoftConfig[:order_schedule][:initial]) rescue nil)
last ||= Order.finished.reorder(:starts).first.try(:starts)
last ||= self.starts
2015-09-23 22:38:20 +02:00
# adjust boxfill and end date
self.boxfill ||= FoodsoftDateUtil.next_occurrence last, self.starts, FoodsoftConfig[:order_schedule][:boxfill] if is_boxfill_useful?
self.ends ||= FoodsoftDateUtil.next_occurrence last, self.starts, FoodsoftConfig[:order_schedule][:ends]
end
self
end
# search GroupOrder of given Ordergroup
2009-01-06 11:49:19 +01:00
def group_order(ordergroup)
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
def stock_group_order
group_orders.where(:ordergroup_id => nil).first
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]]]
def articles_grouped_by_category
@articles_grouped_by_category ||= order_articles.
includes([:article_price, :group_order_articles, :article => :article_category]).
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
def articles_sort_by_category
order_articles.includes(:article).order('articles.name').sort do |a,b|
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
# Requires a valid invoice, belonging to this order
#FIXME: Consider order.foodcoop_result
def profit(options = {})
markup = options[:without_markup] || false
if invoice
groups_sum = markup ? sum(:groups_without_markup) : sum(:groups)
groups_sum - invoice.net_amount
end
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
# :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
2009-02-03 21:14:48 +01:00
if type == :net || type == :gross || type == :fc
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
case type
when :net
total += quantity * oa.price.price
when :gross
total += quantity * oa.price.gross_price
when :fc
total += quantity * oa.price.fc_price
2009-01-06 11:49:19 +01:00
end
end
elsif type == :groups || type == :groups_without_markup
for go in group_orders.includes(group_order_articles: {order_article: [:article, :article_price]})
for goa in go.group_order_articles
2009-01-06 11:49:19 +01:00
case type
when :groups
total += goa.result * goa.order_article.price.fc_price
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
total
2009-01-06 11:49:19 +01:00
end
# 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.
def finish!(user)
unless finished?
Order.transaction do
# set new order state (needed by notify_order_finished)
update_attributes!(:state => 'finished', :ends => Time.now, :updated_by => user)
# Update order_articles. Save the current article_price to keep price consistency
2009-02-03 21:14:48 +01:00
# Also save results for each group_order_result
# Clean up
order_articles.includes(:article).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
end
2009-01-06 11:49:19 +01:00
end
# Update GroupOrder prices
group_orders.each(&:update_price!)
# Stats
ordergroups.each(&:update_stats!)
# Notifications
Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'finished_order', self.id)
end
2009-01-06 11:49:19 +01:00
end
end
2012-10-30 00:20:47 +01:00
# Sets order.status to 'close' and updates all Ordergroup.account_balances
def close!(user, transaction_type = nil)
2013-03-22 01:21:44 +01:00
raise I18n.t('orders.model.error_closed') if closed?
transaction_note = I18n.t('orders.model.notice_close', :name => name,
:ends => ends.strftime(I18n.t('date.formats.default')))
gos = group_orders.includes(:ordergroup) # Fetch group_orders
gos.each { |group_order| group_order.update_price! } # Update prices of group_orders
transaction do # Start updating account balances
for group_order in gos
if group_order.ordergroup
price = group_order.price * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction!(price, transaction_note, user, transaction_type)
end
2009-01-06 11:49:19 +01:00
end
if stockit? # Decreases the quantity of stock_articles
for oa in order_articles.includes(:article)
oa.update_results! # Update units_to_order of order_article
stock_changes.create! :stock_article => oa.article, :quantity => oa.units_to_order*-1
end
end
self.update_attributes! :state => 'closed', :updated_by => user, :foodcoop_result => profit
2009-01-06 11:49:19 +01:00
end
end
# 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?
comments.create(user: user, text: I18n.t('orders.model.close_direct_message')) unless FoodsoftConfig[:charge_members_manually]
update_attributes! state: 'closed', updated_by: user
end
def send_to_supplier!(user)
Mailer.deliver_now_with_default_locale do
Mailer.order_result_supplier(user, self)
end
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
end
end
def self.finish_ended!
orders = Order.where.not(end_action: Order.end_actions[:no_end_action]).where(state: 'open').where('ends <= ?', DateTime.now)
orders.each do |order|
begin
order.do_end_action!
rescue => error
ExceptionNotifier.notify_exception(error, data: {foodcoop: FoodsoftConfig.scope, order_id: order.id})
end
end
end
2009-01-06 11:49:19 +01:00
protected
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
2015-09-23 22:38:20 +02: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)
errors.add(:boxfill, I18n.t('orders.model.error_starts_before_boxfill')) if boxfill && starts && boxfill <= (starts-delta)
end
def include_articles
errors.add(:articles, I18n.t('orders.model.error_nosel')) if article_ids.empty?
2012-10-30 00:20:47 +01:00
end
def keep_ordered_articles
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
to_be_removed_but_ordered = to_be_removed.select { |a| a.quantity > 0 || a.tolerance > 0 }
unless to_be_removed_but_ordered.empty? || ignore_warnings
errors.add(:articles, I18n.t(stockit? ? 'orders.model.warning_ordered_stock' : 'orders.model.warning_ordered'))
2013-09-03 17:37:49 +02:00
@erroneous_article_ids = to_be_removed_but_ordered.map { |a| a.article_id }
end
end
def save_order_articles
# fetch selected articles
articles_list = Article.find(article_ids)
# create new order_articles
(articles_list - articles).each { |article| order_articles.create(:article => article) }
# 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
end
2009-01-06 11:49:19 +01:00
private
2012-10-30 00:20:47 +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.
def update_price_of_group_orders
group_orders.each { |group_order| group_order.update_price! }
end
2009-01-06 11:49:19 +01:00
end