Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3609 Inventory out should include items allocated to kits. Inventory in should include kits allocated #3641

Conversation

zeeshan-haidar
Copy link
Contributor

@zeeshan-haidar zeeshan-haidar commented Jun 4, 2023

Resolves #3609

Description:

It's just a quick draft PR to see if I am in the right direction with my approach
I created one model 'allocations' with enum allocation_type: ['inventory_in', 'inventory_out']

so far code added for allocation only, once got some feedback I will also add code for de-allocation

about 'inventory_out'

at time of kit allocation, I am creating one record for 'allocations' with type 'inventory out' with line_items equal to kit's line_items
having quantity in negative, quantity is based number of kits added

so in-case we add another kit with the same kit_id, it will just find this allocation record and adjust quantity of line_items

about 'inventory_in'

at time of kit allocation, creating one record for allocations with type 'inventory_in', then adding only one line_item with kit.item.id and kit quantity,

in-case user add another kit with same kit_id it will find the allocation record with kit_id and type 'inventory_in'
and adjust the first line_item's quantity

Question:

in-case invetory_in there is always one line_item for one record in allocation
so having has_many association for line_items doesn't makes sense here

instead of making itemizable type model we can have model similar to invetory_item for both invetory_in and invetory_out, wdyt?

@zeeshan-haidar zeeshan-haidar reopened this Jun 4, 2023
@zeeshan-haidar zeeshan-haidar marked this pull request as draft June 4, 2023 16:46
@zeeshan-haidar zeeshan-haidar force-pushed the 3609-kit-allocation-should-create-inventory-in-out branch from 0a6ed90 to 42c5f9d Compare June 5, 2023 12:07
Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

Had some comments/suggestions. The overall approach looks good IMO.

# organization_id :bigint not null
# storage_location_id :bigint not null
#
class Allocation < ApplicationRecord
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we call this KitAllocation to make it obvious that we're dealing with kits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dorner change applied

@@ -205,6 +206,38 @@ def decrease_inventory(itemizable_array)
log
end

def manage_inventory_out(line_items_array, kit_id, operation, adjust_by)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method shouldn't be in the storage location model - it's only marginally related to it. Since it relates to multiple models, it probably should be in a service object.

Also, calling a method manage_x is a code smell indicating it's trying to do too much, or we're not really sure what it's doing. Can you come up with a different name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dorner change applied


def manage_inventory_in(associated_kit, kit_id, operation)
allocation = allocations.find_or_create_by!(kit_id: kit_id, organization_id: organization.id, allocation_type: "inventory_in")
adjust_by = associated_kit[0][:quantity]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is associated_kit an array of hashes? That's not indicative of the argument name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

change applied

def manage_inventory_in(associated_kit, kit_id, operation)
allocation = allocations.find_or_create_by!(kit_id: kit_id, organization_id: organization.id, allocation_type: "inventory_in")
adjust_by = associated_kit[0][:quantity]
adjust_by = operation == 'allocate' ? adjust_by : adjust_by.to_i * (-1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

adjust_by = associated_kit[0][:quantity].to_i
adjust_by *= -1 if operation == 'allocate'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

change applied

@zeeshan-haidar zeeshan-haidar force-pushed the 3609-kit-allocation-should-create-inventory-in-out branch from 42c5f9d to 3327d74 Compare June 7, 2023 05:04
Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

Just one question - otherwise it looks pretty good!

t.references :organization, null: false, foreign_key: true
t.references :storage_location, null: false, foreign_key: true
t.references :kit, null: false, foreign_key: true
t.integer :kit_allocation_type, null: false, default: 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason we can't use Postgres enums here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dorner updated the code with Postgres enum

@zeeshan-haidar zeeshan-haidar marked this pull request as ready for review June 15, 2023 05:25
@zeeshan-haidar
Copy link
Contributor Author

@dorner some specs are failing, I checked they are also failing in main branch where I have made no change, any thoughts?

@@ -13,5 +13,4 @@
class KitAllocation < ApplicationRecord
include Itemizable
belongs_to :storage_location
enum kit_allocation_type: {inventory_in: 0, inventory_out: 1}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why was this renamed from kit_allocation_type? I feel like that was a more indicative name.

Copy link
Contributor Author

@zeeshan-haidar zeeshan-haidar Jul 4, 2023

Choose a reason for hiding this comment

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

reused kit_allocation_type name

safety_assured do
create_enum :kit_allocation_inventory, ["inventory_in", "inventory_out"]
change_table :kit_allocations do |t|
t.enum :inventory, enum_type: "kit_allocation_inventory", default: "inventory_in", null: false
Copy link
Collaborator

Choose a reason for hiding this comment

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

kit_allocation_type was a brand new column introduced in this PR. There's no need to check in migrations that add the column, then remove it and replace it with a different one. Just change the migration itself and fix your local :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -0,0 +1,92 @@
class BringInventoryInAndOutToInvetoryLevel < ActiveRecord::Migration[7.0]
# this data migration is for the perpose to bring inventory_in and inventory_out
Copy link
Collaborator

Choose a reason for hiding this comment

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

"purpose"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed this migration

class BringInventoryInAndOutToInvetoryLevel < ActiveRecord::Migration[7.0]
# this data migration is for the perpose to bring inventory_in and inventory_out
# to current inventory level so that in-case of kit de-allocation we do not face "inconsistency inventory in" error
class AllocationService
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason you copy/pasted this entire class into the migration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed this migration as per discussion with @cielf


def up
StorageLocation.all.each do |location|
location.inventory_items.select { |inventory_item| inventory_item.item.partner_key == "kit" }.each do |inventory_item|
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably cleaner just to check if the item has a kit_id. You can do this with a join rather than a select after the fact:

location.inventory_items.joins(:item).merge(Item.kits)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed this migration as per discussion with @cielf

kit_item = inventory_item.item
kit = kit_item.kit
if kit.present?
service = AllocationService.new(storage_location: location, kit: kit, increase_by: kit_quantity)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are we 100% sure we need to do this for all existing items? I.e. are we sure that the inventory drift issue is a fact across all inventory?

Copy link
Collaborator

Choose a reason for hiding this comment

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

i'm going to have to dig into this a bit, but I don't think the correction was part of the ask in the issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There is an argument for adding in the line items for the previously allocated kits - but I'm not yet convinced we should, because of intervening adjustments by the banks.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We talked about this in the Sunday planning meeting . Our take on it is that this migration should not go in, because of other things we can't fix, and resultant confusion for the banks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cielf removed data migration

Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

I think this is good to go!

@dorner dorner merged commit a99cae2 into rubyforgood:main Jul 6, 2023
@cielf
Copy link
Collaborator

cielf commented Jul 10, 2023

Thanks @zeeshan-haidar -- we pushed this to production this morning -- there are still some things to be done, though -- please check out the re-opened issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inventory out should include items allocated to kits. Inventory in should include kits allocated
3 participants