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