Class: StockArticle

Inherits:
Article 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, #order_articles, #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

Methods included from LocalizeInput

parse

Class Method Details

.ransackable_associations(auth_object = nil) ⇒ Object



16
17
18
# File 'app/models/stock_article.rb', line 16

def self.ransackable_associations(auth_object = nil)
  super(auth_object) - %w[supplier]
end

.ransackable_attributes(auth_object = nil) ⇒ Object



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

def self.ransackable_attributes(auth_object = nil)
  super(auth_object) - %w[supplier_id] + %w[quantity]
end

.stock_valueObject



39
40
41
# File 'app/models/stock_article.rb', line 39

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

Instance Method Details

#check_quantityObject (protected)



50
51
52
# File 'app/models/stock_article.rb', line 50

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

#mark_as_deletedObject



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

def mark_as_deleted
  check_quantity
  super
end

#quantity_availableObject

Check for unclosed orders and substract its ordered quantity



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

def quantity_available
  quantity - quantity_ordered
end

#quantity_historyObject



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

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

#quantity_orderedObject



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

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

#update_price_historyObject (protected)

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



55
56
57
# File 'app/models/stock_article.rb', line 55

def update_price_history
  true
end

#update_quantity!Object

Update the quantity of items in stock



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

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