Calculating the cost of inventory kits
Below is the logic which can be used to calculate the cost of an inventory item (kit) which is made up of other inventory items. The cost of these kits will change when the parts used to make them change. To get the cost, we use a function that calls itself.
Look at the code sample below. We first call this function with the inventory part number that is a kit. The function will then loop through the kit table to find all the inventory items which are used in this kit. For each item in the kit, it calls itself. We do this because a kit can be used as a part for a different kit.
Function GetPartCost Integer iPartNum Returns Number Number nCost nTotal Clear Inv Move iPartNum to Inv.Number Find eq Inv by Index.1 If (Found) Begin If (Inv.Kit="Y") Begin Clear Kit Move Inv.Number to Kit.PartNum Find ge Kit by Index.1 If (Found) Indicate Found as (Kit.PartNum=Inv.Number) While (Found) // Function calls itself with new part number // beware of kits containing themself. Get GetPartCost Kit.KitPartNum to nCost Add (Kit.Qty*nCost) to nTotal Find gt Kit by Index.1 If (Found) Indicate Found as (Kit.PartNum=Inv.Number) Loop // Return the kit's total cost Function_Return nTotal End Else Begin // Return the item's cost Function_Return Inv.Cost End End Else Begin Error 999 ("Part not found: "+String(iPartNum)) Function_Return 0 End End_Function
You are a restaurant owner and you want to know what each of your meals cost. One of the items on your menu is a hamburger. The logic might look something like this:
Famous Dave's Sliders 1 Homemade Kaiser Roll 1/3 pound Ground Sirloin 1 slice of American Cheese 1 slice of Sharp Cheddar Cheese Lettuce 2 slices of Tomato 3 ounces of Secret Sauce 8 ounces of Steak Fries 1 Pickle Wedge
The Kaiser Roll and the Special Sauce are kits, because you make them. So you also need to determine their cost.
Kaiser Rolls 1 pound Flour 2 cups Water 1/3 cup Sugar 12 ounces of yeast 1/4 cup Extra Virgin Olive Oil
Special Sauce 1 Tbsp Ingredient A 1 cup Ingredient B 1/2 Tbsp Ingredient C 1/2 Tbsp Ingredient D 3 ounces Ingredient E 1 Tbsp Ingredient F 1 tsp Ingredient G
So, by calling the GetPartCost of the main menu item, it will automatically traverse all kits to get to the root items and return their cost.
GetPartCost Famous Dave's Sliders GetPartCost Homemade Kaiser Roll GetPartCost Flour GetPartCost Water GetPartCost Sugar GetPartCost Yeast GetPartCost Olive Oil GetPartCost Ground Sirloin GetPartCost American Cheese GetPartCost Sharp Cheddar Cheese GetPartCost Lettuce GetPartCost Tomato GetPartCost Secret Sauce GetPartCost Ingredient A GetPartCost Ingredient B GetPartCost Ingredient C GetPartCost Ingredient D GetPartCost Ingredient E GetPartCost Ingredient F GetPartCost Ingredient G GetPartCost Steak Fries GetPartCost Pickle Wedge