you can transform the search results into a map wh...
# suitescript
b
you can transform the search results into a map where the keys are item internal ids, and the values are arrays of objects (where the objects are ordered by your expiration date and have keys storing the lot internal id and quantity available)
Copy code
{
  "123": [
    {
      "inventorynumber": "1",
      "quantityavailable": 8
    },
    {
      "inventorynumber": "2",
      "quantityavailable": 3
    }
  ],
  "456": [
    {
      "inventorynumber": "3",
      "quantityavailable": 6
    },
    {
      "inventorynumber": "4",
      "quantityavailable": 2
    }
  ]
}
c
with an array in that layout (ES6 Map() isn’t available in NS) I’m thinking indexOf(item[i]) then add 1 to the index and that will give me access to the fields in the array directly next to the item number
b
didnt mean an actual Map, but the key-value data structure it represents
c
lots.push(itemId, {location:location, available: available, lotNumber: lotNumber});
that sort of things?
*thing
I’ve almost finished this - it’s a bit of mind bending logic
b
as in you do your search for all the relevant lot information
then place them in an object where the keys are item internal ids
and the values are arrays of objects
whenever you process an item, use the item internal id to access the lots array
c
yeah that’s what I did thanks
the only difficult part was assigning the lot number and moving to the next lot number if there wasn’t enough inventory for which I used a recursive function
b
what does the recursive function look like? I rarely feel the need to naturally use them
c
Just added it to the main channel
so if the second if branch sees that there is not enough inventory in the lot, it calls itself again and provides a new index number to the next array of lot details
there’s probably a better way but that was my first attempt - I’ll usually rewrite a few times
b
i dont think your line 10 or 11 does what you think it does
c
That’s not passed in via reference
so it won’t update the data structure - but I don’t think it needs to update it
we can just return out of the function and move to the next item
can probably remove those lines completely as they are only local scope due to not being reference.
I think……
b
im assuming you use 9 for something
but lines 10 and 11 do nothing
and you probably need them to remember which lot(s) you assigned so that the next duplicate item works
c
yeah, that’s a good point
So i need to update the values in array ‘lots’
I think it would only matter for lot qty, the qty committed on the item doesn’t matter for the next iteration / dupe item
would this work for line 10?
lotDetails.available = lotDetails.available - itemCommittedInventory;
lotDetails comes from the parameter array lots which I believe is passed by reference
Updated the code in the main chan
b
you probably need to do more
at some point, you will have an item committed that is equal to the lotQty
your code will 0 it out
and the next thing to process it will try assigning a lot with 0 quantity
i dont see any logic for ignore (or better: removing) lots that have been completely assigned
you probably realize by now that you really want to get some unit tests in here
c
Yes, I’m putting tests together tomorrow. I just got previous sales orders to use a stubbed data
I see what you’re saying about the potential 0 qty lot number assignment. Line 9 drops a lot qty to 0 but that doesn’t stop that lot number being considered again
I don’t think it matters though - as itemCommittedInventory can’t be 0 or < 0 - that’s filtered out in the search.
so line 9 setting the lot qty to zero and that if statement on line 7 should never allow for a 0 allocation.
b
if you have two of the same item, there is the possibility that one of your lots will run out of quantity
your current code represents that by setting the quantity available of the object to 0
the second item will have to deal with that 0 quantity lot
c
then that item will never get a lot assignment
true - so need to move onto the next lot in that scenario, or remove the lot when it hits zero
b
again, i dont see your complete code, but it either needs to ignore (or preferably remove) 0 quantity lots. You don't want to treat it the same a non-zero lot
c
yeah
thanks, invoice me for our pair programming time
b
this is not the first time I've seen auto selection of inventory details. Although it is the first attempt I've seen trying to use a recursive function
I've seen loops
c
I could do loops I guess - my issue was the map like data structure requires me to specify an index for the key the you have to add 1 to the index to the get the following inventory array then add another 1 for every subsequent array under that item key. I was always taught that recursion works well in that sort of scenario. I guess you could rewrite it as a loop - should’t make too much difference - maybe it’s the difference between comp sci and soft eng 🙂
b
ill also tell you that you putting everything into an array instead of using an object as a map is probably an abuse of the array structure
c
yeah maybe. I tend to write things several times anyway until i really understand the problem. this is V1