Add StockChanges index

This commit is contained in:
Julius 2013-07-09 21:46:04 +02:00
parent d397132f7b
commit ef62a18ac9
9 changed files with 59 additions and 1 deletions

View file

@ -99,6 +99,11 @@ class ApplicationController < ActionController::Base
@supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id] @supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id]
end 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 # Set config and database connection for each request
# It uses the subdomain to select the appropriate section in the config files # It uses the subdomain to select the appropriate section in the config files
# Use this method as a before filter (first filter!) in ApplicationController # Use this method as a before filter (first filter!) in ApplicationController

View file

@ -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

View file

@ -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

View file

@ -18,6 +18,10 @@ class StockArticle < Article
joins(:order).where("orders.state = 'open' OR orders.state = 'finished'").sum(:units_to_order) joins(:order).where("orders.state = 'open' OR orders.state = 'finished'").sum(:units_to_order)
end end
def quantity_history
stock_changes.reorder('stock_changes.created_at ASC').map{|s| s.quantity}.cumulative_sum
end
def self.stock_value def self.stock_value
available.collect { |a| a.quantity * a.gross_price }.sum available.collect { |a| a.quantity * a.gross_price }.sum
end end

View file

@ -1,6 +1,7 @@
class StockChange < ActiveRecord::Base class StockChange < ActiveRecord::Base
belongs_to :delivery belongs_to :delivery
belongs_to :order belongs_to :order
belongs_to :stock_taking
belongs_to :stock_article belongs_to :stock_article
validates_presence_of :stock_article_id, :quantity validates_presence_of :stock_article_id, :quantity

View file

@ -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]

View file

@ -56,6 +56,7 @@
%td= article.article_category.name %td= article.article_category.name
%td %td
= link_to t('ui.edit'), edit_stock_article_path(article), class: 'btn btn-mini' = 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'), = link_to t('ui.delete'), article, :method => :delete, :confirm => t('.confirm_delete'),
class: 'btn btn-mini btn-danger', :remote => true class: 'btn btn-mini btn-danger', :remote => true
%p %p

View file

@ -10,3 +10,10 @@ class String
end end
end end
end end
class Array
def cumulative_sum
csum = 0
self.map{|val| csum += val}
end
end

View file

@ -96,6 +96,8 @@ Foodsoft::Application.routes.draw do
get :articles_search get :articles_search
get :fill_new_stock_article_form get :fill_new_stock_article_form
end end
resources :stock_changes, :only => [:index], :to => 'stock_changes'
end end
resources :suppliers do resources :suppliers do