diff --git a/app/controllers/deliveries_controller.rb b/app/controllers/deliveries_controller.rb index d8fac1bd..5ecb4888 100644 --- a/app/controllers/deliveries_controller.rb +++ b/app/controllers/deliveries_controller.rb @@ -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 diff --git a/app/helpers/stockit_helper.rb b/app/helpers/stockit_helper.rb index 9873f0de..f6d36896 100644 --- a/app/helpers/stockit_helper.rb +++ b/app/helpers/stockit_helper.rb @@ -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'), diff --git a/app/models/delivery.rb b/app/models/delivery.rb index ca6912b6..b4227317 100644 --- a/app/models/delivery.rb +++ b/app/models/delivery.rb @@ -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 diff --git a/app/models/stock_change.rb b/app/models/stock_change.rb index 139ecce3..7959864b 100644 --- a/app/models/stock_change.rb +++ b/app/models/stock_change.rb @@ -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 diff --git a/app/models/stock_event.rb b/app/models/stock_event.rb new file mode 100644 index 00000000..e98e5b5f --- /dev/null +++ b/app/models/stock_event.rb @@ -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 diff --git a/app/models/stock_taking.rb b/app/models/stock_taking.rb index 1150c7f7..e1b83e5e 100644 --- a/app/models/stock_taking.rb +++ b/app/models/stock_taking.rb @@ -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 diff --git a/app/views/deliveries/show.html.haml b/app/views/deliveries/show.html.haml index 0c925dc0..83f6f15b 100644 --- a/app/views/deliveries/show.html.haml +++ b/app/views/deliveries/show.html.haml @@ -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 diff --git a/db/migrate/20181204070000_create_stock_events.rb b/db/migrate/20181204070000_create_stock_events.rb new file mode 100644 index 00000000..179d8772 --- /dev/null +++ b/db/migrate/20181204070000_create_stock_events.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 554168e1..7a071118 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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