-
Notifications
You must be signed in to change notification settings - Fork 1
/
FleetManager.php
166 lines (144 loc) · 6 KB
/
FleetManager.php
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
<?php
class FleetManager {
private $conn; // database connection object
// constructor
public function __construct($db_conn) {
$this->conn = $db_conn;
}
// method to add a new vehicle to the fleet
public function addVehicle($make, $model, $year, $vin, $gps_tracking_id, $uniqueId) {
$query = "INSERT INTO fleet (make, model, year, vin, gps_tracking_id, uniqueId) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("ssisi", $make, $model, $year, $vin, $gps_tracking_id, $uniqueId);
$stmt->execute();
return $stmt->insert_id;
}
// method to edit a vehicle in the fleet
public function editVehicle($vehicle_id, $make=null, $model=null, $year=null) {
$query = "UPDATE fleet SET ";
$params = array();
$types = "";
if ($make) {
$query .= "make = ?, ";
$params[] = $make;
$types .= "s";
}
if ($model) {
$query .= "model = ?, ";
$params[] = $model;
$types .= "s";
}
if ($year) {
$query .= "year = ?, ";
$params[] = $year;
$types .= "i";
}
$query = rtrim($query, ", "); // remove the last comma
$query .= " WHERE id = ?";
$params[] = $vehicle_id;
$types .= "i";
$stmt = $this->conn->prepare($query);
$stmt->bind_param($types, ...$params);
$stmt->execute();
}
// method to get a list of all vehicles in the fleet
public function getFleet($page, $perPage) {
$query = "SELECT * FROM fleet LIMIT ?, ?";
$stmt = $this->conn->prepare($query);
$offset = ($page - 1) * $perPage;
$stmt->bind_param("ii", $offset, $perPage);
$stmt->execute();
$result = $stmt->get_result();
$fleet = array();
while ($row = $result->fetch_assoc()) {
$fleet[] = $row;
}
return $fleet;
}
// method to get vehicle id based on uniqueId
public function getVehicleIdByUniqueId($uniqueId) {
$query = "SELECT id FROM fleet WHERE uniqueId = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("s", $uniqueId);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
return $row['id'];
}
// method to update gps_tracking_id for a specific vehicle
public function updateGpsTrackingId($vehicle_id, $gps_tracking_id) {
$query = "UPDATE fleet SET gps_tracking_id = ? WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("ii", $gps_tracking_id, $vehicle_id);
$stmt->execute();
}
// method to record GPS tracking data for a vehicle
public function recordGpsTrackingData($uniqueId, $fix_time, $latitude, $longitude, $speed, $heading, $attributes) {
// serialize the attributes into a string
$attributes = json_encode($attributes);
// get the vehicle id based on uniqueId
$vehicle_id = $this->getVehicleIdByUniqueId($uniqueId);
$query = "INSERT INTO gps_tracking (vehicle_id, fix_time, latitude, longitude, speed, heading, attributes) VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("isddds", $vehicle_id, $fix_time, $latitude, $longitude, $speed, $heading, $attributes);
$stmt->execute();
$insert_id = $stmt->insert_id;
// update the gps_tracking_id of the vehicle using the insert id
$this->updateGpsTrackingId($vehicle_id, $insert_id);
}
public function recordGasPurchase($vehicle_id, $date, $odometer, $cost, $attributes, $product) {
$query = "INSERT INTO purchases (vehicle_id, date, odometer, cost, attributes, product) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("isids", $vehicle_id, $date, $odometer, $cost, $attributes, $product);
$stmt->execute();
return $stmt->insert_id;
}
function getPaginatedGasPurchases($page, $perPage) {
$query = "SELECT * FROM purchases LIMIT ?, ?";
$stmt = $conn->prepare($query);
$offset = ($page - 1) * $perPage;
$stmt->bind_param("ii", $offset, $perPage);
$stmt->execute();
$result = $stmt->get_result();
$purchases = [];
while ($row = $result->fetch_assoc()) {
$purchases[] = $row;
}
return $purchases;
}
public function recordRegistrationData($vehicle_id, $registration_number, $registration_date, $renewal_date) {
$query = "UPDATE fleet SET registration_number = ?, registration_date = ?, renewal_date = ? WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("sssi", $registration_number, $registration_date, $renewal_date, $vehicle_id);
$stmt->execute();
}
public function recordMaintenance($vehicle_id, $date, $odometer, $description, $cost, $type) {
$query = "INSERT INTO maintenance (vehicle_id, date, odometer, description, cost, type) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("isisd", $vehicle_id, $date, $odometer, $description, $cost, $type);
$stmt->execute();
return $stmt->insert_id;
}
public function setMaintenanceInterval($vehicle_id, $maintenance_type, $start, $interval) {
$query = "INSERT INTO maintenance_intervals (vehicle_id, maintenance_type, start, interval) VALUES (?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("isii", $vehicle_id, $maintenance_type, $start, $interval);
$stmt->execute();
return $stmt->insert_id;
}
public function manageDigitalDocuments($vehicle_id, $document_type, $file_name) {
// insert the document into the database
$query = "INSERT INTO vehicle_documents (vehicle_id, document_type, file_name) VALUES (?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("iss", $vehicle_id, $document_type, $file_name);
$stmt->execute();
}
public function recordAccident($vehicle_id, $date, $location, $description, $cost) {
$query = "INSERT INTO accidents (vehicle_id, date, location, description, cost) VALUES (?, ?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("isssd", $vehicle_id, $date, $location, $description, $cost);
$stmt->execute();
return $stmt->insert_id;
}
}
?>