Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/adjustments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def new
@adjustment = current_organization.adjustments.new
@adjustment.line_items.build
@storage_locations = current_organization.storage_locations.active
@items = current_organization.items.loose.active.alphabetized
@items = current_organization.concrete_items.active.alphabetized
end

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add current_organization.concrete_items


# POST /adjustments
Expand All @@ -55,7 +55,7 @@ def create

def load_form_collections
@storage_locations = current_organization.storage_locations.active
@items = current_organization.items.loose.alphabetized
@items = current_organization.concrete_items.alphabetized
end

def adjustment_params
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class ItemsController < ApplicationController
def index
@items = current_organization
.items
.includes(:kit, :line_items, :request_units, :item_category)
.includes(:line_items, :request_units, :item_category)
.alphabetized
.class_filter(filter_params)
.group('items.id')
@items = @items.active unless params[:include_inactive_items]

@item_categories = current_organization.item_categories.includes(:items).order('name ASC')
@kits = current_organization.kits.includes(kit_item: {line_items: :item})
@kits = current_organization.kits.includes(line_items: :item)
@storages = current_organization.storage_locations.active.order(id: :asc)

@include_inactive_items = params[:include_inactive_items]
Expand Down Expand Up @@ -139,7 +139,7 @@ def remove_category

def reporting_category_hint
item = current_organization.items.find(params[:id])
if item.kit_id
if item.is_a?(Kit)
"Kits are reported based on their contents."
end
end
Expand Down
25 changes: 9 additions & 16 deletions app/controllers/kits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def show
end

def index
@kits = current_organization.kits.includes(kit_item: {line_items: :item}).class_filter(filter_params)
@kits = current_organization.kits.includes(line_items: :item).class_filter(filter_params)
@inventory = View::Inventory.new(current_organization.id)
unless params[:include_inactive_items]
@kits = @kits.active
Expand All @@ -16,8 +16,7 @@ def new
load_form_collections

@kit = current_organization.kits.new
@kit.kit_item = KitItem.new(organization: current_organization)
@kit.kit_item.line_items.build
@kit.line_items.build
end

def create
Expand All @@ -32,21 +31,17 @@ def create
.map { |error| formatted_error_message(error) }
.join(", ")

# Extract kit and item params separately since line_items belong to Item, not Kit
kit_only_params = kit_params.except(:line_items_attributes)
@kit = Kit.new(kit_only_params)
load_form_collections
@kit.kit_item ||= KitItem.new(organization: current_organization,
**kit_params.slice(:line_items_attributes))
@kit.kit_item.line_items.build if @kit.kit_item.line_items.empty?
@kit = current_organization.kits.new(kit_params)
@kit.line_items.build if @kit.line_items.empty?

render :new
end
end

def deactivate
@kit = current_organization.kits.find(params[:id])
@kit.deactivate
@kit.deactivate!
redirect_back_or_to(dashboard_path, notice: "Kit has been deactivated!")
end

Expand Down Expand Up @@ -92,14 +87,12 @@ def load_form_collections
end

def kit_params
kit_params = params.require(:kit).permit(
params.require(:kit).permit(
:name,
:visible_to_partners,
:value_in_dollars
)
item_params = params.require(:kit_item)
.permit(line_items_attributes: [:item_id, :quantity, :_destroy])
kit_params.to_h.merge(item_params.to_h)
:value_in_dollars,
line_items_attributes: [:item_id, :quantity, :_destroy]
).to_h
end

def kit_adjustment_params
Expand Down
6 changes: 3 additions & 3 deletions app/events/kit_allocate_event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class KitAllocateEvent < Event
def self.event_line_items(kit, storage_location, quantity)
items = kit.kit_item.line_items.map do |item|
items = kit.line_items.map do |item|
EventTypes::EventLineItem.new(
quantity: item.quantity * quantity,
item_id: item.item_id,
Expand All @@ -11,8 +11,8 @@ def self.event_line_items(kit, storage_location, quantity)
end
items.push(EventTypes::EventLineItem.new(
quantity: quantity,
item_id: kit.kit_item.id,
item_value_in_cents: kit.kit_item.value_in_cents,
item_id: kit.id,
item_value_in_cents: kit.value_in_cents,
to_storage_location: storage_location,
from_storage_location: nil
))
Expand Down
6 changes: 3 additions & 3 deletions app/events/kit_deallocate_event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class KitDeallocateEvent < Event
def self.event_line_items(kit, storage_location, quantity)
items = kit.kit_item.line_items.map do |item|
items = kit.line_items.map do |item|
EventTypes::EventLineItem.new(
quantity: item.quantity * quantity,
item_id: item.item_id,
Expand All @@ -11,8 +11,8 @@ def self.event_line_items(kit, storage_location, quantity)
end
items.push(EventTypes::EventLineItem.new(
quantity: quantity,
item_id: kit.kit_item.id,
item_value_in_cents: kit.kit_item.value_in_cents,
item_id: kit.id,
item_value_in_cents: kit.value_in_cents,
from_storage_location: storage_location,
to_storage_location: nil
))
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/kits_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module KitsHelper
def deactivate_kit_button(kit, inventory)
options = {class: "deactivate-kit-button"}
span_options = {}
unless kit.can_deactivate?(inventory)
unless kit.can_deactivate_or_delete?(inventory)
msg = "Can't deactivate while a storage location still has kits."
options[:enabled] = false
span_options = {title: msg, class: "tooltip-target"}
Expand Down
1 change: 1 addition & 0 deletions app/models/concrete_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
# organization_id :integer
#
class ConcreteItem < Item
validates :reporting_category, presence: true
end
26 changes: 8 additions & 18 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ class Item < ApplicationRecord
include Itemizable

after_initialize :set_default_distribution_quantity, if: :new_record?
after_update :update_associated_kit_name, if: -> { kit.present? }
before_destroy :validate_destroy, prepend: true

belongs_to :organization # If these are universal this isn't necessary
belongs_to :base_item, counter_cache: :item_count, primary_key: :partner_key, foreign_key: :partner_key, inverse_of: :items, optional: true
belongs_to :kit, optional: true
belongs_to :item_category, optional: true

validates :additional_info, length: { maximum: 500 }
Expand All @@ -46,7 +44,6 @@ class Item < ApplicationRecord
validates :on_hand_recommended_quantity, numericality: { greater_than_or_equal_to: 0 }, allow_blank: true
validates :on_hand_minimum_quantity, numericality: { greater_than_or_equal_to: 0 }
validates :package_size, numericality: { greater_than_or_equal_to: 0 }, allow_blank: true
validates :reporting_category, presence: true, unless: proc { |i| i.kit }
validate -> { line_items_quantity_is_at_least(1) }

has_many :used_line_items, dependent: :destroy, class_name: "LineItem"
Expand All @@ -57,10 +54,6 @@ class Item < ApplicationRecord
has_many :request_units, class_name: "ItemUnit", dependent: :destroy

scope :active, -> { where(active: true) }

# :housing_a_kit are items which house a kit, NOT items is_in_kit
scope :housing_a_kit, -> { where.not(kit_id: nil) }
scope :loose, -> { where(kit_id: nil) }
scope :inactive, -> { where.not(active: true) }

scope :visible, -> { where(visible_to_partners: true) }
Expand Down Expand Up @@ -106,17 +99,17 @@ def in_request?

def is_in_kit?(kits = nil)
if kits
kits.any? { |k| k.kit_item.line_items.map(&:item_id).include?(id) }
kits.any? { |k| k.line_items.map(&:item_id).include?(id) }
else
organization.kits
.active
.joins(kit_item: :line_items)
.joins(:line_items)
.where(line_items: { item_id: id}).any?
end
end

def can_delete?(inventory = nil, kits = nil)
can_deactivate_or_delete?(inventory, kits) && used_line_items.none? && !barcode_count&.positive? && !in_request? && kit.blank?
can_deactivate_or_delete?(inventory, kits) && used_line_items.none? && !barcode_count&.positive? && !in_request?
end

# @return [Boolean]
Expand All @@ -141,11 +134,7 @@ def deactivate!
unless can_deactivate_or_delete?
raise "Cannot deactivate item - it is in a storage location or kit!"
end
if kit
kit.deactivate
else
update!(active: false)
end
update!(active: false)
end

# @return [String]
Expand Down Expand Up @@ -199,10 +188,11 @@ def sync_request_units!(unit_ids)
private

def set_default_distribution_quantity
self.distribution_quantity ||= kit_id.present? ? 1 : 50
self.distribution_quantity ||= default_distribution_quantity
end

def update_associated_kit_name
kit.update(name: name)
# Overridden in Kit (kits default to 1).
def default_distribution_quantity
50
end
end
67 changes: 33 additions & 34 deletions app/models/kit.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
# == Schema Information
#
# Table name: kits
# Table name: items
#
# id :bigint not null, primary key
# active :boolean default(TRUE)
# name :string not null
# value_in_cents :integer default(0)
# visible_to_partners :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :integer not null
# id :integer not null, primary key
# active :boolean default(TRUE)
# additional_info :text
# barcode_count :integer
# distribution_quantity :integer
# name :string
# on_hand_minimum_quantity :integer default(0), not null
# on_hand_recommended_quantity :integer
# package_size :integer
# partner_key :string
# reporting_category :string
# type :string default("ConcreteItem"), not null
# value_in_cents :integer default(0)
# visible_to_partners :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# item_category_id :integer
# kit_id :integer
# organization_id :integer
#
class Kit < ApplicationRecord
has_paper_trail
include Filterable
include Valuable

belongs_to :organization
has_one :kit_item, dependent: :restrict_with_exception

scope :active, -> { where(active: true) }
scope :alphabetized, -> { order(:name) }
class Kit < Item
scope :by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }

validates :name, presence: true
validates :name, uniqueness: { scope: :organization }

# @param inventory [View::Inventory]
# @return [Boolean]
def can_deactivate?(inventory = nil)
inventory ||= View::Inventory.new(organization_id)
inventory.quantity_for(item_id: kit_item.id).zero?
end

def deactivate
update!(active: false)
kit_item.update!(active: false)
# Kits are managed through the kits UI (deactivate/reactivate), not the normal
# item-deletion path.
def can_delete?(inventory = nil, kits = nil)
false
end

# Kits can't reactivate if they have any inactive items, because now whenever they are allocated
# or deallocated, we are changing inventory for inactive items (which we don't allow).
# @return [Boolean]
def can_reactivate?
kit_item.line_items.joins(:item).where(items: { active: false }).none?
line_items.joins(:item).where(items: {active: false}).none?
end

def reactivate
update!(active: true)
kit_item.update!(active: true)
end

private

# Kits default to a distribution quantity of 1 (a single kit), not 50.
def default_distribution_quantity
1
end
end
29 changes: 0 additions & 29 deletions app/models/kit_item.rb

This file was deleted.

1 change: 1 addition & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Organization < ApplicationRecord
has_many :product_drive_tags, -> { by_type("ProductDrive") },
class_name: "Tag", inverse_of: false
has_many :inventory_items, through: :storage_locations
has_many :concrete_items
has_many :kits
has_many :transfers
has_many :users, -> { distinct }, through: :roles
Expand Down
Loading
Loading