2012-04-16 08:48:01 +02:00
|
|
|
# encoding: utf-8
|
2009-02-06 16:26:35 +01:00
|
|
|
class StockArticle < Article
|
2011-05-27 14:09:01 +02:00
|
|
|
acts_as_paranoid
|
|
|
|
|
2009-02-06 16:26:35 +01:00
|
|
|
has_many :stock_changes
|
|
|
|
|
2011-05-11 12:21:21 +02:00
|
|
|
scope :available, :conditions => "quantity > 0"
|
2009-02-06 16:26:35 +01:00
|
|
|
|
2009-02-12 18:32:20 +01:00
|
|
|
before_destroy :check_quantity
|
|
|
|
|
2009-02-06 16:26:35 +01:00
|
|
|
# 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(exclude_order = nil)
|
|
|
|
available = quantity
|
|
|
|
for order in Order.stockit.all(:conditions => "state = 'open' OR state = 'finished'")
|
|
|
|
unless order == exclude_order
|
|
|
|
order_article = order.order_articles.first(:conditions => {:article_id => id})
|
|
|
|
available -= order_article.units_to_order if order_article
|
|
|
|
end
|
|
|
|
end
|
|
|
|
available
|
|
|
|
end
|
2009-02-12 18:32:20 +01:00
|
|
|
|
2009-08-04 13:41:28 +02:00
|
|
|
def self.stock_value
|
|
|
|
available.collect { |a| a.quantity * a.gross_price }.sum
|
|
|
|
end
|
|
|
|
|
2009-02-12 18:32:20 +01:00
|
|
|
protected
|
|
|
|
|
|
|
|
def check_quantity
|
|
|
|
raise "#{name} kann nicht gelöscht werden. Der Lagerbestand ist nicht null." unless quantity == 0
|
|
|
|
end
|
2009-05-17 16:26:31 +02:00
|
|
|
|
|
|
|
# Overwrite Price history of Article. For StockArticles isn't it necessary.
|
|
|
|
def update_price_history
|
|
|
|
true
|
|
|
|
end
|
2009-02-06 16:26:35 +01:00
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: articles
|
|
|
|
#
|
2011-05-07 21:55:24 +02:00
|
|
|
# id :integer(4) not null, primary key
|
2011-05-07 20:50:39 +02:00
|
|
|
# name :string(255) default(""), not null
|
2011-05-07 21:55:24 +02:00
|
|
|
# supplier_id :integer(4) default(0), not null
|
|
|
|
# article_category_id :integer(4) default(0), not null
|
2011-05-07 20:50:39 +02:00
|
|
|
# unit :string(255) default(""), not null
|
|
|
|
# note :string(255)
|
2011-05-07 21:55:24 +02:00
|
|
|
# availability :boolean(1) default(TRUE), not null
|
2011-05-07 20:50:39 +02:00
|
|
|
# manufacturer :string(255)
|
|
|
|
# origin :string(255)
|
|
|
|
# shared_updated_on :datetime
|
|
|
|
# price :decimal(8, 2)
|
|
|
|
# tax :float
|
|
|
|
# deposit :decimal(8, 2) default(0.0)
|
2011-05-07 21:55:24 +02:00
|
|
|
# unit_quantity :integer(4) default(1), not null
|
2011-05-07 20:50:39 +02:00
|
|
|
# order_number :string(255)
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# deleted_at :datetime
|
|
|
|
# type :string(255)
|
2011-05-07 21:55:24 +02:00
|
|
|
# quantity :integer(4) default(0)
|
2011-05-07 20:50:39 +02:00
|
|
|
#
|
|
|
|
|