How should you check whats in stock on an order? - php

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?

Related

Replace a product code with the product id from another table in sql

the previous developer has done a wrong practice in a sql database with phpmyadmin, which is creating a problem of duplicate contents.
Correct way:
table 1
ORDERS
id_order
id_product
table 2
PRODUCTS
id_product
internal_product_code
———
Wrong way:
table 1
ORDERS
id_order
internal_product_code
table 2
PRODUCTS
id_product
internal_product_code
Unfortunately, the internal_product_code is NOT always unique**, while id_product and id_order are always unique. So today my client has get the first duplicate data content, because there's two products with different id_product but same internal_product_code.
** the same internal_product_code due to a very similar specs (this is another bad practice)**
Is there any way to replace table1.internal_product_code with table2.id_product in order to follow the correct way? I need to do it in the database (after a strong backup).
Thanks to all
UPDATE orders SET products = (SELECT orders.id_order FROM products WHERE orders.id_product = products.internal_product_code LIMIT 1)

Which way to create this inventory in mysql

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.

Passing array of info into database

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

Delete records from MySQL database based on sum of points

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.

Questions about orders table for mulltiple items with multiple options

I am making a new database for an online store and Am trying to figure out how to do the orders table. There are multiple products and each product has many options. I am just wondering the best way to keep track of all this. I keep coming back to a table with all the options for all the items. This seems like a bad thing though. Does anyone have any ideas on how to do this? Any help is much appreciated.
Every product is stored in a row of table product
Every product has zero or N options ( 0 N )
Every order contain one or N products ( 0 N )
You need 4 tables to keep track for all this : options, products , products_orders , orders
I'd start with the table structure below as a base. You will almost definately need more columns than the ones i have included but it seems to me that you need what is below at a minimum.
Order
-----
orderId
someOtherColumn
OrderProducts
-------------
orderId
ProductId
product
--------
productId
productName
productOption
-------------
productId
OptionId
Option
--------
OptionId
OptionName

Categories