@@ -43,30 +43,33 @@ def index
4343
4444 @distributions = current_organization
4545 . distributions
46- . includes ( :partner , :storage_location , line_items : [ :item ] )
47- . order ( 'issued_at DESC' )
48- . apply_filters ( filter_params , helpers . selected_range )
46+ . order ( issued_at : :desc )
47+ . includes ( :partner , :storage_location )
48+ . class_filter ( scope_filters )
4949 @paginated_distributions = @distributions . page ( params [ :page ] )
50- @items = current_organization . items . alphabetized
51- @item_categories = current_organization . item_categories
52- @storage_locations = current_organization . storage_locations . active_locations . alphabetized
53- @partners = @distributions . collect ( & :partner ) . uniq . sort_by ( & :name )
50+ @items = current_organization . items . alphabetized . select ( :id , :name )
51+ @item_categories = current_organization . item_categories . select ( :id , :name )
52+ @storage_locations = current_organization . storage_locations . active_locations . alphabetized . select ( :id , :name )
53+ @partners = current_organization . partners . active . alphabetized . select ( :id , :name )
5454 @selected_item = filter_params [ :by_item_id ] . presence
55- @total_value_all_distributions = total_value ( @distributions )
56- @total_items_all_distributions = total_items ( @distributions , @selected_item )
57- @total_value_paginated_distributions = total_value ( @paginated_distributions )
58- @total_items_paginated_distributions = total_items ( @paginated_distributions , @selected_item )
55+ @distribution_totals = DistributionTotalsService . new ( current_organization . distributions , scope_filters )
56+ @total_value_all_distributions = @distribution_totals . total_value
57+ @total_items_all_distributions = @distribution_totals . total_quantity
58+ paginated_ids = @paginated_distributions . ids
59+ @total_value_paginated_distributions = @distribution_totals . total_value ( paginated_ids )
60+ @total_items_paginated_distributions = @distribution_totals . total_quantity ( paginated_ids )
5961 @selected_item_category = filter_params [ :by_item_category_id ]
6062 @selected_partner = filter_params [ :by_partner ]
6163 @selected_status = filter_params [ :by_state ]
6264 @selected_location = filter_params [ :by_location ]
6365 # FIXME: one of these needs to be removed but it's unclear which at this point
6466 @statuses = Distribution . states . transform_keys ( &:humanize )
67+ @distributions_with_inactive_items = @distributions . joins ( :inactive_items ) . pluck ( :id )
6568
6669 respond_to do |format |
6770 format . html
6871 format . csv do
69- send_data Exports ::ExportDistributionsCSVService . new ( distributions : @distributions , organization : current_organization , filters : filter_params ) . generate_csv , filename : "Distributions-#{ Time . zone . today } .csv"
72+ send_data Exports ::ExportDistributionsCSVService . new ( distributions : @distributions , organization : current_organization , filters : scope_filters ) . generate_csv , filename : "Distributions-#{ Time . zone . today } .csv"
7073 end
7174 end
7275 end
@@ -114,16 +117,12 @@ def create
114117 elsif request_id
115118 @distribution . initialize_request_items
116119 end
117- @items = current_organization . items . alphabetized
120+ @items = current_organization . items . active . alphabetized
118121 @partner_list = current_organization . partners . where . not ( status : 'deactivated' ) . alphabetized
119122
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
123+ inventory = View ::Inventory . new ( @distribution . organization_id )
124+ @storage_locations = current_organization . storage_locations . active_locations . alphabetized . select do |storage_loc |
125+ inventory . quantity_for ( storage_location : storage_loc . id ) . positive?
127126 end
128127
129128 flash_error = insufficient_error_message ( result . error . message )
@@ -148,16 +147,12 @@ def new
148147 @distribution . line_items . build
149148 @distribution . copy_from_donation ( params [ :donation_id ] , params [ :storage_location_id ] )
150149 end
151- @items = current_organization . items . alphabetized
150+ @items = current_organization . items . active . alphabetized
152151 @partner_list = current_organization . partners . where . not ( status : 'deactivated' ) . alphabetized
153152
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
153+ inventory = View ::Inventory . new ( current_organization . id )
154+ @storage_locations = current_organization . storage_locations . active_locations . alphabetized . select do |storage_loc |
155+ inventory . quantity_for ( storage_location : storage_loc . id ) . positive?
161156 end
162157 end
163158
@@ -178,18 +173,14 @@ def edit
178173 if ( !@distribution . complete? && @distribution . future? ) ||
179174 current_user . has_role? ( Role ::ORG_ADMIN , current_organization )
180175 @distribution . line_items . build if @distribution . line_items . size . zero?
181- @items = current_organization . items . alphabetized
176+ @items = current_organization . items . active . alphabetized
182177 @partner_list = current_organization . partners . alphabetized
183178 @audit_warning = current_organization . audits
184179 . where ( storage_location_id : @distribution . storage_location_id )
185180 . 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
181+ inventory = View ::Inventory . new ( @distribution . organization_id )
182+ @storage_locations = current_organization . storage_locations . active_locations . alphabetized . select do |storage_loc |
183+ !inventory . quantity_for ( storage_location : storage_loc . id ) . negative?
193184 end
194185 else
195186 redirect_to distributions_path , error : 'To edit a distribution,
@@ -213,7 +204,7 @@ def update
213204 flash [ :error ] = insufficient_error_message ( result . error . message )
214205 @distribution . line_items . build if @distribution . line_items . size . zero?
215206 @distribution . initialize_request_items
216- @items = current_organization . items . alphabetized
207+ @items = current_organization . items . active . alphabetized
217208 @storage_locations = current_organization . storage_locations . active_locations . alphabetized
218209 render :edit
219210 end
@@ -297,16 +288,6 @@ def request_id
297288 params . dig ( :distribution , :request_attributes , :id )
298289 end
299290
300- def total_items ( distributions , item )
301- query = LineItem . where ( itemizable_type : "Distribution" , itemizable_id : distributions . pluck ( :id ) )
302- query = query . where ( item_id : item . to_i ) if item
303- query . sum ( 'quantity' )
304- end
305-
306- def total_value ( distributions )
307- distributions . sum ( &:value_per_itemizable )
308- end
309-
310291 def daily_items ( pick_ups )
311292 item_groups = LineItem . where ( itemizable_type : "Distribution" , itemizable_id : pick_ups . pluck ( :id ) ) . group_by ( &:item_id )
312293 item_groups . map do |_id , items |
@@ -318,21 +299,26 @@ def daily_items(pick_ups)
318299 end
319300 end
320301
302+ def scope_filters
303+ filter_params
304+ . except ( :date_range )
305+ . merge ( during : helpers . selected_range )
306+ end
307+
321308 helper_method \
322309 def filter_params
323310 return { } unless params . key? ( :filters )
324311
325- params . require ( :filters ) . permit ( :by_item_id , :by_item_category_id , :by_partner , :by_state , :by_location )
312+ params
313+ . require ( :filters )
314+ . permit ( :by_item_id , :by_item_category_id , :by_partner , :by_state , :by_location , :date_range )
326315 end
327316
328317 def perform_inventory_check
329318 inventory_check_result = InventoryCheckService . new ( @distribution ) . call
330319
331- if inventory_check_result . error . present?
332- flash [ :error ] = inventory_check_result . error
333- end
334- if inventory_check_result . alert . present?
335- flash [ :alert ] = inventory_check_result . alert
336- end
320+ alerts = [ inventory_check_result . minimum_alert , inventory_check_result . recommended_alert ]
321+ merged_alert = alerts . compact . join ( "\n " )
322+ flash [ :alert ] = merged_alert if merged_alert . present?
337323 end
338324end
0 commit comments