Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi all!

I'm stuck somewhere. I have a table for Inventory (which contains the available amount of each food component)
What I want to do: If I select a certain food (say salad) I want to be able to deduct the components (amount) of 'salad' from the inventory table.

Consider this:
Salad is made up of 20mil of water, 50 of oil and 100gm of onion (just an example)
When I give someone a Salad, I want to be able to deduct 20mil of water, 50 of oil and 100gm of onion from the inventory table. (So that I know how much of each item in my inventory is left)

The code is to allow these recipes of Salad to be changed/deleted/some others added into it. (Flexibility). I've found this difficult since in order to allow this flexibility, I can't just write that deduction in code as something that won't change forever.(More recipes can be added in the future)

Does anyone has an idea on how to do this?

I use MySQL with VB.NET

Thanks in advance!

P.S: What I have so far:
My tables are:
1.Inventory (item,amount)
2. Menu (menu_name)
3. Salad (recipe, amount)
Other tables will be added for each menu, just like I did for salad.
Now, when I click Salad, since it has two values (recipe for it and amount), how then can I subtract these (recipe & amount) from the (amount) in the inventory table?

I find it challenging.
Posted
Updated 18-Jun-13 2:24am
v2

1 solution

I have worked on scenario somewhat similar to this

Surely there must be a Table which will store recipes for Each Item
1.Lets Says in Inventory Table stores Raw Material

2.Menu table stores the End product like Salad

3.and there should be another table will store Items needed for each menu with quantity (using foreign key over above two tables)

if you have this structure, your recipes can be flexible. and every time you have to use inner join query to find out how much stock you have consumed

________________________________________________________________
Let’s Say Inventory Table have Primary Key PKInvid.
And Menu table (end Product Salad) have Primary Key PKMenuId
The third table will have structure like this

CREATE TABLE [dbo].[Recipe]
(
[PKRecipeid] [int] IDENTITY(1,1) NOT NULL, -- Primary Key
[FK_InvID] INT, -- Foreign Key refering to Inventory Table (raw material)
[FK_MenuID] INT, -- Foreign Key Refering to Menu table
[Qty] Float , -- Inventory Quntity needed

)

This table can hold information like for
FK_MenuID Salad, FK_InvID of water with Qty as 20
FK_MenuID Salad, FK_InvID of Oil with Qty as 50
FK_MenuID Salad, FK_InvID of water with Qty as 500

Now there is order for two salads, you can use query like below
Declare @Order INT -– for 2 orders
DECLARE @Menu INT – for Salads

Finally you have to store this information in table variable and write a while loop to update each quantity row by row

Declare @tab AS TABLE
(
TID Identity(1,1)
FK_MenuID INT,
QTY Float
)
INSERT INTO @Tab
Select FK_InvID, Qty * @Order FROM Recipe where FK_MenuID = @MenuID
DECLARE @RowCOunt INT,@i INT =1
SET @RowCOunt =(select Count(*) form @tab)

WHILE @i < @RowCOunt
BEGIN
SET @QTY = (SELECT QTY FROM @tab WHERE TID = @i)
SET @FK_MenuID = (SELECT FK_MenuID FROM @tab WHERE TID = @i)
UPDATE Invenotry SET QTY =QTY - @QTY where MenuID = @FK_MenuID
SET @i=@i+1
END
 
Share this answer
 
v2
Comments
savedlema 18-Jun-13 8:13am    
Thank you Amol_B!
I have three tables (similar to your solution, except to the third table), can you please tell me more on table 3 of your solution? How is it gonna look like?

Thanks.
savedlema 18-Jun-13 8:23am    
My tables are:
1.Inventory (item,amount)
2. Menu (menu_name)
3. Salad (recipe, amount)
Other tables will be added for each menu, just like I did for salad.
Now, when I click Salad, since it has two values (recipe for it and amount), how then can I subtract these (recipe & amount) from the (amount) in the inventory table?

I find it challenging.
Amol_B 18-Jun-13 8:42am    
please Check updated solution.
If it helps you mark it as answer
Note that this is general idea, there might be another way of doing this
savedlema 18-Jun-13 14:07pm    
Thank you Amol! I think your solution is taking me somewhere good.
I have few questions from your solution:

- You said if there're two orders (Declare @Order INT -– for 2 orders
DECLARE @Menu INT – for Salads ), What will these codes do, I missed the point here.

-Is your code for VB.NET? I think not? Eg. in "Declare @tab AS TABLE", I think VB.NET use Dim instead of "Declare" to declare a variable. Also in "AS TABLE", VB reports an error in "TABLE". What did you want this to do? Are you creating a table called "tab"? (the we could just use the normal CREATE TABLE command?)

-For the "INSERT INTO @Tab", it seems we're inserting these values to a table "Tab"? which is created in the previous lines?

Please help me understand this before I start looping. Please forgive my being a novice.

savedlema 19-Jun-13 13:23pm    
Hi Amol_B, are you there please?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900