473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Recursion - Inventory System

blyxx86
256 100+
Hello Again Everyone,

I've got a few database tables set up as a test scenario to be able to find total inventory by company, by location, shelf, bin, etc..

Very simply the data structure is like this:
inv_location
inv_location_id
location_name
parent_inv_location_id

Then inventory counts go here
inv_stocktage
inv_stocktake_id
part_id
quantity
inv_location_id (FK to inv_location)
stocktake_date



Ok, so database stuff aside, I'm trying to figure out a decent way to recursively go through each inventory location and see what is listed under it in the stocktake table, group them appropriately and store in an inventory object (seems the most beneficial way to recursively loop through and store the results).

I don't need code written, just some help getting the flow setup.

So far, I have something that goes like this (just pseudo code)
Expand|Select|Wrap|Line Numbers
  1. function find_inv($loc_id)
  2. {
  3.     // grab an array of all other inv_locations that have current loc_id as a parent_id
  4.     $array = select items from inv_location table
  5.  
  6.     if $array has records
  7.     {
  8.         foreach(value fround in above-mentioned array as $id)
  9.         {
  10.             // begin recursive loop here
  11.             find_inv($id);
  12.         }
  13.     }
  14.  
  15.     $values = select items from inv_stocktake table and store in some class variable
  16.  
  17.     return this;
  18.  
  19. }
  20.  
Again that is just very rough pseudo code, but I'm just trying to get a feel if my logic is correct.

I get a bit lost on how I will be storing the found $values but I am sure I could work something out.

Any ideas to get me going in the right direction?

Thanks!!
Jul 27 '09 #1
5 1858
bilibytes
128 100+
i don't understand really well what you want to achieve.

maybe this helps you : Modified Preorder Tree Traversal

good luck
Jul 27 '09 #2
Dormilich
8,658 Expert Mod 8TB
don’t know whether that is of relevance: views
Jul 27 '09 #3
blyxx86
256 100+
@Dormilich
I actually use views somewhat regularly in our database here, I would have to develop a recursive stored procedure of sorts to do this, since there can be "N" levels of parents for the inventory location.
Jul 27 '09 #4
blyxx86
256 100+
Let me provide a sample of some desired output (or rough idea):

Expand|Select|Wrap|Line Numbers
  1. Loc_id  Part_id  Qty
  2. +1      ABC      5
  3. --3     ABC      2
  4. -+4     ABC      3
  5.   --5   ABC      1
  6.   --6   ABC      2
  7.  
Where under Loc_id Qty equals 5, as you add up all the locations beneath it. Qty 2 in Loc 3, and then recursively lookup Loc_id 5 and 6 to come with a total Qty of 3 in Location 4 (Which has children 5 and 6).

I hope that makes sense.

Basically I need to make a recursive function that will tell me what is in a Warehouse and then be able to drill it down further.

IE.,
Expand|Select|Wrap|Line Numbers
  1. +Warehouse
  2.     +Area
  3.       +Shelf
  4.          +Column
  5.            +Bin
  6.               +n...
  7.  
I would use the hierarchical method of left & right, except the massive updates to the table would become a bottleneck as inventory movements may happen thousands of times per day and the inventory needs to only be called when certain functions/reports are ran.
Jul 27 '09 #5
blyxx86
256 100+
So I created a class for it that mostly works for what I need it to.

I hope the comments inside help make sense of what is going on.
Expand|Select|Wrap|Line Numbers
  1. class Inventory
  2. {
  3.     var $my_array = array();
  4.     function Inventory($parent_id='', $level=0)
  5.     {
  6.         //constructor calls inner function
  7.         $this->find_inv($parent_id, $level);
  8.     }
  9.  
  10.     /**
  11.      * Function to print out inventory found based on a location_id given
  12.      * @param parent_id
  13.      * @param level
  14.      */
  15.     function find_inv($parent_id='', $level=0)
  16.     {
  17.  
  18.         // choose inventory location to start searching
  19.         // blank value results in showing "IS NULL" for parent_id
  20.         if($parent_id != '')
  21.         {
  22.             $result = mysql_query('SELECT * FROM inv_location '.
  23.                                 'WHERE parent_inv_location_id="'.$parent_id.'";');
  24.         } else {
  25.             $result = mysql_query('SELECT * FROM inv_location ' .
  26.                                 'WHERE parent_inv_location_id IS NULL;');
  27.         }
  28.         while($row = mysql_fetch_object($result))
  29.         {
  30.             // find parts found at current inventory location
  31.             $inner = mysql_query('SELECT * FROM inv_stocktake ' .
  32.                                 'WHERE inv_location_id ='. $row->inv_location_id.';');
  33.  
  34.             // show output of location found, indenting children
  35.             echo str_repeat('  ',$level).$row->name."\n";
  36.  
  37.             while($row2 = mysql_fetch_object($inner))
  38.             {
  39.                 // store total qty found based on parent_id given
  40.                 // my_array[part_id]['qty'] = total quantity
  41.                 if(isset($this->my_array[$row2->part_id]['qty']))
  42.                 {
  43.                     $this->my_array[$row2->part_id]['qty'] += $row2->quantity;
  44.                 } else {
  45.                     $this->my_array[$row2->part_id]['qty'] = $row2->quantity;
  46.                 }
  47.  
  48.                 // store quantity found in each location
  49.                 // this only shows individual pieces found in location, not the recursive total in location
  50.                 // my_array[part_id]['locations'][inv_location_id]['qty'] = qty at location
  51.                 if(isset($this->my_array[$row2->part_id]['locations'][$row->inv_location_id]['qty']))
  52.                 {
  53.                     $this->my_array[$row2->part_id]['locations'][$row->inv_location_id]['qty'] += $row2->quantity;
  54.                 } else {
  55.                     $this->my_array[$row2->part_id]['locations'][$row->inv_location_id]['qty'] = $row2->quantity;
  56.                 }
  57.  
  58.                 // show standard output of inventory found at location
  59.                 echo str_repeat('   ',$level).$row2->part_id." ".$row2->quantity."\n";
  60.             }
  61.  
  62.             // begin recursive lookup
  63.             $this->find_inv($row->inv_location_id, $level+1);
  64.         }
  65.     }
  66.  
  67.     function get_array()
  68.     {
  69.         return $this->my_array;
  70.     }
  71. };
  72.  

I then get to call the function from within another piece of code.
Expand|Select|Wrap|Line Numbers
  1. $a = new Inventory($parent_id, $level);
  2. print_r($a->get_array());
  3.  
I insert about 20,000 rows into my (inv_stocktake) table and it is pulling (and printing) the results in 0.24 seconds, compared to my standard page loads of .06seconds. Thank goodness for MySQL caching!!! I was getting about 100 queries running when I ran the sample data, which is somewhat realistic since most of the locations won't have more than a few individual children.

I would obviously like to improve upon what is here, but I think that this is a good start for what I am doing.
Jul 28 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Henrik | last post by:
Hi all, I'm a .net developer working with developing automation equipment for the sawmill industry. The main focus of our product line is mechanical and measurement equipment but our clients...
4
nexcompac
by: nexcompac | last post by:
Ok, I posted a similar post but now need to jump back into it. Here is what I have been able to clean up. I am using textpad and jbuilder. Still getting used to the whole java world and I am...
9
by: xxplod | last post by:
I am suppose to modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including...
3
by: cblank | last post by:
I need some help if someone could help me. I know everyone is asking for help in java. But for some reason I'm the same as everyone else when it comes to programming in java. I have an inventory...
2
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform...
1
by: jcato77 | last post by:
I need help with a class project I'm working on, Below is my assignment and the code I have currently created. Assignment: Modify the Inventory Program by creating a subclass of the product class...
13
by: jcato77 | last post by:
I am having trouble figuring out my code and was hoping someone could point me in the right direction. Below is my code what I need to due is create a method to add and display the value of the...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
2
by: blitz1989 | last post by:
Hello all, I'm new to this forum and Java and having a alot of problems understanding this language. I am working on an invetory program part 4. The assignment requirements are listed but the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.