Simplifiying balancing view part 2. Lets call it finished.

This commit is contained in:
benni 2012-07-27 18:03:46 +02:00
parent dc0d1b3e46
commit af3c07f031
26 changed files with 185 additions and 219 deletions

View file

@ -13,7 +13,8 @@ class Finance::BalancingController < ApplicationController
end
def new
@order = Order.find(params[:id])
@order = Order.find(params[:order_id])
flash.now.alert = "Achtung, Bestellung wurde schon abgerechnet" if @order.closed?
@comments = @order.comments
if params['sort']
@ -35,131 +36,20 @@ class Finance::BalancingController < ApplicationController
@articles = @articles.to_a.sort { |a,b| b.article.order_number.gsub(/[^[:digit:]]/, "").to_i <=> a.article.order_number.gsub(/[^[:digit:]]/, "").to_i }
end
view = params[:view]
params[:view] = nil
case view.try(:to_sym)
when 'editResults'
render :partial => 'edit_results_by_articles' and return
when :groups_overview
render :partial => 'shared/articles_by_groups', :locals => {:order => @order} and return
when 'articlesOverview'
render :partial => 'shared/articles_by_articles', :locals => {:order => @order} and return
end
render layout: false if request.xhr?
end
def new_group_order_article
goa = OrderArticle.find(params[:id]).group_order_articles.build
render :update do |page|
page["edit_box"].replace_html :partial => "new_group_order_article",
:locals => {:group_order_article => goa}
page["edit_box"].show
end
def edit_note
@order = Order.find(params[:id])
render :layout => false
end
# Creates a new GroupOrderArticle
# If the the chosen Ordergroup hasn't ordered yet, a GroupOrder will also be created
#FIXME: Clean up this messy code !
def create_group_order_article
goa = GroupOrderArticle.new(params[:group_order_article])
order_article = goa.order_article
order = order_article.order
# creates a new GroupOrder if necessary
group_order = GroupOrder.first :conditions => {:order_id => order.id, :ordergroup_id => goa.ordergroup_id}
unless group_order
goa.create_group_order(:order_id => order.id, :ordergroup_id => goa.ordergroup_id)
def update_note
@order = Order.find(params[:id])
if @order.update_attributes(params[:order])
render :layout => false
else
goa.group_order = group_order
end
# If there is an GroupOrderArticle already, only update result attribute.
if group_order_article = GroupOrderArticle.first(:conditions => {:group_order_id => goa.group_order, :order_article_id => goa.order_article})
goa = group_order_article
goa.result = params[:group_order_article]["result"]
end
render :update do |page|
if goa.save
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}
page["group_order_article_#{goa.id}"].visual_effect :highlight, :duration => 2
page["summary"].replace_html :partial => 'summary', :locals => {:order => order}
page["order_profit"].visual_effect :highlight, :duration => 2
else
page["edit_box"].replace_html :partial => "new_group_order_article",
:locals => {:group_order_article => goa}
end
end
end
def edit_group_order_article
group_order_article = GroupOrderArticle.find(params[:id])
render :partial => 'edit_group_order_article',
:locals => {:group_order_article => group_order_article}
end
def update_group_order_article
goa = GroupOrderArticle.find(params[:id])
render :update do |page|
if goa.update_attributes(params[:group_order_article])
goa.group_order.update_price! # Update the price attribute of new GroupOrder
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}
page["order_profit"].visual_effect :highlight, :duration => 2
else
page["edit_box"].replace_html :partial => 'edit_group_order_article'
end
end
end
def update_group_order_article_result
goa = GroupOrderArticle.find(params[:id])
if params[:modifier] == '-'
goa.update_attributes({:result => goa.result - 1})
elsif params[:modifier] == '+'
goa.update_attributes({:result => goa.result + 1})
end
render :update do |page|
goa.group_order.update_price! # Update the price attribute of new GroupOrder
goa.order_article.update_results! if goa.order_article.article.is_a?(StockArticle) # Update units_to_order of order_article
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}
page["order_profit"].visual_effect :highlight, :duration => 2
end
end
def destroy_group_order_article
goa = GroupOrderArticle.find(params[:id])
goa.destroy
goa.group_order.update_price! # Updates the price attribute of new GroupOrder
goa.order_article.update_results! if goa.order_article.article.is_a?(StockArticle) # Update units_to_order of order_article
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}
page["order_profit"].visual_effect :highlight, :duration => 2
render :action => :edit_note, :layout => false
end
end
@ -171,27 +61,20 @@ class Finance::BalancingController < ApplicationController
# Balances the Order, Update of the Ordergroup.account_balances
def close
@order = Order.find(params[:id])
begin
@order.close!(@current_user)
flash[:notice] = "Bestellung wurde erfolgreich abgerechnet, die Kontostände aktualisiert."
redirect_to :action => "index"
rescue => e
flash[:error] = "Ein Fehler ist beim Abrechnen aufgetreten: " + e
redirect_to :action => "new", :id => @order
end
@order.close!(@current_user)
redirect_to finance_balancing_url, notice: "Bestellung wurde erfolgreich abgerechnet, die Kontostände aktualisiert."
rescue => error
redirect_to finance_balancing_url, alert: "Ein Fehler ist beim Abrechnen aufgetreten: #{error.message}"
end
# Close the order directly, without automaticly updating ordergroups account balances
def close_direct
@order = Order.find(params[:id])
if @order.finished?
@order.update_attributes(:state => 'closed', :updated_by => @current_user)
flash[:notice] = 'Die Bestellung wurde auf "gebucht" gesetzt.'
redirect_to :action => 'listOrders', :id => @order
else
flash[:error] = 'Die Bestellung ist noch nicht beendet.'
redirect_to :action => 'listOrders', :id => @order
end
@order.close_direct!(@current_user)
redirect_to finance_balancing_url, notice: "Bestellung wurde geschlossen."
rescue => error
redirect_to finance_balancing_url, alert: "Bestellung kann nicht geschlossen werden: #{error.message}"
end
end

View file

@ -6,5 +6,78 @@ class Finance::GroupOrderArticlesController < ApplicationController
def new
@order_article = OrderArticle.find(params[:order_article_id])
@group_order_article = GroupOrderArticle.new(order_article: @order_article)
end
def create
@group_order_article = GroupOrderArticle.new(params[:group_order_article])
@order_article = @group_order_article.order_article
# As we hide group_order_articles with a result of 0, we should not complain, when an existing group_order_article is found
goa = GroupOrderArticle.where(group_order_id: @group_order_article.group_order_id,
order_article_id: @order_article.id).first
if goa and goa.update_attributes(params[:group_order_article])
@group_order_article = goa
update_summaries(@group_order_article)
render :update
elsif @group_order_article.save
update_summaries(@group_order_article)
render :update
else # Validation failed, show form
render :new
end
end
def edit
@group_order_article = GroupOrderArticle.find(params[:id])
@order_article = @group_order_article.order_article
end
def update
@group_order_article = GroupOrderArticle.find(params[:id])
@order_article = @group_order_article.order_article
if @group_order_article.update_attributes(params[:group_order_article])
update_summaries(@group_order_article)
else
render :edit
end
end
def update_result
group_order_article = GroupOrderArticle.find(params[:id])
@order_article = group_order_article.order_article
if params[:modifier] == '-'
group_order_article.update_attribute :result, group_order_article.result - 1
elsif params[:modifier] == '+'
group_order_article.update_attribute :result, group_order_article.result + 1
end
update_summaries(group_order_article)
render :update
end
def destroy
group_order_article = GroupOrderArticle.find(params[:id])
group_order_article.destroy
update_summaries(group_order_article)
@order_article = group_order_article.order_article
render :update
end
protected
def update_summaries(group_order_article)
# Update the price attribute of new GroupOrder
group_order_article.group_order.update_price!
# Update units_to_order of order_article
group_order_article.order_article.update_results! if group_order_article.order_article.article.is_a?(StockArticle)
end
end

View file

@ -2,7 +2,7 @@ class Finance::OrderArticlesController < ApplicationController
before_filter :authenticate_finance
layout false # We only use this controller to server js snippets, no need for layout rendering
layout false # We only use this controller to serve js snippets, no need for layout rendering
def new
@order = Order.find(params[:order_id])

View file

@ -0,0 +1,13 @@
module Finance::BalancingHelper
def balancing_view_partial
view = params[:view] || 'edit_results'
case view
when 'edit_results' then
'edit_results_by_articles'
when 'groups_overview' then
'shared/articles_by_groups'
when 'articles_overview' then
'shared/articles_by_articles'
end
end
end

View file

@ -1,2 +1,10 @@
module Finance::OrderArticlesHelper
def new_order_articles_collection
if @order.stockit?
StockArticle.order(:name)
else
@order.supplier.articles.order(:name)
end
end
end

View file

@ -18,6 +18,16 @@ class GroupOrderArticle < ActiveRecord::Base
localize_input_of :result
# Setter used in group_order_article#new
# We have to create an group_order, if the ordergroup wasn't involved in the order yet
def ordergroup_id=(id)
self.group_order = GroupOrder.find_or_initialize_by_order_id_and_ordergroup_id(order_article.order_id, id)
end
def ordergroup_id
group_order.try(:ordergroup_id)
end
# Updates the quantity/tolerance for this GroupOrderArticle by updating both GroupOrderArticle properties
# and the associated GroupOrderArticleQuantities chronologically.
#

View file

@ -194,6 +194,12 @@ class Order < ActiveRecord::Base
end
end
# Close the order directly, without automaticly updating ordergroups account balances
def close_direct!(user)
raise "Bestellung wurde schon abgerechnet" if closed?
update_attributes! state: 'closed', updated_by: user
end
protected
def starts_before_ends

View file

@ -1,21 +0,0 @@
%h2 Mengenänderung
%p
= group_order_article.group_order.ordergroup.name
hat von
= group_order_article.order_article.article.name
bekommen:
- remote_form_for group_order_article, |
:url => {:action => 'update_group_order_article', :id => group_order_article }, |
:before => "Element.show('loader')", :success => "Element.hide('loader')" do |form| |
= form.error_messages
%p
%b Menge:
(Einheit:
= group_order_article.order_article.article.unit
)
= form.text_field :result, :size => "6"
= submit_tag "Speichern"
|
= link_to_function 'Abbrechen', "Element.hide('edit_box')"

View file

@ -1,7 +0,0 @@
%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"

View file

@ -7,7 +7,7 @@
%td Einheiten
%td Gesamtpreis
%td{:colspan => "3",:style => "width:14em"}
= link_to '[Gruppe hinzufügen]', new_finance_group_order_article_path(order_id: order_article.order_id),
= link_to '[Gruppe hinzufügen]', new_finance_group_order_article_path(order_article_id: order_article.id),
remote: true
%tbody
- for group_order_article in order_article.group_order_articles.ordered.all(:include => [:group_order])
@ -17,19 +17,17 @@
= group_order_article.group_order.ordergroup.name
%td{:id => "group_order_article_#{group_order_article.id}_quantity", :style => "white-space:nowrap"}
= group_order_article.result
/= button_to_remote( "+", :url => {:action => "update_group_order_article_result", :id => group_order_article, :modifier => '+'}, :html => {:style => "float:left"}, :success => "Element.hide('loader');", :before => "Element.show('loader');")
/= button_to_remote( "-", :url => {:action => "update_group_order_article_result", :id => group_order_article, :modifier => '-'}, :success => "Element.hide('loader');", :before => "Element.show('loader');")
= button_to "+", update_result_finance_group_order_article_path(group_order_article, modifier: '+'),
method: :put, remote: true, style: 'float:left'
= button_to "-", update_result_finance_group_order_article_path(group_order_article, modifier: '-'),
method: :put, remote: true
%td.currency
= number_to_currency(group_order_article.order_article.price.fc_price * group_order_article.result, :unit => "")
%td.actions{:style=>"width:1em"}
= link_to icon(:edit), '#'
/:update => 'edit_box', |
/:url => {:action => 'edit_group_order_article', :id => group_order_article}, |
/:success => "Element.hide('loader'); Element.show('edit_box')" |
= link_to icon(:edit), edit_finance_group_order_article_path(group_order_article), remote: true
%td.actions{:style=>"width:1em"}
= link_to icon(:delete), '#'
/:url => {:action => 'destroy_group_order_article', :id => group_order_article}, |
/:confirm => 'Bist du sicher?', :method => 'post' |
= link_to icon(:delete), finance_group_order_article_path(group_order_article), method: :delete,
remote: true
%td
%tfoot
%tr{:class => cycle('even', 'odd', :name => 'results')}

View file

@ -1,17 +0,0 @@
%h2
Neue Gruppenmenge für
= group_order_article.order_article.article.name
- remote_form_for group_order_article, :url => {:action => 'create_group_order_article'}, |
:before => "Element.show('loader')", :success => "Element.hide('loader')" do |form| |
= form.error_messages
%p
Gruppe:
= form.select :ordergroup_id, Ordergroup.all(:order => "name").collect{ |og| [og.name, og.id] }
%p
Menge:
= form.text_field :result, :size => 5
= form.hidden_field :order_article_id
%p
= submit_tag "Speichern"
|
= link_to_function 'Abbrechen', "Element.hide('edit_box')"

View file

@ -19,7 +19,7 @@
%td= order_article.price.tax
%td= order_article.price.deposit
%td
= link_to icon(:edit), edit_finance_order_order_article_path(@order, order_article), remote: true
= link_to icon(:edit), edit_finance_order_order_article_path(order_article.order, order_article), remote: true
%td
= link_to icon(:delete), finance_order_order_article_path(@order, order_article), method: :delete,
= link_to icon(:delete), finance_order_order_article_path(order_article.order, order_article), method: :delete,
remote: true, confirm: 'Bist du sicher?'

View file

@ -11,6 +11,6 @@
%td= number_to_currency(group_order.price)
%p
%div{:style => "float:left"}
= button_to "Abschließen", :action => "close", :id => @order
= button_to "Abschließen", close_finance_order_path(@order), method: :put
%div{:style => "float:right"}
= link_to 'Zurück zur Abrechnung', :action => 'new', :id => @order
= link_to 'Zurück zur Abrechnung', new_finance_order_path(order_id: @order.id)

View file

@ -58,6 +58,6 @@
%td= order.name
%td= format_date(order.ends)
%td{:class => "currency"}= number_to_currency(order.sum(:fc))
%td= link_to "abrechnen", new_finance_order_path(id: order)
%td= link_to "abrechnen", new_finance_order_path(order_id: order.id)
- else
Super, alles schon abgerechnet...

View file

@ -1,7 +1,4 @@
- title "beendete Bestellungen"
%p{:style => "width:30em"}
%i
Beschreibungstext für die Abrechnunsmodi ....
.left_column{:style => "width:70em"}
.box_title
.column_content
@ -18,15 +15,15 @@
%tbody
- @orders.each do |order|
%tr{:class => cycle("even","odd", :name => "order")}
%td= link_to truncate(order.name), :action => "new", :id => order
%td= link_to truncate(order.name), new_finance_order_path(order_id: order.id)
%td=h format_time(order.ends) unless order.ends.nil?
%td= order.closed? ? "abgerechnet (#{number_to_currency order.foodcoop_result})" : "beendet"
%td= order.updated_by.nil? ? '??' : order.updated_by.nick
%td
- unless order.closed?
= link_to "abrechnen", :action => "new", :id => order
= link_to "abrechnen", new_finance_order_path(order_id: order.id)
|
= link_to 'direkt schließen', {:action => 'close_direct', :id => order}, |
:confirm => 'Wirklich die Bestellung schließen setzen?', :method => "post" |
= link_to 'direkt schließen', close_direct_finance_order_path(order),
:confirm => 'Wirklich die Bestellung schließen setzen?', :method => :put
- else
%i derzeit gibt es keine beendeten Bestellungen

View file

@ -1,8 +1,5 @@
- title "#{@order.name} abrechnen"
- if @order.closed?
%p
%b Achtung, Bestellung wurde schon abgerechnet!
.left_column{:style => 'width: 24em'}
.box_title
%h2 Zusammenfassung
@ -46,11 +43,11 @@
.box_title
#editOrderNav
%ul
%li= link_to 'Gruppenübersicht', new_finance_order_path(@order, view: :groups_overview)
/%li= remote_link_to 'Artikelübersicht', :update => 'results', :url => {:action => 'new', :id => @order, :view => 'articlesOverview'}
/%li= remote_link_to 'Bestellung bearbeiten', :update => 'results', :url => {:action => 'new', :id => @order, :view => 'editResults'}
%li= link_to 'Bestellung bearbeiten', new_finance_order_path(order_id: @order.id, view: 'edit_results'), remote: true
%li= link_to 'Gruppenübersicht', new_finance_order_path(order_id: @order.id, view: 'groups_overview'), remote: true
%li= link_to 'Artikelübersicht', new_finance_order_path(order_id: @order.id, view: 'articles_overview'), remote: true
.column_content
#results
= render :partial => 'edit_results_by_articles'
= render partial: 'edit_results_by_articles'
%p= link_to_top
#edit_box{:style => 'display:none'}

View file

@ -0,0 +1 @@
$('#results').html('<%= escape_javascript(render(partial: balancing_view_partial, locals: {order: @order})) %>');

View file

@ -0,0 +1,9 @@
%h2 Mengenänderung
%p
%b Artikel: #{@order_article.article.name}
- simple_form_for [:finance, @group_order_article], remote: true do |form|
= form.hidden_field :order_article_id
= form.input :ordergroup_id, as: :select, collection: Ordergroup.all.map { |g| [g.name, g.id] }
= form.input :result, hint: "Einheit: #{@order_article.article.unit}"
= form.submit 'Speichern'

View file

@ -0,0 +1 @@
$.fancybox('<%= escape_javascript(render('form')) %>');

View file

@ -0,0 +1 @@
$.fancybox('<%= escape_javascript(render("form")) %>');

View file

@ -0,0 +1,5 @@
$.fancybox.close();
$('#order_article_<%= @order_article.id %>').
html('<%= escape_javascript(render(partial: 'finance/balancing/order_article', locals: {order_article: @order_article})) %>');
$('#group_order_articles_<%= @order_article.id %>').
html('<%= escape_javascript(render(partial: 'finance/balancing/group_order_articles', locals: {order_article: @order_article})) %>');

View file

@ -1,7 +1,6 @@
%h2 Artikel aktualisieren
= form_for [:finance, @order, @order_article], remote: true do |form|
= @order_article.errors.try(:full_messages)
%table
%tr
%th Name

View file

@ -2,5 +2,5 @@
Neuer gelieferter Artikel die Bestellung
= simple_form_for [:finance, @order, @order_article], remote: true do |form|
= form.input :article_id, as: :select, collection: @order.supplier.articles.order(:name)
= form.input :article_id, as: :select, collection: new_order_articles_collection
= form.submit

View file

@ -348,6 +348,9 @@ de:
ends: "Endet am"
order_article:
article_id: Artikel aus dem Katalog wählen
group_order_article:
ordergroup_id: Bestellgruppe
result: Menge
invoice:
supplier: Lieferant
number: Nummer

View file

@ -119,19 +119,26 @@ Foodsoft::Application.routes.draw do
namespace :finance do
root :to => 'balancing#index'
match 'balancing/list' => 'balancing#list', :as => 'balancing'
resources :order, controller: 'balancing', path: 'balancing' do
member do
get :edit_note
put :update_note
get :confirm
put :close
put :close_direct
end
resources :order_articles
end
resources :group_order_articles
match 'balancing/list' => 'balancing#list', :as => 'balancing'
resources :group_order_articles do
member do
put :update_result
end
end
resources :invoices

View file

@ -2,13 +2,13 @@
$(function() {
// Show/Hide a specific DOM element
$('a[data-toggle-this]').click(function() {
$('a[data-toggle-this]').live('click', function() {
$($(this).data('toggle-this')).toggle();
return false;
});
// Remove this item from DOM
$('a[data-remove-this').click(function() {
$('a[data-remove-this]').live('click', function() {
$($(this).data('remove-this')).remove();
return false;
});