diff --git a/app/controllers/distributions_controller.rb b/app/controllers/distributions_controller.rb index 68a0bb39e8..e518e24865 100644 --- a/app/controllers/distributions_controller.rb +++ b/app/controllers/distributions_controller.rb @@ -121,6 +121,11 @@ def create @storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc| inventory.quantity_for(storage_location: storage_loc.id).positive? end + if @distribution.storage_location.present? + @item_labels_with_quantities = inventory + .items_for_location(@distribution.storage_location.id, include_omitted: true) + .map(&:to_dropdown_option) + end flash_error = insufficient_error_message(result.error.message) diff --git a/app/models/view/inventory.rb b/app/models/view/inventory.rb index 08a209fe7e..437372d5ba 100644 --- a/app/models/view/inventory.rb +++ b/app/models/view/inventory.rb @@ -5,9 +5,15 @@ module Types module View # A wrapper around event-driven InventoryAggregate for use in views. class Inventory + ItemDropdownOption = Struct.new(:id, :name) + class ViewInventoryItem < EventTypes::EventItem attribute :db_item, Types::Nominal::Any delegate(*Item.column_names.map(&:to_sym), to: :db_item) + + def to_dropdown_option + ItemDropdownOption.new(id: id, name: "#{name} (#{quantity})") + end end attr_accessor :inventory, :organization_id diff --git a/app/views/line_items/_line_item_fields.html.erb b/app/views/line_items/_line_item_fields.html.erb index 437f278b12..7906e40cd9 100644 --- a/app/views/line_items/_line_item_fields.html.erb +++ b/app/views/line_items/_line_item_fields.html.erb @@ -15,7 +15,7 @@ <%= field.input :item_id, disabled: requested.present?, - collection: @items, prompt: "Choose an item", + collection: @item_labels_with_quantities || @items, prompt: "Choose an item", include_blank: "", label: false, input_html: { class: "my-0 line_item_name", "data-controller": "select2" } %> diff --git a/spec/requests/distributions_requests_spec.rb b/spec/requests/distributions_requests_spec.rb index 54e70166c7..2a2a3c6efb 100644 --- a/spec/requests/distributions_requests_spec.rb +++ b/spec/requests/distributions_requests_spec.rb @@ -130,6 +130,17 @@ expect(response).to have_error end + it "renders #new with item quantities in dropdowns listed" do + create(:item, :with_unit, organization: organization, name: 'Item 1', unit: 'pack') + + post distributions_path(distribution: distribution.except(:partner_id), format: :turbo_stream) + + expect(response).to have_http_status(400) + expect(flash[:error]).to include("Sorry, we weren't able to save the distribution.") + + expect(response.body).to include("Item 1 (0)") + end + context "Deactivated partners should not be displayed in partner dropdown" do before do create(:partner, name: 'Active Partner', organization: organization, status: "approved")