diff --git a/app/controllers/purchases_controller.rb b/app/controllers/purchases_controller.rb index 6a675b3900..e989a6a0ed 100644 --- a/app/controllers/purchases_controller.rb +++ b/app/controllers/purchases_controller.rb @@ -101,7 +101,7 @@ def destroy def load_form_collections @storage_locations = current_organization.storage_locations.active.alphabetized @items = current_organization.items.active.alphabetized - @vendors = current_organization.vendors.alphabetized + @vendors = current_organization.vendors.active.alphabetized end def purchase_params diff --git a/app/controllers/vendors_controller.rb b/app/controllers/vendors_controller.rb index 69630f3ed0..4226997bc9 100644 --- a/app/controllers/vendors_controller.rb +++ b/app/controllers/vendors_controller.rb @@ -3,7 +3,14 @@ class VendorsController < ApplicationController include Importable def index - @vendors = current_organization.vendors.with_volumes.alphabetized + @vendors = current_organization + .vendors + .with_volumes + .alphabetized + .class_filter(filter_params) + + @vendors = @vendors.active unless params[:include_inactive_vendors] + @include_inactive_vendors = params[:include_inactive_vendors] respond_to do |format| format.html @@ -53,6 +60,34 @@ def update end end + def deactivate + vendor = current_organization.vendors.find(params[:id]) + + begin + vendor.deactivate! + rescue => e + flash[:error] = e.message + redirect_back(fallback_location: vendors_path) + return + end + + redirect_to vendors_path, notice: "#{vendor.business_name} has been deactivated." + end + + def reactivate + vendor = current_organization.vendors.find(params[:id]) + + begin + vendor.reactivate! + rescue => e + flash[:error] = e.message + redirect_back(fallback_location: vendors_path) + return + end + + redirect_to vendors_path, notice: "#{vendor.business_name} has been reactivated." + end + private def vendor_params @@ -61,7 +96,9 @@ def vendor_params end helper_method \ - def filter_params - {} + def filter_params(_parameters = nil) + return {} unless params.key?(:filters) + + params.require(:filters).permit(:include_inactive_vendors) end end diff --git a/app/models/vendor.rb b/app/models/vendor.rb index 3e742a436e..b543cfde70 100644 --- a/app/models/vendor.rb +++ b/app/models/vendor.rb @@ -3,6 +3,7 @@ # Table name: vendors # # id :bigint not null, primary key +# active :boolean default(TRUE) # address :string # business_name :string # comment :string @@ -20,16 +21,32 @@ class Vendor < ApplicationRecord has_paper_trail include Provideable include Geocodable + include Filterable has_many :purchases, inverse_of: :vendor, dependent: :destroy validates :business_name, presence: true scope :alphabetized, -> { order(:business_name) } - + scope :active, -> { where(active: true) } scope :with_volumes, -> { left_joins(purchases: :line_items) .select("vendors.*, SUM(COALESCE(line_items.quantity, 0)) AS volume") .group(:id) } + + def volume + LineItem.where( + itemizable_type: "Purchase", + itemizable_id: purchase_ids + ).sum(:quantity) + end + + def deactivate! + update!(active: false) + end + + def reactivate! + update!(active: true) + end end diff --git a/app/views/vendors/_vendor_row.html.erb b/app/views/vendors/_vendor_row.html.erb index 2637601138..ce81480412 100644 --- a/app/views/vendors/_vendor_row.html.erb +++ b/app/views/vendors/_vendor_row.html.erb @@ -7,5 +7,14 @@