Skip to content

Commit b7a331c

Browse files
authored
Merge pull request #4607 from rubyforgood/4333-remove-inventory-items
4333 remove inventory items
2 parents d20e229 + b0f600c commit b7a331c

File tree

86 files changed

+332
-2092
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+332
-2092
lines changed

.rubocop_todo.yml

-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ Layout/SpaceInsideHashLiteralBraces:
225225
- 'app/models/product_drive_participant.rb'
226226
- 'app/models/distribution.rb'
227227
- 'app/models/donation.rb'
228-
- 'app/models/inventory_item.rb'
229228
- 'app/models/item.rb'
230229
- 'app/models/item_category.rb'
231230
- 'app/models/kit.rb'

app/controllers/audits_controller.rb

+2-23
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ def index
1010
end
1111

1212
def show
13-
if Event.read_events?(@audit.organization)
14-
@items = View::Inventory.items_for_location(@audit.storage_location)
15-
else
16-
@inventory_items = @audit.storage_location.inventory_items
17-
end
13+
@items = View::Inventory.items_for_location(@audit.storage_location)
1814
end
1915

2016
def edit
@@ -28,24 +24,7 @@ def finalize
2824
@audit.adjustment = Adjustment.new(organization_id: @audit.organization_id, storage_location_id: @audit.storage_location_id, user_id: current_user.id, comment: 'Created Automatically through the Auditing Process')
2925
@audit.save
3026

31-
inventory_items = @audit.storage_location.inventory_items
32-
33-
inventory_items.each do |inventory_item|
34-
line_item = @audit.line_items.find_by(item: inventory_item.item)
35-
36-
next if line_item.nil?
37-
38-
if line_item.quantity != inventory_item.quantity
39-
@audit.adjustment.line_items.create(item_id: inventory_item.item.id, quantity: line_item.quantity - inventory_item.quantity)
40-
end
41-
end
42-
43-
increasing_adjustment, decreasing_adjustment = @audit.adjustment.split_difference
44-
ActiveRecord::Base.transaction do
45-
@audit.storage_location.increase_inventory(increasing_adjustment.line_item_values)
46-
@audit.storage_location.decrease_inventory(decreasing_adjustment.line_item_values)
47-
AuditEvent.publish(@audit)
48-
end
27+
AuditEvent.publish(@audit)
4928
@audit.finalized!
5029
redirect_to audit_path(@audit), notice: "Audit is Finalized."
5130
rescue => e

app/controllers/distributions_controller.rb

+9-21
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,9 @@ def create
117117
@items = current_organization.items.alphabetized
118118
@partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized
119119

120-
if Event.read_events?(current_organization)
121-
inventory = View::Inventory.new(@distribution.organization_id)
122-
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
123-
inventory.quantity_for(storage_location: storage_loc.id).positive?
124-
end
125-
else
126-
@storage_locations = current_organization.storage_locations.active_locations.has_inventory_items.alphabetized
120+
inventory = View::Inventory.new(@distribution.organization_id)
121+
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
122+
inventory.quantity_for(storage_location: storage_loc.id).positive?
127123
end
128124

129125
flash_error = insufficient_error_message(result.error.message)
@@ -151,13 +147,9 @@ def new
151147
@items = current_organization.items.alphabetized
152148
@partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized
153149

154-
if Event.read_events?(current_organization)
155-
inventory = View::Inventory.new(current_organization.id)
156-
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
157-
inventory.quantity_for(storage_location: storage_loc.id).positive?
158-
end
159-
else
160-
@storage_locations = current_organization.storage_locations.active_locations.has_inventory_items.alphabetized
150+
inventory = View::Inventory.new(current_organization.id)
151+
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
152+
inventory.quantity_for(storage_location: storage_loc.id).positive?
161153
end
162154
end
163155

@@ -183,13 +175,9 @@ def edit
183175
@audit_warning = current_organization.audits
184176
.where(storage_location_id: @distribution.storage_location_id)
185177
.where("updated_at > ?", @distribution.created_at).any?
186-
if Event.read_events?(current_organization)
187-
inventory = View::Inventory.new(@distribution.organization_id)
188-
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
189-
!inventory.quantity_for(storage_location: storage_loc.id).negative?
190-
end
191-
else
192-
@storage_locations = current_organization.storage_locations.active_locations.has_inventory_items.alphabetized
178+
inventory = View::Inventory.new(@distribution.organization_id)
179+
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
180+
!inventory.quantity_for(storage_location: storage_loc.id).negative?
193181
end
194182
else
195183
redirect_to distributions_path, error: 'To edit a distribution,

app/controllers/donations_controller.rb

-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def update
9393
@original_source = @donation.source
9494
ItemizableUpdateService.call(itemizable: @donation,
9595
params: donation_params,
96-
type: :increase,
9796
event_class: DonationEvent)
9897
flash.clear
9998
flash[:notice] = "Donation updated!"

app/controllers/items_controller.rb

+6-16
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,22 @@ def index
1111
@items = @items.active unless params[:include_inactive_items]
1212

1313
@item_categories = current_organization.item_categories.includes(:items).order('name ASC')
14-
@kits = current_organization.kits.includes(line_items: :item, inventory_items: :storage_location)
14+
@kits = current_organization.kits.includes(line_items: :item)
1515
@storages = current_organization.storage_locations.active_locations.order(id: :asc)
1616

1717
@include_inactive_items = params[:include_inactive_items]
1818
@selected_base_item = filter_params[:by_base_item]
1919

2020
@paginated_items = @items.page(params[:page])
2121

22-
if Event.read_events?(current_organization)
23-
@inventory = View::Inventory.new(current_organization.id)
24-
end
22+
@inventory = View::Inventory.new(current_organization.id)
2523
@items_by_storage_collection_and_quantity = ItemsByStorageCollectionAndQuantityQuery.call(organization: current_organization,
2624
inventory: @inventory,
2725
filter_params: filter_params)
2826

2927
respond_to do |format|
3028
format.html
31-
if Event.read_events?(current_organization)
32-
format.csv { send_data Item.generate_csv_from_inventory(@items, @inventory), filename: "Items-#{Time.zone.today}.csv" }
33-
else
34-
format.csv { send_data Item.generate_csv(@items), filename: "Items-#{Time.zone.today}.csv" }
35-
end
29+
format.csv { send_data Item.generate_csv_from_inventory(@items, @inventory), filename: "Items-#{Time.zone.today}.csv" }
3630
end
3731
end
3832

@@ -71,13 +65,9 @@ def edit
7165

7266
def show
7367
@item = current_organization.items.find(params[:id])
74-
if Event.read_events?(current_organization)
75-
@inventory = View::Inventory.new(current_organization.id)
76-
storage_location_ids = @inventory.storage_locations_for_item(@item.id)
77-
@storage_locations_containing = StorageLocation.find(storage_location_ids)
78-
else
79-
@storage_locations_containing = current_organization.items.storage_locations_containing(@item)
80-
end
68+
@inventory = View::Inventory.new(current_organization.id)
69+
storage_location_ids = @inventory.storage_locations_for_item(@item.id)
70+
@storage_locations_containing = StorageLocation.find(storage_location_ids)
8171
@barcodes_for = current_organization.items.barcodes_for(@item)
8272
end
8373

app/controllers/kits_controller.rb

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class KitsController < ApplicationController
22
def index
3-
@kits = current_organization.kits.includes(line_items: :item, inventory_items: :storage_location).class_filter(filter_params)
4-
if Event.read_events?(current_organization)
5-
@inventory = View::Inventory.new(current_organization.id)
6-
end
3+
@kits = current_organization.kits.includes(line_items: :item).class_filter(filter_params)
4+
@inventory = View::Inventory.new(current_organization.id)
75
unless params[:include_inactive_items]
86
@kits = @kits.active
97
end
@@ -56,11 +54,7 @@ def reactivate
5654
def allocations
5755
@kit = Kit.find(params[:id])
5856
@storage_locations = current_organization.storage_locations.active_locations
59-
if Event.read_events?(current_organization)
60-
@inventory = View::Inventory.new(current_organization.id)
61-
else
62-
@item_inventories = @kit.item.inventory_items
63-
end
57+
@inventory = View::Inventory.new(current_organization.id)
6458

6559
load_form_collections
6660
end
@@ -69,21 +63,14 @@ def allocate
6963
@kit = Kit.find(params[:id])
7064
@storage_location = current_organization.storage_locations.active_locations.find(kit_adjustment_params[:storage_location_id])
7165
@change_by = kit_adjustment_params[:change_by].to_i
72-
73-
if @change_by.positive?
74-
service = AllocateKitInventoryService.new(kit: @kit, storage_location: @storage_location, increase_by: @change_by)
75-
service.allocate
76-
flash[:error] = service.error if service.error
77-
elsif @change_by.negative?
78-
service = DeallocateKitInventoryService.new(kit: @kit, storage_location: @storage_location, decrease_by: @change_by.abs)
79-
service.deallocate
80-
flash[:error] = service.error if service.error
81-
end
82-
83-
if service.error
84-
flash[:error] = service.error
85-
else
86-
flash[:notice] = "#{@kit.name} at #{@storage_location.name} quantity has changed by #{@change_by}"
66+
begin
67+
if @change_by.positive?
68+
KitAllocateEvent.publish(@kit, @storage_location.id, @change_by)
69+
else
70+
KitDeallocateEvent.publish(@kit, @storage_location.id, -@change_by)
71+
end
72+
rescue => e
73+
flash[:error] = e.message
8774
end
8875

8976
redirect_to allocations_kit_path(id: @kit.id)

app/controllers/partners/dashboards_controller.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ def show
2424

2525
@families = @partner.families
2626
@children = @partner.children
27-
if Event.read_events?(@partner.organization)
28-
@inventory = View::Inventory.new(@partner.organization_id)
29-
end
27+
@inventory = View::Inventory.new(@partner.organization_id)
3028

3129
@broadcast_announcements = BroadcastAnnouncement.filter_announcements(@parent_org)
3230
end

app/controllers/purchases_controller.rb

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def update
6868
@purchase = current_organization.purchases.find(params[:id])
6969
ItemizableUpdateService.call(itemizable: @purchase,
7070
params: purchase_params,
71-
type: :increase,
7271
event_class: PurchaseEvent)
7372
redirect_to purchases_path
7473
rescue => e

app/controllers/requests_controller.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ def print_unfulfilled
6363
def load_items
6464
return unless @request.request_items
6565

66-
inventory = nil
67-
if Event.read_events?(@request.organization)
68-
inventory = View::Inventory.new(@request.organization_id)
69-
end
70-
66+
inventory = View::Inventory.new(@request.organization_id)
7167
@request.request_items.map { |json| RequestItem.from_json(json, @request, inventory) }
7268
end
7369

app/controllers/storage_locations_controller.rb

+14-45
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ def quantity
99
end
1010

1111
def index
12-
if Event.read_events?(current_organization)
13-
@inventory = View::Inventory.new(current_organization.id)
14-
end
15-
12+
@inventory = View::Inventory.new(current_organization.id)
1613
@selected_item_category = filter_params[:containing]
1714
@items = StorageLocation.items_inventoried(current_organization, @inventory)
1815
@include_inactive_storage_locations = params[:include_inactive_storage_locations].present?
@@ -33,21 +30,7 @@ def index
3330
respond_to do |format|
3431
format.html
3532
format.csv do
36-
if Event.read_events?(current_organization)
37-
send_data StorageLocation.generate_csv_from_inventory(@storage_locations, @inventory), filename: "StorageLocations-#{Time.zone.today}.csv"
38-
else
39-
active_inventory_item_names = []
40-
@storage_locations.each do |storage_location|
41-
active_inventory_item_names <<
42-
storage_location
43-
.active_inventory_items
44-
.joins(:item)
45-
.select('distinct items.name')
46-
.pluck(:name)
47-
end
48-
active_inventory_item_names = active_inventory_item_names.flatten.uniq.sort
49-
send_data StorageLocation.generate_csv(@storage_locations, active_inventory_item_names), filename: "StorageLocations-#{Time.zone.today}.csv"
50-
end
33+
send_data StorageLocation.generate_csv_from_inventory(@storage_locations, @inventory), filename: "StorageLocations-#{Time.zone.today}.csv"
5134
end
5235
end
5336
end
@@ -78,16 +61,14 @@ def show
7861
@items_out_total = ItemsOutTotalQuery.new(organization: current_organization, storage_location: @storage_location).call
7962
@items_in = ItemsInQuery.new(organization: current_organization, storage_location: @storage_location).call
8063
@items_in_total = ItemsInTotalQuery.new(organization: current_organization, storage_location: @storage_location).call
81-
if Event.read_events?(current_organization)
82-
if View::Inventory.within_snapshot?(current_organization.id, params[:version_date])
83-
@inventory = View::Inventory.new(current_organization.id, event_time: params[:version_date])
84-
else
85-
@legacy_inventory = View::Inventory.legacy_inventory_for_storage_location(
86-
current_organization.id,
87-
@storage_location.id,
88-
params[:version_date]
89-
)
90-
end
64+
if View::Inventory.within_snapshot?(current_organization.id, params[:version_date])
65+
@inventory = View::Inventory.new(current_organization.id, event_time: params[:version_date])
66+
else
67+
@legacy_inventory = View::Inventory.legacy_inventory_for_storage_location(
68+
current_organization.id,
69+
@storage_location.id,
70+
params[:version_date]
71+
)
9172
end
9273

9374
respond_to do |format|
@@ -154,22 +135,10 @@ def destroy
154135
end
155136

156137
def inventory
157-
if Event.read_events?(current_organization)
158-
@items = View::Inventory.items_for_location(StorageLocation.find(params[:id]),
159-
include_omitted: params[:include_omitted_items] == "true")
160-
respond_to do |format|
161-
format.json { render :event_inventory }
162-
end
163-
else
164-
@inventory_items = current_organization.storage_locations
165-
.includes(inventory_items: :item)
166-
.find(params[:id])
167-
.inventory_items
168-
.active
169-
170-
@inventory_items += include_omitted_items(@inventory_items.collect(&:item_id)) if params[:include_omitted_items] == "true"
171-
@inventory_items.to_a.sort_by! { |inventory_item| inventory_item.item.name.downcase }
172-
respond_to :json
138+
@items = View::Inventory.items_for_location(StorageLocation.find(params[:id]),
139+
include_omitted: params[:include_omitted_items] == "true")
140+
respond_to do |format|
141+
format.json { render :event_inventory }
173142
end
174143
end
175144

app/events/event_types/event_line_item.rb

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ def same_item?(line_item)
1919
end
2020
end
2121

22+
# @return [EventTypes::EventLineItem]
23+
def negative
24+
self.class.new(
25+
quantity: -quantity,
26+
item_id: item_id,
27+
item_value_in_cents: item_value_in_cents,
28+
from_storage_location: from_storage_location,
29+
to_storage_location: to_storage_location
30+
)
31+
end
32+
2233
# @param line_item [LineItem]
2334
# @param from [Integer]
2435
# @param to [Integer]

app/events/kit_deallocate_event.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ def self.event_line_items(kit, storage_location, quantity)
55
quantity: item.quantity * quantity,
66
item_id: item.item_id,
77
item_value_in_cents: item.item.value_in_cents,
8-
to_storage_location: storage_location.id,
8+
to_storage_location: storage_location,
99
from_storage_location: nil
1010
)
1111
end
1212
items.push(EventTypes::EventLineItem.new(
1313
quantity: quantity,
1414
item_id: kit.item.id,
1515
item_value_in_cents: kit.item.value_in_cents,
16-
from_storage_location: storage_location.id,
16+
from_storage_location: storage_location,
1717
to_storage_location: nil
1818
))
1919
items

0 commit comments

Comments
 (0)