-
-
Notifications
You must be signed in to change notification settings - Fork 493
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
3609 Inventory out should include items allocated to kits. Inventory in should include kits allocated #3641
Conversation
0a6ed90
to
42c5f9d
Compare
There was a problem hiding this 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.
app/models/allocation.rb
Outdated
# organization_id :bigint not null | ||
# storage_location_id :bigint not null | ||
# | ||
class Allocation < ApplicationRecord |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dorner change applied
app/models/storage_location.rb
Outdated
@@ -205,6 +206,38 @@ def decrease_inventory(itemizable_array) | |||
log | |||
end | |||
|
|||
def manage_inventory_out(line_items_array, kit_id, operation, adjust_by) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dorner change applied
app/models/storage_location.rb
Outdated
|
||
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] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change applied
app/models/storage_location.rb
Outdated
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) |
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change applied
42c5f9d
to
3327d74
Compare
…o 3609-kit-allocation-should-create-inventory-in-out
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
…o 3609-kit-allocation-should-create-inventory-in-out
…o 3609-kit-allocation-should-create-inventory-in-out
@dorner some specs are failing, I checked they are also failing in main branch where I have made no change, any thoughts? |
…o 3609-kit-allocation-should-create-inventory-in-out
app/models/kit_allocation.rb
Outdated
@@ -13,5 +13,4 @@ | |||
class KitAllocation < ApplicationRecord | |||
include Itemizable | |||
belongs_to :storage_location | |||
enum kit_allocation_type: {inventory_in: 0, inventory_out: 1} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"purpose"
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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| |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cielf removed data migration
…o 3656-add-shipped-to-delivery-method
…o 3656-add-shipped-to-delivery-method
…o 3656-add-shipped-to-delivery-method
…o 3609-kit-allocation-should-create-inventory-in-out # Conflicts: # db/schema.rb
…han-haidar/human-essentials into 3609-kit-allocation-should-create-inventory-in-out
There was a problem hiding this 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!
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. |
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?