diff --git a/app/controllers/finance/balancing_controller.rb b/app/controllers/finance/balancing_controller.rb index 6233de0d..b766320d 100644 --- a/app/controllers/finance/balancing_controller.rb +++ b/app/controllers/finance/balancing_controller.rb @@ -3,7 +3,7 @@ class Finance::BalancingController < ApplicationController before_filter :authenticate_finance def index - @financial_transactions = FinancialTransaction.find(:all, :order => "created_on DESC", :limit => 8) + @financial_transactions = FinancialTransaction.order(:created_on.desc).limit(8) @orders = Order.finished_not_closed @unpaid_invoices = Invoice.unpaid end @@ -48,117 +48,6 @@ class Finance::BalancingController < ApplicationController end end - def edit_note - @order = Order.find(params[:id]) - render :layout => false - end - - def update_note - @order = Order.find(params[:id]) - if @order.update_attributes(params[:order]) - render :layout => false - else - render :action => :edit_note, :layout => false - 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]) - find_params = { - :conditions => ["LOWER(articles.name) LIKE ?", "%#{params[:article][:name].downcase}%" ], - :order => 'articles.name ASC', - :limit => 8 - } - @articles = if order.stockit? - StockArticle.all find_params - else - order.supplier.articles.all find_params - end - - render :partial => 'shared/auto_complete_articles' - end - - def create_order_article - @order = Order.find(params[:order_id]) - article = Article.find(params[:order_article][:article_id]) - order_article = @order.order_articles.find_by_article_id(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 - - rescue - render :update do |page| - page.replace_html "edit_box", :text => "Keinen Artikel gefunden. Bitte erneut versuchen." - page.insert_html :bottom, "edit_box", :partial => "new_order_article" - 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 - # Updates ordergroup values - order_article.group_order_articles.each { |goa| goa.group_order.update_price! } - - 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_article.order} - page["summary"].visual_effect :highlight, :duration => 2 - end - end - def new_group_order_article goa = OrderArticle.find(params[:id]).group_order_articles.build render :update do |page| diff --git a/app/controllers/finance/group_order_articles_controller.rb b/app/controllers/finance/group_order_articles_controller.rb new file mode 100644 index 00000000..6f780c73 --- /dev/null +++ b/app/controllers/finance/group_order_articles_controller.rb @@ -0,0 +1,10 @@ +class Finance::GroupOrderArticlesController < ApplicationController + + before_filter :authenticate_finance + + layout false # We only use this controller to server js snippets, no need for layout rendering + + def new + @order_article = OrderArticle.find(params[:order_article_id]) + end +end diff --git a/app/controllers/finance/order_articles_controller.rb b/app/controllers/finance/order_articles_controller.rb new file mode 100644 index 00000000..a6e62b55 --- /dev/null +++ b/app/controllers/finance/order_articles_controller.rb @@ -0,0 +1,39 @@ +class Finance::OrderArticlesController < ApplicationController + + before_filter :authenticate_finance + + layout false # We only use this controller to server js snippets, no need for layout rendering + + def new + @order = Order.find(params[:order_id]) + @order_article = @order.order_articles.build + end + + def create + @order = Order.find(params[:order_id]) + @order_article = @order.order_articles.build(params[:order_article]) + unless @order_article.save + render action: :new + end + end + + def edit + @order = Order.find(params[:order_id]) + @order_article = OrderArticle.find(params[:id]) + end + + def update + @order = Order.find(params[:order_id]) + @order_article = OrderArticle.find(params[:id]) + begin + @order_article.update_article_and_price!(params[:article], params[:price], params[:order_article]) + rescue + render action: :edit + end + end + + def destroy + @order_article = OrderArticle.find(params[:id]) + @order_article.destroy + end +end diff --git a/app/helpers/finance/order_articles_helper.rb b/app/helpers/finance/order_articles_helper.rb new file mode 100644 index 00000000..7c97d8da --- /dev/null +++ b/app/helpers/finance/order_articles_helper.rb @@ -0,0 +1,2 @@ +module Finance::OrderArticlesHelper +end diff --git a/app/models/order_article.rb b/app/models/order_article.rb index fec7ec4e..31cd321c 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -8,9 +8,12 @@ class OrderArticle < ActiveRecord::Base validates_presence_of :order_id, :article_id validate :article_and_price_exist + validates_uniqueness_of :article_id, scope: :order_id scope :ordered, :conditions => "units_to_order >= 1" + before_create :init_from_balancing + after_destroy :update_ordergroup_prices # This method returns either the ArticlePrice or the Article # The first will be set, when the the order is finished @@ -112,6 +115,19 @@ class OrderArticle < ActiveRecord::Base errors.add(:article, "muss angegeben sein und einen aktuellen Preis haben") if !(article = Article.find(article_id)) || article.fc_price.nil? end + # Associate with current article price if created in a finished order + def init_from_balancing + if order.present? and order.finished? + self.article_price = article.article_prices.first + self.units_to_order = 1 + end + end + + #TODO: Delayed job maybe?? + def update_ordergroup_prices + group_order_articles.each { |goa| goa.group_order.update_price! } + end + end # == Schema Information diff --git a/app/views/finance/balancing/_edit_results_by_articles.html.haml b/app/views/finance/balancing/_edit_results_by_articles.html.haml index 4bac9bf2..e4a5e388 100644 --- a/app/views/finance/balancing/_edit_results_by_articles.html.haml +++ b/app/views/finance/balancing/_edit_results_by_articles.html.haml @@ -1,7 +1,7 @@ %p{:style => "float:left"} %b Lieferung bearbeiten %p{:style => "float:right"} - /= remote_link_to "Artikel hinzufügen", :url => {:action => "new_order_article", :id => @order} + = link_to "Artikel hinzufügen", new_finance_order_order_article_path(@order), remote: true %table{:class => "ordered_articles", :style => "clear:both"} %thead diff --git a/app/views/finance/balancing/_group_order_articles.html.haml b/app/views/finance/balancing/_group_order_articles.html.haml index e88f3467..e56856f4 100644 --- a/app/views/finance/balancing/_group_order_articles.html.haml +++ b/app/views/finance/balancing/_group_order_articles.html.haml @@ -7,8 +7,8 @@ %td Einheiten %td Gesamtpreis %td{:colspan => "3",:style => "width:14em"} - = link_to '[Gruppe hinzufügen]', '#' - /:url => {:action => "new_group_order_article", :id => order_article} + = link_to '[Gruppe hinzufügen]', new_finance_group_order_article_path(order_id: order_article.order_id), + remote: true %tbody - for group_order_article in order_article.group_order_articles.ordered.all(:include => [:group_order]) %tr{:class => cycle('even', 'odd', :name => 'results')}[group_order_article] diff --git a/app/views/finance/balancing/_new_order_article.html.haml b/app/views/finance/balancing/_new_order_article.html.haml deleted file mode 100644 index 6e52714b..00000000 --- a/app/views/finance/balancing/_new_order_article.html.haml +++ /dev/null @@ -1,15 +0,0 @@ -%h2 - Neuer gelieferter Artikel die Bestellung - -- remote_form_for :order_article, :url => {:action => 'create_order_article', :order_id => @order.id}, | - :before => "Element.show('loader')", :success => "Element.hide('loader')" do |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')" \ No newline at end of file diff --git a/app/views/finance/balancing/_order_article.html.haml b/app/views/finance/balancing/_order_article.html.haml index ec049fa2..cbe12110 100644 --- a/app/views/finance/balancing/_order_article.html.haml +++ b/app/views/finance/balancing/_order_article.html.haml @@ -1,7 +1,5 @@ %td.closed - = link_to order_article.article.name, '#' - /"Element.toggle('group_order_articles_#{order_article.id}'); | - /Element.toggleClassName(this.up('td'), 'open')" | + = link_to order_article.article.name, '#', 'data-toggle-this' => "#group_order_articles_#{order_article.id}" %td= order_article.article.order_number %td = order_article.units_to_order @@ -21,10 +19,7 @@ %td= order_article.price.tax %td= order_article.price.deposit %td - = link_to icon(:edit), '#' - /:url => {:action => 'edit_order_article', :id => order_article} | + = link_to icon(:edit), edit_finance_order_order_article_path(@order, order_article), remote: true %td - = link_to icon(:delete), '#' - /:confirm => 'Bist du sicher?' - /:url => {:action => 'destroy_order_article', :id => order_article}, - /:method => :post + = link_to icon(:delete), finance_order_order_article_path(@order, order_article), method: :delete, + remote: true, confirm: 'Bist du sicher?' \ No newline at end of file diff --git a/app/views/finance/balancing/_order_article_form.html.haml b/app/views/finance/balancing/_order_article_form.html.haml deleted file mode 100644 index d85e894a..00000000 --- a/app/views/finance/balancing/_order_article_form.html.haml +++ /dev/null @@ -1,26 +0,0 @@ -- if @error - %b= @error -%table - %tr - %th Name - %th Nr. - %th - %abbr{:title=>"Anzahl gelieferter Gebinde"} Menge - %th Einheit - %th GebGr - %th Netto - %th MwSt. - %th Pfand - %tr - %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" -| -= link_to_function 'Abbrechen', "Element.hide('edit_box')" \ No newline at end of file diff --git a/app/views/finance/balancing/_order_article_result.html.haml b/app/views/finance/balancing/_order_article_result.html.haml index 16899a23..8728c0fd 100644 --- a/app/views/finance/balancing/_order_article_result.html.haml +++ b/app/views/finance/balancing/_order_article_result.html.haml @@ -1,5 +1,5 @@ %tr{:class => cycle('even', 'odd', :name => 'articles')}[order_article] - = render :partial => 'order_article', :locals => {:order_article => order_article} + = render :partial => 'finance/balancing/order_article', :locals => {:order_article => order_article} %tr{:id => "group_order_articles_#{order_article.id}", :class => "results", :style => "display:none"} - = render :partial => 'group_order_articles', :locals => {:order_article => order_article} \ No newline at end of file + = render :partial => 'finance/balancing/group_order_articles', :locals => {:order_article => order_article} \ No newline at end of file diff --git a/app/views/finance/balancing/index.haml b/app/views/finance/balancing/index.html.haml similarity index 90% rename from app/views/finance/balancing/index.haml rename to app/views/finance/balancing/index.html.haml index 93542e3a..a52928f5 100644 --- a/app/views/finance/balancing/index.haml +++ b/app/views/finance/balancing/index.html.haml @@ -43,7 +43,7 @@ .box_title %h2 noch nicht abgerechnet .column_content - %p= link_to "Bestellungsübersicht", :controller => 'balancing', :action => 'list' + %p= link_to "Bestellungsübersicht", finance_balancing_path - unless @orders.empty? %table.list %thead @@ -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", :action => "new", :id => order + %td= link_to "abrechnen", new_finance_order_path(id: order) - else Super, alles schon abgerechnet... \ No newline at end of file diff --git a/app/views/finance/order_articles/_edit.html.haml b/app/views/finance/order_articles/_edit.html.haml new file mode 100644 index 00000000..7d916395 --- /dev/null +++ b/app/views/finance/order_articles/_edit.html.haml @@ -0,0 +1,26 @@ +%h2 Artikel aktualisieren + += form_for [:finance, @order, @order_article], remote: true do |form| + = @order_article.errors.try(:full_messages) + %table + %tr + %th Name + %th Nr. + %th + %abbr{:title=>"Anzahl gelieferter Gebinde"} Menge + %th Einheit + %th GebGr + %th Netto + %th MwSt. + %th Pfand + %tr + %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" \ No newline at end of file diff --git a/app/views/finance/order_articles/_new.html.haml b/app/views/finance/order_articles/_new.html.haml new file mode 100644 index 00000000..04f7f564 --- /dev/null +++ b/app/views/finance/order_articles/_new.html.haml @@ -0,0 +1,6 @@ +%h2 + 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.submit \ No newline at end of file diff --git a/app/views/finance/order_articles/create.js.erb b/app/views/finance/order_articles/create.js.erb new file mode 100644 index 00000000..e2b8b504 --- /dev/null +++ b/app/views/finance/order_articles/create.js.erb @@ -0,0 +1,3 @@ +$.fancybox.close(); +$('#result_table'). + prepend('<%= escape_javascript(render(partial: 'finance/balancing/order_article_result', locals: {order_article: @order_article}))%>'); \ No newline at end of file diff --git a/app/views/finance/order_articles/destroy.js.erb b/app/views/finance/order_articles/destroy.js.erb new file mode 100644 index 00000000..9e2afb5a --- /dev/null +++ b/app/views/finance/order_articles/destroy.js.erb @@ -0,0 +1 @@ +$('#order_article_<%= @order_article.id %>, #group_order_articles_<%= @order_article.id %>').hide(); \ No newline at end of file diff --git a/app/views/finance/order_articles/edit.js.erb b/app/views/finance/order_articles/edit.js.erb new file mode 100644 index 00000000..dbfced30 --- /dev/null +++ b/app/views/finance/order_articles/edit.js.erb @@ -0,0 +1 @@ +$.fancybox('<%= escape_javascript(render("edit")) %>'); \ No newline at end of file diff --git a/app/views/finance/order_articles/new.js.erb b/app/views/finance/order_articles/new.js.erb new file mode 100644 index 00000000..c19ec9ad --- /dev/null +++ b/app/views/finance/order_articles/new.js.erb @@ -0,0 +1 @@ +$.fancybox('<%= escape_javascript(render("new")) %>'); \ No newline at end of file diff --git a/app/views/finance/order_articles/update.js.erb b/app/views/finance/order_articles/update.js.erb new file mode 100644 index 00000000..b9ef3b5f --- /dev/null +++ b/app/views/finance/order_articles/update.js.erb @@ -0,0 +1,3 @@ +$.fancybox.close(); +$('#order_article_<%= @order_article.id %>'). + html('<%= escape_javascript(render(partial: 'finance/balancing/order_article', locals: {order_article: @order_article})) %>'); \ No newline at end of file diff --git a/config/app_config.yml.SAMPLE b/config/app_config.yml.SAMPLE index 700d114f..ebb3f127 100644 --- a/config/app_config.yml.SAMPLE +++ b/config/app_config.yml.SAMPLE @@ -51,7 +51,7 @@ development: &defaults # Access to sharedLists, the external article-database shared_lists: - adapter: mysql + adapter: mysql2 host: localhost database: sharedlists_development username: root diff --git a/config/locales/de.yml b/config/locales/de.yml index 9ac3c7e3..f9c58c65 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -221,6 +221,7 @@ de: financial_transaction: Kontotransaktion order: Bestellung order_comment: Kommentar + order_article: Bestell-Artikel attributes: article: price: Nettopreis @@ -345,6 +346,8 @@ de: order: starts: "Läuft vom" ends: "Endet am" + order_article: + article_id: Artikel aus dem Katalog wählen invoice: supplier: Lieferant number: Nummer diff --git a/config/routes.rb b/config/routes.rb index 83f08d5d..d78fba8f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -125,8 +125,12 @@ Foodsoft::Application.routes.draw do get :edit_note put :update_note end + + resources :order_articles end + resources :group_order_articles + match 'balancing/list' => 'balancing#list', :as => 'balancing' resources :invoices