2020-08-01 20:11:25 +02:00
|
|
|
class Delivery < StockEvent
|
2009-01-08 16:33:27 +01:00
|
|
|
|
|
|
|
belongs_to :supplier
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :invoice, optional: true
|
2009-01-08 16:33:27 +01:00
|
|
|
|
2014-02-20 15:04:53 +01:00
|
|
|
scope :recent, -> { order('created_at DESC').limit(10) }
|
2009-01-18 17:42:51 +01:00
|
|
|
|
2020-08-01 20:11:25 +02:00
|
|
|
validates_presence_of :supplier_id
|
2013-06-26 17:56:20 +02:00
|
|
|
validate :stock_articles_must_be_unique
|
2009-01-16 02:17:49 +01:00
|
|
|
|
2009-05-17 16:11:39 +02:00
|
|
|
accepts_nested_attributes_for :stock_changes, :allow_destroy => :true
|
|
|
|
|
|
|
|
def new_stock_changes=(stock_change_attributes)
|
2009-01-16 02:17:49 +01:00
|
|
|
for attributes in stock_change_attributes
|
2009-02-06 17:06:08 +01:00
|
|
|
stock_changes.build(attributes) unless attributes[:quantity].to_i == 0
|
2009-01-16 02:17:49 +01:00
|
|
|
end
|
|
|
|
end
|
2013-06-13 23:33:24 +02:00
|
|
|
|
|
|
|
def includes_article?(article)
|
|
|
|
self.stock_changes.map{|stock_change| stock_change.stock_article.id}.include? article.id
|
|
|
|
end
|
2016-03-04 22:26:55 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-06-26 17:56:20 +02:00
|
|
|
protected
|
2019-01-13 07:05:54 +01:00
|
|
|
|
2013-06-26 17:56:20 +02:00
|
|
|
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
|
2011-05-07 21:55:24 +02:00
|
|
|
|
2019-01-13 07:05:54 +01:00
|
|
|
end
|