diff --git a/app/controllers/finance/order_articles_controller.rb b/app/controllers/finance/order_articles_controller.rb index 13c1106c..edd0fd82 100644 --- a/app/controllers/finance/order_articles_controller.rb +++ b/app/controllers/finance/order_articles_controller.rb @@ -26,7 +26,7 @@ class Finance::OrderArticlesController < ApplicationController @order = Order.find(params[:order_id]) @order_article = OrderArticle.find(params[:id]) begin - @order_article.update_article_and_price!(params[:article], params[:article_price], params[:order_article]) + @order_article.update_article_and_price!(params[:order_article], params[:article], params[:article_price]) rescue render action: :edit end diff --git a/app/models/order_article.rb b/app/models/order_article.rb index af257f5d..192d60d5 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -93,7 +93,7 @@ class OrderArticle < ActiveRecord::Base end # Updates order_article and belongings during balancing process - def update_article_and_price!(article_attributes, price_attributes, order_article_attributes) + def update_article_and_price!(order_article_attributes, article_attributes, price_attributes = nil) OrderArticle.transaction do # Updates self self.update_attributes!(order_article_attributes) @@ -102,20 +102,22 @@ class OrderArticle < ActiveRecord::Base article.update_attributes!(article_attributes) # Updates article_price belonging to current order article - article_price.attributes = price_attributes - if article_price.changed? - # Updates also price attributes of article if update_current_price is selected - if update_current_price - article.update_attributes!(price_attributes) - self.article_price = article.article_prices.first # Assign new created article price to order article - else - # Creates a new article_price if neccessary - # Set created_at timestamp to order ends, to make sure the current article price isn't changed - create_article_price!(price_attributes.merge(created_at: order.ends)) and save - end + if price_attributes.present? + article_price.attributes = price_attributes + if article_price.changed? + # Updates also price attributes of article if update_current_price is selected + if update_current_price + article.update_attributes!(price_attributes) + self.article_price = article.article_prices.first # Assign new created article price to order article + else + # Creates a new article_price if neccessary + # Set created_at timestamp to order ends, to make sure the current article price isn't changed + create_article_price!(price_attributes.merge(created_at: order.ends)) and save + end - # Updates ordergroup values - update_ordergroup_prices + # Updates ordergroup values + update_ordergroup_prices + end end end end diff --git a/app/views/finance/order_articles/_edit.html.haml b/app/views/finance/order_articles/_edit.html.haml index 13432620..c2b92be6 100644 --- a/app/views/finance/order_articles/_edit.html.haml +++ b/app/views/finance/order_articles/_edit.html.haml @@ -5,18 +5,20 @@ .modal-body = form.input :units_to_order - = simple_fields_for @order_article.article do |f| + = simple_fields_for :article, @order_article.article do |f| = f.input :name = f.input :order_number = f.input :unit - = simple_fields_for @order_article.article_price do |f| - = f.input :unit_quantity - = f.input :price - = f.input :tax - = f.input :deposit - - = form.input :update_current_price, as: :boolean + - if @order_article.article.is_a?(StockArticle) + %div.alert Preise von Lagerartikeln können nicht geändert werden! + - else + = simple_fields_for :article_price, @order_article.article_price do |f| + = f.input :unit_quantity + = f.input :price + = f.input :tax + = f.input :deposit + = form.input :update_current_price, as: :boolean .modal-footer = button_tag "Schließen", class: 'btn', data: {dismiss: 'modal'} = form.submit class: 'btn btn-primary' \ No newline at end of file