foodsoft/app/models/stock_article.rb
wvengen 7af796c09c Merge remote-tracking branch 'bennibu/rails3' into rails3
Conflicts:
	app/controllers/admin/ordergroups_controller.rb
	app/controllers/finance/balancing_controller.rb
	app/controllers/suppliers_controller.rb
	app/views/articles/_article.html.haml
	app/views/finance/balancing/_summary.haml
	app/views/finance/balancing/new.html.haml
	app/views/group_orders/_form.html.haml
	app/views/home/_apple_bar.html.haml
	app/views/suppliers/index.haml
2013-03-21 22:08:09 +01:00

41 lines
955 B
Ruby

# encoding: utf-8
class StockArticle < Article
has_many :stock_changes
scope :available, -> { undeleted.where'quantity > 0' }
before_destroy :check_quantity
# Update the quantity of items in stock
def update_quantity!
update_attribute :quantity, stock_changes.collect(&:quantity).sum
end
# Check for unclosed orders and substract its ordered quantity
def quantity_available
quantity - OrderArticle.where(article_id: id).
joins(:order).where("orders.state = 'open' OR orders.state = 'finished'").sum(:units_to_order)
end
def self.stock_value
available.collect { |a| a.quantity * a.gross_price }.sum
end
def mark_as_deleted
check_quantity
super
end
protected
def check_quantity
raise I18n.t('stockit.check.not_empty', :name => name) unless quantity == 0
end
# Overwrite Price history of Article. For StockArticles isn't it necessary.
def update_price_history
true
end
end