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.
Related
My issue:
I am trying to make an system which checks if orders has their items in stock.
I am using 3 databases for this.
Products Database:
Product ID Name Disposable
ABC123 Chair 15
AFG999 Table 1
CHO555 Sofa 15
Order Details:
ID Customer ID Date
1555 123 2016-04-12
2666 333 2016-04-13
Order Details:
ProductID Quantity OrderID
ABC123 5 1555
CHO555 2 2666
AFG999 2 2666
What I'm trying to achieve is that it iterates through the ordered products and checks if they are in stock.
Also imagine that some orders will have over 20 products on them, how would I do this most efficiently?
What I have tried:
I have tried to solve it by iterating through the order details database and splitting them by a comma "," in php, like so:
$getorderdetails = mysqli_query($connect,
"SELECT corders.id, GROUP_CONCAT(corderdetails.productid) as productid,
GROUP_CONCAT(corderdetails.quantity) AS quantity
FROM corders, corderdetails
WHERE corders.id = corderdetails.orderid
GROUP BY corders.id
ORDER BY corders.id");
This outputs something like this:
Order ID Ordered Products Ordered Quantity
2666 CHO555,AFG999 2,2
So I tried splitting it up in arrays and searching the database one by on but I never really got it to work and I was wondering if someone else may share their solution?
I have the following form which sends each field in the form in an email to someone. However I wish to store the fields into the database and link each order to a sales rep (Which I have set up) Am I supposed to create a another table for order_details for example to store this. Pease help
It all depends how you want to structure your data. There is no silver bullet solution. But I would make three tables. SALES_REPRESENTATIVE, ORDER and ORDER_DETAILS. A sales representative has orders and an order has order details.
So based on your input.
If you have a table with product definitions you might have a product called Coca-Cola and another one Fanta together with their unit price.
The table of order details could then contain a link to the product table and specifying the quantity.
The order table would have a link to the order details for that order with for example also the date of when it was ordered.
The sales representative may then have a link to all orders that relate to him.
Concrete example:
PRODUCTS table
Id Name Price in $
1 Coca-cola 1.00
2 Fanta 1.10
ORDER_DETAILS
Id Order_Id Product_Id Quantity
1 1 1 10 // meaning this order detail is 10 Coca-cola's
2 1 2 20 // meaning this order detail is 20 Fanta's (because the tast is better :))
3 2 2 10 // meaning this order detail is 10 Fanta's
//Here Order_id is a foreign key to the Column Id of Table ORDERS
//And Product_id is a foreign key to the Column Id of the Table PRODUCTS
ORDERS
Id Sales_Representative_Id Order_Date
1 2 10/17/2014 // Order one made by Luke on 17th october 2014
2 1 10/19/2014 // Order two made by Anna on 19th october 2014
//Here Sales_Representative_id is a foreign key to the Column Id of Table SALES_REPRESENTATIVES
SALES_REPRESENTATIVES
Id Name
1 Anna
2 Luke
I have in the database rewards points table :
customer_reward_id customer_id order_id points
1 1 3 20
2 1 4 40
3 1 5 30
4 1 6 30
5 1 7 40
Collecting 100 points (sum) and delete from table
where customer_id =1 order by customer_reward_id ASC
any idea?
If it is about deleting record for customer who are having 100 points or more (summed up). Here is the query:
delete from rewards_points
where customer_id in (
SELECT customer_id
FROM (select * from `rewards_points`) dummy
group by customer_id
having sum(points)>=100);
Note: In MySQL you can't specify same table in from clause directly, in which you are performing delete/update operation. So, In this answer I've added extra sub-query and given alias dummy
In real applications, we do not delete the records to deduct the points from the customers. Because every record with the orders are required for reporting and management purposes. Instead the best way to deduct 100 points from the customer is to add the points with a negative sign and then sum up all the points so that you'll get the final value.
For example do the following:
Insert new record with negative value.
insert into reward_points (customer_id,order_id,points) values (1,8,-100);
Then sum the total value as below.
select sum(points) as total_rewards where customer_id=1;
This way you'll keep all the records of the previous orders as well as reduce the rewards too.
Hope this helps for your requirement.
I'm struggling a bit on the best way to do this with as little performance hit as possible.
Here's the setup...
Search results page with search refining filters that make an AJAX call to a PHP handler which returns a new (refined) set of results.
I have 4 tables that contain all of the data I need to connect to in the PHP handler code.
Table 1 - Main table of records with main details
Table 2 - Ratings for each product from professional rating company #1
Table 3 - Ratings for each product from professional rating company #2
Table 4 - Ratings for each product from professional rating company #3
The refiners on the search results page are jquery sliders with ranges from the lowest allowed rating to the highest for each.
When a slider handle is moved, a new AJAX call is made with the new value(s) and the database query will run to create a fresh set of refined results.
Getting the data I need from Table 1 is the easy part. What I'm struggling with is how to efficiently include a join on the other 3 tables and only picking up rows that match the refining values/ranges. Table 2, 3, and 4 all have multiple columns for year (2004-2012) and when I made an initial attempt to put it all into one query, it bogged down.
Table 2, 3, and 4 hold the various ratings for each record in Table 1.
The columns in Table 2, 3, and 4 are...
id - productID - y2004 - y2005 - y2006 - y2007 - ... you get the idea.
Each year column has a numeric value for each record (default is 0).
What I need to do is efficiently select records that match the refiner ranges selected by the user across all 4 tables at once.
An example refiner search would be...get all records from Table 1 where price is between $25 and $50 AND where Table 2 records have a rating (from any year/column) between 1 - 4 AND where Table 3 records have a rating (from any year/column) between 80 - 100 AND where Table 4 records have a rating (from any year/column) between 80 - 100.
Any advice on how to set this up with as much performance as possible?
My suggestion would be to use a different table structure. You should merge Table 2, 3 and 4 into a single ratings table with the following structure:
id | productID | companyID | year | rating
Then you could rewrite your query as:
SELECT *
FROM products p
JOIN ratings r ON p.id = r.productID
WHERE p.price BETWEEN 25 AND 50
AND (
( r.companyID = 1 AND r.rating BETWEEN 1 AND 4 )
OR ( r.companyID = 2 AND r.rating BETWEEN 80 AND 100 )
OR ( r.companyID = 3 AND r.rating BETWEEN 80 AND 100 )
)
This way the performance would surely increase. Also, your tables will be more scalable, both with the years and the number of companies.
One more thing: if you have a lot of fields in your products table, it might be more useful to execute 2 queries instead of joining. The reason for this is that you are fetching redundant data - every joined row will have the columns for product, even though you only need it once. This is a side-effect of joins, and there is probably a performance threshold where it will be more useful to query twice than to join. It is up to you to decide if/when that is the case.
I have the following scenario:
The user will input an interval and/or single comma-separated number codes for each product.
Partial table example:
name | codes
-----------------------------------
Product 1 | 239,300-350
Product 2 | 430-450,500,29
Product 3 | 780,2
For example, when the user searches for code 321, it should return the row of Product 1.
Is there a way for doing that search with a single query?
I'd suggest doing this with two tables instead:
products:
- name
- id
code_ranges:
- product_id
- range_start
- range_end
Then you can get what you want (for some $input_id) by using a join:
SELECT product.id, product.name
FROM products
JOIN code_ranges ON products.id = code_ranges.product_id
WHERE code_ranges.range_start <= $input_id
AND code_ranges.range_end >= $input_id
You can have multiple rows in the code_ranges table for each product to represent multiple different ranges (e.g. 2-4,7-10 would have two rows, one with range_start=2 and range_end=4, and another for the 7-10 range).
A product code that isn't a range would simply be treated as a range where the start and end are the same (e.g. 7 would be range_start=7 and range_end=7).
I have same problem but that first answer is not helpful.Because when you looking for item code range=239 then you can't difine that item also contains 300-350