diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fc9a7dccba..84863fa2fa 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -86,10 +86,6 @@ Layout/ExtraSpacing: - 'Guardfile' - 'app/models/barcode_item.rb' - 'app/queries/items_by_storage_collection_query.rb' - - 'app/queries/items_in_query.rb' - - 'app/queries/items_in_total_query.rb' - - 'app/queries/items_out_query.rb' - - 'app/queries/items_out_total_query.rb' - 'config/environments/development.rb' - 'config/environments/test.rb' - 'config/puma.rb' @@ -169,10 +165,6 @@ Layout/MultilineMethodCallIndentation: - 'app/models/partner_distribution.rb' - 'app/models/storage_location.rb' - 'app/queries/items_by_storage_collection_query.rb' - - 'app/queries/items_in_query.rb' - - 'app/queries/items_in_total_query.rb' - - 'app/queries/items_out_query.rb' - - 'app/queries/items_out_total_query.rb' - 'app/services/reports/acquisition_report_service.rb' - 'app/services/reports/adult_incontinence_report_service.rb' - 'app/services/reports/children_served_report_service.rb' diff --git a/app/controllers/storage_locations_controller.rb b/app/controllers/storage_locations_controller.rb index 620c3a40df..2d828e628f 100644 --- a/app/controllers/storage_locations_controller.rb +++ b/app/controllers/storage_locations_controller.rb @@ -54,15 +54,12 @@ def edit @storage_location = current_organization.storage_locations.find(params[:id]) end - # TODO: Move these queries to Query Object def show + setup_date_range_picker @storage_location = current_organization.storage_locations.find(params[:id]) version_date = params[:version_date].presence&.to_date - # TODO: Find a way to do these with less hard SQL. These queries have to be manually updated because they're not in-sync with the Model - @items_out = ItemsOutQuery.new(organization: current_organization, storage_location: @storage_location).call - @items_out_total = ItemsOutTotalQuery.new(organization: current_organization, storage_location: @storage_location).call - @items_in = ItemsInQuery.new(organization: current_organization, storage_location: @storage_location).call - @items_in_total = ItemsInTotalQuery.new(organization: current_organization, storage_location: @storage_location).call + @show_flow_tab = params[:tab] == "flow" + @date_range_filters = date_range_filters if View::Inventory.within_snapshot?(current_organization.id, version_date) @inventory = View::Inventory.new(current_organization.id, event_time: version_date) else @@ -135,6 +132,14 @@ def destroy end end + # The Inventory Flow tab's table, loaded into a lazy turbo frame on the show + # page so the inventory replay only runs when someone opens that tab. + def inventory_flow + @storage_location = current_organization.storage_locations.find(params[:id]) + @flow = ItemsFlowQuery.new(organization: current_organization, storage_location: @storage_location, + date_range: helpers.selected_range).call + end + def inventory @items = View::Inventory.items_for_location(StorageLocation.find(params[:id]), include_omitted: params[:include_omitted_items] == "true") @@ -155,6 +160,14 @@ def include_omitted_items(existing_item_ids = []) end end + # @return [Hash] the Inventory Flow tab's date range filters, if any + def date_range_filters + filters = params[:filters] + return {} unless filters.is_a?(ActionController::Parameters) + + filters.permit(:date_range, :date_range_label).to_h.compact_blank + end + helper_method \ def filter_params return {} unless params.key?(:filters) diff --git a/app/events/audit_event.rb b/app/events/audit_event.rb index aab873ada2..99ee945e55 100644 --- a/app/events/audit_event.rb +++ b/app/events/audit_event.rb @@ -14,4 +14,9 @@ def self.publish(audit) ) ) end + + # Sets quantities outright rather than editing an earlier version. + def editable? + false + end end diff --git a/app/events/inventory_aggregate.rb b/app/events/inventory_aggregate.rb index bfc6a2bdff..300bfdbc90 100644 --- a/app/events/inventory_aggregate.rb +++ b/app/events/inventory_aggregate.rb @@ -34,14 +34,13 @@ def inventory_for(organization_id, event_time: nil, validate: false) event_hash = {} events.group_by(&:group_id).each do |_, event_batch| last_grouped_event = event_batch.max_by(&:updated_at) - # don't do grouping for UpdateExistingEvents - if event_batch.any? { |e| e.is_a?(UpdateExistingEvent) } - handle(last_grouped_event, inventory, validate: validate) - yield last_grouped_event, inventory if block_given? - next + previous_event = nil + # only versions of an editable record are diffed against its previous version + if last_grouped_event.editable? + key = [last_grouped_event.eventable_type, last_grouped_event.eventable_id] + previous_event = event_hash[key] + event_hash[key] = last_grouped_event end - previous_event = event_hash[[last_grouped_event.eventable_type, last_grouped_event.eventable_id]] - event_hash[[last_grouped_event.eventable_type, last_grouped_event.eventable_id]] = last_grouped_event handle(last_grouped_event, inventory, validate: validate, previous_event: previous_event) yield last_grouped_event, inventory if block_given? end diff --git a/app/events/kit_allocate_event.rb b/app/events/kit_allocate_event.rb index aa34b0c078..f6fd988030 100644 --- a/app/events/kit_allocate_event.rb +++ b/app/events/kit_allocate_event.rb @@ -30,4 +30,9 @@ def self.publish(kit, storage_location, quantity) ) ) end + + # Each allocation is its own change rather than an edit of the kit. + def editable? + false + end end diff --git a/app/events/kit_deallocate_event.rb b/app/events/kit_deallocate_event.rb index 88521212e1..960cad9d09 100644 --- a/app/events/kit_deallocate_event.rb +++ b/app/events/kit_deallocate_event.rb @@ -30,4 +30,9 @@ def self.publish(kit, storage_location, quantity) ) ) end + + # Each deallocation is its own change rather than an edit of the kit. + def editable? + false + end end diff --git a/app/events/snapshot_event.rb b/app/events/snapshot_event.rb index 0b746c2a18..cae241a04b 100644 --- a/app/events/snapshot_event.rb +++ b/app/events/snapshot_event.rb @@ -19,4 +19,9 @@ def self.publish(organization) data: inventory ) end + + # A snapshot replaces the whole inventory rather than versioning a record. + def editable? + false + end end diff --git a/app/events/update_existing_event.rb b/app/events/update_existing_event.rb index 90133e2e39..fbc72a377a 100644 --- a/app/events/update_existing_event.rb +++ b/app/events/update_existing_event.rb @@ -60,4 +60,10 @@ def publish(itemizable, previous_line_items, original_storage_location) ) end end + + # Carries the difference from the previous version rather than a full + # version, so it is applied on its own instead of replacing an earlier event. + def editable? + false + end end diff --git a/app/helpers/date_range_helper.rb b/app/helpers/date_range_helper.rb index d6c51e1e2c..88becea1a3 100644 --- a/app/helpers/date_range_helper.rb +++ b/app/helpers/date_range_helper.rb @@ -1,11 +1,11 @@ # Encapsulates methods used on the Dashboard that need some business logic module DateRangeHelper def date_range_params - params.dig(:filters, :date_range).presence || default_date + date_range_filter(:date_range).presence || default_date end def date_range_label - case (params.dig(:filters, :date_range_label).presence || "this year").downcase + case (date_range_filter(:date_range_label).presence || "this year").downcase when "today" "today" when "yesterday" @@ -46,6 +46,13 @@ def selected_range (start_date.beginning_of_day)..(end_date.end_of_day) end + # @param key [Symbol] + # @return [String, nil] the filter's value, or nil if params[:filters] isn't a hash + def date_range_filter(key) + filters = params[:filters] + filters[key] if filters.respond_to?(:key?) + end + def selected_range_described start_date, end_date = selected_interval if start_date == Time.zone.today diff --git a/app/models/event.rb b/app/models/event.rb index 44d9972948..b6a34760cb 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -54,6 +54,15 @@ def no_intervening_snapshot end end + # Whether events of this type are versions of an editable record: each one, + # the first included, is a full version of the eventable (e.g. a donation, + # then each later edit of it), and replaces the previous event for that + # record. Events that are standalone changes return false. + # @return [Boolean] + def editable? + true + end + # @return [Array