Make StockEvent a base class for Delivery and StockTaking

This helps to share code between the two entities and allows easier
extensions in the future.
This commit is contained in:
Patrick Gansterer 2020-08-01 20:11:25 +02:00
parent a5582e9542
commit 785313ac23
9 changed files with 69 additions and 33 deletions

View file

@ -9,6 +9,7 @@ class DeliveriesController < ApplicationController
def show
@delivery = Delivery.find(params[:id])
@stock_changes = @delivery.stock_changes.includes(:stock_article).order('articles.name ASC')
end
def new

View file

@ -6,15 +6,15 @@ module StockitHelper
end
def link_to_stock_change_reason(stock_change)
if stock_change.delivery_id
if stock_change.delivery
link_to Delivery.model_name.human, supplier_delivery_path(stock_change.delivery.supplier, stock_change.delivery)
elsif stock_change.order_id
elsif stock_change.order
link_to Order.model_name.human, order_path(stock_change.order)
elsif stock_change.stock_taking_id
elsif stock_change.stock_taking
link_to StockTaking.model_name.human, stock_taking_path(stock_change.stock_taking)
end
end
def stock_article_price_hint(stock_article)
t('simple_form.hints.stock_article.edit_stock_article.price',
:stock_article_copy_link => link_to(t('stockit.form.copy_stock_article'),

View file

@ -1,12 +1,11 @@
class Delivery < ApplicationRecord
class Delivery < StockEvent
belongs_to :supplier
belongs_to :invoice
has_many :stock_changes, -> { includes(:stock_article).order('articles.name ASC') }, :dependent => :destroy
scope :recent, -> { order('created_at DESC').limit(10) }
validates_presence_of :supplier_id, :date
validates_presence_of :supplier_id
validate :stock_articles_must_be_unique
accepts_nested_attributes_for :stock_changes, :allow_destroy => :true

View file

@ -1,7 +1,7 @@
class StockChange < ApplicationRecord
belongs_to :delivery
belongs_to :delivery, foreign_key: 'stock_event_id'
belongs_to :order
belongs_to :stock_taking
belongs_to :stock_taking, foreign_key: 'stock_event_id'
belongs_to :stock_article
validates_presence_of :stock_article_id, :quantity

View file

@ -0,0 +1,8 @@
class StockEvent < ApplicationRecord
has_many :stock_changes, dependent: :destroy
has_many :stock_articles, through: :stock_changes
validates_presence_of :date
end

View file

@ -1,10 +1,4 @@
class StockTaking < ApplicationRecord
has_many :stock_changes, :dependent => :destroy
has_many :stock_articles, :through => :stock_changes
validates_presence_of :date
class StockTaking < StockEvent
def stock_change_attributes=(stock_change_attributes)
for attributes in stock_change_attributes
stock_changes.build(attributes) unless attributes[:quantity].to_i == 0

View file

@ -24,7 +24,7 @@
%th.numeric= t '.sum'
%tbody
- total_net, total_gross = 0,0
- @delivery.stock_changes.each do |stock_change|
- @stock_changes.each do |stock_change|
- quantity = stock_change.quantity
- sum = quantity * stock_change.stock_article.price
- total_net += sum