From ef62a18ac9ec289f8f370a9778c4e1126445f139 Mon Sep 17 00:00:00 2001 From: Julius Date: Tue, 9 Jul 2013 21:46:04 +0200 Subject: [PATCH 01/10] Add StockChanges index --- app/controllers/application_controller.rb | 5 +++++ app/controllers/stock_changes_controller.rb | 8 ++++++++ app/helpers/stock_changes_helper.rb | 13 +++++++++++++ app/models/stock_article.rb | 4 ++++ app/models/stock_change.rb | 1 + app/views/stock_changes/index.html.haml | 17 +++++++++++++++++ app/views/stockit/index.html.haml | 1 + config/initializers/extensions.rb | 9 ++++++++- config/routes.rb | 2 ++ 9 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 app/controllers/stock_changes_controller.rb create mode 100644 app/helpers/stock_changes_helper.rb create mode 100644 app/views/stock_changes/index.html.haml diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 29b752a0..d3a1530d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -99,6 +99,11 @@ class ApplicationController < ActionController::Base @supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id] end + # Get stock_article in nested resources + def find_stock_article + @stock_article = StockArticle.undeleted.find(params[:stock_article_id]) + end + # Set config and database connection for each request # It uses the subdomain to select the appropriate section in the config files # Use this method as a before filter (first filter!) in ApplicationController diff --git a/app/controllers/stock_changes_controller.rb b/app/controllers/stock_changes_controller.rb new file mode 100644 index 00000000..57aee78e --- /dev/null +++ b/app/controllers/stock_changes_controller.rb @@ -0,0 +1,8 @@ +#encoding: utf-8 +class StockChangesController < ApplicationController + before_filter :find_stock_article + + def index + @stock_changes = @stock_article.stock_changes(:readonly => true).order('stock_changes.created_at DESC') + end +end diff --git a/app/helpers/stock_changes_helper.rb b/app/helpers/stock_changes_helper.rb new file mode 100644 index 00000000..c5dee87e --- /dev/null +++ b/app/helpers/stock_changes_helper.rb @@ -0,0 +1,13 @@ +module StockChangesHelper + + def link_to_stock_change_reason(stock_change) + if stock_change.delivery_id + t '.delivery' + elsif stock_change.order_id + t '.order' + elsif stock_change.stock_taking_id + t '.stock_taking' + end + end + +end diff --git a/app/models/stock_article.rb b/app/models/stock_article.rb index 0124219a..5d45802e 100644 --- a/app/models/stock_article.rb +++ b/app/models/stock_article.rb @@ -17,6 +17,10 @@ class StockArticle < Article quantity - OrderArticle.where(article_id: id). joins(:order).where("orders.state = 'open' OR orders.state = 'finished'").sum(:units_to_order) end + + def quantity_history + stock_changes.reorder('stock_changes.created_at ASC').map{|s| s.quantity}.cumulative_sum + end def self.stock_value available.collect { |a| a.quantity * a.gross_price }.sum diff --git a/app/models/stock_change.rb b/app/models/stock_change.rb index 6a7adc75..9c4bf082 100644 --- a/app/models/stock_change.rb +++ b/app/models/stock_change.rb @@ -1,6 +1,7 @@ class StockChange < ActiveRecord::Base belongs_to :delivery belongs_to :order + belongs_to :stock_taking belongs_to :stock_article validates_presence_of :stock_article_id, :quantity diff --git a/app/views/stock_changes/index.html.haml b/app/views/stock_changes/index.html.haml new file mode 100644 index 00000000..f4fe2b07 --- /dev/null +++ b/app/views/stock_changes/index.html.haml @@ -0,0 +1,17 @@ +- title t('.stock_changes', :article_name => @stock_article.name) + +%table.table.table-hover#stock_changes + %thead + %tr + %th= t '.datetime' + %th= t '.reason' + %th= t '.change_quantity' + %th= t '.new_quantity' + %tbody + - reversed_history = @stock_article.quantity_history.reverse + - @stock_changes.each_with_index do |stock_change, index| + %tr + %td= l stock_change.created_at + %td= link_to_stock_change_reason(stock_change) + %td= stock_change.quantity + %td= reversed_history[index] diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index 777bb4b1..c2966ace 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -56,6 +56,7 @@ %td= article.article_category.name %td = link_to t('ui.edit'), edit_stock_article_path(article), class: 'btn btn-mini' + = link_to t('ui.history'), stock_article_stock_changes_path(article), class: 'btn btn-mini' = link_to t('ui.delete'), article, :method => :delete, :confirm => t('.confirm_delete'), class: 'btn btn-mini btn-danger', :remote => true %p diff --git a/config/initializers/extensions.rb b/config/initializers/extensions.rb index a6577545..1f71d542 100644 --- a/config/initializers/extensions.rb +++ b/config/initializers/extensions.rb @@ -9,4 +9,11 @@ class String string end end -end \ No newline at end of file +end + +class Array + def cumulative_sum + csum = 0 + self.map{|val| csum += val} + end +end diff --git a/config/routes.rb b/config/routes.rb index cbcf2828..34a77b05 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -96,6 +96,8 @@ Foodsoft::Application.routes.draw do get :articles_search get :fill_new_stock_article_form end + + resources :stock_changes, :only => [:index], :to => 'stock_changes' end resources :suppliers do From de9541a0dfa586782e8ae564817d365c351d45a9 Mon Sep 17 00:00:00 2001 From: Julius Date: Tue, 9 Jul 2013 21:49:39 +0200 Subject: [PATCH 02/10] Ask how to fetch readonly associations --- app/controllers/stock_changes_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/stock_changes_controller.rb b/app/controllers/stock_changes_controller.rb index 57aee78e..d596a383 100644 --- a/app/controllers/stock_changes_controller.rb +++ b/app/controllers/stock_changes_controller.rb @@ -3,6 +3,6 @@ class StockChangesController < ApplicationController before_filter :find_stock_article def index - @stock_changes = @stock_article.stock_changes(:readonly => true).order('stock_changes.created_at DESC') + @stock_changes = @stock_article.stock_changes(:readonly => true).order('stock_changes.created_at DESC') # The readonly has no effect, what is the proper way to achieve that? end end From ffcb9490c96de025cc247accd1218dba9b5e9c4e Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 11 Jul 2013 18:20:24 +0200 Subject: [PATCH 03/10] Make stock_changes readonly in index view --- app/controllers/stock_changes_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/stock_changes_controller.rb b/app/controllers/stock_changes_controller.rb index d596a383..3178ed0b 100644 --- a/app/controllers/stock_changes_controller.rb +++ b/app/controllers/stock_changes_controller.rb @@ -3,6 +3,6 @@ class StockChangesController < ApplicationController before_filter :find_stock_article def index - @stock_changes = @stock_article.stock_changes(:readonly => true).order('stock_changes.created_at DESC') # The readonly has no effect, what is the proper way to achieve that? + @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC').each {|s| s.readonly!} end end From 7c27017306630f72166244279f9b8115a5575305 Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 11 Jul 2013 18:37:55 +0200 Subject: [PATCH 04/10] Link delivery/stock_taking/order in stock_changes index --- app/helpers/stock_changes_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/helpers/stock_changes_helper.rb b/app/helpers/stock_changes_helper.rb index c5dee87e..ac6b7675 100644 --- a/app/helpers/stock_changes_helper.rb +++ b/app/helpers/stock_changes_helper.rb @@ -2,11 +2,11 @@ module StockChangesHelper def link_to_stock_change_reason(stock_change) if stock_change.delivery_id - t '.delivery' + link_to t('.delivery'), supplier_delivery_path(stock_change.delivery.supplier, stock_change.delivery) elsif stock_change.order_id - t '.order' + link_to t('.order'), order_path(stock_change.order) elsif stock_change.stock_taking_id - t '.stock_taking' + link_to t('.stock_taking'), stock_taking_path(stock_change.stock_taking) end end From 54afbb7af5ff278b00e24438ef1bb4303a680454 Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 11 Jul 2013 19:21:56 +0200 Subject: [PATCH 05/10] Add locales for stock_changes index --- config/locales/de.yml | 11 +++++++++++ config/locales/en.yml | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/config/locales/de.yml b/config/locales/de.yml index 648881e0..e0c13be1 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -1637,6 +1637,16 @@ de: mark: ! '*' text: benötigt 'yes': Ja + stock_changes: + index: + change_quantity: Veränderung + datetime: Zeitpunkt + delivery: Lieferung + new_quantity: Neuer Bestand + order: Bestellung + reason: Ereignis + stock_changes: Verlauf anzeigen für »%{article_name}« + stock_taking: Inventur stock_takings: create: notice: Inventur wurde erfolgreich angelegt. @@ -1821,6 +1831,7 @@ de: close: Schließen delete: Löschen edit: Bearbeiten + history: Verlauf anzeigen marks: close: ! '×' or_cancel: oder abbrechen diff --git a/config/locales/en.yml b/config/locales/en.yml index 8205bb75..562d5194 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1639,6 +1639,16 @@ en: mark: ! '*' text: required 'yes': 'Yes' + stock_changes: + index: + change_quantity: Change + datetime: Time + delivery: Delivery + new_quantity: New quantity + order: Order + reason: Reason + stock_changes: Stock quantity changes of ‘%{article_name}’ + stock_taking: Inventory stock_takings: create: notice: Inventory was created successfully. @@ -1823,6 +1833,7 @@ en: close: Close delete: Delete edit: Edit + history: Show history marks: close: ! '×' or_cancel: or cancel From edaf91b029eea92fdd37491d1268652bac497083 Mon Sep 17 00:00:00 2001 From: Julius Date: Fri, 12 Jul 2013 22:19:11 +0200 Subject: [PATCH 06/10] Inline before_filter of stock_changes_controller --- app/controllers/application_controller.rb | 5 ----- app/controllers/stock_changes_controller.rb | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d3a1530d..29b752a0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -99,11 +99,6 @@ class ApplicationController < ActionController::Base @supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id] end - # Get stock_article in nested resources - def find_stock_article - @stock_article = StockArticle.undeleted.find(params[:stock_article_id]) - end - # Set config and database connection for each request # It uses the subdomain to select the appropriate section in the config files # Use this method as a before filter (first filter!) in ApplicationController diff --git a/app/controllers/stock_changes_controller.rb b/app/controllers/stock_changes_controller.rb index 3178ed0b..4f91a07c 100644 --- a/app/controllers/stock_changes_controller.rb +++ b/app/controllers/stock_changes_controller.rb @@ -1,8 +1,8 @@ #encoding: utf-8 class StockChangesController < ApplicationController - before_filter :find_stock_article def index + @stock_article = StockArticle.undeleted.find(params[:stock_article_id]) @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC').each {|s| s.readonly!} end end From 730dc7cf2a6c177e2f5ac1a6aed907f10112c8da Mon Sep 17 00:00:00 2001 From: Julius Date: Sat, 13 Jul 2013 09:32:27 +0200 Subject: [PATCH 07/10] Move StockChanges#index to StockArticles#history --- app/controllers/stock_changes_controller.rb | 8 -------- app/controllers/stockit_controller.rb | 5 +++++ .../index.html.haml => stockit/history.haml} | 0 app/views/stockit/index.html.haml | 2 +- config/locales/de.yml | 19 +++++++++---------- config/locales/en.yml | 19 +++++++++---------- config/routes.rb | 2 +- 7 files changed, 25 insertions(+), 30 deletions(-) delete mode 100644 app/controllers/stock_changes_controller.rb rename app/views/{stock_changes/index.html.haml => stockit/history.haml} (100%) diff --git a/app/controllers/stock_changes_controller.rb b/app/controllers/stock_changes_controller.rb deleted file mode 100644 index 4f91a07c..00000000 --- a/app/controllers/stock_changes_controller.rb +++ /dev/null @@ -1,8 +0,0 @@ -#encoding: utf-8 -class StockChangesController < ApplicationController - - def index - @stock_article = StockArticle.undeleted.find(params[:stock_article_id]) - @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC').each {|s| s.readonly!} - end -end diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index 9ee40efe..3e3b43b2 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -55,4 +55,9 @@ class StockitController < ApplicationController render :partial => 'form', :locals => {:stock_article => stock_article} end + + def history + @stock_article = StockArticle.undeleted.find(params[:stock_article_id]) + @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC').each {|s| s.readonly!} + end end diff --git a/app/views/stock_changes/index.html.haml b/app/views/stockit/history.haml similarity index 100% rename from app/views/stock_changes/index.html.haml rename to app/views/stockit/history.haml diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index c2966ace..4673b967 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -56,7 +56,7 @@ %td= article.article_category.name %td = link_to t('ui.edit'), edit_stock_article_path(article), class: 'btn btn-mini' - = link_to t('ui.history'), stock_article_stock_changes_path(article), class: 'btn btn-mini' + = link_to t('ui.history'), stock_article_history_path(article), class: 'btn btn-mini' = link_to t('ui.delete'), article, :method => :delete, :confirm => t('.confirm_delete'), class: 'btn btn-mini btn-danger', :remote => true %p diff --git a/config/locales/de.yml b/config/locales/de.yml index e0c13be1..092f5a5f 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -1637,16 +1637,6 @@ de: mark: ! '*' text: benötigt 'yes': Ja - stock_changes: - index: - change_quantity: Veränderung - datetime: Zeitpunkt - delivery: Lieferung - new_quantity: Neuer Bestand - order: Bestellung - reason: Ereignis - stock_changes: Verlauf anzeigen für »%{article_name}« - stock_taking: Inventur stock_takings: create: notice: Inventur wurde erfolgreich angelegt. @@ -1687,6 +1677,15 @@ de: title: Lagerartikel bearbeiten form: price_hint: Um Chaos zu vermeiden können bis auf weiteres die Preise von angelegten Lagerartikeln nicht mehr verändert werden. + history: + change_quantity: Veränderung + datetime: Zeitpunkt + delivery: Lieferung + new_quantity: Neuer Bestand + order: Bestellung + reason: Ereignis + stock_changes: Verlauf anzeigen für »%{article_name}« + stock_taking: Inventur index: article: article: Artikel diff --git a/config/locales/en.yml b/config/locales/en.yml index 562d5194..535986e8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1639,16 +1639,6 @@ en: mark: ! '*' text: required 'yes': 'Yes' - stock_changes: - index: - change_quantity: Change - datetime: Time - delivery: Delivery - new_quantity: New quantity - order: Order - reason: Reason - stock_changes: Stock quantity changes of ‘%{article_name}’ - stock_taking: Inventory stock_takings: create: notice: Inventory was created successfully. @@ -1689,6 +1679,15 @@ en: title: Edit stock articles form: price_hint: To avoid choas, it is not possible to edit the prices of already added stock articles until further notice. + history: + change_quantity: Change + datetime: Time + delivery: Delivery + new_quantity: New quantity + order: Order + reason: Reason + stock_changes: Stock quantity changes of ‘%{article_name}’ + stock_taking: Inventory index: article: article: Article diff --git a/config/routes.rb b/config/routes.rb index 34a77b05..094ef4cb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -97,7 +97,7 @@ Foodsoft::Application.routes.draw do get :fill_new_stock_article_form end - resources :stock_changes, :only => [:index], :to => 'stock_changes' + get :history end resources :suppliers do From 12506e54b86f52a33757995e46987b17de39456b Mon Sep 17 00:00:00 2001 From: Julius Date: Sat, 13 Jul 2013 09:34:19 +0200 Subject: [PATCH 08/10] Remove stock_changes_helper.rb --- app/helpers/stock_changes_helper.rb | 13 ------------- app/helpers/stockit_helper.rb | 10 ++++++++++ 2 files changed, 10 insertions(+), 13 deletions(-) delete mode 100644 app/helpers/stock_changes_helper.rb diff --git a/app/helpers/stock_changes_helper.rb b/app/helpers/stock_changes_helper.rb deleted file mode 100644 index ac6b7675..00000000 --- a/app/helpers/stock_changes_helper.rb +++ /dev/null @@ -1,13 +0,0 @@ -module StockChangesHelper - - def link_to_stock_change_reason(stock_change) - if stock_change.delivery_id - link_to t('.delivery'), supplier_delivery_path(stock_change.delivery.supplier, stock_change.delivery) - elsif stock_change.order_id - link_to t('.order'), order_path(stock_change.order) - elsif stock_change.stock_taking_id - link_to t('.stock_taking'), stock_taking_path(stock_change.stock_taking) - end - end - -end diff --git a/app/helpers/stockit_helper.rb b/app/helpers/stockit_helper.rb index 2888603c..f6a0a1fc 100644 --- a/app/helpers/stockit_helper.rb +++ b/app/helpers/stockit_helper.rb @@ -4,4 +4,14 @@ module StockitHelper class_names << "unavailable" if article.quantity_available <= 0 class_names.join(" ") end + + def link_to_stock_change_reason(stock_change) + if stock_change.delivery_id + link_to t('.delivery'), supplier_delivery_path(stock_change.delivery.supplier, stock_change.delivery) + elsif stock_change.order_id + link_to t('.order'), order_path(stock_change.order) + elsif stock_change.stock_taking_id + link_to t('.stock_taking'), stock_taking_path(stock_change.stock_taking) + end + end end From 3fd498d29fb3167b6023671a2c68ab8d3062e8d8 Mon Sep 17 00:00:00 2001 From: wvengen Date: Sat, 13 Jul 2013 12:37:20 +0200 Subject: [PATCH 09/10] translations update --- config/locales/de.yml | 2 +- config/locales/en.yml | 4 ++-- config/locales/nl.yml | 10 ++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/config/locales/de.yml b/config/locales/de.yml index 05f58afb..139ec6e2 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -1428,7 +1428,7 @@ de: sessions: logged_in: Angemeldet! logged_out: Abgemeldet! - login_invalid: + login_invalid: Ungültiger Benutzername oder Passwort new: forgot_password: Passwort vergessen? login: Anmelden diff --git a/config/locales/en.yml b/config/locales/en.yml index 7ad6ec46..f9721711 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -602,8 +602,8 @@ en: orders: clear: accounting cleared: accounted (%{amount}) - close: settle directly - confirm: Do you really want to settle the order? + close: close directly + confirm: Do you really want to fully close the order? end: End ended: closed last_edited_by: Last edited by diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 378d988e..64e54f15 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -1567,6 +1567,15 @@ nl: title: form: price_hint: + history: + change_quantity: + datetime: + delivery: + new_quantity: + order: + reason: + stock_changes: + stock_taking: index: article: article: @@ -1713,6 +1722,7 @@ nl: close: Sluiten delete: Verwijder edit: Bewerk + history: marks: close: ! '×' or_cancel: of annuleren From 5e9a49b2c7375401dea3d3b1fc16188a8b47b5aa Mon Sep 17 00:00:00 2001 From: wvengen Date: Mon, 15 Jul 2013 18:03:02 +0200 Subject: [PATCH 10/10] make uniqueness_of_name validations work again (closes foodcoops/foodsoft#157) --- app/models/ordergroup.rb | 10 +++++----- app/models/supplier.rb | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/models/ordergroup.rb b/app/models/ordergroup.rb index afa90211..6e1b65dc 100644 --- a/app/models/ordergroup.rb +++ b/app/models/ordergroup.rb @@ -108,13 +108,13 @@ class Ordergroup < Group # Make sure, the name is uniq, add usefull message if uniq group is already deleted def uniqueness_of_name - id = new_record? ? nil : self.id - group = Ordergroup.where('groups.id != ? AND groups.name = ?', id, name).first - if group.present? - message = group.deleted? ? :taken_with_deleted : :taken + group = Ordergroup.where('groups.name = ?', name) + group = group.where('groups.id != ?', self.id) unless new_record? + if group.exists? + message = group.first.deleted? ? :taken_with_deleted : :taken errors.add :name, message end end - + end diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 01f93d35..4ac1572e 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -82,10 +82,10 @@ class Supplier < ActiveRecord::Base # Make sure, the name is uniq, add usefull message if uniq group is already deleted def uniqueness_of_name - id = new_record? ? nil : self.id - supplier = Supplier.where('suppliers.id != ? AND suppliers.name = ?', id, name).first - if supplier.present? - message = supplier.deleted? ? :taken_with_deleted : :taken + supplier = Supplier.where('suppliers.name = ?', name) + supplier = supplier.where('suppliers.id != ?', self.id) unless new_record? + if supplier.exists? + message = supplier.first.deleted? ? :taken_with_deleted : :taken errors.add :name, message end end