Moved order stats and cleanup stuff into rake task.

Closes #14
This commit is contained in:
Benjamin Meichsner 2010-02-09 21:45:57 +01:00
parent a6646d06a6
commit 55d2976e98
3 changed files with 30 additions and 26 deletions

View file

@ -95,7 +95,7 @@ class OrdersController < ApplicationController
def finish def finish
order = Order.find(params[:id]) order = Order.find(params[:id])
order.finish!(@current_user) order.finish!(@current_user)
call_rake "foodsoft:notify_order_finished", :order_id => order.id call_rake "foodsoft:finished_order_tasks", :order_id => order.id
flash[:notice] = "Die Bestellung wurde beendet." flash[:notice] = "Die Bestellung wurde beendet."
redirect_to order redirect_to order
end end

View file

@ -51,14 +51,14 @@ class Order < ActiveRecord::Base
end end
def articles_for_ordering def articles_for_ordering
if stockit? if stockit?
StockArticle.available.without_deleted(:include => :article_category, StockArticle.available.without_deleted(:include => :article_category,
:order => 'article_categories.name, articles.name').reject{ |a| :order => 'article_categories.name, articles.name').reject{ |a|
a.quantity_available <= 0 a.quantity_available <= 0
}.group_by { |a| a.article_category.name } }.group_by { |a| a.article_category.name }
else else
supplier.articles.available.without_deleted.group_by { |a| a.article_category.name } supplier.articles.available.without_deleted.group_by { |a| a.article_category.name }
end end
end end
# Fetch last orders from same supplier, to generate an article selection proposal # Fetch last orders from same supplier, to generate an article selection proposal
@ -174,20 +174,6 @@ class Order < ActiveRecord::Base
# set new order state (needed by notify_order_finished) # set new order state (needed by notify_order_finished)
update_attributes(:state => 'finished', :ends => Time.now, :updated_by => user) update_attributes(:state => 'finished', :ends => Time.now, :updated_by => user)
# Update GroupOrder prices
group_orders.each { |go| go.update_price! }
# Clean up
# Delete no longer required order-history (group_order_article_quantities) and
# TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?)
order_articles.each do |oa|
oa.group_order_articles.each { |goa| goa.group_order_article_quantities.clear }
end
# Stats
# TODO: Move into background, if possible
ordergroups.each { |o| o.update_stats! }
end end
end end
end end
@ -220,7 +206,7 @@ class Order < ActiveRecord::Base
protected protected
def starts_before_ends def starts_before_ends
errors.add(:ends, "muss nach dem Bestellstart liegen (oder leer bleiben)") if (ends && starts && ends <= starts) errors.add(:ends, "muss nach dem Bestellstart liegen (oder leer bleiben)") if (ends && starts && ends <= starts)
end end
def include_articles def include_articles

View file

@ -68,13 +68,31 @@ namespace :foodsoft do
end end
end end
desc "Notify users of finished orders" desc "finished order tasks, cleanup, notifications, stats ..."
task :notify_order_finished => :environment do task :finished_order_tasks => :environment do
puts "Start: #{Time.now}"
order = Order.find(ENV["ORDER_ID"]) order = Order.find(ENV["ORDER_ID"])
# Update GroupOrder prices
order.group_orders.each { |go| go.update_price! }
# Clean up
# Delete no longer required order-history (group_order_article_quantities) and
# TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?)
order.order_articles.each do |oa|
oa.group_order_articles.each { |goa| goa.group_order_article_quantities.clear }
end
# Notifications
for group_order in order.group_orders for group_order in order.group_orders
for user in group_order.ordergroup.users for user in group_order.ordergroup.users
Mailer.deliver_order_result(user, group_order) if user.settings["notify.orderFinished"] == '1' Mailer.deliver_order_result(user, group_order) if user.settings["notify.orderFinished"] == '1'
end end
end end
# Stats
order.ordergroups.each { |o| o.update_stats! }
puts "End: #{Time.now}"
end end
end end