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/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 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/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/models/supplier.rb b/app/models/supplier.rb index b1466a70..0dba2a1e 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -80,10 +80,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 diff --git a/app/views/stockit/history.haml b/app/views/stockit/history.haml new file mode 100644 index 00000000..f4fe2b07 --- /dev/null +++ b/app/views/stockit/history.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..4673b967 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_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/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/locales/de.yml b/config/locales/de.yml index 97870458..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 @@ -1676,6 +1676,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 @@ -1822,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 ba8d92e7..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 @@ -1678,6 +1678,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 @@ -1824,6 +1833,7 @@ en: close: Close delete: Delete edit: Edit + history: Show history marks: close: ! '×' or_cancel: or cancel 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 diff --git a/config/routes.rb b/config/routes.rb index cbcf2828..094ef4cb 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 + + get :history end resources :suppliers do