Class: Delivery

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/delivery.rb

Instance Method Summary collapse

Instance Method Details

#includes_article?(article) ⇒ Boolean


20
21
22
# File 'app/models/delivery.rb', line 20

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

#new_stock_changes=(stock_change_attributes) ⇒ Object


14
15
16
17
18
# File 'app/models/delivery.rb', line 14

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)


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

def stock_articles_must_be_unique
  unless 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
end

#sum(type = :gross) ⇒ Object


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

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