diff --git a/.rbenv-version b/.rbenv-version deleted file mode 100644 index 546d4d8a..00000000 --- a/.rbenv-version +++ /dev/null @@ -1 +0,0 @@ -1.9.3-p327 \ No newline at end of file diff --git a/app/controllers/finance/balancing_controller.rb b/app/controllers/finance/balancing_controller.rb index 1b55d177..71ce19ed 100644 --- a/app/controllers/finance/balancing_controller.rb +++ b/app/controllers/finance/balancing_controller.rb @@ -10,24 +10,22 @@ class Finance::BalancingController < Finance::BaseController flash.now.alert = "Achtung, Bestellung wurde schon abgerechnet" if @order.closed? @comments = @order.comments - if params['sort'] - sort = case params['sort'] - when "name" then "articles.name" - when "order_number" then "articles.order_number" - when "name_reverse" then "articles.name DESC" - when "order_number_reverse" then "articles.order_number DESC" - end - else - sort = "id" - end + @articles = @order.order_articles.ordered.includes(:order, :article_price, + group_order_articles: {group_order: :ordergroup}) - @articles = @order.order_articles.ordered.includes(:article).order(sort) - - if params[:sort] == "order_number" - @articles = @articles.to_a.sort { |a,b| a.article.order_number.gsub(/[^[:digit:]]/, "").to_i <=> b.article.order_number.gsub(/[^[:digit:]]/, "").to_i } - elsif params[:sort] == "order_number_reverse" - @articles = @articles.to_a.sort { |a,b| b.article.order_number.gsub(/[^[:digit:]]/, "").to_i <=> a.article.order_number.gsub(/[^[:digit:]]/, "").to_i } - end + sort_param = params['sort'] || 'name' + @articles = case sort_param + when 'name' then + OrderArticle.sort_by_name(@articles) + when 'name_reverse' then + OrderArticle.sort_by_name(@articles).reverse + when 'order_number' then + OrderArticle.sort_by_order_number(@articles) + when 'order_number_reverse' then + OrderArticle.sort_by_order_number(@articles).reverse + else + @articles + end render layout: false if request.xhr? end diff --git a/app/models/article.rb b/app/models/article.rb index 09f17fa4..32554e84 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -7,7 +7,7 @@ class Article < ActiveRecord::Base localize_input_of :price, :tax, :deposit # Associations - belongs_to :supplier + belongs_to :supplier, :with_deleted => true belongs_to :article_category has_many :article_prices, :order => "created_at DESC" diff --git a/app/models/article_price.rb b/app/models/article_price.rb index e946fc92..686b891c 100644 --- a/app/models/article_price.rb +++ b/app/models/article_price.rb @@ -1,6 +1,6 @@ class ArticlePrice < ActiveRecord::Base - belongs_to :article + belongs_to :article, :with_deleted => true has_many :order_articles validates_presence_of :price, :tax, :deposit, :unit_quantity diff --git a/app/models/delivery.rb b/app/models/delivery.rb index 3add6fdf..b9c04616 100644 --- a/app/models/delivery.rb +++ b/app/models/delivery.rb @@ -1,6 +1,6 @@ class Delivery < ActiveRecord::Base - belongs_to :supplier + belongs_to :supplier, :with_deleted => true has_one :invoice has_many :stock_changes, :dependent => :destroy diff --git a/app/models/financial_transaction.rb b/app/models/financial_transaction.rb index 354a3d2a..bdd7e48a 100644 --- a/app/models/financial_transaction.rb +++ b/app/models/financial_transaction.rb @@ -1,7 +1,7 @@ # financial transactions are the foodcoop internal financial transactions # only ordergroups have an account balance and are happy to transfer money class FinancialTransaction < ActiveRecord::Base - belongs_to :ordergroup + belongs_to :ordergroup, :with_deleted => true belongs_to :user validates_presence_of :amount, :note, :user_id, :ordergroup_id diff --git a/app/models/group_order.rb b/app/models/group_order.rb index 9685d3bb..04e69de7 100644 --- a/app/models/group_order.rb +++ b/app/models/group_order.rb @@ -4,7 +4,7 @@ class GroupOrder < ActiveRecord::Base attr_accessor :group_order_articles_attributes belongs_to :order - belongs_to :ordergroup + belongs_to :ordergroup, :with_deleted => true has_many :group_order_articles, :dependent => :destroy has_many :order_articles, :through => :group_order_articles belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by_user_id" @@ -22,35 +22,25 @@ class GroupOrder < ActiveRecord::Base data = {} data[:available_funds] = ordergroup.get_available_funds(self) - unless new_record? - # Group has already ordered, so get the results... - goas = {} - group_order_articles.all.each do |goa| - goas[goa.order_article_id] = { - :quantity => goa.quantity, - :tolerance => goa.tolerance, - :quantity_result => goa.result(:quantity), - :tolerance_result => goa.result(:tolerance), - :total_price => goa.total_price - } - end - end - # load prices and other stuff.... data[:order_articles] = {} - #order.order_articles.each do |order_article| order.articles_grouped_by_category.each do |article_category, order_articles| order_articles.each do |order_article| + + # Get the result of last time ordering, if possible + goa = group_order_articles.detect { |goa| goa.order_article_id == order_article.id } + + # Build hash with relevant data data[:order_articles][order_article.id] = { :price => order_article.article.fc_price, :unit => order_article.article.unit_quantity, - :quantity => (new_record? ? 0 : goas[order_article.id][:quantity]), - :others_quantity => order_article.quantity - (new_record? ? 0 : goas[order_article.id][:quantity]), - :used_quantity => (new_record? ? 0 : goas[order_article.id][:quantity_result]), - :tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance]), - :others_tolerance => order_article.tolerance - (new_record? ? 0 : goas[order_article.id][:tolerance]), - :used_tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance_result]), - :total_price => (new_record? ? 0 : goas[order_article.id][:total_price]), + :quantity => (goa ? goa.quantity : 0), + :others_quantity => order_article.quantity - (goa ? goa.quantity : 0), + :used_quantity => (goa ? goa.result(:quantity) : 0), + :tolerance => (goa ? goa.result(:tolerance) : 0), + :others_tolerance => order_article.tolerance - (goa ? goa.result(:tolerance) : 0), + :used_tolerance => (goa ? goa.result(:tolerance) : 0), + :total_price => (goa ? goa.total_price : 0), :missing_units => order_article.missing_units, :quantity_available => (order.stockit? ? order_article.article.quantity_available : 0) } diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 0feb3ede..a9f1d9ca 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -1,6 +1,6 @@ class Invoice < ActiveRecord::Base - belongs_to :supplier + belongs_to :supplier, :with_deleted => true belongs_to :delivery belongs_to :order diff --git a/app/models/order.rb b/app/models/order.rb index 6e48756c..5afc166a 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -10,7 +10,7 @@ class Order < ActiveRecord::Base has_one :invoice has_many :comments, :class_name => "OrderComment", :order => "created_at" has_many :stock_changes - belongs_to :supplier + belongs_to :supplier, :with_deleted => true belongs_to :updated_by, :class_name => 'User', :foreign_key => 'updated_by_user_id' belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id' @@ -19,8 +19,7 @@ class Order < ActiveRecord::Base validate :starts_before_ends, :include_articles # Callbacks - after_update :update_price_of_group_orders - after_save :save_order_articles + after_save :save_order_articles, :update_price_of_group_orders # Finders scope :open, where(state: 'open').order('ends DESC') @@ -215,7 +214,24 @@ class Order < ActiveRecord::Base end def save_order_articles - self.articles = Article.find(article_ids) + #self.articles = Article.find(article_ids) # This doesn't deletes the group_order_articles, belonging to order_articles, + # # see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many + # + ## Ensure to delete also the group_order_articles, belonging to order_articles + ## This case is relevant, when removing articles from a running order + #goa_ids = GroupOrderArticle.where(group_order_id: group_order_ids).includes(:order_article). + # select { |goa| goa.order_article.nil? }.map(&:id) + #GroupOrderArticle.delete_all(id: goa_ids) unless goa_ids.empty? + + + # fetch selected articles + articles_list = Article.find(article_ids) + # create new order_articles + (articles_list - articles).each { |article| order_articles.create(:article => article) } + # delete old order_articles + articles.reject { |article| articles_list.include?(article) }.each do |article| + order_articles.detect { |order_article| order_article.article_id == article.id }.destroy + end end private diff --git a/app/models/order_article.rb b/app/models/order_article.rb index a6b13a27..b68bfe82 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -4,7 +4,7 @@ class OrderArticle < ActiveRecord::Base attr_reader :update_current_price belongs_to :order - belongs_to :article + belongs_to :article, :with_deleted => true belongs_to :article_price has_many :group_order_articles, :dependent => :destroy @@ -17,6 +17,17 @@ class OrderArticle < ActiveRecord::Base before_create :init_from_balancing after_destroy :update_ordergroup_prices + def self.sort_by_name(order_articles) + order_articles.sort { |a,b| a.article.name <=> b.article.name } + end + + def self.sort_by_order_number(order_articles) + order_articles.sort do |a,b| + a.article.order_number.to_s.gsub(/[^[:digit:]]/, "").to_i <=> + b.article.order_number.to_s.gsub(/[^[:digit:]]/, "").to_i + end + end + # This method returns either the ArticlePrice or the Article # The first will be set, when the the order is finished def price diff --git a/app/views/articles/_article.html.haml b/app/views/articles/_article.html.haml index ac2679e1..ed22dec0 100644 --- a/app/views/articles/_article.html.haml +++ b/app/views/articles/_article.html.haml @@ -1,4 +1,4 @@ -%tr{class: row_classes(article)} +%tr{class: row_classes(article)}[article] %td= check_box_tag 'selected_articles[]', article.id.to_s, false, {:id => "checkbox_#{article.id}", 'data-ignore-onchange' => true} %td{'data-check-this' => "#checkbox_#{article.id}", :class => 'click-me'}= article.name %td= article.origin @@ -14,6 +14,4 @@ %td= link_to "Bearbeiten", edit_supplier_article_path(@supplier, article), :remote => true, class: 'btn btn-mini' %td= link_to "Löschen", [@supplier, article], - :method => :delete, :confirm => 'Bist du sicher?', :remote => true, class: 'btn btn-mini btn-danger' - - \ No newline at end of file + :method => :delete, :confirm => 'Bist du sicher?', :remote => true, class: 'btn btn-mini btn-danger' \ No newline at end of file diff --git a/app/views/finance/balancing/_group_order_articles.html.haml b/app/views/finance/balancing/_group_order_articles.html.haml index 3cc34dfd..a4a6bb7a 100644 --- a/app/views/finance/balancing/_group_order_articles.html.haml +++ b/app/views/finance/balancing/_group_order_articles.html.haml @@ -10,7 +10,7 @@ = link_to 'Gruppe hinzufügen', new_finance_group_order_article_path(order_article_id: order_article.id), remote: true, class: 'btn btn-mini' %tbody - - for group_order_article in order_article.group_order_articles.ordered.all(:include => [:group_order]) + - for group_order_article in order_article.group_order_articles.select { |goa| goa.result > 0 } %tr[group_order_article] %td %td{:style=>"width:50%"} diff --git a/app/views/finance/balancing/new.html.haml b/app/views/finance/balancing/new.html.haml index 50cb3d4c..e151ccd0 100644 --- a/app/views/finance/balancing/new.html.haml +++ b/app/views/finance/balancing/new.html.haml @@ -20,7 +20,7 @@ .well.well-small %h3 Kommentare - #comments= render :partial => 'shared/comments', locals: {comments: @order.comments} + #comments= render :partial => 'shared/comments', locals: {comments: @order.comments.includes(:user)} - content_for :actionbar do .btn-group diff --git a/app/views/group_orders/_form.html.haml b/app/views/group_orders/_form.html.haml index b1103462..b040354e 100644 --- a/app/views/group_orders/_form.html.haml +++ b/app/views/group_orders/_form.html.haml @@ -45,6 +45,8 @@ %thead %tr %th Name + - if @order.stockit? + %th{style: 'width:120px'} Lieferant %th{style: "width:13px;"} %th{style: "width:4.5em;"} Preis %th{style: "width:4.5em;"} Einheit @@ -66,6 +68,8 @@ - order_articles.each do |order_article| %tr{class: "#{cycle('even', 'odd', name: 'articles')} order-article", valign: "top"} %td.name= order_article.article.name + - if @order.stockit? + %td= truncate order_article.article.supplier.name, length: 15 %td= h order_article.article.origin %td= number_to_currency(@ordering_data[:order_articles][order_article.id][:price]) %td= order_article.article.unit diff --git a/app/views/home/_apple_bar.html.haml b/app/views/home/_apple_bar.html.haml index 6cbdd875..c7c062ba 100644 --- a/app/views/home/_apple_bar.html.haml +++ b/app/views/home/_apple_bar.html.haml @@ -7,4 +7,5 @@ Deine aktueller Äpfelpunktestand: #{apple_bar.apples} Konkret: Pro #{number_to_currency(apple_bar.mean_order_amount_per_job, :precision => 0 )} Bestellsumme solltest Du eine Aufgabe machen! - if FoodsoftConfig[:stop_ordering_under].present? %strong Achtung, - hast Du weniger als #{FoodsoftConfig[:stop_ordering_under]} Äpfel, darfst Du nicht mehr bestellen! \ No newline at end of file + hast Du weniger als #{FoodsoftConfig[:stop_ordering_under]} Äpfel, darfst Du nicht mehr bestellen! + = link_to 'Mehr Informationen', 'https://github.com/bennibu/foodsoft/wiki/%C3%84pfel-u.-Birnen', target: '_blank' \ No newline at end of file diff --git a/config/initializers/rack.rb b/config/initializers/rack.rb new file mode 100644 index 00000000..30970ec9 --- /dev/null +++ b/config/initializers/rack.rb @@ -0,0 +1,3 @@ +# Increase key space for post request. +# Warning, this is dangerous. See http://stackoverflow.com/questions/12243694/getting-error-exceeded-available-parameter-key-space +Rack::Utils.key_space_limit = 262144