Skip to content

Commit 6e923a6

Browse files
author
Olexii Kasianenko
committed
Add date range filtering to ItemsFlowQuery and update Inventory Flow tab
1 parent bb56879 commit 6e923a6

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

app/controllers/storage_locations_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def show
5858
setup_date_range_picker
5959
@storage_location = current_organization.storage_locations.find(params[:id])
6060
version_date = params[:version_date].presence&.to_date
61-
@items = ItemsFlowQuery.new(storage_location: @storage_location, organization: current_organization).call.to_a
62-
@total_quantity_in = @items.first["total_quantity_in"].to_i
63-
@total_quantity_out = @items.first["total_quantity_out"].to_i
61+
@items = ItemsFlowQuery.new(storage_location: @storage_location, organization: current_organization, filter_params: date_range).call.to_a
62+
@total_quantity_in = @items.count.positive? ? @items.first["total_quantity_in"].to_i : 0
63+
@total_quantity_out = @items.count.positive? ? @items.first["total_quantity_out"].to_i : 0
6464
@total_quantity_change = @total_quantity_in - @total_quantity_out
6565
if View::Inventory.within_snapshot?(current_organization.id, version_date)
6666
@inventory = View::Inventory.new(current_organization.id, event_time: version_date)
@@ -163,6 +163,8 @@ def filter_params
163163
end
164164

165165
def date_range
166+
return unless filter_params[:date_range].present?
167+
166168
date_range = filter_params[:date_range].split(" - ")
167169
start_date = Date.parse(date_range[0])
168170
end_date = Date.parse(date_range[1])

app/queries/items_flow_query.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def call
5454
LEFT JOIN adjustments ON adjustments.id = li.itemizable_id AND li.itemizable_type = 'Adjustment'
5555
LEFT JOIN transfers ON transfers.id = li.itemizable_id AND li.itemizable_type = 'Transfer'
5656
LEFT JOIN items it ON it.id = li.item_id
57+
WHERE it.created_at BETWEEN :start_date AND :end_date
5758
)
5859
SELECT
5960
item_id,
@@ -71,7 +72,12 @@ def call
7172
SQL
7273

7374
ActiveRecord::Base.connection.exec_query(
74-
ActiveRecord::Base.send(:sanitize_sql_array, [query, {id: @storage_location.id, organization_id: @organization.id}])
75+
ActiveRecord::Base.send(:sanitize_sql_array, [query, {
76+
id: @storage_location.id,
77+
organization_id: @organization.id,
78+
start_date: @filter_params ? @filter_params[0] : 20.years.ago,
79+
end_date: @filter_params ? @filter_params[1] : Time.current.end_of_day
80+
}])
7581
)
7682
end
7783
end

app/views/storage_locations/show.html.erb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@
6868
<div class="card card-primary card-outline card-outline-tabs">
6969
<div class="card-header p-0 border-bottom-0">
7070
<ul class="nav nav-tabs" id="custom-tabs-three-tab" role="tablist">
71-
<li class="nav-item active">
72-
<a class="nav-link active" id="custom-tabs-inventory-tab" data-bs-toggle="pill" href="#custom-tabs-inventory" role="tab" aria-controls="custom-tabs-one-home-tab" aria-selected="true">Inventory</a>
71+
<li class="nav-item <%= params.dig(:filters, :date_range).nil? ? 'active' : '' %>">
72+
<a class="nav-link <%= params.dig(:filters, :date_range).nil? ? 'active' : '' %>" id="custom-tabs-inventory-tab" data-bs-toggle="pill" href="#custom-tabs-inventory" role="tab" aria-controls="custom-tabs-one-home-tab" aria-selected="true">Inventory</a>
7373
</li>
74-
<li class="nav-item">
75-
<a class="nav-link" id="custom-tabs-inventory-flow-tab" data-bs-toggle="pill" href="#custom-tabs-inventory-flow" role="tab" aria-controls="custom-tabs-two-home-tab" aria-selected="false">Inventory Flow</a>
74+
<li class="nav-item <%= params.dig(:filters, :date_range).present? ? 'active' : '' %>">
75+
<a class="nav-link <%= params.dig(:filters, :date_range).present? ? 'active' : '' %>" id="custom-tabs-inventory-flow-tab" data-bs-toggle="pill" href="#custom-tabs-inventory-flow" role="tab" aria-controls="custom-tabs-two-home-tab" aria-selected="false">Inventory Flow</a>
7676
</li>
7777
</ul>
7878
</div>
7979
<div class="card-body">
8080
<div class="tab-content" id="custom-tabs-three-tabContent">
8181

82-
<div class="tab-pane fade show active" id="custom-tabs-inventory" role="tabpanel" aria-labelledby="custom-tabs-one-home-tab">
82+
<div class="tab-pane fade <%= params.dig(:filters, :date_range).nil? ? 'show active' : '' %>" id="custom-tabs-inventory" role="tabpanel" aria-labelledby="custom-tabs-one-home-tab">
8383
<%= form_for @storage_location, method: :get do %>
8484
Show Inventory at Date: <%= date_field_tag 'version_date', params[:version_date], min: InventoryItem::EARLIEST_VERSION, autocomplete: "on" %>
8585
<%= filter_button(text: 'View') %>
@@ -128,7 +128,7 @@
128128
</table>
129129
</div><!-- /.box-body.table-responsive -->
130130

131-
<div class="tab-pane fade show" id="custom-tabs-inventory-flow" role="tabpanel" aria-labelledby="custom-two-home-tab">
131+
<div class="tab-pane fade <%= params.dig(:filters, :date_range).present? ? 'show active' : '' %>" id="custom-tabs-inventory-flow" role="tabpanel" aria-labelledby="custom-two-home-tab">
132132
<%= form_for @storage_location, method: :get do %>
133133
<%= label_tag "Date Range" %>
134134
<div class="row">

spec/queries/items_flow_query_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,51 @@
4646
expect(subject.to_a).to match_array(result)
4747
end
4848
end
49+
50+
context "with filter params" do
51+
let(:filter_params) { [11.days.ago, 9.days.ago] }
52+
let!(:old_items) do
53+
[
54+
create(:item, organization: organization, created_at: 10.days.ago.beginning_of_day),
55+
create(:item, organization: organization, created_at: 10.days.ago.end_of_day)
56+
]
57+
end
58+
let(:other_location) { create(:storage_location, organization: organization) }
59+
60+
subject { described_class.new(organization: organization, storage_location: storage_location, filter_params: filter_params).call }
61+
62+
before do
63+
create(:donation, :with_items, item: old_items[0], item_quantity: 10, storage_location: storage_location)
64+
create(:distribution, :with_items, item: old_items[1], item_quantity: 5, storage_location: storage_location)
65+
end
66+
67+
let(:filtered_result) do
68+
[
69+
{
70+
item_id: old_items[0].id,
71+
item_name: old_items[0].name,
72+
quantity_in: 10,
73+
quantity_out: 0,
74+
change: 10,
75+
total_quantity_in: 10,
76+
total_quantity_out: 5,
77+
total_change: 5
78+
},
79+
{
80+
item_id: old_items[1].id,
81+
item_name: old_items[1].name,
82+
quantity_in: 0,
83+
quantity_out: 5,
84+
change: -5,
85+
total_quantity_in: 10,
86+
total_quantity_out: 5,
87+
total_change: 5
88+
}
89+
].map(&:with_indifferent_access)
90+
end
91+
92+
it "returns array of hashes" do
93+
expect(subject.to_a).to match_array(filtered_result)
94+
end
95+
end
4996
end

0 commit comments

Comments
 (0)