2009-01-08 16:33:27 +01:00
|
|
|
# == Schema Information
|
2009-02-02 16:35:43 +01:00
|
|
|
# Schema version: 20090119155930
|
2009-01-08 16:33:27 +01:00
|
|
|
#
|
|
|
|
# Table name: orders
|
|
|
|
#
|
2009-02-02 16:35:43 +01:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# supplier_id :integer
|
2009-01-29 01:57:51 +01:00
|
|
|
# note :text
|
|
|
|
# starts :datetime
|
2009-01-08 16:33:27 +01:00
|
|
|
# ends :datetime
|
2009-01-29 01:57:51 +01:00
|
|
|
# state :string(255) default("open")
|
2009-02-02 16:35:43 +01:00
|
|
|
# lock_version :integer default(0), not null
|
|
|
|
# updated_by_user_id :integer
|
2009-01-08 16:33:27 +01:00
|
|
|
#
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
class Order < ActiveRecord::Base
|
2009-01-29 01:57:51 +01:00
|
|
|
extend ActiveSupport::Memoizable # Ability to cache method results. Use memoize :expensive_method
|
|
|
|
acts_as_ordered :order => "ends" # easyier find of next or previous model
|
|
|
|
|
|
|
|
# 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
|
2009-01-14 12:46:01 +01:00
|
|
|
has_many :ordergroups, :through => :group_orders
|
2009-01-29 01:57:51 +01:00
|
|
|
has_one :invoice
|
|
|
|
has_many :comments, :class_name => "OrderComment", :order => "created_at"
|
2009-01-06 11:49:19 +01:00
|
|
|
belongs_to :supplier
|
|
|
|
belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by_user_id"
|
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
# Validations
|
|
|
|
validates_presence_of :supplier_id, :starts
|
|
|
|
validate_on_create :starts_before_ends, :include_articles
|
2009-01-10 22:22:16 +01:00
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
# Callbacks
|
|
|
|
after_update :update_price_of_group_orders
|
|
|
|
|
|
|
|
# Finders
|
|
|
|
named_scope :open, :conditions => { :state => 'open' },
|
|
|
|
:order => 'ends DESC'
|
2009-01-30 22:27:55 +01:00
|
|
|
named_scope :finished, :conditions => "state = 'finished' OR state = 'closed'",
|
|
|
|
:order => 'ends DESC'
|
|
|
|
named_scope :finished_not_closed, :conditions => { :state => 'finished' },
|
|
|
|
:order => 'ends DESC'
|
2009-01-29 01:57:51 +01:00
|
|
|
named_scope :closed, :conditions => { :state => 'closed' },
|
|
|
|
:order => 'ends DESC'
|
2009-01-10 22:22:16 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Create or destroy OrderArticle associations on create/update
|
|
|
|
def article_ids=(ids)
|
|
|
|
# fetch selected articles
|
|
|
|
articles_list = Article.find(ids)
|
|
|
|
# create new order_articles
|
|
|
|
(articles_list - articles).each { |article| order_articles.build(: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-29 01:57:51 +01:00
|
|
|
|
|
|
|
def open?
|
|
|
|
state == "open"
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
|
|
|
|
def finished?
|
|
|
|
state == "finished"
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
|
|
|
|
def closed?
|
|
|
|
state == "closed"
|
2009-01-06 11:49:19 +01:00
|
|
|
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)
|
2009-01-29 01:57:51 +01:00
|
|
|
group_orders.first :conditions => { :ordergroup_id => ordergroup.id }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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 get_articles
|
2009-01-29 01:57:51 +01:00
|
|
|
order_articles.all(:include => [:article, :article_price], :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-01-29 01:57:51 +01:00
|
|
|
memoize :get_articles
|
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
|
|
|
|
def profit(options = {})
|
|
|
|
markup = options[:with_markup] || true
|
|
|
|
if invoice
|
|
|
|
groups_sum = markup ? sum(:groups) : sum(:groups_without_markup)
|
|
|
|
groups_sum - invoice.net_amount
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
if type == :clear || type == :gross || type == :fc
|
|
|
|
for oa in order_articles.ordered.all(:include => [:article,:article_price])
|
|
|
|
quantity = oa.units_to_order * oa.price.unit_quantity
|
|
|
|
case type
|
|
|
|
when :clear
|
|
|
|
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
|
2009-01-29 01:57:51 +01:00
|
|
|
end
|
|
|
|
elsif type == :groups || type == :groups_without_markup
|
|
|
|
for go in group_orders
|
|
|
|
for goa in go.group_order_articles
|
2009-01-06 11:49:19 +01:00
|
|
|
case type
|
2009-01-29 01:57:51 +01:00
|
|
|
when :groups
|
|
|
|
total += goa.quantity * goa.order_article.price.fc_price
|
|
|
|
when :groups_without_markup
|
|
|
|
total += goa.quantity * 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)
|
|
|
|
unless finished?
|
|
|
|
Order.transaction do
|
|
|
|
# Update order_articles. Save the current article_price to keep price consistency
|
|
|
|
order_articles.all(:include => :article).each do |oa|
|
|
|
|
oa.update_attribute(:article_price, oa.article.article_prices.first)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
# set new order state (needed by notify_order_finished)
|
|
|
|
update_attributes(:state => 'finished', :ends => Time.now, :updated_by => user)
|
|
|
|
|
|
|
|
# TODO: delete data, which is no longer required ...
|
|
|
|
# group_order_article_quantities... order_articles with units_to_order == 0 ? ...
|
|
|
|
end
|
|
|
|
|
|
|
|
# notify order groups
|
|
|
|
notify_order_finished
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
|
|
|
|
# TODO: I can't understand, why its going out from the group_order_articles perspective.
|
|
|
|
# Why we can't just iterate through the order_articles?
|
|
|
|
#
|
2009-01-06 11:49:19 +01:00
|
|
|
# Updates the ordered quantites of all OrderArticles from the GroupOrderArticles.
|
2009-01-29 01:57:51 +01:00
|
|
|
# This method is fired after an ordergroup has saved/updated his order.
|
|
|
|
def update_quantities
|
|
|
|
indexed_order_articles = {} # holds the list of updated OrderArticles indexed by their id
|
2009-01-06 11:49:19 +01:00
|
|
|
# Get all GroupOrderArticles for this order and update OrderArticle.quantity/.tolerance/.units_to_order from them...
|
2009-01-29 01:57:51 +01:00
|
|
|
group_order_articles = GroupOrderArticle.all(:conditions => ['group_order_id IN (?)', group_order_ids],
|
|
|
|
:include => [:order_article])
|
|
|
|
for goa in group_order_articles
|
|
|
|
if (order_article = indexed_order_articles[goa.order_article.id.to_s])
|
|
|
|
# order_article has already been fetched, just update...
|
|
|
|
order_article.quantity = order_article.quantity + goa.quantity
|
|
|
|
order_article.tolerance = order_article.tolerance + goa.tolerance
|
|
|
|
order_article.units_to_order = order_article.article.calculate_order_quantity(order_article.quantity, order_article.tolerance)
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
|
|
|
# First update to OrderArticle, need to store in orderArticle hash...
|
2009-01-29 01:57:51 +01:00
|
|
|
order_article = goa.order_article
|
|
|
|
order_article.quantity = goa.quantity
|
|
|
|
order_article.tolerance = goa.tolerance
|
|
|
|
order_article.units_to_order = order_article.article.calculate_order_quantity(order_article.quantity, order_article.tolerance)
|
|
|
|
indexed_order_articles[order_article.id.to_s] = order_article
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
# Commit changes to database...
|
|
|
|
OrderArticle.transaction do
|
2009-01-29 01:57:51 +01:00
|
|
|
indexed_order_articles.each_value { | value | value.save! }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-30 22:27:55 +01:00
|
|
|
# Sets order.status to 'close' and updates all Ordergroup.account_balances
|
|
|
|
def close!(user)
|
|
|
|
raise "Bestellung wurde schon abgerechnet" if closed?
|
|
|
|
transaction_note = "Bestellung: #{supplier.name}, bis #{ends.strftime('%d.%m.%Y')}"
|
|
|
|
|
|
|
|
gos = group_orders.all(:include => :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
|
|
|
|
price = group_order.price * -1 # decrease! account balance
|
|
|
|
group_order.ordergroup.addFinancialTransaction(price, transaction_note, user)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-30 22:27:55 +01:00
|
|
|
self.update_attributes! :state => 'closed', :updated_by => user
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
def starts_before_ends
|
|
|
|
errors.add(:ends, "muss nach dem Bestellstart liegen (oder leer bleiben)") if (ends && starts && ends <= starts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_articles
|
|
|
|
errors.add(:order_articles, "Es muss mindestens ein Artikel ausgewählt sein") if order_articles.empty?
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
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.
|
|
|
|
def update_price_of_group_orders
|
|
|
|
group_orders.each { |group_order| group_order.update_price! }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends "order finished" messages to users who have participated in this order.
|
|
|
|
def notify_order_finished
|
|
|
|
for group_order in self.group_orders
|
|
|
|
ordergroup = group_order.ordergroup
|
|
|
|
logger.debug("Send 'order finished' message to #{ordergroup.name}")
|
|
|
|
# Determine users that want a notification message:
|
|
|
|
users = ordergroup.users.reject{|u| u.settings["notify.orderFinished"] != '1'}
|
|
|
|
unless users.empty?
|
|
|
|
# Create user notification messages:
|
|
|
|
Message.from_template(
|
|
|
|
'order_finished',
|
|
|
|
{:group => ordergroup, :order => self, :group_order => group_order},
|
|
|
|
{:recipients_ids => users.collect(&:id), :subject => "Bestellung beendet: #{supplier.name}"}
|
|
|
|
).save!
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|