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,11 +6,11 @@ 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

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

View File

@ -0,0 +1,41 @@
class CreateStockEvents < ActiveRecord::Migration
class StockEvent < ActiveRecord::Base; end
class StockTaking < ActiveRecord::Base; end
def change
rename_table :deliveries, :stock_events
rename_column :stock_changes, :delivery_id, :stock_event_id
add_column :stock_events, :type, :string, default: 'Delivery', null: false
reversible do |dir|
dir.up do
change_column_default :stock_events, :type, nil
diff = [StockEvent.maximum(:id) + 1 - StockTaking.minimum(:id), 0].max
execute "UPDATE stock_changes SET stock_event_id = stock_taking_id + #{diff}
WHERE stock_taking_id IS NOT NULL"
execute "INSERT INTO stock_events (type, id, date, note, created_at)
SELECT 'StockTaking', id + #{diff}, date, note, created_at FROM stock_takings"
remove_column :stock_changes, :stock_taking_id
drop_table :stock_takings
end
dir.down do
create_table :stock_takings do |t|
t.date :date
t.text :note
t.datetime :created_at
end
add_column :stock_changes, :stock_taking_id, :integer
execute "INSERT INTO stock_takings (id, date, note, created_at)
SELECT id, date, note, created_at FROM stock_events WHERE type = 'StockTaking'"
execute "DELETE FROM stock_events WHERE type = 'StockTaking'"
execute "UPDATE stock_changes SET stock_taking_id = stock_event_id, stock_event_id = NULL
WHERE stock_event_id IN (SELECT id FROM stock_takings)"
end
end
end
end

View File

@ -90,16 +90,6 @@ ActiveRecord::Schema.define(version: 20181205010000) do
add_index "bank_transactions", ["financial_link_id"], name: "index_bank_transactions_on_financial_link_id", using: :btree
create_table "deliveries", force: :cascade do |t|
t.integer "supplier_id", limit: 4
t.date "date"
t.datetime "created_at"
t.text "note", limit: 65535
t.integer "invoice_id", limit: 4
end
add_index "deliveries", ["supplier_id"], name: "index_deliveries_on_supplier_id", using: :btree
create_table "documents", force: :cascade do |t|
t.string "name", limit: 255
t.string "mime", limit: 255
@ -472,24 +462,27 @@ ActiveRecord::Schema.define(version: 20181205010000) do
add_index "settings", ["thing_type", "thing_id", "var"], name: "index_settings_on_thing_type_and_thing_id_and_var", unique: true, using: :btree
create_table "stock_changes", force: :cascade do |t|
t.integer "delivery_id", limit: 4
t.integer "stock_event_id", limit: 4
t.integer "order_id", limit: 4
t.integer "stock_article_id", limit: 4
t.integer "quantity", limit: 4, default: 0
t.datetime "created_at"
t.integer "stock_taking_id", limit: 4
end
add_index "stock_changes", ["delivery_id"], name: "index_stock_changes_on_delivery_id", using: :btree
add_index "stock_changes", ["stock_article_id"], name: "index_stock_changes_on_stock_article_id", using: :btree
add_index "stock_changes", ["stock_taking_id"], name: "index_stock_changes_on_stock_taking_id", using: :btree
add_index "stock_changes", ["stock_event_id"], name: "index_stock_changes_on_stock_event_id", using: :btree
create_table "stock_takings", force: :cascade do |t|
create_table "stock_events", force: :cascade do |t|
t.integer "supplier_id", limit: 4
t.date "date"
t.text "note", limit: 65535
t.datetime "created_at"
t.text "note", limit: 65535
t.integer "invoice_id", limit: 4
t.string "type", null: false
end
add_index "stock_events", ["supplier_id"], name: "index_stock_events_on_supplier_id", using: :btree
create_table "supplier_categories", force: :cascade do |t|
t.string "name", limit: 255, null: false
t.string "description", limit: 255