This illustrates the mechanics of the process.
It processes the posted quantities, writes them to an order and order item file then generates the invoice html. The invoice is written to orders/order_xxx.html where xxx is the order number
Example invoice output
The tables used are
customer order
+-------------+-------------+ +-------------+----------+
| Field | Type | | Field | Type |
+-------------+-------------+ +-------------+----------+
| customer_id | int(11) | | id | int(11) |
| fname | varchar(45) | | customer_id | int(11) |
| lname | varchar(45) | | order_date | datetime |
+-------------+-------------+ +-------------+----------+
product order_item
+-------------+---------------+ +------------+---------------+
| Field | Type | | Field | Type |
+-------------+---------------+ +------------+---------------+
| id | int(11) | | id | int(11) |
| description | varchar(50) | | order_id | int(11) |
| price | decimal(10,2) | | product_id | int(11) |
+-------------+---------------+ | qty | int(11) |
| price | decimal(10,2) |
+------------+---------------+ `
Code
<?php
include '../db_inc.php'; ## use your own
$pdo = pdoConnect(); ## connecction code
session_start();
$_SESSION['customer'] = 125; ## I am assuming this would be set when the customer signed in
$order_id = null;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$quantities = array_filter($_POST['qty']); # remove 0 qtys
try {
$pdo->beginTransaction();
// write order record
$stmt = $pdo->prepare("INSERT INTO `order` (customer_id) VALUES (?)");
$stmt->execute([ $_SESSION['customer'] ]);
// get the id of the order
$order_id = $pdo->lastInsertId();
// now write the order item records, also storing the current prices frmm product table
$stmt = $pdo->prepare("INSERT INTO `order_item` (order_id, product_id, qty, price)
SELECT ?, id, ?, price
FROM product
WHERE id = ?
");
foreach ($quantities as $id => $qty) {
$stmt->execute([ $order_id, $qty, $id ]);
}
$pdo->commit();
}
catch (PDOException $e) {
$pdo->rollBack();
$order_id = null;
throw $e;
}
// prepare the invoice output
$res = $pdo->prepare("SELECT oi.order_id
, DATE_FORMAT(o.order_date, '%M %e %Y') as order_date
, concat(c.fname, ' ', c.lname) as customer
, p.description as product
, oi.qty
, oi.qty * p.price as amount
FROM order_item oi
JOIN `order` o ON o.id = oi.order_id
JOIN customer c ON c.customer_id = o.customer_id
JOIN product p ON oi.product_id = p.id
WHERE oi.order_id = ?
");
$res->execute([ $order_id ]);
$row = $res->fetch();
$inv_total = 0;
$inv_output = "<!DOCTYPE html>
<html lang='en'>
<head>
<title>Sample order/invoice</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body>
<table style = 'width: 400px;'><tr><td><b>Customer<b></td><td>{$row['customer']}</td></tr>
<tr><td><b>Order No<b></td><td>" . sprintf('%06d', $order_id) . "</td></tr>
<tr><td><b>Order Date<b></td><td>{$row['order_date']}</td></tr>
</table><br><br>
<table style='width: 600px; border-collapse: collapse;'>
<tr style='background-color:#000; color: #FFF'>
<td><b>Product</b></td>
<td><b>Quantity</b></td>
<td><b>Amount</b></td>
<tr>
";
do {
$inv_output .= "<tr><td>{$row['product']}</td>
<td>{$row['qty']}</td>
<td>{$row['amount']}</td>
</tr>";
$inv_total += $row['amount'];
} while ($row = $res->fetch());
$inv_output .= "<tr><td colspan='3'> </td></tr>
<tr><td colspan='2'><b>TOTAL</b></td><td>$inv_total</td><?tr>
</table>
</body>
<?html>";
// WRITE INVOICE OUTPUT TO HTML FILE
file_put_contents("orders/order_{$order_id}.html", $inv_output);
header("Refresh: 0");
exit;
}
else {
$results = $pdo->query("SELECT id
, description
, price
FROM product");
$form_output = "
<form method='POST'>
<table style='width:500px'>
<tr style='background-color: #ccc'><td>ID</td><td>Product</td><td>Quantity</td><td>Price</td></tr>
";
foreach($results as $r) {
$form_output .= "<tr><td>{$r['id']}</td>
<td>{$r['description']}</td>
<td>
<input type='number' value='0' name='qty[{$r['id']}]'>
</td>
<td style='text-align: right;'>{$r['price']}</td>
<tr> ";
}
$form_output .= "
</table>
<br>
<button>Submit</button>
</form>
";
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Sample order/invoice</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body>
<?php
if ($order_id) { // was a new order created?
echo $inv_output;
}
else { // if not, dsplay form
echo $form_output;
}
?>
</body>
</html>