Fixed updating order article in stockit balancing.

This commit is contained in:
Benjamin Meichsner 2013-03-17 18:33:04 +01:00
parent f7e885de7e
commit dd0bd0ad7e
3 changed files with 27 additions and 23 deletions

View file

@ -26,7 +26,7 @@ class Finance::OrderArticlesController < ApplicationController
@order = Order.find(params[:order_id]) @order = Order.find(params[:order_id])
@order_article = OrderArticle.find(params[:id]) @order_article = OrderArticle.find(params[:id])
begin 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 rescue
render action: :edit render action: :edit
end end

View file

@ -93,7 +93,7 @@ class OrderArticle < ActiveRecord::Base
end end
# Updates order_article and belongings during balancing process # 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 OrderArticle.transaction do
# Updates self # Updates self
self.update_attributes!(order_article_attributes) self.update_attributes!(order_article_attributes)
@ -102,20 +102,22 @@ class OrderArticle < ActiveRecord::Base
article.update_attributes!(article_attributes) article.update_attributes!(article_attributes)
# Updates article_price belonging to current order article # Updates article_price belonging to current order article
article_price.attributes = price_attributes if price_attributes.present?
if article_price.changed? article_price.attributes = price_attributes
# Updates also price attributes of article if update_current_price is selected if article_price.changed?
if update_current_price # Updates also price attributes of article if update_current_price is selected
article.update_attributes!(price_attributes) if update_current_price
self.article_price = article.article_prices.first # Assign new created article price to order article article.update_attributes!(price_attributes)
else self.article_price = article.article_prices.first # Assign new created article price to order article
# Creates a new article_price if neccessary else
# Set created_at timestamp to order ends, to make sure the current article price isn't changed # Creates a new article_price if neccessary
create_article_price!(price_attributes.merge(created_at: order.ends)) and save # Set created_at timestamp to order ends, to make sure the current article price isn't changed
end create_article_price!(price_attributes.merge(created_at: order.ends)) and save
end
# Updates ordergroup values # Updates ordergroup values
update_ordergroup_prices update_ordergroup_prices
end
end end
end end
end end

View file

@ -5,18 +5,20 @@
.modal-body .modal-body
= form.input :units_to_order = 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 :name
= f.input :order_number = f.input :order_number
= f.input :unit = 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 .modal-footer
= button_tag "Schließen", class: 'btn', data: {dismiss: 'modal'} = button_tag "Schließen", class: 'btn', data: {dismiss: 'modal'}
= form.submit class: 'btn btn-primary' = form.submit class: 'btn btn-primary'