Last part of order refactoring. Now order_article create/update is possible.
This commit is contained in:
parent
4d796b8e73
commit
6202e05841
|
@ -5,3 +5,4 @@ db/*.sqlite3
|
|||
nbproject/
|
||||
config/environments/development.rb
|
||||
capfile
|
||||
config/environments/fcschinke09.rb
|
||||
|
|
|
@ -42,70 +42,86 @@ class Finance::BalancingController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
#TODO: Implement create/update of articles/article_prices...
|
||||
# def new_order_article
|
||||
# @order = Order.find(params[:id])
|
||||
# order_article = @order.order_articles.build(:tax => 7, :deposit => 0)
|
||||
# render :update do |page|
|
||||
# page["edit_box"].replace_html :partial => "new_order_article", :locals => {:order_article => order_article}
|
||||
# page["edit_box"].show
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# def create_order_article
|
||||
# @order = Order.find(params[:order_article][:order_id])
|
||||
# order_article = OrderArticle.new(params[:order_article])
|
||||
#
|
||||
# render :update do |page|
|
||||
# if order_article.save
|
||||
# page["edit_box"].hide
|
||||
# page["summary"].replace_html :partial => 'summary'
|
||||
# page.insert_html :bottom, "result_table", :partial => "order_article_result", :locals => {:order_article => order_article}
|
||||
# page["order_article_#{order_article.id}"].visual_effect :highlight, :duration => 2
|
||||
# page["group_order_articles_#{order_article.id}"].show
|
||||
# else
|
||||
# page["edit_box"].replace_html :partial => "new_order_article", :locals => {:order_article => order_article}
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# def editArticleResult
|
||||
# @article = OrderArticleResult.find(params[:id])
|
||||
# render :update do |page|
|
||||
# page["edit_box"].replace_html :partial => 'editArticleResult'
|
||||
# page["edit_box"].show
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# def updateArticleResult
|
||||
# @article = OrderArticleResult.find(params[:id])
|
||||
# @article.attributes=(params[:order_article_result]) # update attributes but doesn't save
|
||||
# @article.make_gross
|
||||
# @order = @article.order
|
||||
# @ordered_articles = @order.order_article_results
|
||||
# @group_orders = @order.group_order_results
|
||||
# render :update do |page|
|
||||
# if @article.save
|
||||
# page["edit_box"].hide
|
||||
# page["summary"].replace_html :partial => 'summary'
|
||||
# page["summary"].visual_effect :highlight, :duration => 2
|
||||
# page["order_article_result_#{@article.id}"].replace_html :partial => 'articleResult'
|
||||
# page['order_article_result_'+@article.id.to_s].visual_effect :highlight, :delay => 0.5, :duration => 2
|
||||
# page["group_order_article_results_#{@article.id}"].replace_html :partial => "groupOrderArticleResults"
|
||||
# else
|
||||
# page['edit_box'].replace_html :partial => 'editArticleResult'
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
def new_order_article
|
||||
@order = Order.find(params[:id])
|
||||
render :update do |page|
|
||||
page["edit_box"].replace_html :partial => "new_order_article"
|
||||
page["edit_box"].show
|
||||
end
|
||||
end
|
||||
|
||||
def auto_complete_for_article_name
|
||||
order = Order.find(params[:order_id])
|
||||
type = order.stockit? ? "type = 'StockArticle'" : "type IS NULL"
|
||||
@articles = Article.find(:all,
|
||||
:conditions => [ "supplier_id = ? AND #{type} AND LOWER(name) LIKE ?",
|
||||
order.supplier_id,
|
||||
'%' + params[:article][:name].downcase + '%' ],
|
||||
:order => 'name ASC',
|
||||
:limit => 8)
|
||||
render :partial => 'shared/auto_complete_articles'
|
||||
|
||||
end
|
||||
|
||||
def create_order_article
|
||||
@order = Order.find(params[:order_id])
|
||||
order_article = @order.order_articles.find_by_article_id(params[:order_article][:article_id])
|
||||
|
||||
unless order_article
|
||||
# Article wasn't already assigned with this order, lets create a new one
|
||||
order_article = @order.order_articles.build(params[:order_article])
|
||||
order_article.article_price = order_article.article.article_prices.first
|
||||
end
|
||||
# Set units to order to 1, so the article is visible on page
|
||||
order_article.units_to_order = 1
|
||||
|
||||
render :update do |page|
|
||||
if order_article.save
|
||||
page["edit_box"].hide
|
||||
page.insert_html :top, "result_table", :partial => "order_article_result", :locals => {:order_article => order_article}
|
||||
page["order_article_#{order_article.id}"].visual_effect :highlight, :duration => 2
|
||||
page["group_order_articles_#{order_article.id}"].show
|
||||
else
|
||||
page["edit_box"].replace_html :partial => "new_order_article"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def edit_order_article
|
||||
@order_article = OrderArticle.find(params[:id])
|
||||
render :update do |page|
|
||||
page["edit_box"].replace_html :partial => 'edit_order_article'
|
||||
page["edit_box"].show
|
||||
end
|
||||
end
|
||||
|
||||
# Update this article and creates a new articleprice if neccessary
|
||||
def update_order_article
|
||||
@order_article = OrderArticle.find(params[:id])
|
||||
begin
|
||||
@order_article.update_article_and_price!(params[:article], params[:price], params[:order_article])
|
||||
render :update do |page|
|
||||
page["edit_box"].hide
|
||||
page["summary"].replace_html :partial => 'summary', :locals => {:order => @order_article.order}
|
||||
page["summary"].visual_effect :highlight, :duration => 2
|
||||
page["order_article_#{@order_article.id}"].replace_html :partial => 'order_article', :locals => {:order_article => @order_article}
|
||||
page["order_article_#{@order_article.id}"].visual_effect :highlight, :delay => 0.5, :duration => 2
|
||||
page["group_order_articles_#{@order_article.id}"].replace_html :partial => "group_order_articles", :locals => {:order_article => @order_article}
|
||||
end
|
||||
rescue => @error
|
||||
render :update do |page|
|
||||
page['edit_box'].replace_html :partial => 'edit_order_article'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def destroy_order_article
|
||||
order_article = OrderArticle.find(params[:id])
|
||||
order_article.destroy
|
||||
@order = order_article.order
|
||||
render :update do |page|
|
||||
page["order_article_#{order_article.id}"].remove
|
||||
page["group_order_articles_#{order_article.id}"].remove
|
||||
page["summary"].replace_html :partial => 'summary', :locals => {:order => @order}
|
||||
page["summary"].replace_html :partial => 'summary', :locals => {:order => order_article.order}
|
||||
page["summary"].visual_effect :highlight, :duration => 2
|
||||
end
|
||||
end
|
||||
|
@ -139,6 +155,7 @@ class Finance::BalancingController < ApplicationController
|
|||
goa.group_order.update_price! # Update the price attribute of new GroupOrder
|
||||
order_article.update_results! if order_article.article.is_a?(StockArticle) # Update units_to_order of order_article
|
||||
page["edit_box"].hide
|
||||
page["order_article_#{order_article.id}"].replace_html :partial => 'order_article', :locals => {:order_article => order_article}
|
||||
|
||||
page["group_order_articles_#{order_article.id}"].replace_html :partial => 'group_order_articles',
|
||||
:locals => {:order_article => order_article}
|
||||
|
@ -168,6 +185,7 @@ class Finance::BalancingController < ApplicationController
|
|||
goa.order_article.update_results! if goa.order_article.article.is_a?(StockArticle) # Update units_to_order of order_article
|
||||
|
||||
page["edit_box"].hide
|
||||
page["order_article_#{goa.order_article.id}"].replace_html :partial => 'order_article', :locals => {:order_article => goa.order_article}
|
||||
page["group_order_articles_#{goa.order_article.id}"].replace_html :partial => 'group_order_articles',
|
||||
:locals => {:order_article => goa.order_article}
|
||||
page["summary"].replace_html :partial => 'summary', :locals => {:order => goa.order_article.order}
|
||||
|
@ -186,6 +204,7 @@ class Finance::BalancingController < ApplicationController
|
|||
|
||||
render :update do |page|
|
||||
page["edit_box"].hide
|
||||
page["order_article_#{goa.order_article.id}"].replace_html :partial => 'order_article', :locals => {:order_article => goa.order_article}
|
||||
page["group_order_articles_#{goa.order_article.id}"].replace_html :partial => 'group_order_articles',
|
||||
:locals => {:order_article => goa.order_article}
|
||||
page["summary"].replace_html :partial => 'summary', :locals => {:order => goa.order_article.order}
|
||||
|
|
|
@ -17,6 +17,21 @@ class ArticlePrice < ActiveRecord::Base
|
|||
belongs_to :article
|
||||
has_many :order_articles
|
||||
|
||||
# Custom attribute setter that accepts decimal numbers using localized decimal separator.
|
||||
def price=(price)
|
||||
self[:price] = String.delocalized_decimal(price)
|
||||
end
|
||||
|
||||
# Custom attribute setter that accepts decimal numbers using localized decimal separator.
|
||||
def tax=(tax)
|
||||
self[:tax] = String.delocalized_decimal(tax)
|
||||
end
|
||||
|
||||
# Custom attribute setter that accepts decimal numbers using localized decimal separator.
|
||||
def deposit=(deposit)
|
||||
self[:deposit] = String.delocalized_decimal(deposit)
|
||||
end
|
||||
|
||||
# The financial gross, net plus tax and deposit.
|
||||
def gross_price
|
||||
((price + deposit) * (tax / 100 + 1))
|
||||
|
|
|
@ -21,18 +21,11 @@ class OrderArticle < ActiveRecord::Base
|
|||
belongs_to :article_price
|
||||
has_many :group_order_articles, :dependent => :destroy
|
||||
|
||||
validates_presence_of :order_id
|
||||
validates_presence_of :article_id
|
||||
validates_uniqueness_of :article_id, :scope => :order_id # an article can only have one record per order
|
||||
validates_presence_of :order_id, :article_id
|
||||
validate :article_and_price_exist
|
||||
|
||||
named_scope :ordered, :conditions => "units_to_order >= 1"
|
||||
|
||||
# TODO: How to create/update articles/article_prices during balancing
|
||||
# # Accessors for easy create of new order_articles in balancing process
|
||||
# attr_accessor :name, :order_number, :units_to_order, :unit_quantity, :unit, :net_price, :tax, :deposit
|
||||
#
|
||||
# before_validation_on_create :create_new_article
|
||||
|
||||
# This method returns either the ArticlePrice or the Article
|
||||
# The first will be set, when the the order is finished
|
||||
|
@ -88,16 +81,33 @@ class OrderArticle < ActiveRecord::Base
|
|||
(units_to_order * price.unit_quantity) == group_orders_sum[:quantity]
|
||||
end
|
||||
|
||||
# Updates order_article and belongings during balancing process
|
||||
def update_article_and_price!(article_attributes, price_attributes, order_article_attributes)
|
||||
OrderArticle.transaction do
|
||||
# Updates article
|
||||
article.update_attributes!(article_attributes)
|
||||
|
||||
article_price.attributes = price_attributes
|
||||
if article_price.changed?
|
||||
# Creates a new article_price if neccessary
|
||||
price = build_article_price(price_attributes)
|
||||
price.created_at = order.ends
|
||||
price.save!
|
||||
|
||||
# Updates ordergroup values
|
||||
group_order_articles.each { |goa| goa.group_order.update_price! }
|
||||
end
|
||||
|
||||
# Updates units_to_order
|
||||
self.attributes = order_article_attributes
|
||||
self.save!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def article_and_price_exist
|
||||
errors.add(:article, "muss angegeben sein und einen aktuellen Preis haben") if !(article = Article.find(article_id)) || article.fc_price.nil?
|
||||
end
|
||||
|
||||
# def create_new_article
|
||||
# old_article = order.articles.find_by_name(name) # Check if there is already an Article with this name
|
||||
# unless old_article
|
||||
# self.article.build
|
||||
# end
|
||||
# end
|
||||
end
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
%h2
|
||||
Bearbeiten von
|
||||
= @article.name
|
||||
|
||||
- remote_form_for 'order_article_result', @article, :url => {:action => 'updateArticleResult', :id => @article }, |
|
||||
:before => "Element.show('loader')", :success => "Element.hide('loader')" do |@form| |
|
||||
|
||||
= render :partial => "articleResultForm"
|
|
@ -0,0 +1,7 @@
|
|||
%h2
|
||||
Bearbeiten von
|
||||
= @order_article.article.name
|
||||
|
||||
- remote_form_for :order_article, :url => {:action => 'update_order_article', :id => @order_article }, |
|
||||
:before => "Element.show('loader')", :success => "Element.hide('loader')" do |
|
||||
= render :partial => "order_article_form"
|
|
@ -1,7 +1,7 @@
|
|||
%p{:style => "float:left"}
|
||||
%b Lieferung bearbeiten
|
||||
%p{:style => "float:right"}
|
||||
//= remote_link_to "Artikel hinzufügen", :url => {:action => "newArticleResult", :id => @order}
|
||||
= remote_link_to "Artikel hinzufügen", :url => {:action => "new_order_article", :id => @order}
|
||||
|
||||
%table{:class => "ordered_articles", :style => "clear:both"}
|
||||
%thead
|
||||
|
|
|
@ -1,7 +1,15 @@
|
|||
%h2
|
||||
Neuer gelieferter Artikel die Bestellung
|
||||
|
||||
- remote_form_for order_article, :url => {:action => 'create_order_article' }, |
|
||||
- remote_form_for :order_article, :url => {:action => 'create_order_article', :order_id => @order.id}, |
|
||||
:before => "Element.show('loader')", :success => "Element.hide('loader')" do |form| |
|
||||
|
||||
= render :partial => 'order_article_form', :locals => {:form => form}
|
||||
%p
|
||||
Suche im Katalog
|
||||
= text_field_with_auto_complete :article, :name, {}, |
|
||||
{:url => {:action => 'auto_complete_for_article_name', :order_id => @order.id}, |
|
||||
:after_update_element => 'setHiddenId'} |
|
||||
%p
|
||||
= form.hidden_field :article_id, :id => 'hidden_id'
|
||||
= submit_tag "Neuen Artikel hinzufügen"
|
||||
|
|
||||
= link_to_function "Abbrechen", "Element.hide('edit_box')"
|
|
@ -13,8 +13,8 @@
|
|||
%td= order_article.price.tax
|
||||
%td= order_article.price.deposit
|
||||
%td
|
||||
//= remote_link_to icon(:edit), |
|
||||
//:url => {:action => 'edit_order_article', :id => order_article} |
|
||||
= remote_link_to icon(:edit), |
|
||||
:url => {:action => 'edit_order_article', :id => order_article} |
|
||||
%td
|
||||
= remote_link_to icon(:delete), :confirm => 'Bist du sicher?', |
|
||||
:url => {:action => 'destroy_order_article', :id => order_article}, |
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
= form.error_messages
|
||||
- if @error
|
||||
%b= @error
|
||||
%table
|
||||
%tr
|
||||
%th Name
|
||||
%th Nr.
|
||||
%th
|
||||
%abbr{:title=>"Anzahl gelieferter Gebinde"} Menge
|
||||
%th GebGr
|
||||
%th Einheit
|
||||
%th GebGr
|
||||
%th netto
|
||||
%th MwSt.
|
||||
%th Pfand
|
||||
%tr
|
||||
%td= form.text_field 'name', :size => 20
|
||||
%td= form.text_field 'order_number', :size => 3
|
||||
%td= form.text_field 'units_to_order', :size => 5
|
||||
%td= form.text_field 'unit_quantity', :size => 3
|
||||
%td= form.text_field 'unit', :size => 5
|
||||
%td= form.text_field 'net_price', :size => 3
|
||||
%td= form.text_field 'tax', :size => 3
|
||||
%td= form.text_field 'deposit', :size => 3
|
||||
= form.hidden_field "order_id"
|
||||
%td= text_field_tag 'article[name]', @order_article.article.name, :size => 20
|
||||
%td= text_field_tag 'article[order_number]', @order_article.article.order_number, :size => 3
|
||||
%td= text_field_tag 'order_article[units_to_order]', @order_article.units_to_order, :size => 5
|
||||
%td= text_field_tag 'article[unit]', @order_article.article.unit, :size => 5
|
||||
%td= text_field_tag 'price[unit_quantity]', @order_article.price.unit_quantity, :size => 3
|
||||
%td= text_field_tag 'price[price]', @order_article.price.price, :size => 3
|
||||
%td= text_field_tag 'price[tax]', @order_article.price.tax, :size => 3
|
||||
%td= text_field_tag 'price[deposit]', @order_article.price.deposit, :size => 3
|
||||
%br/
|
||||
= submit_tag "Speichern"
|
||||
|
|
||||
|
|
|
@ -44,7 +44,9 @@
|
|||
Aktionen:
|
||||
- if @order.open?
|
||||
= link_to "Bearbeiten", edit_order_path(@order)
|
||||
|
|
||||
= link_to 'Beenden!', finish_order_path(@order), :method => :post, :confirm => "Willst Du wirklich die Bestellung beenden?\nEs gibt kein zurück.."
|
||||
|
|
||||
- unless @order.closed?
|
||||
= link_to "Löschen", @order, :confirm => "Willst du wirklich die Bestellung löschen?", :method => :delete
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
%ul.autocomplete
|
||||
- for article in @articles
|
||||
%li{:id => article.id.to_s}= "#{article.name} (#{article.unit_quantity} * #{article.unit} | #{number_to_currency(article.price)})"
|
|
@ -1,4 +1,4 @@
|
|||
class RefactorOrderLogic < ActiveRecord::Migration
|
||||
class RoadToVersionThree < ActiveRecord::Migration
|
||||
def self.up
|
||||
# TODO: Combine migrations since foodsoft3-development into one file
|
||||
# and try to build a migration path from old data.
|
|
@ -45,3 +45,10 @@ function redirectTo(newLoc) {
|
|||
document.location.href = nextPage
|
||||
}
|
||||
}
|
||||
|
||||
// Use with auto_complete to set a unique id,
|
||||
// e.g. when the user selects a (may not unique) name
|
||||
// There must be a hidden field with the id 'hidden_field'
|
||||
function setHiddenId(text, li) {
|
||||
$('hidden_id').value = li.id;
|
||||
}
|
Loading…
Reference in New Issue