diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index 61812c0f..1321da89 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -1,10 +1,10 @@ class StockitController < ApplicationController before_action :new_empty_article_ratio, only: [:edit, :update, :new, :create, :derive, :copy] before_action :load_article_units, only: [:edit, :update, :new, :create, :derive, :copy] + before_action :prepare_params_for_create_update, only: [:create, :update] def index - @stock_articles = StockArticle.undeleted.joins(:supplier, latest_article_version: [:article_category]) - .order('suppliers.name, article_categories.name, article_versions.name') + @stock_articles = StockArticle.with_latest_versions_and_categories.joins(:supplier).order('suppliers.name, article_categories.name, article_versions.name').undeleted end def index_on_stock_article_create # See publish/subscribe design pattern in /doc. @@ -19,6 +19,11 @@ def index_on_stock_article_update # See publish/subscribe design pattern in /doc render :layout => false end + def show + @stock_article = StockArticle.find(params[:id]) + @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC') + end + # three possibilites to fill a new_stock_article form # (1) start from blank or use params def new @@ -46,10 +51,15 @@ def derive render :layout => false end + def edit + @stock_article = StockArticle.find(params[:id]) + + render :layout => false + end + def create StockArticle.transaction do - @stock_article = StockArticle.create(quantity: 0, supplier_id: params[:article_version][:article][:supplier_id]) - params[:article_version].delete :article + @stock_article = StockArticle.create(quantity: 0, supplier_id: @supplier_id) @stock_article.attributes = { latest_article_version_attributes: params[:article_version] } @stock_article.save end @@ -58,28 +68,15 @@ def create render :action => 'new', :layout => false end - def edit - @stock_article = StockArticle.find(params[:id]) - - render :layout => false - end - def update @stock_article = StockArticle.find(params[:id]) - supplier_id = params[:article_version][:article][:supplier_id] - params[:article_version].delete :article article_version_attributes = params[:article_version] - @stock_article.update_attributes!(supplier_id: supplier_id, latest_article_version_attributes: article_version_attributes) + @stock_article.update_attributes!(supplier_id: @supplier_id, latest_article_version_attributes: article_version_attributes) render :layout => false rescue ActiveRecord::RecordInvalid render :action => 'edit', :layout => false end - def show - @stock_article = StockArticle.find(params[:id]) - @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC') - end - def show_on_stock_article_update # See publish/subscribe design pattern in /doc. @stock_article = StockArticle.find(params[:id]) @@ -112,4 +109,14 @@ def new_empty_article_ratio @empty_article_unit_ratio.article_version = @article.latest_article_version unless @article.nil? @empty_article_unit_ratio.sort = -1 end + + def prepare_params_for_create_update + @supplier_id = params[:article_version][:article][:supplier_id] + params[:article_version].delete :article + + # Stock article form doesn't have some of the unit fields -> set them to supplier_order_unit: + params[:article_version][:group_order_unit] = params[:article_version][:supplier_order_unit] + params[:article_version][:billing_unit] = params[:article_version][:supplier_order_unit] + params[:article_version][:price_unit] = params[:article_version][:supplier_order_unit] + end end diff --git a/app/models/order_article.rb b/app/models/order_article.rb index 9d22a2ee..26abce82 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -213,8 +213,6 @@ def update_or_create_article_version(version_attributes, original_article_versio modifying_earlier_version = self.article_version.article.latest_article_version.id != self.article_version_id finished_order_article_using_same_version = OrderArticle.belonging_to_finished_order.where(article_version_id: self.article_version_id).where.not(id: self.id) - require 'byebug' - byebug if (!update_global_price && modifying_earlier_version && !finished_order_article_using_same_version.exists?) || (update_global_price && !modifying_earlier_version) # update in place: diff --git a/db/migrate/20221026102301_alter_articles_add_more_unit_logic.rb b/db/migrate/20221026102301_alter_articles_add_more_unit_logic.rb index 1eb66c95..70a4e73d 100644 --- a/db/migrate/20221026102301_alter_articles_add_more_unit_logic.rb +++ b/db/migrate/20221026102301_alter_articles_add_more_unit_logic.rb @@ -36,7 +36,7 @@ def up new_version_id = insert(%{ INSERT INTO article_versions ( - article_id, price, tax, deposit, created_at, name, article_category_id, note, availability, manufacturer, origin, order_number, updated_at, supplier_order_unit, group_order_granularity, group_order_unit) + article_id, price, tax, deposit, created_at, name, article_category_id, note, availability, manufacturer, origin, order_number, updated_at, supplier_order_unit, group_order_granularity, group_order_unit, billing_unit, price_unit) VALUES( #{quote latest_article_version['article_id']}, #{quote latest_article_version['price'].to_f * unit_quantity}, @@ -53,7 +53,9 @@ def up NOW(), #{quote new_unit_data[:supplier_order_unit]}, #{quote new_unit_data[:group_order_granularity]}, - #{quote new_unit_data[:group_order_unit]} + #{quote new_unit_data[:group_order_unit]}, + #{quote new_unit_data[:supplier_order_unit]}, + #{quote new_unit_data[:supplier_order_unit]} ) })