Class: Delivery

Inherits:
StockEvent show all
Defined in:
app/models/delivery.rb

Instance Method Summary collapse

Instance Method Details

#includes_article?(article) ⇒ Boolean

Returns:

  • (Boolean)


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

def includes_article?(article)
  stock_changes.map { |stock_change| stock_change.stock_article.id }.include? article.id
end

#new_stock_changes=(stock_change_attributes) ⇒ Object



12
13
14
15
16
# File 'app/models/delivery.rb', line 12

def new_stock_changes=(stock_change_attributes)
  for attributes in stock_change_attributes
    stock_changes.build(attributes) unless attributes[:quantity].to_i == 0
  end
end

#stock_articles_must_be_uniqueObject (protected)



41
42
43
44
45
# File 'app/models/delivery.rb', line 41

def stock_articles_must_be_unique
  return if stock_changes.reject { |sc| sc.marked_for_destruction? }.map { |sc| sc.stock_article.id }.uniq!.nil?

  errors.add(:base, I18n.t('model.delivery.each_stock_article_must_be_unique'))
end

#sum(type = :gross) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/delivery.rb', line 22

def sum(type = :gross)
  total = 0
  for sc in stock_changes
    article = sc.stock_article
    quantity = sc.quantity
    case type
    when :net
      total += quantity * article.price
    when :gross
      total += quantity * article.gross_price
    when :fc
      total += quantity * article.fc_price
    end
  end
  total
end