foodsoft/app/models/stock_article.rb

50 lines
1.1 KiB
Ruby
Raw Normal View History

# 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
2013-12-06 22:43:36 +01:00
quantity - quantity_ordered
end
def quantity_ordered
OrderArticle.where(article_id: id).
joins(:order).where(orders: {state: ['open', 'finished']}).sum(:units_to_order)
end
2013-12-06 22:43:36 +01:00
2013-07-09 21:46:04 +02:00
def quantity_history
stock_changes.reorder('stock_changes.created_at ASC').map{|s| s.quantity}.cumulative_sum
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
2013-02-11 23:37:52 +01:00
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