foodsoft/app/models/delivery.rb
Philipp Rothmann fb2b4d8a8a chore: rubocop
chore: fix api test conventions

chore: rubocop -A spec/

chore: more rubocop -A

fix failing test

rubocop fixes

removes helper methods that are in my opinion dead code

more rubocop fixes

rubocop -a --auto-gen-config
2023-06-09 17:35:05 +02:00

46 lines
1.2 KiB
Ruby

class Delivery < StockEvent
belongs_to :supplier
belongs_to :invoice, optional: true
scope :recent, -> { order('created_at DESC').limit(10) }
validates :supplier_id, presence: true
validate :stock_articles_must_be_unique
accepts_nested_attributes_for :stock_changes, allow_destroy: :true
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
def includes_article?(article)
stock_changes.map { |stock_change| stock_change.stock_article.id }.include? article.id
end
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
protected
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
end