Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/inventory dashboard #455

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ node_modules/
yarn-debug.log*
.yarn-integrity

# Ignore vim files
*.swp
*.swo

# Ignore uploaded files in development
/storage/*
!/storage/.keep
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ gem 'sidekiq'

gem 'rubocop-rails'

gem 'chartkick', '~> 3.3.0'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (~> 1.5)
xpath (~> 3.2)
chartkick (3.3.1)
childprocess (1.0.1)
rake (< 13.0)
coderay (1.1.2)
Expand Down Expand Up @@ -293,6 +294,7 @@ DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
capybara (>= 2.15)
chartkick (~> 3.3.0)
devise (>= 4.7.1)
devise_invitable (~> 2.0.0)
factory_bot_rails
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class HomeController < ApplicationController
skip_before_action :authenticate_user!, only: [:contact]

def index
filters = InventoryDateFilter.call(permitted_params.to_h)

@presenter = ::InventoryDashboardPresenter.new(**filters)
end

def contact
Expand All @@ -11,4 +14,10 @@ def contact
def admin
render :admin, layout: 'application_sidebar'
end

private

def permitted_params
params.permit(inventory: [:start_date, :end_date])
end
end
2 changes: 2 additions & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// global dependencies
require("@rails/ujs").start()
require("@rails/activestorage").start()
require("chartkick")
require("chart.js")

import 'jquery'
import 'bootstrap/js/dist/util'
Expand Down
61 changes: 61 additions & 0 deletions app/javascript/src/stylesheets/_custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,64 @@ footer {
text-align: center;
}
}

.row {
padding: 20px;
}

.bg-green {
background-color: #00a65a;
}

.bg-aqua {
background-color: #00c0ef;
}

.info-box {
display: block;
min-height: 90px;
background: #f0f0f0;
max-width: 210px;
border-radius: 2px;
margin-bottom: 15px;
margin: 20px;
box-shadow: 0 1px 1px rgba(0,0,0,0.1);
}

.info-box-icon {
display: block;
float: left;
height: 90px;
width: 90px;
text-align: center;
line-height: 90px;
font-weight: bold;
font-size: 45px;
}

.info-box-content {
padding: 5px 15px;
font-size: 25px;
font-weight: bold;
}

.info-box-text {
padding: 0px 15px;
}

.filter {
position: absolute;
}

@media only screen and (min-width: 768px) {
.filter {
right: 130px;
}
}
@media only screen and (min-width: 1200px) {
.filter {
right: 180px;
}
}


13 changes: 13 additions & 0 deletions app/javascript/src/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ body {
z-index: 3;
}

.btn {
border-radius: 3px;
border: 1px solid transparent;
font-weight: 400;
text-align: center;
cursor: pointer;
}

.btn-success {
background-color: #00a65a;
color: #fff;
border-color: #008d4c;
}

footer {
background-color: $footer-bg;
Expand Down
1 change: 0 additions & 1 deletion app/models/inventory_adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ class InventoryAdjustment < ApplicationRecord
belongs_to :box_item, optional: true

validates :inventory_tally, presence: true

end
28 changes: 28 additions & 0 deletions app/presenters/inventory_dashboard_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class InventoryDashboardPresenter
attr_reader :start_date, :end_date

def initialize(start_date: Date.current, end_date: Date.current)
@start_date = start_date.to_date
@end_date = end_date.to_date
end

def total_box_requests
BoxRequestsQuery.call(start_date: start_date, end_date: end_date)
end

def total_box_shipped
BoxShippedQuery.call(start_date: start_date, end_date: end_date)
end

def purchases_total
PurchasesTotalQuery.call(start_date: start_date, end_date: end_date)
end

def inventory_per_location
InventoryPerLocationQuery.call(start_date: start_date, end_date: end_date)
end

def total_items
TotalItemsQuery.call(start_date: start_date, end_date: end_date)
end
end
21 changes: 21 additions & 0 deletions app/queries/base_time_filtered_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class BaseTimeFilteredQuery
def self.call(start_date: Date.current, end_date: Date.current)
new.call(start_date: start_date, end_date: end_date)
end

def call(start_date: Date.current, end_date: Date.current)
relation.where(search_field => start_date..end_date).public_send(function)
end

def function
raise NotImplementedError
end

def search_field
raise NotImplementedError
end

def relation
raise NotImplementedError
end
end
13 changes: 13 additions & 0 deletions app/queries/box_requests_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class BoxRequestsQuery < BaseTimeFilteredQuery
def function
:count
end

def search_field
:created_at
end

def relation
BoxRequest.all
end
end
13 changes: 13 additions & 0 deletions app/queries/box_shipped_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class BoxShippedQuery < BaseTimeFilteredQuery
def function
:count
end

def search_field
:created_at
end

def relation
Box.includes(:shipped_by)
end
end
13 changes: 13 additions & 0 deletions app/queries/inventory_per_location_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class InventoryPerLocationQuery < BaseTimeFilteredQuery
def function
:count
end

def search_field
:created_at
end

def relation
InventoryTally.joins(:storage_location).group("locations.id")
end
end
13 changes: 13 additions & 0 deletions app/queries/purchases_total_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class PurchasesTotalQuery < BaseTimeFilteredQuery
def function
:count
end

def search_field
:created_at
end

def relation
Purchase.joins("INNER JOIN boxes on boxes.shipping_payment_id = purchases.id").group("boxes.shipping_payment_id")
end
end
13 changes: 13 additions & 0 deletions app/queries/total_items_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class TotalItemsQuery < BaseTimeFilteredQuery
def function
:count
end

def search_field
:created_at
end

def relation
BoxItem.joins(:inventory_type).group("inventory_types.name")
end
end
32 changes: 32 additions & 0 deletions app/services/inventory_date_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class InventoryDateFilter
INITIAL_FILTERS = {}

attr_reader :params

def self.call(params = INITIAL_FILTERS)
new(params).call
end

def initialize(params = INITIAL_FILTERS)
@params = params
end

def set_value_for_inventory(hash)
inventory_params.each do |key, value|
hash[key.to_sym] = value if value.present?
end
end

def call
INITIAL_FILTERS.tap do |hash|
set_value_for_inventory(hash) if inventory_params.present?
end
end

private

def inventory_params
params[:inventory]
end

end
33 changes: 33 additions & 0 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,36 @@
<!-- Image Header -->
<img class="img-fluid rounded mb-4" src="https://voices-of-consent-static-images.s3.amazonaws.com/voc_logo.jpg" alt="">

<h2>Dashboard</h2>

<div class="filter">
<%= form_for :inventory, action: root_path, method: :get do |f| %>
<%= f.date_field :start_date, value: params.dig(:inventory, :start_date) %>
<%= f.date_field :end_date, value: params.dig(:inventory, :end_date) %>
<%= f.submit 'Filter', :class => 'btn btn-success' %>
<% end %>
</div>

<div class="row">
<div class="info-box">
<span class="info-box-icon bg-green"><i class="fas fa-box"></i></span>
<span class="info-box-text">Box Shipped</span>
<span class="info-box-content"><%= @presenter.total_box_shipped %></span>
</div>

<div class="info-box">
<span class="info-box-icon bg-aqua"><i class="fas fa-box-open"></i></span>
<span class="info-box-text">Box Requests<span>
<span class="info-box-content"><%= @presenter.total_box_requests %></span>
</div>
</div>

<h3>Total Items grouped by inventory</h3>
<%= pie_chart(@presenter.total_items) %>

<h3>Inventory type per location</h3>
<%= pie_chart(@presenter.inventory_per_location) %>

<h3>Total Purchases that have a box via shipping payment</h3>
<%= pie_chart(@presenter.purchases_total) %>

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"@rails/webpacker": "^4.0.7",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"bootstrap": "^4.3.1",
"chart.js": "^2.9.3",
"chartkick": "^3.2.0",
"jquery": "^3.4.1",
"popper.js": "^1.15.0",
"prop-types": "^15.7.2",
Expand Down
Loading