Class: StockArticle

Inherits:
Article
  • Object
show all
Defined in:
app/models/stock_article.rb

Instance Attribute Summary

Attributes inherited from Article

#article_category, #article_prices, #availability, #deposit, #manufacturer, #name, #note, #order_number, #origin, #price, #supplier, #tax, #unit, #unit_quantity

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Article

#check_article_in_use, compare_attributes, #convert_units, #deleted?, #in_open_order, #ordered_in_order?, #price_changed?, #recently_updated, #shared_article, #shared_article_changed?, #unequal_attributes, #uniqueness_of_name

Methods included from PriceCalculation

#fc_price, #gross_price

Class Method Details

.stock_valueObject


31
32
33
# File 'app/models/stock_article.rb', line 31

def self.stock_value
  available.collect { |a| a.quantity * a.gross_price }.sum
end

Instance Method Details

#check_quantityObject (protected)


42
43
44
# File 'app/models/stock_article.rb', line 42

def check_quantity
  raise I18n.t('stockit.check.not_empty', :name => name) unless quantity == 0
end

#mark_as_deletedObject


35
36
37
38
# File 'app/models/stock_article.rb', line 35

def mark_as_deleted
  check_quantity
  super
end

#quantity_availableObject

Check for unclosed orders and substract its ordered quantity


18
19
20
# File 'app/models/stock_article.rb', line 18

def quantity_available
  quantity - quantity_ordered
end

#quantity_historyObject


27
28
29
# File 'app/models/stock_article.rb', line 27

def quantity_history
  stock_changes.reorder('stock_changes.created_at ASC').map{|s| s.quantity}.cumulative_sum
end

#quantity_orderedObject


22
23
24
25
# File 'app/models/stock_article.rb', line 22

def quantity_ordered
  OrderArticle.where(article_id: id).
      joins(:order).where(orders: {state: ['open', 'finished']}).sum(:units_to_order)
end

#update_price_historyObject (protected)

Overwrite Price history of Article. For StockArticles isn't it necessary.


47
48
49
# File 'app/models/stock_article.rb', line 47

def update_price_history
  true
end

#update_quantity!Object

Update the quantity of items in stock


13
14
15
# File 'app/models/stock_article.rb', line 13

def update_quantity!
  update_attribute :quantity, stock_changes.collect(&:quantity).sum
end