-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.php
More file actions
249 lines (215 loc) · 9.15 KB
/
Copy pathimport.php
File metadata and controls
249 lines (215 loc) · 9.15 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
require 'db.php';
ini_set('display_errors', 1);
error_reporting(E_ALL);
define('CONFIG_FILE', __DIR__ . '/config.json');
$defaultCost = 0.034;
// Load electricity cost
if (file_exists(CONFIG_FILE)) {
$config = json_decode(file_get_contents(CONFIG_FILE), true);
$costPerKw = isset($config['cost']) ? floatval($config['cost']) : $defaultCost;
} else {
$costPerKw = $defaultCost;
}
$errors = [];
$imported = 0;
// === Geocoding function ===
// Restituisce [lat, lng] oppure [null, null]
function geocodeAddress($address) {
if ($address === null || $address === '') {
return [null, null];
}
$url = 'https://nominatim.openstreetmap.org/search?format=json&q=' . urlencode($address);
$opts = ["http" => ["header" => "User-Agent: trip-logger/1.0 (+contact:youremail@example.com)\r\n"]];
$context = stream_context_create($opts);
// Piccola pausa per rispetto rate-limit Nominatim
usleep(300000); // 0.3s
$response = @file_get_contents($url, false, $context);
if ($response) {
$json = json_decode($response, true);
if (!empty($json[0])) {
return [floatval($json[0]['lat']), floatval($json[0]['lon'])];
}
}
return [null, null];
}
// === Handle CSV Upload ===
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv'])) {
if (!is_uploaded_file($_FILES['csv']['tmp_name'])) {
$errors[] = "Upload non valido.";
} else {
$csv = fopen($_FILES['csv']['tmp_name'], 'r');
if ($csv === false) {
$errors[] = "Impossibile aprire il file CSV.";
} else {
$header = fgetcsv($csv, 0, ';', '"');
$lineNumber = 2;
// PREPARO le query in anticipo
// 1) INSERT IGNORE senza geocoding (coordinate a NULL)
$insertSql = "INSERT IGNORE INTO trips
(category, started_at, start_odometer, start_address,
ended_at, end_odometer, end_address, duration_min,
distance_km, battery_consumed_kwh, user_note,
average_consumption, avg_speed_kmh, cost_per_kw,
start_lat, start_lng, end_lat, end_lng)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, NULL, NULL)";
$insertStmt = $conn->prepare($insertSql);
if (!$insertStmt) {
$errors[] = "Errore prepare INSERT: " . $conn->error;
}
// 2) UPDATE delle sole coordinate, per il record appena inserito
$updateSql = "UPDATE trips
SET start_lat = ?, start_lng = ?, end_lat = ?, end_lng = ?
WHERE id = ?";
$updateStmt = $conn->prepare($updateSql);
if (!$updateStmt) {
$errors[] = "Errore prepare UPDATE: " . $conn->error;
}
while (($row = fgetcsv($csv, 0, ';', '"')) !== false) {
if (count($row) < 11) {
$errors[] = "Linea $lineNumber: riga incompleta (attese 11 colonne).";
$lineNumber++;
continue;
}
// Map CSV columns
[$category, $started, $start_km, $start_address, $ended, $end_km,
$end_address, $duration, $distance, $consumption, $note] = $row;
// Parse datetime
$started_ts = strtotime($started);
$ended_ts = strtotime($ended);
if (!$started_ts || !$ended_ts) {
$errors[] = "Linea $lineNumber: formato data/ora non valido.";
$lineNumber++;
continue;
}
$started_at = date('Y-m-d H:i:s', $started_ts);
$ended_at = date('Y-m-d H:i:s', $ended_ts);
// Parse numeric values
$start_odometer = floatval(str_replace([' km', ','], ['', '.'], $start_km));
$end_odometer = floatval(str_replace([' km', ','], ['', '.'], $end_km));
$distance_km = floatval(str_replace([' km', ','], ['', '.'], $distance));
$battery_kwh = floatval(str_replace(',', '.', $consumption));
$duration_min = intval($duration);
if ($start_odometer <= 0 || $end_odometer <= 0 || $distance_km <= 0 || $battery_kwh < 0 || $duration_min <= 0) {
$errors[] = "Linea $lineNumber: valori numerici non validi.";
$lineNumber++;
continue;
}
// Derived values
$avg_consumption = $distance_km > 0 ? ($battery_kwh / $distance_km * 100) : null;
$avg_speed = $duration_min > 0 ? ($distance_km / ($duration_min / 60)) : null;
// ========== 1) INSERIMENTO SENZA GEO ==========
if ($insertStmt) {
// Tipi corretti per i 14 parametri:
// s,s,d,s,s,d,s,i,d,d,s,d,d,d
$insertStmt->bind_param(
"ssdssdsiddsddd",
$category,
$started_at,
$start_odometer,
$start_address,
$ended_at,
$end_odometer,
$end_address,
$duration_min,
$distance_km,
$battery_kwh,
$note,
$avg_consumption,
$avg_speed,
$costPerKw
);
if (!$insertStmt->execute()) {
$errors[] = "Linea $lineNumber: errore INSERT - " . $insertStmt->error;
$lineNumber++;
continue;
}
} else {
// Se non è stato possibile preparare l'INSERT, non possiamo proseguire
$lineNumber++;
continue;
}
// Se affected_rows > 0 significa che è un NUOVO viaggio
if ($insertStmt->affected_rows > 0) {
$imported++;
$newTripId = $conn->insert_id;
// ========== 2) GEO SOLO PER I NUOVI ==========
[$start_lat, $start_lng] = geocodeAddress($start_address);
[$end_lat, $end_lng] = geocodeAddress($end_address);
if ($updateStmt) {
// Tipi: d d d d i
$updateStmt->bind_param(
"ddddi",
$start_lat,
$start_lng,
$end_lat,
$end_lng,
$newTripId
);
if (!$updateStmt->execute()) {
$errors[] = "Linea $lineNumber: errore UPDATE geocoding - " . $updateStmt->error;
}
} else {
$errors[] = "Linea $lineNumber: impossibile preparare UPDATE geocoding.";
}
}
// Se affected_rows == 0, il record esisteva già -> NIENTE geocoding
$lineNumber++;
}
if ($insertStmt) $insertStmt->close();
if ($updateStmt) $updateStmt->close();
fclose($csv);
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Import Trips CSV</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>📥 Import Trips CSV</h1>
<div>
<a href="index.php" class="nav-link">⬅️ Back to Home</a>
</div>
<?php if ($imported > 0): ?>
<div class="message-box">
✅ <?= $imported ?> trip(s) imported successfully.
</div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<div class="error-box">
<strong>Some rows were skipped / had issues:</strong>
<ul>
<?php foreach ($errors as $e): ?>
<li><?= htmlspecialchars($e) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form method="POST" enctype="multipart/form-data">
<label for="csv"><strong>Select CSV File:</strong></label>
<input type="file" name="csv" id="csv" accept=".csv" required>
<button type="submit" class="btn">Upload & Import</button>
</form>
<div style="font-size:14px; margin-bottom:20px;">
Current electricity cost: <strong><?= number_format($costPerKw, 4) ?> EUR/kWh</strong>
</div>
<!-- ⚠️ Geocoding warning -->
<div class="warning-box">
⚠️ <strong>Note:</strong> Geocoding will run <em>only</em> for newly imported trips.
If a trip already exists in the DB, no geocoding is performed for it.
If geocoding fails, the trip is still imported but won't appear on the map.
</div>
<!-- ⚠️ Cost warning -->
<div class="warning-box">
⚠️ <strong>Reminder:</strong> The electricity cost per kWh defined in <em>Settings</em> will
only be applied to trips imported from now on.
Previously imported trips retain their original value.
</div>
</body>
</html>