-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy-orders.php
More file actions
224 lines (179 loc) · 8.4 KB
/
my-orders.php
File metadata and controls
224 lines (179 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
session_start();
require "include/template2.inc.php";
require "include/dbms.inc.php";
require "include/auth.inc.php";
function formatPrice($price) {
return number_format($price, 2, ',', '.');
}
if (isset($_SESSION['user'])) {
$order_id = $mysqli->real_escape_string($_GET['id']);
// stato dell'ordine
$order_status_query = $mysqli->query("SELECT state FROM orders WHERE id = '{$order_id}'");
$main = new Template("skins/motor-html-package/motor/frame-customer.html");
if ($order_status_query && $order_status_query->num_rows > 0) {
$order_status_data = $order_status_query->fetch_assoc();
if ($order_status_data['state'] == 'delivered') {
$body = new Template("skins/motor-html-package/motor/my-orders.html");
} else {
$body = new Template("skins/motor-html-package/motor/my-pending-orders.html");
}
$code_query = $mysqli->query("SELECT number, totalPrice FROM orders WHERE id = '{$order_id}'");
$code_query_data = $code_query->fetch_assoc();
$body->setContent("order_id", $order_id);
$body->setContent("order_number", $code_query_data['number']);
$body->setContent("order_total", $code_query_data['totalPrice']);
// dettagli
$shipping_address_query = $mysqli->query(" SELECT
sa.name,
sa.surname,
sa.province,
sa.city,
sa.streetAddress,
sa.cap,
sa.phone
FROM
orders o
JOIN
shipping_address sa ON o.shipping_address_id = sa.id
WHERE
o.id = '{$order_id}'");
if ($order_status_query && $order_status_query->num_rows > 0) {
$shipping_address_data = $shipping_address_query->fetch_assoc();
$body->setContent("ad_name", $shipping_address_data['name']);
$body->setContent("ad_surname", $shipping_address_data['surname']);
$body->setContent("ad_province", $shipping_address_data['province']);
$body->setContent("ad_city", $shipping_address_data['city']);
$body->setContent("ad_street", $shipping_address_data['streetAddress']);
$body->setContent("ad_cap", $shipping_address_data['cap']);
$body->setContent("ad_phone", $shipping_address_data['phone']);
} else {
error_log("Indirizzo non trovato per l'ordine con ID: $order_id");
}
} else {
error_log("Ordine non trovato per l'ordine con ID: $order_id");
header("Location: /MotorShop/customer-dashboard.php");
}
$main->setContent('name', $_SESSION['user']['name']);
$main->setContent('surname', $_SESSION['user']['surname']);
$main->setContent('email', $_SESSION['user']['email']);
// Paginazione
$itemsPerPage = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $itemsPerPage;
$totalItemsQuery = "SELECT COUNT(*) AS total FROM orders_has_products WHERE order_id = '{$order_id}'";
$totalItemsResult = $mysqli->query($totalItemsQuery);
$totalItemsRow = $totalItemsResult->fetch_assoc();
$totalItems = $totalItemsRow['total'];
$totalPages = ceil($totalItems / $itemsPerPage);
// Recupera i prodotti per la pagina corrente
$oid = $mysqli->query("SELECT id, sub_products_id, quantity FROM orders_has_products WHERE order_id = '{$order_id}' LIMIT $offset, $itemsPerPage");
$products = [];
if ($oid && $oid->num_rows > 0) {
foreach ($oid as $sub) {
$body->setContent("id", $sub['id']);
$body->setContent("sub_id", $sub['sub_products_id']);
$body->setContent("sub_quantity", $sub['quantity']);
$products[$sub['sub_products_id']] = $sub['quantity'];
// query per size, color e price
$sub_product_id = $sub['sub_products_id'];
$sub_product_query = $mysqli->query("SELECT size, color, price, products_id FROM sub_products WHERE id = '{$sub_product_id}'");
if ($sub_product_query && $sub_product_query->num_rows > 0) {
$sub_product_data = $sub_product_query->fetch_assoc();
$calculatedPrice = $sub_product_data['price'] * $sub['quantity']; // prezzo totale
$body->setContent("size", $sub_product_data['size']);
function getColorName($hexColor) {
// nomi dei colori
$colorMap = [
"#000000" => "Nero",
"#9E9E9E" => "Bianco",
"#66BB6A" => "Verde",
"#FFEB3B" => "Giallo",
"#FF9800" => "Arancione",
"#795548" => "Marrone",
"#FF5252" => "Rosso",
"#AB47BC" => "Viola",
"#42A5F5" => "Blue"
];
return isset($colorMap[$hexColor]) ? $colorMap[$hexColor] : "Colore sconosciuto";
}
$colore = getColorName($sub_product_data['color']);
$body->setContent("color", $colore);
$body->setContent("price", formatPrice($calculatedPrice));
// query per ottenere il titolo del prodotto
$product_id = $sub_product_data['products_id'];
$order_title_query = $mysqli->query("SELECT title FROM products WHERE id = '{$product_id}'");
if ($order_title_query && $order_title_query->num_rows > 0) {
$order_title_data = $order_title_query->fetch_assoc();
$body->setContent("title", $order_title_data['title']);
} else {
// Se non viene trovato alcun prodotto corrispondente
error_log("Product title not found for product ID: $product_id");
$body->setContent("title", 'Titolo non disponibile');
}
} else {
// Se non viene trovato alcun sottoprodotto corrispondente
error_log("Sub-product not found for ID: $sub_product_id");
$body->setContent("size", '');
$body->setContent("color", '');
$body->setContent("price", '');
$body->setContent("title", 'Titolo non disponibile');
}
}
} else {
// Nessun ordine trovato
$body->setContent("id", 'Non abbiamo trovato il tuo ordine');
$body->setContent("sub_id", '');
$body->setContent("sub_quantity", '');
$body->setContent("size", '');
$body->setContent("color", '');
$body->setContent("price", '');
$body->setContent("title", '');
}
if ($totalPages > 1) {
$pagination = '';
for ($i = 1; $i <= $totalPages; $i++) {
if ($i == $page) {
$pagination .= "<span class='current-page'>$i</span> ";
} else {
$pagination .= "<a href='/MotorShop/order-summary.php?id={$order_id}&page=$i'>$i</a> ";
}
}
$body->setContent("pagination", $pagination);
} else {
$body->setContent("pagination", '');
}
// Funzione per cancellare un ordine
function cancelOrder($orderId) {
global $mysqli;
global $products;
$deleteQuery = "DELETE FROM orders WHERE id = '{$orderId}'";
$mysqli->query($deleteQuery);
// eliminare da orders_has_products le varie istanze
$deleteHasQuery = "DELETE FROM orders_has_products WHERE order_id = '{$orderId}'";
$mysqli->query($deleteHasQuery);
// ripristinare le quantity dei sub, con cambio da 0 a 1 di availability eventualmente
foreach($products as $id => $quantity) {
$mysqli->query("UPDATE sub_products SET quantity = quantity + " . intval($quantity) . ", availability = 1 WHERE id = " . intval($id));
}
header("Location: /MotorShop/customer-dashboard.php");
return $mysqli->query($deleteQuery);
}
// cancellazione di un ordine
if (isset($_GET['action']) && $_GET['action'] === 'cancel' && isset($_GET['id'])) {
$orderId = $mysqli->real_escape_string($_GET['id']);
// riaggiungere la quantity ai subproduct
// funzione per cancellare l'ordine
if (cancelOrder($orderId)) {
header("Location: /MotorShop/customer-dashboard.php");
} else {
echo "Errore durante la cancellazione dell'ordine.";
}
}
$main->setContent("dynamic", $body->get());
$main->close();
} else {
header("Location: /MotorShop/login.php");
exit;
}
?>