prepare($insert_sql);
$stmt->execute([
':name' => $_POST['name'],
':wbs' => $_POST['wbs'] ?? null,
':startDate' => empty($_POST['startDate']) ? null : $_POST['startDate'],
':endDate' => empty($_POST['endDate']) ? null : $_POST['endDate'],
':budget' => (float)($_POST['budget'] ?? 0),
':recoverability' => (float)($_POST['recoverability'] ?? 100),
':targetMargin' => (float)($_POST['targetMargin'] ?? 0),
]);
// Create initial forecasting version
$projectId = $pdo_form->lastInsertId();
$forecast_sql = "INSERT INTO forecasting (projectId, versionNumber, createdAt) VALUES (:projectId, 1, NOW())";
$forecast_stmt = $pdo_form->prepare($forecast_sql);
$forecast_stmt->execute([':projectId' => $projectId]);
header("Location: " . $_SERVER['PHP_SELF']);
exit();
} catch (PDOException $e) {
$form_error = "Database error: " . $e->getMessage();
}
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete_project') {
try {
require_once __DIR__ . '/db/config.php';
$pdo_delete = db();
$delete_sql = "DELETE FROM projects WHERE id = :id";
$stmt = $pdo_delete->prepare($delete_sql);
$stmt->execute([':id' => $_POST['id']]);
header("Location: " . $_SERVER['PHP_SELF']);
exit();
} catch (PDOException $e) {
$form_error = "Database error: " . $e->getMessage();
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_project') {
if (empty($_POST['id']) || empty($_POST['name'])) {
$form_error = "ID and Project Name are required for an update.";
} else {
try {
require_once __DIR__ . '/db/config.php';
$pdo_update = db();
$update_sql = "UPDATE projects SET
name = :name,
wbs = :wbs,
startDate = :startDate,
endDate = :endDate,
budget = :budget,
recoverability = :recoverability,
targetMargin = :targetMargin
WHERE id = :id";
$stmt = $pdo_update->prepare($update_sql);
$stmt->execute([
':id' => $_POST['id'],
':name' => $_POST['name'],
':wbs' => $_POST['wbs'] ?? null,
':startDate' => empty($_POST['startDate']) ? null : $_POST['startDate'],
':endDate' => empty($_POST['endDate']) ? null : $_POST['endDate'],
':budget' => (float)($_POST['budget'] ?? 0),
':recoverability' => (float)($_POST['recoverability'] ?? 100),
':targetMargin' => (float)($_POST['targetMargin'] ?? 0),
]);
header("Location: " . $_SERVER['PHP_SELF']);
exit();
} catch (PDOException $e) {
$form_error = "Database error: " . $e->getMessage();
}
}
}
// --- DATABASE INITIALIZATION ---
require_once __DIR__ . '/db/config.php';
function execute_sql_from_file($pdo, $filepath) {
try {
$sql = file_get_contents($filepath);
$pdo->exec($sql);
return true;
} catch (PDOException $e) {
if (strpos($e->getMessage(), 'already exists') === false) {
error_log("SQL Execution Error: " . $e->getMessage());
}
return false;
}
}
$projects_data = [];
try {
$pdo = db();
// Apply all migrations
$migration_files = glob(__DIR__ . '/db/migrations/*.sql');
sort($migration_files);
foreach ($migration_files as $file) {
execute_sql_from_file($pdo, $file);
}
$stmt = $pdo->query("SELECT * FROM projects ORDER BY name");
$projects_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$db_error = "Database connection failed: " . $e->getMessage();
}
// --- RENDER PAGE ---
?>
Projects - Project Financials
Project Financials
| Name |
WBS |
Start Date |
End Date |
Budget |
Recoverability |
Target Margin |
Actions |
| No projects found. |
|
|
|
|
€ |
% |
% |
|