Skip to content

Commit

Permalink
calculate_the_difference_instead_of_replace_option
Browse files Browse the repository at this point in the history
  • Loading branch information
fchatterji committed Jul 2, 2023
1 parent fcb2b0d commit f48e19d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/models/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.storage_locations_adjusted_for(organization)
end

def split_difference
pre_adjustment = line_items.partition { |line_item| line_item.quantity.positive? }
pre_adjustment = line_items.reject { |line_item| line_item.quantity == 0 }.partition { |line_item| line_item.quantity.positive? }
increasing_adjustment, decreasing_adjustment = pre_adjustment.map { |adjustment| Adjustment.new(line_items: adjustment) }

decreasing_adjustment.line_items.each { |line_item| line_item.quantity *= -1 }
Expand Down
16 changes: 10 additions & 6 deletions app/models/storage_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ def self.import_csv(csv, organization)

# NOTE: We should generalize this elsewhere -- Importable concern?
def self.import_inventory(filename, org, loc)
raise Errors::InventoryAlreadyHasItems if StorageLocation.find(loc.to_i).size > 0

current_org = Organization.find(org)
storage_location = StorageLocation.find(loc.to_i)

raise Errors::InventoryAlreadyHasItems if storage_location.size > 0

adjustment = current_org.adjustments.create!(storage_location_id: loc.to_i,
user_id: User.with_role(Role::ORG_ADMIN, current_org).first&.id,
Expand All @@ -129,15 +130,18 @@ def self.import_inventory(filename, org, loc)
adjustment.line_items
.create(quantity: row[0].to_i, item_id: current_org.items.find_by(name: row[1]))
end
adjustment.storage_location.increase_inventory(adjustment, replace_inventory: true)

increasing_adjustment, _decreasing_adjustment = adjustment.split_difference

storage_location.increase_inventory increasing_adjustment
end

def remove_empty_items
inventory_items.where(quantity: 0).delete_all
end

# FIXME: After this is stable, revisit how we do logging
def increase_inventory(itemizable_array, replace_inventory: false)
def increase_inventory(itemizable_array)
itemizable_array = itemizable_array.to_a

# This is, at least for now, how we log changes to the inventory made in this call
Expand All @@ -148,8 +152,8 @@ def increase_inventory(itemizable_array, replace_inventory: false)
# Locate the storage box for the item, or create a new storage box for it
inventory_item = inventory_items.find_or_create_by!(item_id: item_hash[:item_id])
# Increase the quantity-on-record for that item
new_quantity = item_hash[:quantity].to_i
new_quantity += inventory_item.quantity unless replace_inventory
new_quantity = inventory_item.quantity + item_hash[:quantity].to_i

inventory_item.update!(quantity: new_quantity)
# Record in the log that this has occurred
log[item_hash[:item_id]] = "+#{item_hash[:quantity]}"
Expand Down
7 changes: 0 additions & 7 deletions spec/models/storage_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@

expect(subject.item_total(item.id)).to eq 76
end

it "replaces inventory quantities if the replace inventory option is used" do
expect do
subject.increase_inventory(donation, replace_inventory: true)
end.to change { subject.size }.by(56)
expect(subject.item_total(item.id)).to eq 66
end
end

context "when providing a new item that does not yet exist" do
Expand Down

0 comments on commit f48e19d

Please sign in to comment.