Added: Create new refuel record
All checks were successful
Build and Deploy Zola Website / build_and_deploy (push) Successful in 14s
All checks were successful
Build and Deploy Zola Website / build_and_deploy (push) Successful in 14s
This commit is contained in:
parent
860a20d946
commit
3b0f672009
@ -2,8 +2,73 @@
|
|||||||
|
|
||||||
class RefuelController extends Controller {
|
class RefuelController extends Controller {
|
||||||
public function create() {
|
public function create() {
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$vehicle_id = $_POST['vehicle'] ?? '';
|
||||||
|
$fuel_type = $_POST['fuel_type'] ?? '';
|
||||||
|
$liters = $_POST['liters'] ?? '';
|
||||||
|
$price_per_liter = $_POST['price_per_liter'] ?? '';
|
||||||
|
$total_price = $_POST['total_price'] ?? '';
|
||||||
|
$note = $_POST['note'] ?? '';
|
||||||
|
|
||||||
|
$validator = new Validator();
|
||||||
|
$validator->required('vehicle', $vehicle_id);
|
||||||
|
$validator->required('fuel_type', $fuel_type);
|
||||||
|
$validator->required('liters', $liters);
|
||||||
|
$validator->required('price_per_liter', $price_per_liter);
|
||||||
|
$validator->required('total_price', $total_price);
|
||||||
|
$validator->number('liters', $liters);
|
||||||
|
$validator->number('price_per_liter', $price_per_liter);
|
||||||
|
$validator->number('total_price', $total_price);
|
||||||
|
|
||||||
|
if($note == "") $note = NULL;
|
||||||
|
|
||||||
|
if (!$validator->passes()) {
|
||||||
|
$vehicle = new Vehicle();
|
||||||
|
$vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
|
||||||
|
$this->view('vehicle/create', [
|
||||||
|
'error' => 'Please correct the errors below.',
|
||||||
|
'validationErrors' => $validator->errors() ?: [],
|
||||||
|
'vehicles' => $vehicles
|
||||||
|
]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($liters * $price_per_liter != $total_price) {
|
||||||
|
$vehicle = new Vehicle();
|
||||||
|
$vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
|
||||||
|
$this->view('vehicle/create', [
|
||||||
|
'error' => 'Please correct the errors below.',
|
||||||
|
'validationErrors' => ["Price calculation is wrong"],
|
||||||
|
'vehicles' => $vehicles
|
||||||
|
]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//var_dump(number_format((float)$price_per_liter, 2, '.', ''));
|
||||||
|
$record = new Refuel();
|
||||||
|
$result = $record->create([
|
||||||
|
'user_id' => $_SESSION['user']['id'],
|
||||||
|
'vehicle_id' => $vehicle_id,
|
||||||
|
'fuel_type' => $fuel_type,
|
||||||
|
'note' => $note,
|
||||||
|
'liters' => $liters,
|
||||||
|
'price_per_liter' => $price_per_liter,
|
||||||
|
'total_price' => $total_price,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($result === true) {
|
||||||
|
$this->redirect('/');
|
||||||
|
} else {
|
||||||
|
$vehicle = new Vehicle();
|
||||||
|
$vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
|
||||||
|
$this->view('refuel/create', ['title' => 'New refuel record', 'error' => $result, 'validationErrors' => [], 'vehicles' => $vehicles] );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
$vehicle = new Vehicle();
|
$vehicle = new Vehicle();
|
||||||
$vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
|
$vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
|
||||||
$this->view('refuel/create', ['title' => "New refuel record", 'vehicles' => $vehicles]);
|
$this->view('refuel/create', ['title' => "New refuel record", 'vehicles' => $vehicles]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -1,5 +1,37 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class Refuel {
|
class Refuel {
|
||||||
|
private $db;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->db = Database::getInstance()->getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create($data) {
|
||||||
|
try{
|
||||||
|
$stmt = $this->db->prepare("
|
||||||
|
INSERT INTO refueling_records (user_id, vehicle_id, fuel_type, note, liters, price_per_liter, total_price, created_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, NOW())
|
||||||
|
");
|
||||||
|
|
||||||
|
$stmt->bind_param(
|
||||||
|
"iissddd",
|
||||||
|
$data['user_id'],
|
||||||
|
$data['vehicle_id'],
|
||||||
|
$data['fuel_type'],
|
||||||
|
$data['note'],
|
||||||
|
$data['liters'],
|
||||||
|
$data['price_per_liter'],
|
||||||
|
$data['total_price'],
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($stmt->execute()) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return "Error: " . $stmt->error;
|
||||||
|
}
|
||||||
|
} catch(mysqli_sql_exception $e) {
|
||||||
|
return $e->getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<meta name="author" content="Filip Rojek | http://filiprojek.cz">
|
<meta name="author" content="Filip Rojek | http://filiprojek.cz">
|
||||||
<meta name="email" content="webmaster(@)fofrweb.com">
|
<meta name="email" content="webmaster(@)fofrweb.com">
|
||||||
<meta name="copyright" content="(c) filiprojek.cz">
|
<meta name="copyright" content="(c) filiprojek.cz">
|
||||||
<title>Fuel Stats | <?= $data['title'] ?></title>
|
<title>Fuel Stats<?= isset($data['title']) ? " | " . $data['title'] : "" ?></title>
|
||||||
<link rel="stylesheet" href="/css/main.css">
|
<link rel="stylesheet" href="/css/main.css">
|
||||||
<link rel="stylesheet" href="/css/global.css">
|
<link rel="stylesheet" href="/css/global.css">
|
||||||
<link rel="stylesheet" href="/css/vars.css">
|
<link rel="stylesheet" href="/css/vars.css">
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<meta name="author" content="Filip Rojek | http://filiprojek.cz">
|
<meta name="author" content="Filip Rojek | http://filiprojek.cz">
|
||||||
<meta name="email" content="webmaster(@)fofrweb.com">
|
<meta name="email" content="webmaster(@)fofrweb.com">
|
||||||
<meta name="copyright" content="(c) filiprojek.cz">
|
<meta name="copyright" content="(c) filiprojek.cz">
|
||||||
<title>Fuel Stats | <?= $data['title'] ?></title>
|
<title>Fuel Stats<?= isset($data['title']) ? " | " . $data['title'] : "" ?></title>
|
||||||
<link rel="stylesheet" href="/css/main.css">
|
<link rel="stylesheet" href="/css/main.css">
|
||||||
<link rel="stylesheet" href="/css/global.css">
|
<link rel="stylesheet" href="/css/global.css">
|
||||||
<link rel="stylesheet" href="/css/vars.css">
|
<link rel="stylesheet" href="/css/vars.css">
|
||||||
|
@ -29,15 +29,13 @@
|
|||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="liters">Liters</label>
|
<label for="liters">Liters</label>
|
||||||
<input type="number" name="liters" id="liters" value="<?= htmlspecialchars($_POST['liters'] ?? '') ?>">
|
<input type="number" name="liters" id="liters" min="0" step=".01" value="<?= htmlspecialchars($_POST['liters'] ?? '0.0') ?>">
|
||||||
|
|
||||||
<!-- TODO: name and id -->
|
<label for="price_per_liter">Price per liter</label>
|
||||||
|
<input type="number" name="price_per_liter" id="price_per_liter" min="0" step=".01" value="<?= htmlspecialchars($_POST['price_per_liter'] ?? '0.0') ?>">
|
||||||
|
|
||||||
<label for="liters">Price per liter</label>
|
<label for="total_price">Total price</label>
|
||||||
<input type="number" name="price_per_liter" id="price_per_liter" value="<?= htmlspecialchars($_POST['liters'] ?? '') ?>">
|
<input type="number" name="total_price" id="total_price" min="0" step=".01" value="<?= htmlspecialchars($_POST['total_price'] ?? '0.0') ?>">
|
||||||
|
|
||||||
<label for="liters">Total price</label>
|
|
||||||
<input type="number" name="liters" id="liters" value="<?= htmlspecialchars($_POST['liters'] ?? '') ?>">
|
|
||||||
|
|
||||||
<label for="note">Note</label>
|
<label for="note">Note</label>
|
||||||
<input type="text" name="note" id="note" value="<?= htmlspecialchars($_POST['note'] ?? '') ?>">
|
<input type="text" name="note" id="note" value="<?= htmlspecialchars($_POST['note'] ?? '') ?>">
|
||||||
@ -47,6 +45,40 @@
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
const inp_lit = document.querySelector("input#liters")
|
||||||
|
const inp_ppl = document.querySelector("input#price_per_liter")
|
||||||
|
const inp_tot = document.querySelector("input#total_price")
|
||||||
|
|
||||||
|
const rnd = (num) => Math.round((num + Number.EPSILON) * 100) / 100
|
||||||
|
|
||||||
|
function calculate(){
|
||||||
|
let liters = Number(inp_lit.value)
|
||||||
|
let price_per_liter = Number(inp_ppl.value)
|
||||||
|
let total_price = Number(inp_tot.value)
|
||||||
|
|
||||||
|
if(price_per_liter > 0 && liters > 0) {
|
||||||
|
total_price = liters * price_per_liter
|
||||||
|
}
|
||||||
|
|
||||||
|
if(price_per_liter > 0 && total_price > 0) {
|
||||||
|
liters = total_price / price_per_liter
|
||||||
|
}
|
||||||
|
|
||||||
|
if(liters > 0 && total_price > 0) {
|
||||||
|
price_per_liter = total_price / liters
|
||||||
|
}
|
||||||
|
|
||||||
|
inp_lit.value = liters
|
||||||
|
inp_ppl.value = price_per_liter
|
||||||
|
inp_tot.value = total_price
|
||||||
|
}
|
||||||
|
|
||||||
|
[inp_lit, inp_ppl, inp_tot].forEach(inp => {
|
||||||
|
inp.addEventListener("change", () => {
|
||||||
|
calculate()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
const vehicles = <?= json_encode($data['vehicles']); ?>;
|
const vehicles = <?= json_encode($data['vehicles']); ?>;
|
||||||
const fuel_sel = document.querySelector("#fuel_type")
|
const fuel_sel = document.querySelector("#fuel_type")
|
||||||
const vehic_sel = document.querySelector("#vehicle")
|
const vehic_sel = document.querySelector("#vehicle")
|
||||||
@ -66,6 +98,4 @@
|
|||||||
vehic_sel.addEventListener("change", () => {
|
vehic_sel.addEventListener("change", () => {
|
||||||
selectFuel()
|
selectFuel()
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: function for auto calculation price/price_per_liter/liters
|
|
||||||
</script>
|
</script>
|
||||||
|
@ -91,13 +91,16 @@ class Database {
|
|||||||
$refuelingTableQuery = "
|
$refuelingTableQuery = "
|
||||||
CREATE TABLE IF NOT EXISTS refueling_records (
|
CREATE TABLE IF NOT EXISTS refueling_records (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
user_id INT NOT NULL,
|
||||||
vehicle_id INT NOT NULL,
|
vehicle_id INT NOT NULL,
|
||||||
fuel_type ENUM('Diesel', 'Gasoline 95', 'Gasoline 98', 'Premium Diesel', 'Premium Gasoline 95', 'Premium Gasoline 98', 'Other') NOT NULL,
|
fuel_type ENUM('Diesel', 'Gasoline 95', 'Gasoline 98', 'Premium Diesel', 'Premium Gasoline 95', 'Premium Gasoline 98', 'Other') NOT NULL,
|
||||||
liters DECIMAL(10, 2) NOT NULL,
|
note VARCHAR(150) NULL,
|
||||||
price_per_liter DECIMAL(10, 2) NOT NULL,
|
liters DOUBLE(10, 2) NOT NULL,
|
||||||
total_price DECIMAL(10, 2) NOT NULL,
|
price_per_liter DOUBLE(10, 2) NOT NULL,
|
||||||
|
total_price DOUBLE(10, 2) NOT NULL,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (vehicle_id) REFERENCES vehicles(id) ON DELETE CASCADE
|
FOREIGN KEY (vehicle_id) REFERENCES vehicles(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||||
) ENGINE=InnoDB;
|
) ENGINE=InnoDB;
|
||||||
";
|
";
|
||||||
|
|
||||||
|
@ -12,6 +12,15 @@ class Validator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a field contains numbers
|
||||||
|
*/
|
||||||
|
public function number($field, $value, $message = null) {
|
||||||
|
if(!is_numeric($value)) {
|
||||||
|
$this->errors[$field] = $message ?? "$field must be an number";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a field meets minimum length
|
* Check if a field meets minimum length
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user