Skip to content

Commit

Permalink
Fixes conversion error with stock articles
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Oct 12, 2023
1 parent a4316a1 commit 67c0e38
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
43 changes: 25 additions & 18 deletions app/controllers/stockit_controller.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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])

Expand Down Expand Up @@ -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
2 changes: 0 additions & 2 deletions app/models/order_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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]}
)
})

Expand Down

0 comments on commit 67c0e38

Please sign in to comment.