I'm using PHP, and SQL to create a small website application for a bake shop.
I have established 2 data tables:
First one named Inventory with columns:
itemid INT(Primary key)
itemname text,
volume float,
volume type text (kg,g,lb,L,ml...)
quantity INT.
Second one named Product with columns:
prodid INT Primary Key
prodname TEXT
recipes (don't know which data type yet)
quantity INT.
The column recipes should contain numerous items from the inventory table. My friend wants it that if she make new product, the inventory database will automatically decrease the number of quantities while increasing the quantities
FOR EXAMPLE:
If she makes 100 buns(prodname) and the recipe for that 100 buns is
(quantity- itemname)
20- flour,
20- eggs,
10- sugars
then the quantity column in the inventory table should decrease by -20,-20,-10 Where the itemname are flour, eggs, sugars respectively and at the same time the product table with prodname buns should have an increase of 100 in quantity column.
Related
I am trying to build complex query in laravel for making sum for item quantity.
I have total four tables used in this query. My main table Inventories table
Inventories
My datatable is displaying by looping on inventories table.
I have another table is composite item table, which contains bunch of multiple items.
Composite Inventories
composite item and inventory table has pivot table which is relation between inventory table & composite item. relations something like many to many.
Composite has inventories
Now individual items or composite item are added to purchased order table
Purchase order
Purchase order table has one child table purchase order details which Contains item & composite items.
Purchase order details
In the purchase order details inventory_item field contains item or composite item name.
I need to get total quantity of item which has approved status in purchase order table.
For example inventory_item (item) = "stest2" (quantity="10"),
inventory_item (composite item) = "aa-buycomp" (quantity="2")
Now see setes2 item = 10 + composite item (stest2 quantity)
see stest2 item id in inventories table is "22"
Now check composite item "aa-buycomp" in composite item table it has id = "1"
Now see composite_inventorie_id = "1" & inventory_id="22" & type="purchase" in composite_has_inventories table which has quantity = "1"
I need to final sum of quantity = 10 + 1 = "11"
Please help to build laravel complex query.
I'm trying to create a web app based on a few mysql tables and forms.
I have the ADD Shipment page in which the user adds a list of products and a Invoice no., so that in my Shipment_Products table I have
id invoice_no product_code qty
1 34 HP222 4
2 34 HL234 1
I also have a Sold page in which the user adds a list of products sold from his stock, so the table Sold_Products get filled like this
id invoice_no product_code qty
1 1 HP222 2
2 34 HL234 1
I need to have a third table called Stock in which I have to get the total number of items in stock, but I'm stuck on how to auto_generate it based on these two existing tables and then keep it updated.
Any suggestions ?
I don't think what you want is as simple as what you might be trying to short-cut. First, if your company is dealing with FIFO vs LIFO inventory, you take inventory out of counts as they were available for COGS (cost of goods sold) purposes based on the cost from a given vendor at a given purchase time.
You might just have a purchases table showing every item and count received, then, as goods are sold, have another table showing the reduction from each purchase received available quantity out. It is somewhat tricky to deal with, given such a simple scenario as...
Purchase 10 of product "X" from a vendor, then another 4 of product "X" shortly after. Total of 14.
Now, for sale activity, you have one order selling 3, then 2, then 4, then 3. Total of 12 of the 14 sold, but whatever way is computed FIFO vs LIFO, there would be a split of which quantity was accounted for from which originating purchase... Ex: FIFO would be accounted for from the original 10 then 4 puchases.
10 purchase
10 - 3 = 7
7 - 2 = 5
5 - 4 = 1
1 - 3 = -2 -- WRONG... Only 1 of the 3 against the original 10 purchase
1 - (1 of 3) = 0
4 purchase
4 - (remaining 2 of 3) = 2 -- here is the remaining 2 entries from the last sale, leaving 2 left in inventory.
So, no absolute table structure to handle, but I think you should check how the inventory management requirements may need them.
I'm going to try and explain it better.
Sorry for my bad english !
I now have a table received in wich every item in a particular invoice is added when it arrives, so let's say today we receive some items, 3 items and the unique invoice_id or shipment_id (doesn't really matter) is S1. In the table received I have
id shipment_id product_code qty
1 S1 HL223 2
2 S1 XLS21 1
3 S1 BenqWHL 1
I have another similar table called sold wich works the same way, I add a list of items by their product_code and give the sale an ID (for other purposes). So the sold table looks like this:
id sold_id product_code qty
1 B1 HL223 1
2 B1 XLS21 1
Now, I just need to see the items left in stock, either by creating a table in wich I store the items grouped by their unique product_code and just count the entries as qty and then maybe when doing a sale I can substract the qty sold in this table stock ?
I don't care about invoice numbers or IDs, users, etc.
This is the bad way, but at least is the way to begins.
make a table: inventory/Stock or something with the next structure: ID, Item_id, item_quantity.
When purchase/receive, add a row to this table with a positive quantity.
When Sales/output, add a row to this table with a negative quantity.
To calculate the stock perform a query with the sum() of a particular item_id.
I'm looking for some ideas for a mySQL database to store products that have multiple (and variable) options and prices per product. E.g. some products may require several columns to store details for each of the product options:
Product 1:
SKU
Name
General product description
--------------------------
options for product 1:
Height Width Depth Weight Material Other_1 Other_2 Etc... Price
Product 2:
SKU
Name
General product description
--------------------------
options for product 2:
Flow-rate Material Weight Other_1 Other_2 Etc... Price
I'm a little stumped because the number of columns and column names might be different for each product and each product may contain numerous options/prices.
So far I'm using a 'products' table and a 'prices' table:
CREATE TABLE IF NOT EXISTS `products` (
product_id int(11) NOT NULL AUTO_INCREMENT
,product_type text
,product_page_1 text
,product_page_2 text
,product_page_3 text
,product_page_4 text
,product_sku text
,product_name text
,product_head text
,product_desc text
,product_image text
,product_sequence text
,product_flags text
,product_status text
,PRIMARY KEY (product_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=102;
CREATE TABLE IF NOT EXISTS `prices` (
price_id int(11) NOT NULL AUTO_INCREMENT
,price_type text
,price_assoc text
,price_desc text
,price_option text
,price text
,price_sequence text
,price_flags text
,price_status text
,PRIMARY KEY (price_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=109;
There can be any number of 'prices' per product. 'prices' are associated with a product ID via 'price_assoc' field.
So I can have countless price entries for each product, but whats the best way to add multiple price options per price being that each product may have varying options for its associated prices?
What you are looking for, is a EAV table. Entity-attribute-value.
Thats the kind of structure where u have table of records, and then a table of subrecords.
Records table:
id
name
active
Subrecords table:
id
record_id
key
value
And then you can do a joined query where u get a record, and all the subrecords with it. So subrecords are in one to many relationship with records.
There is php example available in github: here
I would probably use the same column for prices even though it is the same products. So instead of having to specify 4 columns for prices and descriptions for each row. Why not create a table with only on row for prices, one row for description/options/text etc. and then maybe ad a 'price-ID' field to keep track of different levels of prices.
Lets say you have one product, Product A, which have 4 different prices, then your prices table would look something like below:
product_id product_name price description option text price_ID
1 Product A 100 'Bla bla' A bla 1
1 Product A 250 'Bla bla' B bla 2
1 Product A 400 'Bla bla' C bla 3
1 Product A 500 'Bla bla' D bla 4
This way you can have as many prices for each product as you wish, and it takes down the complexity of your database a bit
What is the best practice to do a searching form with filters, where the filters are depending on the category?
e.g.:If you go to visit the ebay and select a category then the filters are different on the left hand side in the cell phones (filters: brand, operating system...) or the fashion category (filters: size, color...)...
In my mind I would do more table in DB. Each table for one category (cat_cellphone, cat_fashion...). Then put a product one of these tables depending on the category (not one products table where one column contains the category ID).
These tables are different where the columns names are characterized by the category.
Next, should do more searching forms and call a form where the filter belongs to the category.
Is it a good concept or there is an other accepted practice in big projects?
No, having multiple tables is a bad idea.
In general. use a table with a primary key over two columns instead.
(The primary key may span more than one column.)
The columns would be categoryname / filtername.
If you don't like such primary keys, and always use an autoincrement column, you can still create an index over the two columns.
The columns would be: id / categoryname / filtername / filtertext
Using multiple tables to store your products isn't a good idea. Because there will be products that overlap in categories which would result storing the same product in more than one table.
Just use a product table with the ID, product_number, description, etc. and a category table to store the different categories. Then you could link them directly like:
Product table:
ID product_number description category
1 00001 Screwdriver 1
Category table:
ID description
1 Tools
And you could even expand the category table with an extra column to use subcategories by addressing the parent of the subcategory:
Category table with subcategories:
ID description parent
1 Tools NULL
2 Automatic tools 1
And if you don't like directly linking to the category table from the product table you could use a link table:
Product_category:
Product_ID Category_ID
1 1
Hope this answers your question.
Edit, added filter table:
To add filtering for a product you can use a table for a the filters and a link table, like so:
Filter:
Filter_ID description value
1 brand Bosch
2 brand Bahco
3 type Phillips
Product_Filter:
Product_ID Filter_ID
1 1
1 3
That way you can link more than one filter to each product and use the same filter more than once.
You could even expand this further by using another table for the filter values, but that could make things a bit to complicated:
Filter:
Filter_ID description value
1 brand 1
Filter_value:
Filter_ID value
1 Bosch
2 Bahco
I am trying to create a link table in mySQL to manage the association of products and skus. An example, the product is a t-shirt. The sku is each t-shirt's size and color combination. I want to handle products and skus many-to-many relationships because we sell items and bulk. Bulk products may be comprised of individual skus. Ergo, one sku could be associated with many products.
So, I am pretty sure I need products and skus to have a many-to-many relationship. What I don't understand though is how to poulate the Link table. In other words, when I create a product I generate a productid in the product table. When I create the sku I have a skuid in the sku table. How to I take the productid and skuid (each primary keys in the thier own table) and add them to the link table. Each skuID and productID pair should be unique so I would like those two fields to serve as my primary key.
Anyway, when I try to create the relationship in phpmyadmin using the designer I get "Error: relation not added".
so basically you are trying to link 2 tables 1 for t-shirt product and 2 thsirt size and color, for table 1 make a product id which is primary and for table 2 create a product id field similar to table 1 which will link your two tables the only difference is that in table 2 product id is not your primary key